sample and hold

Status
Not open for further replies.

chandrian

Member
Hello,

First time post so I apologize in advance if the wording is confusing or lacking details.

I was wondering how you might have a system where you can turn on a sample and hold function. Basically I want to be able to sample for 100ms and play that sample, mixed with the current audio input.

I was trying to use feedback delay and a digital input to "take sample" and then hold, but I was not able to figure out how to keep feeding the same sample without adding updated delay signals as well.

Maybe another strategy is much more simple.

Thank you,
Aaron
 
So I found some code to read in to a buffer/queue and play that buffer, but is there a way to get a bigger buffer so that I can have a 100ms sample and keep playing this over? I had an idea of writing the input buffer (queue2) to a larger array that I can save and play over and over but then I will have to play that array somehow while still playing the original audio input at the same time. I could add the audio output (queue1) to a mixer, but then how do I add the array as another input. Also my SD card stopped working so I am unable to use that right now unfortunately.

(I have not added the new array that stores the input)

Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.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, 1, queue2, 0);
AudioConnection          patchCord2(queue1, 0, i2s2, 0);
AudioConnection          patchCord3(queue1, 0, i2s2, 1);
AudioControlSGTL5000     sgtl5000_1;     //xy=446,103
// GUItool: end automatically generated code
int x = 0;
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(){
  for(x=0;x<100;x++)
  {
  if(queue2.available() >= 2){ //input buffer
    memcpy(buffer, queue2.readBuffer(), 256);
     
  }
   while(1)
    {
    queue2.freeBuffer();      
    int16_t *outBuf = queue1.getBuffer(); //
      memcpy(outBuf, buffer, 256);
      queue1.playBuffer();}
    }
    delay(2000);
}
 
Hello again,

So the application I am trying is something like an infinite sustain pedal. Sample the audio or "note" and hold it. You will be able to stop playing the note and it should hold. I also want to mix this note with incoming audio; so essentially you could play different notes while that note is holding.


Here is the code saving to an array and trying to play that. It works for the most part...

I kept bumping up the queue size until I got to:
uint16_t sampleQueue[16384];

and then this did not work (below):
uint16_t sampleQueue[32768];

it just stopped playing audio at that point.

any thoughts?

also i saw other code where this line (below) used queueP + 128 and im not sure why its not 256 since thats the size of the data being copied:
memcpy(queueP + 256*x, queue2.readBuffer(), 256);


please help.



Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
// GUItool: begin automatically generated code

AudioPlayMemory          mySound;
AudioInputI2S            i2s1;           //xy=77,57
AudioRecordQueue         queue2;         //xy=208,57
AudioPlayQueue           queue1;         //xy=327,55
AudioOutputI2S           i2s2;           //xy=459,52
AudioConnection          patchCord1(i2s1, 1, queue2, 0);
AudioConnection          patchCord2(queue1, 0, i2s2, 0);
AudioConnection          patchCord3(queue1, 0, i2s2, 1);
AudioControlSGTL5000     sgtl5000_1;     //xy=446,103
// GUItool: end automatically generated code
int x = 0;
int y = 0;
int z = 0;
  
void setup(){
//  Serial.begin(9600);
  AudioMemory(64);
  sgtl5000_1.enable();
  sgtl5000_1.inputSelect(AUDIO_INPUT_LINEIN);
  sgtl5000_1.volume(0.5);
  sgtl5000_1.lineInLevel(0);
  sgtl5000_1.lineOutLevel(13);
  queue2.begin();
  pinMode(12, INPUT_PULLUP);

}

void loop()
{  
uint16_t sampleQueue[16384];
//uint16_t sampleQueue[32768];
uint16_t *queueP = sampleQueue; 
  //if(!digitalRead(12))
  {
    for(x=0;x<16;x++)
    {
      if(queue2.available() >= 2)
        {
          memcpy(queueP + 256*x, queue2.readBuffer(), 256);
        }
      queue2.freeBuffer();  
    }  
  }
 
    for(y=0;y<16; y++)
    {
        int16_t *outBuf = queue1.getBuffer(); //
        memcpy(outBuf, queueP + y*256, 256);
        queue1.playBuffer();
    }
  
       
}
 
Status
Not open for further replies.
Back
Top