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

Thread: Audio: How to start SD files from a particular point, not the start

  1. #1
    Junior Member
    Join Date
    Oct 2014
    Posts
    8

    Audio: How to start SD files from a particular point, not the start

    ///Update below, progress made///

    I'd like to be able to play Wav files from SD, not from the start, but from arbitrary points in the file.

    I'm experimenting with play_sd_raw in the library (because the RAW code seems to be simpler). It's working fine in terms of loading and playing files, but I can't get it to trigger mid-file. I'm using the Audio shield.

    Here's what I've tried:

    I added this function to play_sd_raw.cpp:

    Code:
    bool AudioPlaySdRaw::playFrom(const char *filename, unsigned long start)
    {
    	stop();
    	AudioStartUsingSPI();
    	__disable_irq();
    	rawfile = SD.open(filename);
    	__enable_irq();
    	if (!rawfile) {
    		//Serial.println("unable to open file");
    		return false;
    	}
    	file_size = rawfile.size();
    	file_offset = start;
    	//Serial.println("able to open file");
    	playing = true;
    	return true;
    }
    I was trying to control the file_offset variable, thinking that is the 'play head' for the wav. This file compiles and plays fine, and can be called (my full sketch below), but it behaves exactly the same as the normal play(filename) command - in the sketch below, it plays the first two seconds of the file over and over.

    Have I missed something obvious? - or is it more difficult than it seems...

    Full sketch:

    Code:
    // Simple WAV file player example
    //
    // Requires the audio shield:
    //   http://www.pjrc.com/store/teensy3_audio.html
    //
    // Data files to put on your SD card can be downloaded here:
    //   http://www.pjrc.com/teensy/td_libs_AudioDataFiles.html
    //
    // This example code is in the public domain.
    
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    
    // GUItool: begin automatically generated code
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    
    // GUItool: begin automatically generated code
    AudioPlaySdRaw           playRaw1;       //xy=183,98
    AudioPlaySdRaw           playRaw2;       //xy=217,155
    AudioOutputI2S           i2s1;           //xy=346,101
    AudioConnection          patchCord1(playRaw1, 0, i2s1, 0);
    AudioConnection          patchCord2(playRaw2, 0, i2s1, 1);
    
    AudioControlSGTL5000     sgtl5000_1;     //xy=240,153
    // GUItool: end automatically generated code
    
    void setup() {
      Serial.begin(9600);
    
      // Audio connections require memory to work.  For more
      // detailed information, see the MemoryAndCpuUsage example
      AudioMemory(5);
    
      sgtl5000_1.enable();
      sgtl5000_1.volume(0.5);
    
      SPI.setMOSI(7);
      SPI.setSCK(14);
      if (!(SD.begin(10))) {
        // stop here, but print a message repetitively
        while (1) {
          Serial.println("Unable to access the SD card");
          delay(500);
        }
      }
    }
    
    void playFiles(const char *filename,const char *filename2)
    {
    
    
      uint32_t randnum = random(1000000000000);
      uint32_t randnum2 = random(1000000000000);
    
      playRaw1.playFrom(filename,randnum);
       playRaw2.playFrom(filename2,randnum2);
    
    }
    
    
    void loop() {
        
      playFiles("R1.RAW", "R2.RAW");
      delay(2000);
    
    }
    Last edited by tombola; 10-10-2014 at 01:46 AM.

  2. #2
    Junior Member
    Join Date
    Oct 2014
    Posts
    8
    OK, I realised that file_offset is just a pointer.
    I added a rawfile.seek(startPoint);
    This now just about works.
    It's very rough, I think file_offset may now be messed up - I seem to get some audio dropouts.


    Code:
    bool AudioPlaySdRaw::playFrom(const char *filename, unsigned long startPoint)
    {
    	stop();
    	AudioStartUsingSPI();
    	__disable_irq();
    	rawfile = SD.open(filename);
    	__enable_irq();
    	if (!rawfile) {
    		//Serial.println("unable to open file");
    		return false;
    	}
    	file_size = rawfile.size();
    	rawfile.seek(startPoint);
    	file_offset = startPoint;
    
    	//Serial.println("able to open file");
    	playing = true;
    	return true;
    }

  3. #3
    Senior Member
    Join Date
    Feb 2013
    Posts
    563
    this is what i'm doing (in play_sd_wav), it works fine for stereo 16bit / 44.1k; haven't tried with other formats:

    http://forum.pjrc.com/threads/24793-...ll=1#post55274


    edit: NB: the offset is given in 512 byte sectors. not sure though why doing seek in "PlayFrom" wouldn't work with sd_raw. i'm doing the seek in consume, otherwise it would interfere with the header check
    Last edited by mxxx; 10-10-2014 at 09:33 AM.

  4. #4
    Junior Member
    Join Date
    Oct 2014
    Posts
    8
    Hi Mxxx - loving your work! I'm doing something similar but probably simpler and smaller, could possibly end up as another Eurorack project...

    Learning about bitwise operators has helped a lot.

  5. #5
    Senior Member
    Join Date
    Feb 2013
    Posts
    563
    Quote Originally Posted by tombola View Post
    Hi Mxxx - loving your work! I'm doing something similar but probably simpler and smaller, could possibly end up as another Eurorack project...

    Learning about bitwise operators has helped a lot.
    did you get it working? ... wondering because it would be convenient to have some sort of "seek" be part of the official library at some point.

    and thanks. the thing is quite simple in fact ... but i can see a 4HP ish little module might be more than enough.

  6. #6
    Senior Member
    Join Date
    Dec 2014
    Posts
    134
    I have this for RAW, did you guys get it good for WAV? we need to start pull requesting all this useful stuff and get it into the bistro.. there are many cool and extremely useful objects now that are floating through the forum..

  7. #7
    Senior Member
    Join Date
    Feb 2013
    Posts
    563
    Quote Originally Posted by MacroMachines View Post
    I have this for RAW, did you guys get it good for WAV? we need to start pull requesting all this useful stuff and get it into the bistro.. there are many cool and extremely useful objects now that are floating through the forum..
    here's one way: https://github.com/mxmxmx/eurotrash_...ibraries/Audio

    that's coming with some primitive prefetch thing, too.

Posting Permissions

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