Audio Library Synth Generator to AudioRecordQueue, Teensy 4.0 w/ Audio Shield

Status
Not open for further replies.

jjvande

Member
Hi all,

For a school project I'm working on, I need to be able to generate a waveform with the Teensy 4.0 and copy to AudioRecordQueue to do some manipulations of the array before outputting sound (white noise, sine, etc., it doesn't matter at this point what type).
I've had success in the past using AudioRecordQueue with mic inputs (i2s input). I'd like to do the same thing, but I'm not getting results as I would expect.

My code is below. I'm just generating white noise, sending it to an AudioRecordQueue object, copying the values from the record queue object to the play queue object, and repeating.

What I get is maybe 200ms of noise and then silence. I also added in some Serial.print lines to debug where it's hanging up, but I'm not getting consistent outputs with this.

Am I misunderstanding how AudioSynthNoiseWhite works by assuming I can use it with AudioRecordQueue the same as the i2s mic input?

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

AudioSynthNoiseWhite noise; //white noise source
AudioRecordQueue Q_1_rec_noise; //object for recording white noise source to array
AudioPlayQueue Q_1_play;  //object to write array to for output
AudioOutputI2S i2s_out; //Audio output
AudioConnection patchcord0(noise,0,Q_1_rec_noise,0);
AudioConnection patchcord1(Q_1_play,0,i2s_out,0);
AudioConnection patchcord2(Q_1_play,0,i2s_out,1);
AudioControlSGTL5000 sgtl5000;

const uint32_t blockSize=AUDIO_BLOCK_SAMPLES;
int16_t * ptr_in_noise;
int16_t * ptr_out;

void setup() {
   Serial.begin(115200);
   AudioMemory(10);
   sgtl5000.enable();
   sgtl5000.volume(0.2);
   noise.amplitude(0.3);
   Q_1_rec_noise.begin(); 
}

void loop() {
  Serial.println(Q_1_rec_noise.available());

  if(Q_1_rec_noise.available()){
    Serial.print("loop \n");
    
    int knob2 = analogRead(A2); //Setting volume of output with potentiometer
    float vol = knob2 / 1023.0;
    sgtl5000.volume(vol);
    
    ptr_in_noise = Q_1_rec_noise.readBuffer();
    ptr_out = Q_1_play.getBuffer();

    for(uint8_t i=0;i<blockSize;i++){ //Copy input buffer to output buffer
      ptr_out[i] = ptr_in_noise[i];
    }
    
    Q_1_rec_noise.freeBuffer();
    Q_1_play.playBuffer();
    Serial.print("end loop \n");
  }

}
 
Last edited:
I commented all the Serial.print statements and your code works for me on a T3.6 with Arduino 1.8.12 and TD 1.51 beta1.
The code looks good too - you are handling the audio buffers correctly.

Pete
 
Pete,

Wow, you're right. Thanks for looking into that. I did the same commenting out all the serial prints and now it works. I didn't think the serial prints would cause an issue like this.
Do you have any ideas on why this is the case?

-Josh
 
It seems like some sort of bug with Serial.print().
If I only keep the Serial.print("loop \n"); line uncommented, the sketch will run like I expect ONLY if the serial monitor is open. When I close the serial monitor, the noise stops being outputted. This is really strange behavior.
 
Status
Not open for further replies.
Back
Top