Skip to content

Commit

Permalink
Fix potential thread issues
Browse files Browse the repository at this point in the history
Fix potential thread issues between user input thread and audio thread, like when jamming a sample, toggling tuning tone, playing sample in the sample edtior, adjusting sample loop, etc.
  • Loading branch information
8bitbubsy committed Oct 28, 2021
1 parent 94ae360 commit 6c56bc9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/pt2_audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ void unlockAudio(void)

void mixerUpdateLoops(void) // updates Paula loop (+ scopes)
{
const bool audioWasntLocked = !audio.locked;
if (audioWasntLocked)
lockAudio();

for (int32_t i = 0; i < AMIGA_VOICES; i++)
{
const moduleChannel_t *ch = &song->channels[i];
Expand All @@ -180,6 +184,9 @@ void mixerUpdateLoops(void) // updates Paula loop (+ scopes)
paulaSetLength(i, s->loopLength >> 1);
}
}

if (audioWasntLocked)
unlockAudio();
}

void mixerKillVoice(int32_t ch)
Expand Down
8 changes: 8 additions & 0 deletions src/pt2_edit.c
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,8 @@ void handleSampleJamming(SDL_Scancode scancode) // used for the sampling feature
if (s->length <= 1)
return;

lockAudio();

song->channels[ch].n_samplenum = editor.currSample; // needed for sample playback/sampling line

const int8_t *n_start = &song->sampleData[s->offset];
Expand All @@ -937,6 +939,8 @@ void handleSampleJamming(SDL_Scancode scancode) // used for the sampling feature
// these take effect after the current DMA cycle is done
paulaSetData(ch, NULL);
paulaSetLength(ch, 1);

unlockAudio();
}

void jamAndPlaceSample(SDL_Scancode scancode, bool normalMode)
Expand Down Expand Up @@ -970,6 +974,8 @@ void jamAndPlaceSample(SDL_Scancode scancode, bool normalMode)
// don't play sample if we quantized to another row (will be played in modplayer instead)
if (editor.currMode != MODE_RECORD || !editor.didQuantize)
{
lockAudio();

chn->n_samplenum = editor.currSample;
chn->n_volume = s->volume;
chn->n_period = tempPeriod;
Expand All @@ -994,6 +1000,8 @@ void jamAndPlaceSample(SDL_Scancode scancode, bool normalMode)
// these take effect after the current DMA cycle is done
paulaSetData(ch, chn->n_loopstart);
paulaSetLength(ch, chn->n_replen);

unlockAudio();
}

// normalMode = normal keys, or else keypad keys (in jam mode)
Expand Down
2 changes: 1 addition & 1 deletion src/pt2_header.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "pt2_unicode.h"
#include "pt2_palette.h"

#define PROG_VER_STR "1.36"
#define PROG_VER_STR "1.37"

#ifdef _WIN32
#define DIR_DELIMITER '\\'
Expand Down
8 changes: 8 additions & 0 deletions src/pt2_sampler.c
Original file line number Diff line number Diff line change
Expand Up @@ -1262,13 +1262,17 @@ void toggleTuningTone(void)
if (editor.tuningNote > 35)
editor.tuningNote = 35;

lockAudio();

song->channels[ch].n_volume = 64; // we need this for the scopes

paulaSetPeriod(ch, periodTable[editor.tuningNote]);
paulaSetVolume(ch, 64);
paulaSetData(ch, tuneToneData);
paulaSetLength(ch, sizeof (tuneToneData) / 2);
paulaStartDMA(ch);

unlockAudio();
}
else
{
Expand Down Expand Up @@ -1770,6 +1774,8 @@ static void playCurrSample(uint8_t chn, int32_t startOffset, int32_t endOffset,
assert(chn < AMIGA_VOICES);
assert(editor.currPlayNote <= 35);

lockAudio();

s = &song->samples[editor.currSample];
ch = &song->channels[chn];

Expand Down Expand Up @@ -1818,6 +1824,8 @@ static void playCurrSample(uint8_t chn, int32_t startOffset, int32_t endOffset,
}

updateSpectrumAnalyzer(ch->n_volume, ch->n_period);

unlockAudio();
}

void samplerPlayWaveform(void)
Expand Down

0 comments on commit 6c56bc9

Please sign in to comment.