Audio Peak Question

Ceiling

Member
Hey-

I've been trying to use the peak feature to determine if there is audio playing via the line-in. I'd like to make an audio triggered recorder. I tried to use peak.available but it ends up returning as true the first time because even though there is no audio playing, there is still new initial peak data. I also tried reading the peak value and then only start recording when the peak value is greater than the previous value, but that ended up returning true too. It seems like the peak value reached 1.0 relatively quickly.

Does anyone have advice on how I could achieve a recorder that starts recording when the audio is playing via line-in?

Thanks
 
Perhaps add a high-pass filter before the peak detect so that DC offsets are ignored.
 
I wrote this function for your purpose.
It may not be the idealest way but it works.
You can test it and replace the Serial.println() statements with record/stoprecord functions.

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

//Simple audiothroughput from usb to AudioAnalyzePeak
//Usb can be replaced by analog input, audioshield or any other sorce.
AudioInputUSB            usb1;
AudioOutputI2S           i2s1;
AudioAnalyzePeak         ANALOG_IN;
AudioConnection          patchCord1(usb1, ANALOG_IN);
AudioConnection          patchCord2(usb1, 0, i2s1, 0);
AudioConnection          patchCord3(usb1, 1, i2s1, 1);


void setup() {
  AudioMemory(12);
  //For testing there is no need for the audioshield
//  sgtl5000_1.enable();
//  sgtl5000_1.volume(0.6);
}

void loop() {
  audioCheckAnalogAudio();
}

//Threshold value for function to detect signal
const float threshold = 0.010;
//Counts up to this and uses it to determine if there is a signal or only a random noise spike 
//Make value higher for less sensitivity
const int maxCount = 10;
//Time in ms to wait to stop recording after no signal is detected 
const int timeOutfterSignalLost = 3000;

void audioCheckAnalogAudio() {
  static bool recording = false;  
  static elapsedMillis timer = 0;
  static unsigned int count = 0;

  float analogInVal = ANALOG_IN.available() ? ANALOG_IN.read() : 0;
  bool analogHasSignal = (analogInVal > threshold);

  if(!recording && analogHasSignal)
  {
    count++;
    timer = 0;
  }
  else if(recording)
  {
    if(!analogHasSignal)
    {
      if(timer > timeOutfterSignalLost)
      {
          recording = false;
          //Put a function to stop recording here:
          Serial.println("stoprecording();");
      }
    }
    else
    {
      timer = 0;
    }
  }

  if(timer > 300)
  {
    count = 0;
  }

  if(!recording && count > maxCount)
  {
    recording = true;
    //Put a recording function here:
    Serial.println("record();");
  }

}
 
Back
Top