Can't get looping SoundSequence to stop playing

, ,

I'm experimenting with SoundSequences using the C API, and I've got it playing some notes in a looping pattern. However, when I tell it to stop playing, it doesn't actually stop. What in fact happens is kind of odd.

If there's a note playing when I call stop, then that note will continue to sustain endlessly. However, the sequence itself keeps playing. It's like there's two sequences playing at the same time, and the stop function only stops one of them, although it doesn't cut off any active notes of the first one.

Here's my test code:

AudioSample *sample1;
PDSynth *synth1;
PDSynthInstrument *inst1;
SoundSequence *seq1;
SequenceTrack *track1;

#define NOTE_C3 (NOTE_C4 - 12)
#define NOTE_D3 (NOTE_C4 - 10)
#define NOTE_G3 (NOTE_C4 - 5)
#define NOTE_G4 (NOTE_C4 + 7)

void DoMusicTest(void)
{
    sample1 = pd->sound->sample->load("audio/tone-instrument");
    
    samplePlayer1 = pd->sound->sampleplayer->newPlayer();
    pd->sound->sampleplayer->setSample(samplePlayer1, sample1);
    
    synth1 = pd->sound->synth->newSynth();
    pd->sound->synth->setSample(synth1, sample1, 2064, 4266);
    
    inst1 = pd->sound->instrument->newInstrument();
    pd->sound->instrument->addVoice(inst1, synth1, 0, 128, 0);
    pd->sound->channel->addSource(pd->sound->getDefaultChannel(), (SoundSource *)inst1);
    
    seq1 = pd->sound->sequence->newSequence();
    track1 = pd->sound->sequence->addTrack(seq1);
    pd->sound->track->setInstrument(track1, inst1);
    pd->sound->sequence->setTempo(seq1, 4);
    
    pd->sound->track->addNoteEvent(track1, 0, 1, NOTE_C4, 0.2f);
    pd->sound->track->addNoteEvent(track1, 2, 1, NOTE_C4 + 2, 0.2f);
    pd->sound->track->addNoteEvent(track1, 4, 1, NOTE_C4 + 4, 0.2f);
    pd->sound->track->addNoteEvent(track1, 5, 1, NOTE_C4 + 6, 0.2f);
    pd->sound->track->addNoteEvent(track1, 6, 1, NOTE_C4 + 8, 0.2f);
    
    pd->sound->track->addNoteEvent(track1, 8, 1, NOTE_C3, 0.2f);
    pd->sound->track->addNoteEvent(track1, 10, 1, NOTE_C3 + 2, 0.2f);
    pd->sound->track->addNoteEvent(track1, 12, 1, NOTE_C3 + 4, 0.2f);
    pd->sound->track->addNoteEvent(track1, 13, 1, NOTE_C3 + 6, 0.2f);
    pd->sound->track->addNoteEvent(track1, 14, 1, NOTE_C3 + 8, 0.2f);
    
    pd->sound->track->addNoteEvent(track1, 16, 1, NOTE_G4, 0.2f);
    pd->sound->track->addNoteEvent(track1, 18, 1, NOTE_G4 + 2, 0.2f);
    pd->sound->track->addNoteEvent(track1, 20, 1, NOTE_G4 + 4, 0.2f);
    pd->sound->track->addNoteEvent(track1, 21, 1, NOTE_G4 + 6, 0.2f);
    pd->sound->track->addNoteEvent(track1, 22, 1, NOTE_G4 + 8, 0.2f);
    
    pd->sound->track->addNoteEvent(track1, 24, 1, NOTE_D3, 0.2f);
    pd->sound->track->addNoteEvent(track1, 26, 1, NOTE_D3 + 2, 0.2f);
    pd->sound->track->addNoteEvent(track1, 28, 1, NOTE_D3 + 4, 0.2f);
    pd->sound->track->addNoteEvent(track1, 29, 1, NOTE_D3 + 6, 0.2f);
    pd->sound->track->addNoteEvent(track1, 30, 1, NOTE_D3 + 8, 0.2f);
    
    pd->sound->sequence->setLoops(seq1, 0, 31, 0);
    pd->sound->sequence->play(seq1, 0, 0);
}

void OnAButtonPressed(void)
{
    pd->sound->sequence->stop(seq1);
    printLog("is playing! %d", pd->sound->sequence->isPlaying(seq1));
}

DoMusicTest() gets called before the game loop starts, and naturally OnAButtonPressed() gets called when I press and release the A button. When I do so, it prints is playing! 0 indicating that playback has stopped. And yet it keeps playing!

What am I doing wrong? Any assistance is much appreciated!

Okay, this turned out to be a simple one. I was accidentally calling DoMusicTest twice. Nothing to see here, move along!

For posterity: it turns out that if you simultaneously play two identical sequences with two identical PDSynths referencing the same sample, then you can get a strange error where telling one of the sequences to stop will cause the instrument to keep sustaining forever!

1 Like