Help with audio delay program

Status
Not open for further replies.

Jaredhello

New member
Hello!

I'll go right out and say I have very very little experience with coding. A friend of mine did a good majority of the work in the program I'll link below, and I basically added a few things here and there and learned a bit about how it works.

Code:
IntervalTimer myTimer;     // Intervaltimer is a library that configures your hardware timers easily. Here, we create an "object" called "myTimer"


#define sample_Hz 16000   // To keep things organized, we use a compiler definition "sample_Hz". 
                          // sample_Hz is your sample rate. 
                          // That is, every instance of "sample_Hz" will be replaced with 16000 upon compile. 
                     
#define bit_depth 12      // the ADC bit depth. 12 bits = 2^12 = 4096 levels

uint8_t buffer[sample_Hz] = {0}; // Create an array of size sample_Hz, and fill it with zeros
uint32_t arrayIndex, replayIndex; // Create some variables, unsigned, 32 bit. arrayIndex will hold your current "write" address, replayIndex will hold your "read" address
const int sensorPin = A1;    // select the input pin for the potentiometer
int sensorValue = 0;  // variable to store the value coming from the sensor
int replay_delay = 0; //variable for the "delay" process

// Setup is an arduino function that runs only once

void setup(void) {
  float sample_period = 1.0/(float)sample_Hz; // Convert Hz to period, because timers require units of seconds. 
                                          // Typecast to (float) so the compiler knows sample_Hz should be treated as a float
    
  analogReadResolution(bit_depth); // Set analogRead resolution
  analogReadAveraging(1); // Set analogRead averaging to 1, (that is, use only 1 sample at a time). Noisy, but fast.
  
  myTimer.begin(sample, sample_period);  // call the myTimer object's begin(function, float period) function, to start your interrupt timer.
}





void loop(void) {
  //do nothing in your main loop. If you wanted to adjust delay, run an lcd screen or something, put that stuff here
    sensorValue = analogRead(sensorPin);
    replay_delay = map(sensorValue, 0, 4096, 6400, 12800);    
}





// Sample is called every sample_period microseconds by 

void sample(void) { // function that expects no arguments, and returns nothing. called by myTimer

  arrayIndex ++; // Incriment arrayIndex by one, otherwise we go nowhere
  
  if(arrayIndex >= sample_Hz){ // if arrayIndex is larger than sample_Hz (that is, you're at the end of your array)...
    arrayIndex = 0; // go back to the start. this makes a circular buffer.
  };
  
  buffer[arrayIndex] = analogRead(A0); // write an analogRead(pin) sample to your array at arrayIndex
  
  int replayIndex = arrayIndex + replay_delay; // fill replayIndex with arrayIndex + the amount of samples you wish to look at in the future
  
  if(replayIndex >= sample_Hz){ // make sure replayIndex isn't out of bounds either
    replayIndex = replayIndex - sample_Hz; 
  };
  
  analogWrite(A14, buffer[replayIndex]); // write your stored sample to the DAC pin
}

Right! So, I'm using a Teensy 3.1. From my understanding, and what my friend has described to me the program above takes a sample from an analog input, and puts it in a buffer array for a delay, and then spits it back out again through the DAC. Think of two sticks spinning in a circle, with one drawing and the other reading. There is also a sensor input (a potentiometer) to adjust the delay of the output, which uses a map function to change the value of the sensor to something more proper in terms of actual delay.

However, in practice, the program does not seem to work. I have an electret microphone with an amplifier hooked up to the analog input pin, with a simple op amplifier to help boost the voltage up a bit. When I hook the output of the DAC pin to my stereo amplifier, nothing is output. I've also read the the output with my oscilloscope, and again, still nothing.

I have no clue how to go about debugging this program, and I'd appreciate anyone's help very very very much. If you could explain what's wrong, why it's wrong and how I could fix it...

and I'm guessing it uses this library http://www.pjrc.com/teensy/td_timing_IntervalTimer.html#interrupts for the intervaltimer bit.

Thank you so much!
 
Can you please explain a little more on what exactly is you aim. I mean what do exactly want to do. Also I would suggest to 1st get readings out of the POT then proceed towards getting delay out of it.
So 1st execute a simple program that will just get the output of the POT and display it on computer.
Then without using the POT just create the delay that you wish to obtain.
Then combine both to get a desired result.
Making them all at once will be very difficult.
Please go through the Page "http://arduino.cc/en/Reference/AnalogRead" for a little understanting on what is happening.
Just run the code on this page with proper connection on the teensy board you should see the values changing in the Serial terminal of the Arduino program.
I hope this will help you.
 
Last edited:
Status
Not open for further replies.
Back
Top