I wan't to play a sound file for a well timed period. I have for example a sound file of 20 seconds which I want to play for 5 milliseconds. Right now I'm using the following for this:
- ESP Wemos D32 pro (with SD card reader on board)
- i2s breakout: https://www.adafruit.com/product/3678
- i2s library (see attachment)
Please find my code below. It seems that I can only play the complete file or nothing. I can't stop the i2s.write() on a specific time. I also played around with the buffer, data[], .. but I can't get it right.
Any suggestions on how to solve this? I'm open to new libraries, idea's, ....
For the moment I solved the problem by making the sound files to the desired length. This is clearly not the most efficient way of reaching my goal.
Code: Select all
#include <WiFi.h>
#include "I2S.h"
#include <FS.h>
#include <SD.h>
#include <Wire.h>
#define SD_CS 4 // Chiçp select pin
#define AMP_SD 32 // Amplifier shut down pin
int intervalStartTime = 0;
int audioStartTime = 0;
const int offset = 0x2C;
char data[150016];
int number = 0;
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_OFF);
pinMode(SD_CS, OUTPUT);
digitalWrite(SD_CS, HIGH);
pinMode(AMP_SD, OUTPUT);
digitalWrite(AMP_SD, HIGH);
if (!SD.begin(SD_CS)) {
Serial.println("SD mount failed!");
}
else {
Serial.println("SD mount succes!");
}
xTaskCreatePinnedToCore(output, "output", 10000, NULL, 1, NULL, 0);
I2S_Init();
File file = SD.open("/TB-250Hz.wav"); // 44100Hz, 16bit, stereo, linear PCM
Serial.println(file.size());
Serial.println(sizeof(char));
file.seek(offset);
file.readBytes(data, sizeof(data));
file.close();
}
void output (void * parameters) {
Serial.printf("Output loop pinned to core \t %d\n", xPortGetCoreID());
for (;;) {
// Play audio
audioStartTime = millis();
I2S_Write(data, sizeof(data));
Serial.println(millis() - audioStartTime);
while (millis() - audioStartTime < 5) {
}
Serial.println("..");
}
}
void loop() {
}