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

Status
Not open for further replies.

tombola

Member
///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:
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;
}
 
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.
 
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.
 
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..
 
Status
Not open for further replies.
Back
Top