History

Reset sample counter when pressing Play Song (reduces tiny delay)

Also some code cleanup (more sensible variable names).

Olav Sørensen committed 6 years ago

Commit e8a29be1 parent b8597d34 files · +113 −112Browse files

Showing the changes to src (3 of 4 files). Show all

Unified · Split

3 files
  1. src/ft2_audio.c +24 −26
  2. src/ft2_audio.h +2 −1
  3. src/ft2_replayer.c +2 −0
src/ft2_audio.c+24−26
@@ -20,7 +20,7 @@ · 19 unmodified lines
2020
2121 static int8_t pmpCountDiv, pmpChannels = 2;
2222 static uint16_t smpBuffSize;
23−static int32_t masterVol, oldAudioFreq, pmpLeft, randSeed = INITIAL_DITHER_SEED;
23+static int32_t masterVol, oldAudioFreq, randSeed = INITIAL_DITHER_SEED;
2424 static int32_t prngStateL, prngStateR;
2525 static uint32_t tickTimeLen, tickTimeLenFrac;
2626 static float fAudioAmpMul;
@@ -161,7 +161,7 @@ void setSpeed(uint16_t bpm) · 134 unmodified lines
161161 if (bpm > MAX_BPM)
162162 return;
163163
164− audio.speedVal = audio.speedValTab[bpm];
164+ audio.samplesPerTick = audio.speedValTab[bpm];
165165
166166 // get tick time length for audio/video sync timestamp
167167 const uint64_t tickTimeLen64 = audio.tickTimeLengthTab[bpm];
@@ -261,7 +261,7 @@ static void voiceUpdateVolumes(int32_t i, uint8_t status) · 93 unmodified lines
261261 }
262262 else
263263 {
264− v->SVolIPLen = audio.speedVal;
264+ v->SVolIPLen = audio.samplesPerTick;
265265 v->SLVolIP = ((int64_t)destVolL * audio.rampSpeedValMul) >> 32;
266266 v->SRVolIP = ((int64_t)destVolR * audio.rampSpeedValMul) >> 32;
267267 }
@@ -584,8 +584,8 @@ uint32_t mixReplayerTickToBuffer(uint8_t *stream, uint8_t bitDepth) · 316 unmodified lines
584584 voice_t *v, *r;
585585
586586 assert(audio.speedVal <= MAX_WAV_RENDER_SAMPLES_PER_TICK);
587− memset(audio.mixBufferL, 0, audio.speedVal * sizeof (int32_t));
588− memset(audio.mixBufferR, 0, audio.speedVal * sizeof (int32_t));
587+ memset(audio.mixBufferL, 0, audio.samplesPerTick * sizeof (int32_t));
588+ memset(audio.mixBufferR, 0, audio.samplesPerTick * sizeof (int32_t));
589589
590590 // mix channels
591591 v = voice; // normal voices
@@ -594,24 +594,24 @@ uint32_t mixReplayerTickToBuffer(uint8_t *stream, uint8_t bitDepth) · 2 unmodified lines
594594 for (int32_t i = 0; i < song.antChn; i++, v++, r++)
595595 {
596596 // call the mixing routine currently set for the voice
597− if (v->mixRoutine != NULL) v->mixRoutine(v, audio.speedVal); // mix normal voice
598− if (r->mixRoutine != NULL) r->mixRoutine(r, audio.speedVal); // mix volume ramp voice
597+ if (v->mixRoutine != NULL) v->mixRoutine(v, audio.samplesPerTick); // mix normal voice
598+ if (r->mixRoutine != NULL) r->mixRoutine(r, audio.samplesPerTick); // mix volume ramp voice
599599 }
600600
601601 // normalize mix buffer and send to audio stream
602602 if (bitDepth == 16)
603603 {
604604 if (config.specialFlags2 & DITHERED_AUDIO)
605− sendSamples16BitDitherStereo(stream, audio.speedVal, 2);
605+ sendSamples16BitDitherStereo(stream, audio.samplesPerTick, 2);
606606 else
607− sendSamples16BitStereo(stream, audio.speedVal, 2);
607+ sendSamples16BitStereo(stream, audio.samplesPerTick, 2);
608608 }
609609 else
610610 {
611− sendSamples24BitStereo(stream, audio.speedVal, 2);
611+ sendSamples24BitStereo(stream, audio.samplesPerTick, 2);
612612 }
613613
614− return audio.speedVal;
614+ return audio.samplesPerTick;
615615 }
616616
617617 int32_t pattQueueReadSize(void)
@@ -930,18 +930,16 @@ static void fillVisualsSyncBuffer(void) · 312 unmodified lines
930930
931931 static void SDLCALL audioCallback(void *userdata, Uint8 *stream, int len)
932932 {
933− int32_t a, b;
934−
935933 assert(len < 65536); // limitation in mixer
936934 assert(pmpCountDiv > 0);
937935
938− a = len / pmpCountDiv;
939− if (a <= 0)
936+ int32_t samplesLeft = len / pmpCountDiv;
937+ if (samplesLeft <= 0)
940938 return;
941939
942− while (a > 0)
940+ while (samplesLeft > 0)
943941 {
944− if (pmpLeft == 0)
942+ if (audio.tickSampleCounter == 0)
945943 {
946944 // new replayer tick
947945
@@ -954,20 +952,20 @@ static void SDLCALL audioCallback(void *userdata, Uint8 *stream, int len) · 6 unmodified lines
954952 mix_UpdateChannelVolPanFrq();
955953 fillVisualsSyncBuffer();
956954
957− pmpLeft = audio.speedVal;
955+ audio.tickSampleCounter = audio.samplesPerTick;
958956
959957 replayerBusy = false;
960958 }
961959
962− b = a;
963− if (b > pmpLeft)
964− b = pmpLeft;
960+ int32_t samplesToMix = samplesLeft;
961+ if (samplesToMix > audio.tickSampleCounter)
962+ samplesToMix = audio.tickSampleCounter;
965963
966− mixAudio(stream, b, pmpChannels);
967− stream += b * pmpCountDiv;
964+ mixAudio(stream, samplesToMix, pmpChannels);
965+ stream += samplesToMix * pmpCountDiv;
968966
969− a -= b;
970− pmpLeft -= b;
967+ samplesLeft -= samplesToMix;
968+ audio.tickSampleCounter -= samplesToMix;
971969 }
972970
973971 (void)userdata;
@@ -1225,7 +1223,7 @@ bool setupAudio(bool showErrorMsg) · 251 unmodified lines
12251223
12261224 stopAllScopes();
12271225
1228− pmpLeft = 0; // reset sample counter
1226+ audio.tickSampleCounter = 0; // zero tick sample counter so that it will instantly initiate a tick
12291227
12301228 calcReplayRate(audio.freq);
12311229
src/ft2_audio.h+2−1
@@ -51,8 +51,9 @@ typedef struct audio_t · 50 unmodified lines
5151 int32_t inputDeviceNum, outputDeviceNum, lastWorkingAudioFreq, lastWorkingAudioBits;
5252 int32_t quickVolSizeVal, *mixBufferL, *mixBufferR, *mixBufferLUnaligned, *mixBufferRUnaligned;
5353 int32_t rampQuickVolMul, rampSpeedValMul, speedValTab[MAX_BPM+1], rampSpeedValMulTab[MAX_BPM+1];
54+ int32_t tickSampleCounter;
5455 uint32_t freq;
55− uint32_t audLatencyPerfValInt, audLatencyPerfValFrac, speedVal, musicTimeSpeedVal;
56+ uint32_t audLatencyPerfValInt, audLatencyPerfValFrac, samplesPerTick, musicTimeSpeedVal;
5657 uint64_t tickTime64, tickTime64Frac, tickTimeLengthTab[MAX_BPM+1];
5758 double dAudioLatencyMs;
5859 SDL_AudioDeviceID dev;
src/ft2_replayer.c+2−0
@@ -2780,6 +2780,8 @@ void startPlaying(int8_t mode, int16_t row) · 2779 unmodified lines
27802780 if (song.tempo == 0)
27812781 song.tempo = song.initialTempo;
27822782
2783+ audio.tickSampleCounter = 0; // zero tick sample counter so that it will instantly initiate a tick
2784+
27832785 unlockMixerCallback();
27842786
27852787 ui.updatePosSections = true;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t