I have a fix. The disconnect() was not properly releasing a used audio buffer.
In AudioStream.cpp change the disconnect() function to this:
Code:
void AudioConnection::disconnect(void)
{
AudioConnection *p;
if (!isConnected) return;
if (dest_index > dst.num_inputs) return;
__disable_irq();
// Remove destination from source list
p = src.destination_list;
if (p == NULL) {
//>>> PAH re-enable the IRQ
__enable_irq();
return;
}
/*else <<< Don't need an else here */
if (p == this) {
if (p->next_dest) {
src.destination_list = next_dest;
} else {
src.destination_list = NULL;
}
} else {
while (p) {
if (p == this) {
if (p->next_dest) {
p = next_dest;
break;
} else {
p = NULL;
break;
}
}
p = p->next_dest;
}
}
//>>> PAH release the audio buffer properly
//Remove possible pending src block from destination
if(dst.inputQueue[dest_index] != NULL) {
AudioStream::release(dst.inputQueue[dest_index]);
// release() re-enables the IRQ. Need it to be off a little longer
__disable_irq();
dst.inputQueue[dest_index] = NULL;
}
//Check if the disconnected AudioStream objects should still be active
src.numConnections--;
if (src.numConnections == 0) {
src.active = false;
}
dst.numConnections--;
if (dst.numConnections == 0) {
dst.active = false;
}
isConnected = false;
__enable_irq();
}
I tested it with this code which allocates only 2 buffers and generates a 50ms tone in stereo.
Code:
// See https://forum.pjrc.com/threads/54760
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
AudioControlSGTL5000 sgtl;
AudioOutputI2S mainOutput;
AudioSynthWaveform sineWave;
AudioConnection connection1(sineWave,0,mainOutput,0);
AudioConnection connection2(sineWave,0,mainOutput,1);
void setup() {
AudioMemory(2);
sgtl.enable();
sgtl.volume(0.2);
sineWave.begin(0.5,440,WAVEFORM_SINE);
}
// This loop generates a stereo sine wave which is switched rapidly on and off forever
void loop() {
connection1.disconnect();
connection2.disconnect();
delay(50);
connection1.connect();
connection2.connect();
delay(50);
}
Pete