file.seek(pos)

Status
Not open for further replies.
After i dont get any answer to my post " Seek() within a wavefile ??? " I started today to think about it::confused:

I know from this file play_sd_wav.cpp that there are some attributes for playing , stopping etc.

  • bool AudioPlaySdWav::play(const char *filename) {}
  • void AudioPlaySdWav::stop(void) { }

i know from the Simple WAV file player example that i can access to this attributes :
  • playWav1.play(filename);
  • playWav1.stop();

I know that the Teensy Audio Libray is using the Class SD.h .

I know that the SD.h has this here implemented: https://www.arduino.cc/en/Reference/FileSeek
  • file.seek(pos)
  • Parameters
  • file: an instance of the File class (returned by SD.open())
  • pos: the position to which to seek (unsigned long)

My question : Can i used this file.seek(pos) on the top or should i write a new attribute with in play_sd_wav.cpp using this file.seek(pos)????
 
My question : Can i used this file.seek(pos) on the top

No, it's not that simple. If it were this easy, it would have been done long ago.


or should i write a new attribute with in play_sd_wav.cpp using this file.seek(pos)????

WAV files can have other data (RIFF chunks), either before or after the audio samples. Seeking needs to be done based on awareness of the WAV header and location of the RIFF chunk containing the audio samples. This has been on my todo list for quite some time, but it's complicated to do correctly.
 
.... but it's complicated to do correctly.

For my audio project i need randomly seek() function that jumps somewhere in the wave or raw file and plays it back in a loop. The starting cue point dont need to be always correctly. Right now im using the waveshield from adafruit and its working.

I'm thinking if i can try to port this code from the WaveHc Library to the play_sd_wav.cpp or if there is a quick and dirty solution ?

void WaveHC::seek(uint32_t pos) {
// make sure buffer fill interrupt doesn't happen
cli();
if (fd) {
pos -= pos % PLAYBUFFLEN;
if (pos < PLAYBUFFLEN) pos = PLAYBUFFLEN; //don't play metadata
uint32_t maxPos = fd->readPosition() + remainingBytesInChunk;
if (maxPos > fd->fileSize()) maxPos = fd->fileSize();
if (pos > maxPos) pos = maxPos;
if (fd->seekSet(pos)) {
// assumes a lot about the wave file
remainingBytesInChunk = maxPos - pos;
}
}
sei();
}
 
Last edited:
My question : Can i used this file.seek(pos) on the top or should i write a new attribute with in play_sd_wav.cpp using this file.seek(pos)????

fwiw, here's two ways to do this:

1. wait until the header has been parsed, then seek: you'd do this approx. here

2. slightly more complicated, separate the file open / file parse, play/seek, and stop/close operations, and keep a "prefetch" buffer, as was done here. the advantage being it somewhat minimizes the latency if/when files are being played repeatedly, because the current file is being kept open.

NB: i haven't updated those modded libraries in a while, so they might not play nice with more recent versions of the audio library; also: the value passed on to "seek" resp. left shift isn't very scientific, just a quick hack that was sufficient for my purposes.
 
Status
Not open for further replies.
Back
Top