AudioPlayQueue getBuffer(): How to get audio into and out of the queue objects?

Status
Not open for further replies.

MacroMachines

Well-known member
I am currently working on a sample recorder/player. Initially I started modifying the example for SD raw record, and it works pretty well.
My collaborator Brad and I are in the process of adding additional functionality into the raw playback part of the library to allow for reverse and pitch playback, etc...

Along the way I thought maybe it might be a more flexible and better route to use the queue playback object to actually push out the audio and allow for manipulation to a buffer. I have been looking up how to go about using the queue play on the forum and on the Octo LED video screen example linked from the Audio System Design Tool. I haven't found anything particularly helpful, the Octo example has this relevant section of the code:

Code:
else if (header[0] == '%') {
        // found a chunk of audio data
        unsigned int size = (header[1] | (header[2] << 8)) * 2;
        // Serial.printf("a: %u", size);
	// Serial.println();
	while (size > 0) {
	  unsigned int len = size;
	  if (len > 256) len = 256;
	  int16_t *p = audio.getBuffer();
	  if (!sd_card_read(p, len)) {
	    error("unable to read audio frame data");
            return;
	  }
	  if (len < 256) {
            for (int i=len; i < 256; i++) {
              *((char *)p + i) = 0;  // fill rest of buffer with zero
            }
	  }
          audio.playBuffer();
	  size -= len;
	}

Which was all I could find that was relevant. I do not understand exactly what I am meant to take away from using this *p pointer = audio.getBuffer(); I thought maybe it is setting a pointer to the buffer space that is in the audio library memory pool, and "if (!sd_card_read(p, len)) " is reading the data into that buffer pointer, am I correct?

so here is my very simple test code to see how to pull from the queue in, and push to the queue out:

Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>

// GUItool: begin automatically generated code
AudioInputI2S            i2s1;           //xy=77,57
AudioRecordQueue         queue2;         //xy=208,57
AudioPlayQueue           queue1;         //xy=327,55
AudioOutputI2S           i2s2;           //xy=459,52
AudioConnection          patchCord1(i2s1, 0, queue2, 0);
AudioConnection          patchCord2(queue1, 0, i2s2, 0);
AudioControlSGTL5000     sgtl5000_1;     //xy=446,103
// GUItool: end automatically generated code

uint16_t buffer[512];

void setup(){
  AudioMemory(64);
  sgtl5000_1.enable();
  sgtl5000_1.inputSelect(AUDIO_INPUT_LINEIN);
  sgtl5000_1.volume(0.8);
  sgtl5000_1.lineInLevel(0);
  sgtl5000_1.lineOutLevel(13);
  queue2.begin();
}

void loop(){
  if(queue2.available() >= 2){ //input buffer
    memcpy(buffer, queue2.readBuffer(), 256);
    queue2.freeBuffer();
    int16_t *outBuf = queue1.getBuffer(); //
    memcpy(outBuf, buffer, 256);
    queue1.playBuffer();}
}

Is this correct? It seems like its working properly, but I can't be sure.. I am going to see if I can transform the buffer somehow.. Is it possible for me to make a much larger (maybe like 1/2 second or more) buffer in the arduino code for more complex manipulation and possibly granular type playback?
 
Status
Not open for further replies.
Back
Top