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

Thread: playsdwav, play from the specific time of the file.

  1. #1
    Junior Member
    Join Date
    Apr 2015

    playsdwav, play from the specific time of the file.

    I wander if there's a possibility to play audio file from the certain time of the file, not from the beginning?
    The second question is accurate syncing of several files while playing.

    Best, Jun

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Sorry, no, but this is on my todo list...

  3. #3

    There is no quick function to do that, but it's pretty simple to add. Depending on your project.
    Also, implementation actually depend of if you are playing WAV or RAW audio files. I suggest to start with RAW files for ease.
    You will have to add a couple of things in the "play_sd_raw.cpp" library file.
    Here is a simplified example for a modified play() public method. My explainations in the comments :
    // dont forget to define it BEFORE play()
    // this work better with teensy 3.5 - 3.6.
    #define B2M (uint32_t)((double)4294967296000.0 / AUDIO_SAMPLE_RATE / 2.0) // 97352592
    // On older teensys use :
    //#define B2M (uint32_t)((double)4294967296000.0 / AUDIO_SAMPLE_RATE_EXACT / 2.0) // 97352592
    bool AudioPlaySdRaw::play(const char *filename, int32_t startPosMs ) // We pass the parameter "startPosMs" that will be used. dont forget to update the .h file too
    #if defined(HAS_KINETIS_SDHC)
    	if (!(SIM_SCGC3 & SIM_SCGC3_SDHC)) AudioStartUsingSPI();
    	rawfile =;
    	if (!rawfile) {
    		//Serial.println("unable to open file");
    		#if defined(HAS_KINETIS_SDHC)
    			if (!(SIM_SCGC3 & SIM_SCGC3_SDHC)) AudioStopUsingSPI();
    		return false;
    	// Get the file size in bytes
    	file_size = rawfile.size();
    	// init the file offset to zero at start, we will modify it later
    	file_offset = 0;
    	// Here we convert the total raw byte size to Milliseconds, assuming your sample rate is 44100khz
    	// Now you get the total duration in MS
    	uint64_t duration = ((uint64_t)file_size * B2M) >> 32;
    	// Because of the byte nature of things, we need to seek the file in discrete increments. If not (arbitrary value) you will hear terrible distortion.
    	// Here, the slice padding is 2 blocks, we calculate the number of blocks in the entire file.
    	uint32_t dataSlicedBlocksNb = dataSize / AUDIO_BLOCK_SAMPLES * 2; // ---------------> You can also write : datasize >> 8;
    	// Be carrefull of the limits! Here I just add 20ms padding on maximum, to prevent unpredicable results.
    	if (startPosMs < duration - 20) {
    		// Now map the range to adapt MS values to a block safe environement
      		file_offset = startPosMs * dataSlicedBlocksNb / duration; 
      		// Then run block by 2 blocks
      		file_offset *= AUDIO_BLOCK_SAMPLES * 2; 
    		// finally, seek the data
    	playing = true;
    	return true;
    Now, for .Wav files. You'II have a lot of coding/dealing with non audio data. But the principle is the same. A good example of how it can be implemented is the TomWhitwell/RadioMusic module. Take a look :

    Hope it help!

  4. #4
    Junior Member
    Join Date
    Apr 2015
    Thank you so much for your reply and it is good starting point for me.
    I'll try it and see if it works.
    Again I do appreciate your help.

    Best, Jun

Posting Permissions

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