Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 5 of 5

Thread: file.seek(pos)

  1. #1
    Junior Member
    Join Date
    Dec 2016
    Location
    Berlin
    Posts
    12

    file.seek(pos)

    After i dont get any answer to my post " Seek() within a wavefile ??? " I started today to think about it:

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

    • bool AudioPlaySdWav:lay(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)????

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    21,323
    Quote Originally Posted by Jupiter Moll View Post
    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.

  3. #3
    Junior Member
    Join Date
    Dec 2016
    Location
    Berlin
    Posts
    12
    Quote Originally Posted by PaulStoffregen View Post
    .... 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 by Jupiter Moll; 01-04-2017 at 10:04 PM.

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    21,323
    The quick and dirty solution is using raw format instead of wav. Then take a look at this:

    https://github.com/TomWhitwell/Radio...teensy%20files

  5. #5
    Senior Member
    Join Date
    Feb 2013
    Posts
    563
    Quote Originally Posted by Jupiter Moll View Post

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •