I am making a rudimentary sort of beat generator, and have noticed oddities whenever I include a tonesweep. The exact details vary based on the exact effects and timing i am using, but it always involves the audio dropping on one or more of the effects after a few iterations. Here is a simple piece of code to replicate. In this one the triangle wave keeps working, the drums stop, and the tonesweep is replaced by 3 pulses? Not sure where those came from actually. I added the Serial prints to make sure it wasn't an issue with my triggering timing: *1 = drum1, *2 = drum2, -> is the tonesweep, and w1+/w1- mean waveform on/off.
Any ideas what is going on?
Any ideas what is going on?
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
// GUItool: begin automatically generated code
AudioSynthWaveform waveform1; //xy=245,438
AudioSynthSimpleDrum drum2; //xy=252,337
AudioSynthToneSweep tonesweep1; //xy=255,382
AudioSynthSimpleDrum drum1; //xy=262,281
AudioMixer4 mixer1; //xy=528,371
AudioOutputI2S i2s1; //xy=763,360
AudioConnection patchCord1(waveform1, 0, mixer1, 3);
AudioConnection patchCord2(drum2, 0, mixer1, 1);
AudioConnection patchCord3(tonesweep1, 0, mixer1, 2);
AudioConnection patchCord4(drum1, 0, mixer1, 0);
AudioConnection patchCord5(mixer1, 0, i2s1, 0);
AudioConnection patchCord6(mixer1, 0, i2s1, 1);
AudioControlSGTL5000 sgtl5000_1; //xy=242,174
// GUItool: end automatically generated code
byte beat = 0;
int beatLength = 250;
long beatTime = beatLength;
long measureTime;
long measureStart;
long loopMillis;
void setup() {
Serial.begin(115200);
sgtl5000_1.enable();
sgtl5000_1.volume(0.5);
AudioMemory(4);
AudioProcessorUsageMaxReset();
AudioMemoryUsageMaxReset();
drum1.frequency(100);
drum1.length(200);
drum2.frequency(220);
drum2.length(240);
waveform1.begin(0,220,WAVEFORM_TRIANGLE);
measureStart = millis();
}
void loop() {
loopMillis = millis();
measureTime = loopMillis - measureStart;
if (measureTime > beatTime) {
beat++;
if (beat == 16) {
beat = 0;
measureStart = loopMillis;
beatTime = 0; // will be incremented below
Serial.print("Proc = ");
Serial.print(AudioProcessorUsage());
Serial.print(" (");
Serial.print(AudioProcessorUsageMax());
Serial.print("), Mem = ");
Serial.print(AudioMemoryUsage());
Serial.print(" (");
Serial.print(AudioMemoryUsageMax());
Serial.println(")");
Serial.print("millis = ");
Serial.println(millis());
}
if (beat % 2 == 0) {
drum1.noteOn();
Serial.print("*1 ");
}
if (beat % 4 == 0) {
drum2.noteOn();
Serial.print("*2 ");
}
switch(beat) {
case 3 : waveform1.amplitude(0.5);
Serial.print("w1+ ");
break;
case 11 : waveform1.amplitude(0);
Serial.print("w1- ");
break;
case 13 : tonesweep1.play(0.5, 110, 880, 0.5);
Serial.print("-> ");
break;
}
beatTime += beatLength;
}
}