I am using the ArduinoSound Library and want to use the SDWaveFile.cue() function. In my understanding it should play a wavfile starting from the given time. But when I call it it only start from the beginning!
Here is the relevant part of my Code:
void audioCue(int t){
if(t>0 && t<currentAudioFile.duration()){
currentAudioFile.cue(t);
Serial.println("Set playback to:");
Serial.println(currentAudioFile.currentTime());
}else{
Serial.println("Cued Time not valid");
}
}
currentAudioFile is a global var holding the currently played wav file (type: SDWaveFile). Also the wavfile is already playing!
This issue seems very old because I already found the same question from 2018: Arduino Forum
Is this library still in development? I also made an Issue on github but it seams that you dont get a response there.
I also looked into the Code of the Library and it seems that there are few things wrong:
I am looking at the follwing code:
int SDWaveFile::cue(long time)
{
if (time < 0) {
return 1;
}
long offset = (time * _blockAlign) - _dataOffset;
if (offset < 0) {
offset = 0;
}
// make sure it's multiple of 512
offset = (offset / 512) * 512;
if ((uint32_t)offset > _file.size()) {
return 1;
}
_file.seek(offset);
return 0;
}
In the doc it is stated that on failure it returns 0 and on success 1, but isnt it a failure when the offset is bigger than the file size or when the time var is smaller than 0? and why would it call _file.seek() on failure?
Also what is this line doing:
offset = (offset / 512) * 512;
It wont change the offset and therfore doesnt ensure that it is a multiple of 512
Okay I am very impatient, so I fixed the library code! I made a PullRequest and if accepted the function will work! For now you will have to go to youre locally installed Library and open the file src/SDWaveFile.cpp. Ther replace following code:
int SDWaveFile::cue(long time)
{
if (time < 0) {
return 1;
}
long offset = (time * _blockAlign) - _dataOffset;
if (offset < 0) {
offset = 0;
}
// make sure it's multiple of 512
offset = (offset / 512) * 512;
if ((uint32_t)offset > _file.size()) {
return 1;
}
_file.seek(offset);
return 0;
}
with following code:
int SDWaveFile::cue(long time)
{
if (time < 0) {
return 0;
}
long offset = (time * _blockAlign * _sampleRate) + _dataOffset;
if (offset < 0) {
offset = 0;
}
// make sure it's multiple of 512
offset = (offset / 512) * 512;
if ((uint32_t)offset > _file.size()) {
return 0;
}
_file.seek(offset);
return 1;
}
P.S. For the function to work reliable you have to pause the playback, call cue() and then resume the playback!