randomly playing sound files. AudioZero.play freezes after 25 files
JimWeaverOK opened this issue · 1 comments
I've made an sound player using the Arduino simple sound player hardware and software as a guide. I need the player to randomly select sound files and play them at semi random intervals. The code works for the first 25 times, then freezes. Is there a limit within AudioZero.play()? Is there a way to reset the library? or an internal limit?
I've attached my code:
-
the method randomSound() picks the file from a selection of 16 files
-
all 16 files are known to play
-
random Sound() calls playFile() with the selected file, after 25 files, AudioZero.play does not respond and hangs the code.
-
(The last line with commented while(true); glitches the speaker. Doesn't impact playing the first 25 files.)
/*
* Touch the art script
* Adapted by Randy and Jim Weaver.
*
* Audio player code adapted from Arduino tutorial code found at
* https://docs.arduino.cc/tutorials/generic/simple-audio-player
* note: pins 1 and 8 of the lm386 amplifier should not be connected
* note: use 8.3 convention for sound file names 8 characters in file name; 3 in extension
*
* adapted by Randy And Jim Weaver.
*
*
********************* Audio Player Hardware Setup ****************************
* The setup includes an amplifier circuit - see README.md for setup
*/
#include <SD.h>
#include <SPI.h>
#include <AudioZero.h>
bool light_builtin_status = false;
const int chipSelect = SDCARD_SS_PIN;
int iCount = 0;
long longRandom;
//seven and a half minutes in milliseconds = 7.5*60*1000 = 450,000
long lInterval;
long lIntervalMax;
long lIntervalMin;
long lIncrement;
long lIntervalOld;
void setup()
{
// ******************** AUDIO PLAYER SETUP **********************************
delay(150);
Serial.println("Starting Setup");
// set pins for leds to OUTPUT
pinMode(LED_BUILTIN, OUTPUT);
// debug output at 115200 baud
Serial.begin(9600);
// setup SD-card
Serial.println("Initializing SD card...");
//
if (!SD.begin(chipSelect)) {
Serial.println("1 failed!");
while(true);
}
Serial.println(" done.");
//set up random generator
//"if analog input pin 0 is unconnected, random analog
//noise will casue the call to randomseed() to generate
//different seed numbers each time the sketch runs.
//randomSeed()will then shuffle the random function." (from arduino documentation)
randomSeed(analogRead(0));
lIntervalMin = 3000;
lIntervalMax = 120000;
lIncrement = (lIntervalMax - lIntervalMin)/50;
lIncrement = 1000;
lInterval = lIntervalMin;
Serial.print("increment ");
Serial.println(lIncrement);
}
void loop()
{
longRandom = random(100);
//set the delay as a fraction of the standard interval
long lDelay = longRandom*lInterval/100;
Serial.print(" ");
Serial.print("delay ");
Serial.println(lDelay);
delay(lDelay);
randomSound();
if (lInterval > lIntervalMax){lInterval = lIntervalMin;}
lIntervalOld = lInterval;
lInterval = lInterval + lIncrement;
Serial.print("next interval ");
Serial.print(lInterval);
Serial.print(" old interval ");
Serial.println(lIntervalOld);
Serial.print(" count ");
Serial.println(iCount);
iCount++;
}
void randomSound()
{
//select a default
String sFile="thunder2.wav";
long lRandomSound = random(0,32);
//pick a random sound
if (0<=lRandomSound && lRandomSound<2){sFile = "geese2.wav";}
if (2<=lRandomSound && lRandomSound<4){sFile = "cicada2.wav";}
if (4<=lRandomSound && lRandomSound<6){sFile = "bison1.wav";}
if (6<=lRandomSound && lRandomSound<8){sFile = "bison2.wav";}
if (8<=lRandomSound && lRandomSound<10){sFile = "car2.wav";}
if (10<=lRandomSound && lRandomSound<12){sFile = "sy02-1.wav";}
if (12<=lRandomSound && lRandomSound<14){sFile = "sy00-1.wav";}
if (14<=lRandomSound && lRandomSound<16){sFile = "sy05-1.wav";}
if (16<=lRandomSound && lRandomSound<18){sFile = "sy06_1.wav";}
if (18<=lRandomSound && lRandomSound<20){sFile = "sy06_2.wav";}
if (20<=lRandomSound && lRandomSound<22){sFile = "sy07_1.wav";}
if (22<=lRandomSound && lRandomSound<24){sFile = "frog2.wav";}
if (24<=lRandomSound && lRandomSound<26){sFile = "coyote2.wav";}
if (26<=lRandomSound && lRandomSound<28){sFile = "rain3.wav";}
if (28<=lRandomSound && lRandomSound<30){sFile = "thunder2.wav";}
if (30<=lRandomSound && lRandomSound<33){sFile = "sirene2.wav";}
Serial.print("sFile ");
Serial.println(sFile);
digitalWrite(LED_BUILTIN, HIGH);
playFile(sFile);
digitalWrite(LED_BUILTIN,LOW);
}
void playFile(String fileName)
{
File soundFile = SD.open(fileName);
if (!soundFile)
{
// if the file didn't open, print an error and stop
Serial.print("error opening ");
Serial.println(fileName);
//return;
while (true);
}
// until the file is not finished
AudioZero.begin(44100);
AudioZero.play(soundFile);
AudioZero.end();
//while(true);
}Hi @JimWeaverOK. Thanks for your interest in this open source project. This issue tracker is only to be used to report bugs or feature requests specific to the source content of the Arduino Language Reference hosted in this repository. This topic is more appropriate for the Arduino Forum. I'm sure we will be able to help you out over there: