File: player.cpp

package info (click to toggle)
tiatracker 1.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,664 kB
  • sloc: cpp: 8,875; asm: 664; makefile: 8
file content (500 lines) | stat: -rw-r--r-- 18,565 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/* TIATracker, (c) 2016 Andre "Kylearan" Wichmann.
 * Website: https://bitbucket.org/kylearan/tiatracker
 * Email: andre.wichmann@gmx.de
 * See the file "license.txt" for information on usage and redistribution
 * of this file.
 */

#include <QThread>
#include <QTimer>
#include <iostream>
#include "track/track.h"
#include "track/instrument.h"
#include "track/pattern.h"
#include "player.h"
#include <SDL.h>
#include <QElapsedTimer>
#include <QVector>
#include <QCoreApplication>


namespace Emulation {

Player::Player(Track::Track *parentTrack, QObject *parent) : QObject(parent)
{
    pTrack = parentTrack;

    tiaSound.channels(2, false);
    sdlSound.setFrameRate(50.0);
    sdlSound.open();
    sdlSound.mute(false);
    sdlSound.setEnabled(true);
    sdlSound.setVolume(100);

    sdlSound.set(AUDC0, 0, 10);
    sdlSound.set(AUDV0, 0, 15);
    sdlSound.set(AUDF0, 0, 18);
}

Player::~Player()
{
/*
    delete eTimer;

    for (int i = 0; i < deltas.size(); ++i) {
        std::cout << elapsedValues[i] << "/" << deltas[i] << ", ";
    }
    std::cout << "\nnumFrames: " << statsNumFrames - 120 << ", average delta: " << double(statsJitterSum)/double(statsNumFrames - 120) << ", max: " << statsJitterMax << "\n";
*/
}

/*************************************************************************/

void Player::setFrameRate(float rate) {
    sdlSound.close();
    sdlSound.setFrameRate(rate);
    sdlSound.open();
}

/*************************************************************************/

void Player::startTimer() {
    QElapsedTimer elapsedTimer;
    elapsedTimer.start();
    double lastReplayTime = 0;
    doReplay = true;
    unsigned long long timestamp;
    // Replay loop
    while (doReplay) {
        double frameDuration = replayTvStandard == TiaSound::TvStandard::PAL ? 1000.0/50.0 : 1000.0/60.0;
        // Wait and allow events until shortly before next update
        while (elapsedTimer.elapsed() < lastReplayTime + (frameDuration - 10.0)) {
            QCoreApplication::processEvents(QEventLoop::AllEvents, 5);
        }
        // Busy-wait until next update
        do {
            timestamp = elapsedTimer.elapsed();
        } while (timestamp < lastReplayTime + frameDuration);
        // Do updates
        while (lastReplayTime < timestamp) {
            timerFired();
            lastReplayTime += frameDuration;
        }
    }

/*
    eTimer = new QElapsedTimer;
    eTimer->start();
*/
}

/*************************************************************************/

void Player::stopTimer() {
    //timer->stop();
    doReplay = false;
}

/*************************************************************************/

void Player::silence() {
    mode = PlayMode::None;
}

/*************************************************************************/

void Player::playInstrument(Track::Instrument *instrument, int frequency) {
    if (mode != PlayMode::Instrument) {
        currentInstrument = instrument;
        currentInstrumentFrequency = frequency;
        currentInstrumentFrame = 0;
        mode = PlayMode::Instrument;
    }
}

/*************************************************************************/

void Player::playInstrumentOnce(Track::Instrument *instrument, int frequency) {
    currentInstrument = instrument;
    currentInstrumentFrequency = frequency;
    currentInstrumentFrame = 0;
    mode = PlayMode::InstrumentOnce;
}

/*************************************************************************/

void Player::stopInstrument() {
    if (currentInstrument != nullptr) {
        currentInstrumentFrame = currentInstrument->getReleaseStart();
    }
}

/*************************************************************************/

void Player::playPercussion(Track::Percussion *percussion) {
    currentPercussion = percussion;
    currentPercussionFrame = 0;
    mode = PlayMode::Percussion;
}

/*************************************************************************/

void Player::stopPercussion() {
    mode = PlayMode::None;
}

/*************************************************************************/

void Player::stopTrack() {
    mode = PlayMode::None;
}

/*************************************************************************/

void Player::setChannel0(int distortion, int frequency, int volume) {
    sdlSound.set(AUDC0, distortion, 10);
    sdlSound.set(AUDV0, volume, 15);
    sdlSound.set(AUDF0, frequency, 18);
}

/*************************************************************************/

void Player::setChannel(int channel, int distortion, int frequency, int volume) {
    int audC = channel == 0 ? AUDC0 : AUDC1;
    int audV = channel == 0 ? AUDV0 : AUDV1;
    int audF = channel == 0 ? AUDF0 : AUDF1;
    sdlSound.set(audC, distortion, 10);
    sdlSound.set(audV, volume, 15);
    sdlSound.set(audF, frequency, 18);
}

/*************************************************************************/

void Player::playWaveform(TiaSound::Distortion waveform, int frequency, int volume) {
    int distortion = TiaSound::getDistortionInt(waveform);
    setChannel0(distortion, frequency, volume);
    mode = PlayMode::Waveform;
}

/*************************************************************************/

void Player::playTrack(int start1, int start2) {
    pTrack->lock();
    trackCurNoteIndex[0] = pTrack->getNoteIndexInPattern(0, start1);
    trackCurNoteIndex[1] = pTrack->getNoteIndexInPattern(1, start2);
    trackCurEntryIndex[0] = pTrack->getSequenceEntryIndex(0, start1);
    trackCurEntryIndex[1] = pTrack->getSequenceEntryIndex(1, start2);
    trackCurTick = 0;
    trackMode[0] = Track::Note::instrumentType::Hold;
    trackMode[1] = Track::Note::instrumentType::Hold;
    // InstrumentNumber -1 means: No note yet
    Track::Note startNote(Track::Note::instrumentType::Instrument, -1, -1);
    trackCurNote[0] = startNote;
    trackCurNote[1] = startNote;
    trackIsOverlay[0] = false;
    trackIsOverlay[1] = false;
    mode = PlayMode::Track;
    isFirstNote = true;
    pTrack->unlock();
}

/*************************************************************************/

void Player::selectedChannelChanged(int newChannel) {
    channelSelected = newChannel;
}

/*************************************************************************/

void Player::toggleLoop(bool toggle) {
    loopPattern = toggle;
}

/*************************************************************************/

void Player::setTVStandard(int iNewStandard) {
    replayTvStandard = static_cast<TiaSound::TvStandard>(iNewStandard);
    if (replayTvStandard == TiaSound::TvStandard::PAL) {
        setFrameRate(50.0);
    } else {
        setFrameRate(60.0);
    }
}

/*************************************************************************/

void Player::updateSilence() {
    setChannel(0, 0, 0, 0);
    setChannel(1, 0, 0, 0);
}

/*************************************************************************/

void Player::updateInstrument() {
    /* Play current frame */
    // Check if instrument has changed and made currentFrame illegal
    if (currentInstrumentFrame >= currentInstrument->getEnvelopeLength()) {
        // Go into silence if currentFrame is illegal
        mode = PlayMode::None;
    } else {
        int CValue = currentInstrument->getAudCValue(currentInstrumentFrequency);
        int realFrequency = currentInstrumentFrequency <= 31 ? currentInstrumentFrequency : currentInstrumentFrequency - 32;
        int FValue = realFrequency + currentInstrument->frequencies[currentInstrumentFrame];
        // Check if envelope has caused an underrun
        if (FValue < 0) {
            FValue = 256 + FValue;
        }
        int VValue = currentInstrument->volumes[currentInstrumentFrame];
        setChannel0(CValue, FValue, VValue);

        /* Advance frame */
        currentInstrumentFrame++;
        // Check for end of sustain or release
        if (mode != PlayMode::InstrumentOnce
                && currentInstrumentFrame == currentInstrument->getReleaseStart()) {
            currentInstrumentFrame = currentInstrument->getSustainStart();
        } else if (currentInstrumentFrame == currentInstrument->getEnvelopeLength()) {
            // End of release: Go into silence
            mode = PlayMode::None;
        }
    }
}

/*************************************************************************/

void Player::updatePercussion() {
    /* Play current frame */
    if (currentPercussionFrame >= currentPercussion->getEnvelopeLength()) {
        // Go into silence if currentFrame is illegal
        mode = PlayMode::None;
    } else {
        TiaSound::Distortion waveform = currentPercussion->waveforms[currentPercussionFrame];
        int CValue = TiaSound::getDistortionInt(waveform);
        int FValue = currentPercussion->frequencies[currentPercussionFrame];
        int VValue = currentPercussion->volumes[currentPercussionFrame];
        setChannel0(CValue, FValue, VValue);

        /* Advance frame. End of waveform is handled next frame */
        currentPercussionFrame++;
    }
}

/*************************************************************************/

void Player::sequenceChannel(int channel) {
    if (mode == PlayMode::None) {
        return;
    }
    // Get next note if not first one and not in overlay mode
    if (!isFirstNote && !trackIsOverlay[channel]
            && !pTrack->getNextNoteWithGoto(channel, &(trackCurEntryIndex[channel]), &(trackCurNoteIndex[channel]),
                                            channel == channelSelected && loopPattern)) {
        mode = PlayMode::None;
        return;
    }
    int patternIndex = pTrack->channelSequences[channel].sequence[trackCurEntryIndex[channel]].patternIndex;
    Track::Note *nextNote = &(pTrack->patterns[patternIndex].notes[trackCurNoteIndex[channel]]);
    // Parse and validate next note
    switch(nextNote->type) {
    case Track::Note::instrumentType::Hold:
        break;
    case Track::Note::instrumentType::Instrument:
        // No need to do anything if it has been pre-fetched by overlay already
        if (!trackIsOverlay[channel]) {
            trackMode[channel] = Track::Note::instrumentType::Instrument;
            trackCurNote[channel] = *nextNote;
            trackCurEnvelopeIndex[channel] = 0;
        }
        break;
    case Track::Note::instrumentType::Pause:
        if (trackMode[channel] != Track::Note::instrumentType::Instrument) {
            mode = PlayMode::None;
            updateSilence();
            emit invalidNoteFound(channel, trackCurEntryIndex[channel], trackCurNoteIndex[channel],
                                  "A pause can follow only after a melodic instrument!");
        } else {
            trackMode[channel] = Track::Note::instrumentType::Hold;
            Track::Instrument *curInstrument = &(pTrack->instruments[trackCurNote[channel].instrumentNumber]);
            trackCurEnvelopeIndex[channel] = curInstrument->getReleaseStart();
        }
        break;
    case Track::Note::instrumentType::Percussion:
        trackMode[channel] = Track::Note::instrumentType::Percussion;
        trackCurNote[channel] = *nextNote;
        trackCurEnvelopeIndex[channel] = 0;
        break;
    case Track::Note::instrumentType::Slide:
        if (trackMode[channel] != Track::Note::instrumentType::Instrument) {
            mode = PlayMode::None;
            updateSilence();
            emit invalidNoteFound(channel, trackCurEntryIndex[channel], trackCurNoteIndex[channel],
                                  "A slide can follow only after a melodic instrument!");
        } else {
            trackCurNote[channel].value += nextNote->value;
            // Check for over-/underflow
            if (trackCurNote[channel].value < 0
                    || (trackCurNote[channel].value)%32 > 31) {
                mode = PlayMode::None;
                updateSilence();
                emit invalidNoteFound(channel, trackCurEntryIndex[channel], trackCurNoteIndex[channel],
                                      "A slide cannot change a frequency value to below 0 or above 31!");
            }
        }
        break;
    }
    trackIsOverlay[channel] = false;
}

/*************************************************************************/

void Player::updateChannel(int channel) {
    if (mode == PlayMode::None) {
        return;
    }
    // Play current note
    if (trackCurNote[channel].instrumentNumber != -1) {
        switch(trackCurNote[channel].type) {
        case Track::Note::instrumentType::Instrument:
        {
            Track::Instrument *curInstrument = &(pTrack->instruments[trackCurNote[channel].instrumentNumber]);
            int CValue = curInstrument->getAudCValue(trackCurNote[channel].value);
            // If at end of release, play silence; otherwise ADSR envelope
            if (trackCurEnvelopeIndex[channel] >= curInstrument->getEnvelopeLength()) {
                setChannel(channel, CValue, 0, 0);
            } else {
                int curFrequency = trackCurNote[channel].value;
                int realFrequency = curFrequency <= 31 ? curFrequency : curFrequency - 32;
                int FValue = realFrequency + curInstrument->frequencies[trackCurEnvelopeIndex[channel]];
                // Check if envelope has caused an underrun
                if (FValue < 0) {
                    FValue = 256 + FValue;
                }
                int VValue = curInstrument->volumes[trackCurEnvelopeIndex[channel]];
                setChannel(channel, CValue, FValue, VValue);
                // Advance frame
                trackCurEnvelopeIndex[channel]++;
                // Check for end of sustain
                if (trackCurEnvelopeIndex[channel] == curInstrument->getReleaseStart()) {
                    trackCurEnvelopeIndex[channel] = curInstrument->getSustainStart();
                }
            }
            break;
        }
        case Track::Note::instrumentType::Percussion:
        {
            Track::Percussion *curPercussion = &(pTrack->percussion[trackCurNote[channel].instrumentNumber]);
            if (!trackIsOverlay[channel]
                    && trackCurEnvelopeIndex[channel] < curPercussion->getEnvelopeLength()) {
                TiaSound::Distortion waveform = curPercussion->waveforms[trackCurEnvelopeIndex[channel]];
                int CValue = TiaSound::getDistortionInt(waveform);
                int FValue = curPercussion->frequencies[trackCurEnvelopeIndex[channel]];
                int VValue = curPercussion->volumes[trackCurEnvelopeIndex[channel]];
                setChannel(channel, CValue, FValue, VValue);
                // Advance frame
                trackCurEnvelopeIndex[channel]++;
                // Check for overlay
                if (trackCurEnvelopeIndex[channel] == curPercussion->getEnvelopeLength()
                        && curPercussion->overlay) {
                    // Get next note
                    if (!pTrack->getNextNoteWithGoto(channel, &(trackCurEntryIndex[channel]), &(trackCurNoteIndex[channel]))) {
                        mode = PlayMode::None;
                    } else {
                        trackIsOverlay[channel] = true;
                        int patternIndex = pTrack->channelSequences[channel].sequence[trackCurEntryIndex[channel]].patternIndex;
                        Track::Note *nextNote = &(pTrack->patterns[patternIndex].notes[trackCurNoteIndex[channel]]);
                        // Only start if melodic instrument
                        if (nextNote->type == Track::Note::instrumentType::Instrument) {
                            // Start in sustain
                            trackMode[channel] = Track::Note::instrumentType::Instrument;
                            trackCurNote[channel] = *nextNote;
                            trackCurEnvelopeIndex[channel] = pTrack->instruments[nextNote->instrumentNumber].getSustainStart();
                        }
                    }
                }
            } else {
                // Percussion has finished, or overlay is active but no instrument followed: Silence
                // Get last AUDC value, to mimick play routine. Needed?
                TiaSound::Distortion waveform = curPercussion->waveforms.last();
                int CValue = TiaSound::getDistortionInt(waveform);
                setChannel(channel, CValue, 0, 0);
            }
            break;
        }
        default:
            break;
        }
    }
}

/*************************************************************************/

void Player::updateTrack() {
    if (--trackCurTick < 0) {
        sequenceChannel(0);
        sequenceChannel(1);
        if (mode == PlayMode::None) {
            return;
        }
        isFirstNote = false;
        if (pTrack->globalSpeed) {
            trackCurTick = trackCurNoteIndex[0]%2 == 0 ? pTrack->oddSpeed - 1 : pTrack->evenSpeed - 1;
        } else {
            int patternIndex = pTrack->channelSequences[0].sequence[trackCurEntryIndex[0]].patternIndex;
            Track::Pattern *curPattern = &(pTrack->patterns[patternIndex]);
            trackCurTick = trackCurNoteIndex[0]%2 == 0 ? curPattern->oddSpeed - 1 : curPattern->evenSpeed - 1;
        }
        int pos1 = pTrack->channelSequences[0].sequence[trackCurEntryIndex[0]].firstNoteNumber
                + trackCurNoteIndex[0];
        int pos2 = pTrack->channelSequences[1].sequence[trackCurEntryIndex[1]].firstNoteNumber
                + trackCurNoteIndex[1];
        emit newPlayerPos(pos1, pos2);
    }
    for (int channel = 0; channel < 2; ++channel) {
        if (!channelMuted[channel]) {
            updateChannel(channel);
        } else {
            setChannel(channel, 0, 0, 0);
        }
    }
}

/*************************************************************************/

void Player::timerFired() {
/*
    // Jitter test statistics
    long elapsed = (long)eTimer->elapsed();
    eTimer->restart();
    elapsedValues.append(elapsed);
    deltas.append(int(20 - elapsed));
    statsNumFrames++;
    if (statsNumFrames >= 120) {
        long delta = std::abs(20 - elapsed);
        statsJitterSum += delta;
        if (delta > statsJitterMax) {
            statsJitterMax = delta;
        }
    }
*/

    pTrack->lock();
    switch (mode) {
    case PlayMode::Instrument:
    case PlayMode::InstrumentOnce:
        updateInstrument();
        break;
    case PlayMode::Waveform:
        break;
    case PlayMode::Percussion:
        updatePercussion();
        break;
    case PlayMode::Track:
        updateTrack();
        break;
    default:
        updateSilence();
    }
    pTrack->unlock();
}

}