Problem with Audio Peak-Object

Status
Not open for further replies.

Ben

Well-known member
Hello,

I am currently trying to explore the new Audio Library and I'm stuck with a problem I can't resolve myself.

I'm using a DC-object to feed a Peak-object (and a I2S-Output, which is currently commented out).

I control the DC over Serial, but when I do peak.read() it returns -1.0, no matter what value I set the DC-Source. In Addition, peak.available() never returns true.

This is the code:

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

// GUItool: begin automatically generated code
AudioSynthWaveformDc     dc1;            //xy=354,281
AudioAnalyzePeak         peak1;          //xy=529,230
//AudioOutputI2S           i2s1;           //xy=531,283
//AudioConnection          patchCord1(dc1, 0, i2s1, 0);
//AudioConnection          patchCord2(dc1, 0, i2s1, 1);
AudioConnection          patchCord3(dc1, peak1);
// GUItool: end automatically generated code


int serial_int;
float serial_float;

void setup(){
  Serial.begin(9600);
  Serial.setTimeout(20);
  delay(5000); //Time to open Serial Monitor on Windows Systems
  Serial.println("Enter any integer between -32768 and 32767.");
  Serial.println("First valid integer in string is used. Rest is discarded.");
  Serial.println("");
}

void loop(){
  if (Serial.available()){
    serial_int = Serial.parseInt();
    while (Serial.available()) Serial.read();
    serial_int = constrain(serial_int, -32768, 32767);
    //constrain to 16 bit signed
    serial_float = float(serial_int);
    serial_float /= 32768.0;
    Serial.print("New DC level: ");
    Serial.println(serial_float);
    dc1.amplitude(serial_float);
    //even delay(100); here does not help
    if (peak1.available()){
      Serial.print("Readback value from peak1: ");
    }
    else{
      Serial.print("peak1 does NOT return available == true and reads: ");
    }
    Serial.println(peak1.read());
    Serial.println("");
  }
  delay(1);
}

Using Arduino 1.0.6 with Teensyduino 1.20 on Windows 7 and a Teensy 3.1 (from OSH-Park with purple (?) Solder Mask ;) )

Can anyone reproduce the problem or is it me?

Ben
 
Hi Ben,

I didn't check this thoroughly, but to start with it looks like you need to declare audio memory in setup(). I pulled this from the Peak example, but you might need more than 4 blocks.
Code:
AudioMemory(4);
I love my OSHPurple Teensy too! ;)
 
You must have at least one input or output object. The library doesn't actually run with only the internal objects. The input & output objects are special, causing the entire library to actually run.

The web-based design tool really ought to "know" this and either warn you or automatically add one.
 
Thanks to both of you. You both were right.
Of course I had to add AudioMemory(), and when I un-commented the I2S output, peak1.available() began to return true.
However the peak-object seems to behave strangely (to me). I presumed** it would return the highest value (unsigned) it ever read on it's input since the last call of peak.read(), bit it seems to return the biggest value difference instead. So i can't use it to read back the dc signal:(.
My whole intention in this was to do a clipping detection, but with peak's current behaviour that seems impossible. Maybe some kind of "maxValue" or "maxLevel" object would be helpful?


Ben

** I'm not a native English speaker, so maybe I just misunderstood :rolleyes:

Edit: grammar
 
I just played with the Peak Mono example using my analog Mic...
The peak function returns the value of the greatest amplitude in the latest sample. I'm guessing you'll need to move that sample into an array of previous values.
I'm actually looking into analyzing past samples. And how to set up a rolling-buffer (I'm not clear what these are called, actually!) but check this out:

Statistics functions in the arm math library:
http://www.keil.com/pack/doc/CMSIS/DSP/html/group___max.html#ga5b89d1b04575aeec494f678695fb87d8
(would return the max of the peak sample array)
 
Status
Not open for further replies.
Back
Top