Little audio project

Status
Not open for further replies.
Those microphone preamps are probably too much drain on that 3V3 line and it will be better if you can wire them to the supply I assume is powering Teensy via the VIN pin - for better advice as to how to beat the noise a reasonably good picture (schematic even, if possible) of how what is really connected to what will help a lot.

Assuming the preamps have their own gain controls, just setting those reasonably well should get the best result (provided both mics are shielded at least roughly same from noise and second mic is reasonably well shielded from the voice) but the library has a delay object, which doesn't look too hard to drive, if you really need to phase shift one of the inputs.

I haven't played with SD card stuff that much, I expect what you want is doable but may require a better SD card and it may be easiest if you have the two inputs record as the left & right in a single stereo file rather than trying to write two files simultaneously.

Edit: All the inputs on Teensy 3.1 which aren't marked specifically analog are 5V tolerant (to best of my memory and understanding).

Glad to hear it is looking promising at least :)
 
Last edited:
Those microphone preamps are probably too much drain on that 3V3 line and it will be better if you can wire them to the supply I assume is powering Teensy via the VIN pin - for better advice as to how to beat the noise a reasonably good picture (schematic even, if possible) of how what is really connected to what will help a lot.

Assuming the preamps have their own gain controls, just setting those reasonably well should get the best result (provided both mics are shielded at least roughly same from noise and second mic is reasonably well shielded from the voice) but the library has a delay object, which doesn't look too hard to drive, if you really need to phase shift one of the inputs.

I haven't played with SD card stuff that much, I expect what you want is doable but may require a better SD card and it may be easiest if you have the two inputs record as the left & right in a single stereo file rather than trying to write two files simultaneously.

Edit: All the inputs on Teensy 3.1 which aren't marked specifically analog are 5V tolerant (to best of my memory and understanding).

Glad to hear it is looking promising at least :)
The amps are draining 0,38mA each in clipping and 0,21 in "silence" according to my chineese multimeter.
i hope to not need to phase correct the signal, i will put all the stuff in the car in 2 weeks and i will see how it goes.
Cool i can use the the radio output to trigger recording, i had a quick look to the record example and from the gui the queue is a "mono" object so i'm a bit puzzled in how to make a stereo recording but i havn't looked that much in to it yet.
 
I was thinking that would be great if we could use 2 digital microphones like INMP441 or STMP34DT as imput for the audio library to get rid of the gains and preamps pains is it such ardware already supported by the audio library ?
 
38*2 + Audio Adapter + must supply itself may be a bit much, I haven't read as much of the technirati that I could about it but reasonably certain the 3V3 ldo regulator in the MK20DX256VLH7 doesn't want to have to give more than 100mA, probably prefer not to quite be giving 100mA all the time either.

such hardware, the digital microphones, are not directly supported by any objects that are in the library yet - I imagine they are very configurable (bit late here to go googling for datasheets and trying to determine much about them right now) and can be set up to share the I2S bus in such a way as to come in as left and right using the regular AudioInputI2S object but maybe not.

No promises, but I will take a quick squiz at one or both of them shortly and see if I feel like I can offer a suitable control object in reasonable timing or not.
 
38*2 + Audio Adapter + must supply itself may be a bit much, I haven't read as much of the technirati that I could about it but reasonably certain the 3V3 ldo regulator in the MK20DX256VLH7 doesn't want to have to give more than 100mA, probably prefer not to quite be giving 100mA all the time either.

such hardware, the digital microphones, are not directly supported by any objects that are in the library yet - I imagine they are very configurable (bit late here to go googling for datasheets and trying to determine much about them right now) and can be set up to share the I2S bus in such a way as to come in as left and right using the regular AudioInputI2S object but maybe not.

No promises, but I will take a quick squiz at one or both of them shortly and see if I feel like I can offer a suitable control object in reasonable timing or not.
0,38 mA Rob, not 38 mA but anyway, i've ordered other amps for the proper capsules that are dynamic and not electret they will have a different power supply.
Thanks !
 
oh, didn't recognise your use of comma as decimal point whereas I have seen it used like that before and could, maybe even should, have realised.

380uA * 2 should not be imposing terribly on the 3V3 line and, at least perhaps, I was wrong. If you load the cap intended for OTG applications (a C or D case SMD tantalum, 220uF 6.3V or higher) it may help decrease noise, another cap (22uF electro) between 3V3 and GND may help but, I learnt recently, you cannot make it as big as the cap you can put on the 5V pin.

If that remains too noisy then there may be benefits from inductors, or even alternatively well placed caps - those preamp circuits may benefit from a 100n (C0G/X7R/Ceramic, not polarised) cap placed as decouplers as close to (them, the preamps) where they connect to 3V3 and GND from Teensy as possible - they should have similar or better set up inside but while you are dealing with unwanted noise (which is no longer acoustic but instead electrical) you can check that they do have their own decouplers like they should.
 
hi all, i'm working on the sketch i will put in the car next week to test the filter, i'used the record example to record some files on the 32gb class10 sd i did put on audio board.
The library records RAW data then i should be able to convert it trough audacity but the format is unknown to me, how many bits, PCM U-law A-law, GSM, DWW or ? guess is mono but big endian or little endian ? frequency i guess is 44kHz ?
Then i'm writing 2 files on the sd card and those are the timings i have to write 512 bytes, they looks ok to me but your opinion is wellcome:

Shortest us=1100, longest us=27154 the limit should be 301700 from the recording example.

Then the last question is about the patch cords, if i have a patch cord from one objet to 2 objects (in my case from mixer4 to queue and to output) are both working at the same time ?
Code:
/*
 * A simple hardware test which receives audio from the audio shield
 * Line-In pins and send it to the Line-Out pins and headphone jack.
 *
 * This example code is in the public domain.
 */
#include <Bounce.h>
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>

// GUItool: begin automatically generated code
AudioInputI2S            i2s_in;           //xy=200,69
AudioMixer4              mix1;
AudioOutputI2S           i2s_out;           //xy=365,94
AudioRecordQueue         queue_raw;         //xy=462,208
AudioRecordQueue         queue_filtered;         //xy=652,225

AudioConnection          patchCord1(i2s_in, 0, mix1, 0);
AudioConnection          patchCord2(i2s_in, 0, queue_raw, 0);
AudioConnection          patchCord3(i2s_in, 1, mix1, 1);
AudioConnection          patchCord4(mix1, 0, i2s_out, 0);
AudioConnection          patchCord5(mix1, 0, i2s_out, 1);
AudioConnection          patchCord6(mix1, queue_filtered);


AudioControlSGTL5000     sgtl5000_1;     //xy=302,184

Bounce buttonRecord = Bounce(11, 8);
Bounce buttonStop =   Bounce(12, 8);  // 8 = 8 ms debounce time
File raw;
File filtered;
int mode = 0;

// GUItool: end automatically generated code


const int myInput = AUDIO_INPUT_LINEIN;
//const int myInput = AUDIO_INPUT_MIC;


void setup() 
{
  delay(20000);
  Serial.begin(115200);
  Serial.println("Teensy Audio filter v01");
  pinMode(11, INPUT_PULLUP);  //Start Record
  pinMode(12, INPUT_PULLUP);  //Stop Record
  // Audio connections require memory to work.  For more
  // detailed information, see the MemoryAndCpuUsage example
  AudioMemory(60);
  mix1.gain(0,1);
  mix1.gain(1,-1);
  // Enable the audio shield, select input, and enable output
  sgtl5000_1.enable();
  sgtl5000_1.inputSelect(myInput);
  sgtl5000_1.volume(0.5);
  
  SPI.setMOSI(7);
  SPI.setSCK(14);
  if (!(SD.begin(10))) 
   {
    // stop here if no SD card, but print a message
    while (1) 
       {
        Serial.println("Unable to access the SD card");
        delay(500);
       }
    }
}



void loop() 
 {
   buttonRecord.update();
   buttonStop.update();         
  
   if (buttonRecord.fallingEdge()) {
    Serial.println("Record Button Press");
    
    if (mode == 0) startRecording();
  }
  if (buttonStop.fallingEdge()) {
    Serial.println("Stop Button Press");
    if (mode == 1) stopRecording();
    
  }
    if (mode == 1) {
    continueRecording();
    }
 }
 
 void startRecording() 
 {
  Serial.println("startRecording");
  if (SD.exists("RAW.RAW")) 
    {
      // The SD library writes new data to the end of the
      // file, so to start a new recording, the old file
      // must be deleted before new data is written.
      SD.remove("RAW.RAW");
    }
  if (SD.exists("FILTERED.RAW")) 
    {
      // The SD library writes new data to the end of the
      // file, so to start a new recording, the old file
      // must be deleted before new data is written.
      SD.remove("FILTERED.RAW");
    }
  raw = SD.open("RAW.RAW", FILE_WRITE);
   if (raw) 
     {
      queue_raw.begin();
      mode = 1;
     }
  filtered = SD.open("FILTERED.RAW", FILE_WRITE);
   if (filtered) 
     {
      queue_filtered.begin();
      mode = 1;
     }
 }
 
 void continueRecording() 
{
  if (queue_filtered.available() >= 2) 
  {
    byte buffer[512];
    memcpy(buffer, queue_filtered.readBuffer(), 256);
    queue_filtered.freeBuffer();
    memcpy(buffer+256, queue_filtered.readBuffer(), 256);
    queue_filtered.freeBuffer();
    elapsedMicros usec = 0;
    filtered.write(buffer, 512);
    Serial.print("SD write, us=");
    Serial.println(usec);
   }
   if (queue_raw.available() >= 2) 
   {
    byte buffer[512];
    memcpy(buffer, queue_raw.readBuffer(), 256);
    queue_raw.freeBuffer();
    memcpy(buffer+256, queue_raw.readBuffer(), 256);
    queue_raw.freeBuffer();
    elapsedMicros usec = 0;
    raw.write(buffer, 512);
    Serial.print("SD write, us=");
    Serial.println(usec);
   }
}
 
void stopRecording() 
{
  Serial.println("stopRecording");
  queue_filtered.end();
  if (mode == 1) {
    while (queue_filtered.available() > 0) {
      filtered.write((byte*)queue_filtered.readBuffer(), 256);
      queue_filtered.freeBuffer();
    }
    filtered.close();
  }
  
  queue_raw.end();
  if (mode == 1) {
    while (queue_raw.available() > 0) {
      raw.write((byte*)queue_raw.readBuffer(), 256);
      queue_raw.freeBuffer();
    }
    raw.close();
  }
  mode = 0;
  
}
 
Last edited:
Posting from my phone, supposedly on holiday so just last qiestion this post: Yes, if patchcord goes from a to b and other patchcord goes grom a to c then b and c will both get from a at same time.

Back later with more reply perhaps.
 
Thanks Rob, i just realized that i've used pin 11 and 12 to control the recording start and stop but those pins are used by the audio board, yesterday i was happy to see the files in the sd card but i didn't relized that the audio part was not working at all, i hope is due to the 2 i've used! I dig a bit into the docs but is still don't find the raw file format, i guess is 16bit from the little i understoodin the code....thanks.
 
ok i got the first record out of the sketch here below but as soon as i try to open it with audacity as signed 16 bit PCM mono aural 44,1 i just have fuzzy digital noise instead of a background noise of my home is an audacity problem o a sketch problem ?

https://dl.dropboxusercontent.com/u/12305643/record.rar

Code:
/*
 * A simple hardware test which receives audio from the audio shield
 * Line-In pins and send it to the Line-Out pins and headphone jack.
 *
 * This example code is in the public domain.
 */
#include <Bounce.h>
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>

// GUItool: begin automatically generated code
AudioInputI2S            i2s_in;           //xy=200,69
AudioMixer4              mix1;
AudioOutputI2S           i2s_out;           //xy=365,94
AudioRecordQueue         queue_raw;         //xy=462,208
AudioRecordQueue         queue_filtered;         //xy=652,225

AudioConnection          patchCord1(i2s_in, 0, mix1, 0);
AudioConnection          patchCord2(i2s_in, 0, queue_raw, 0);
AudioConnection          patchCord3(i2s_in, 1, mix1, 1);
AudioConnection          patchCord4(mix1, 0, i2s_out, 0);
AudioConnection          patchCord5(mix1, 0, i2s_out, 1);
AudioConnection          patchCord6(mix1, queue_filtered);


AudioControlSGTL5000     sgtl5000_1;     //xy=302,184


int pin_record = 0;
int pin_stop = 1;
Bounce buttonRecord = Bounce(pin_record, 8);
Bounce buttonStop =   Bounce(pin_stop, 8);  // 8 = 8 ms debounce time
File raw;
File filtered;
int mode = 0;

// GUItool: end automatically generated code


const int myInput = AUDIO_INPUT_LINEIN;
//const int myInput = AUDIO_INPUT_MIC;


void setup() 
{
  delay(20000);
  Serial.begin(115200);
  Serial.println("Teensy Audio filter v01");
  pinMode(pin_record, INPUT_PULLUP);  //Start Record
  pinMode(pin_stop, INPUT_PULLUP);  //Stop Record
  // Audio connections require memory to work.  For more
  // detailed information, see the MemoryAndCpuUsage example
  AudioMemory(60);
  mix1.gain(0,1);
  mix1.gain(1,-1);
  // Enable the audio shield, select input, and enable output
  sgtl5000_1.enable();
  sgtl5000_1.inputSelect(myInput);
  sgtl5000_1.volume(0.5);
  
  SPI.setMOSI(7);
  SPI.setSCK(14);
  if (!(SD.begin(10))) 
   {
    // stop here if no SD card, but print a message
    while (1) 
       {
        Serial.println("Unable to access the SD card");
        delay(500);
       }
    }
}



void loop() 
 {
   buttonRecord.update();
   buttonStop.update();         
  
   if (buttonRecord.fallingEdge()) {
    Serial.println("Record Button Press");
    
    if (mode == 0) startRecording();
  }
  if (buttonStop.fallingEdge()) {
    Serial.println("Stop Button Press");
    if (mode == 1) stopRecording();
    
  }
    if (mode == 1) {
    continueRecording();
    }
    
 }
 
 void startRecording() 
 {
  Serial.println("startRecording");
  if (SD.exists("RAW.RAW")) 
    {
      // The SD library writes new data to the end of the
      // file, so to start a new recording, the old file
      // must be deleted before new data is written.
      SD.remove("RAW.RAW");
    }
  if (SD.exists("FILTERED.RAW")) 
    {
      // The SD library writes new data to the end of the
      // file, so to start a new recording, the old file
      // must be deleted before new data is written.
      SD.remove("FILTERED.RAW");
    }
  raw = SD.open("RAW.RAW", FILE_WRITE);
   if (raw) 
     {
      queue_raw.begin();
      mode = 1;
     }
  filtered = SD.open("FILTERED.RAW", FILE_WRITE);
   if (filtered) 
     {
      queue_filtered.begin();
      mode = 1;
     }
 }
 
 void continueRecording() 
{
  if (queue_filtered.available() >= 2) 
  {
    byte buffer[512];
    memcpy(buffer, queue_filtered.readBuffer(), 256);
    queue_filtered.freeBuffer();
    memcpy(buffer+256, queue_filtered.readBuffer(), 256);
    queue_filtered.freeBuffer();
    elapsedMicros usec = 0;
    filtered.write(buffer, 512);
    Serial.print("SD write, us=");
    Serial.println(usec);
   }
   if (queue_raw.available() >= 2) 
   {
    byte buffer[512];
    memcpy(buffer, queue_raw.readBuffer(), 256);
    queue_raw.freeBuffer();
    memcpy(buffer+256, queue_raw.readBuffer(), 256);
    queue_raw.freeBuffer();
    elapsedMicros usec = 0;
    raw.write(buffer, 512);
    Serial.print("SD write, us=");
    Serial.println(usec);
   }
}
 
void stopRecording() 
{
  Serial.println("stopRecording");
  queue_filtered.end();
  if (mode == 1) {
    while (queue_filtered.available() > 0) {
      filtered.write((byte*)queue_filtered.readBuffer(), 256);
      queue_filtered.freeBuffer();
    }
    filtered.close();
  }
  
  queue_raw.end();
  if (mode == 1) {
    while (queue_raw.available() > 0) {
      raw.write((byte*)queue_raw.readBuffer(), 256);
      queue_raw.freeBuffer();
    }
    raw.close();
  }
  mode = 0;
  
}
 
Posting from phone agaim, sorry.

Look for options regarding raw audio data - can't remember if signed or unsigned is recorded to file, but look for 16 bit raw options, try each...
 
Posting from phone agaim, sorry.

Look for options regarding raw audio data - can't remember if signed or unsigned is recorded to file, but look for 16 bit raw options, try each...

audacity only has signed pcm, i tryed all the options with audacity with no luck this is why i wonder if there is something wrong with the sketch
 
Is there not an import option for raw? I get the feeling you are using file->open where file-import->raw should be there to use ?
 
Last edited:
Is there not an import option for raw? I get the feeling you are using file->open where file-import->raw should be there to use ?

i did import but there is no all the options (look at the picture), my problem is that the raw format is totally unknown to me, i only know is monoaural 16 bit little endian, but i don't know if is signed or unsigned or integer or floating i'm playing with sox but i haven't managed to get anything out of it, the sintax of that damn software is quite weird to me (my problem of course :D )
Immagine.png
 
In Audacity navigate through File->Import->Raw Data:

Encoding: Signed 16-bit PCM
Byte Order: Little-endian
Channels: 1 Channel (Mono)
Sample rate: 44100

Import

that work?
 
In Audacity navigate through File->Import->Raw Data:

Encoding: Signed 16-bit PCM
Byte Order: Little-endian
Channels: 1 Channel (Mono)
Sample rate: 44100

Import

that work?

nope, that would have been quite easy, i just got digital noise.
 
nope, that would have been quite easy, i just got digital noise.

I tried your RAW.RAW file, and if you try other settings it seems like digital noise.

But that 16-bit signed PCM, mono, little-endian, seems like real audio. Yes, probably not the audio you are looking for, but it does seem like real samples. In other words the samples aren't totally discontinuous.

There are even quieter portions and louder portions.
 
I tried your RAW.RAW file, and if you try other settings it seems like digital noise.

But that 16-bit signed PCM, mono, little-endian, seems like real audio. Yes, probably not the audio you are looking for, but it does seem like real samples. In other words the samples aren't totally discontinuous.

There are even quieter portions and louder portions.

with that settings to me still sounds digital noise and not background noise, i will retest today anyway

Edit:
I tested filtered.raw is actually ok, is background noise as expected, raw.raw is digital noise here is my problem....i will check back the sketch....
 
Last edited:
i did some further testing and i found out the following:
if recording is off i have no digital noise in the earpieces (and i quess in the queue but i can't know) if i trigger recording i start to have this terrible digital noise both in recorded files and earpieces, on the output (filtered.raw and hearpieces) is attenuated by the filtering but in the RAW side is terribly high, i've tryed to use a direct 3v3 power supply directly to 3,3 line but no change i also notice that the little orange led flickers, both my power supply are bad or a run into some troubles ?

EDIT:
Ok i powered the teensy with a batt trough the Vin pin with a capacitor and now the noise is no longer recording dependent but is still there and is environmental noise i will have to change all the wiring from the mic amps to the audio shield to get rid of, so i wonder if this wiring will make the job, sorry for asking this silly questions but i'm a noob in this.
thanks.
Immagine1.png
 
Last edited:
It isn't clear whether you are depicting shielding you are going to add around the preamps or if you are depicting a new connection to shielding that was always there - if the latter my comment is that such shielding should always be connected to GND, if the former then adding such shielding often helps.

Where you write that the noise seemed absent from output while not recording but became apparent while recording (in the output, not just recording) makes me wonder if writing two streams to SD might take a bit long or something but difficult to believe 2 streams cannot be written at same time.

It might be worth while to consider this noise while reviewing the that document about combating noise in audio systems for vehicles I linked previously.
 
It isn't clear whether you are depicting shielding you are going to add around the preamps or if you are depicting a new connection to shielding that was always there - if the latter my comment is that such shielding should always be connected to GND, if the former then adding such shielding often helps.


I whant to put a cable with a connector in the middle beween the audio board and the mic amps, right now i have just patch cords with no shield at all, the schematic represents the cable i whant to do but i'm not sure if that will prevent some noise.

Where you write that the noise seemed absent from output while not recording but became apparent while recording (in the output, not just recording) makes me wonder if writing two streams to SD might take a bit long or something but difficult to believe 2 streams cannot be written at same time.

It might be worth while to consider this noise while reviewing the that document about combating noise in audio systems for vehicles I linked previously.

Well i think i run into several noise sources here is a brief of the test i carried out:

1. teensy powered from USB:
Big noise on record small noise while not recording
2. Teensy powered to the 3.3v pin
Big noise on record big noise while not recording
3. Teensy powered troug the VIN
Big noise in record and while not recording
4. Teensy powered trough batt (9V to VIN)
No noise from hearpieces i havn't tested record yet.

I think i got power level noise, power noise (wall plug) and environment noise.
 
Audio signals always benefit from shielding, shield the Teensy and Audio Adapter as best you can (google: faraday cage, sensitive electronics in small faraday cage) and then try to connect the shield on the cable(s) inside the shielding you provide the little PCBs for best results.

Running in the car can be powered using main battery (regulated to inside 3.7 to 5.5V) but some steps usually taken for hifi audio installations in regular cars may be necessary.
 
ok i shielded all the audio part and while not recording i have no noise, but as soon as i start recording i start haveing a very bad noise, in the link audio outpur recorded from teensy to pc testing the 2 microphones and then triggering the recording.
https://dl.dropboxusercontent.com/u/12305643/recording_test.wav.
Teensy was powered from usb i've tryed from 9V battery to teensy vin but no changes i can't do other test becouse i think i've burnt one of the amps i will have to test again with the new amps.
 
I am reasonably convinced that the noise is not electrical at all, there is no amount of changing power supply configuration or shielding that will help if I am right about what is causing it.

Please post your entire sketch (either attached or between [code]here[/code] tags) and confirm for me that you are using the copy of the audio library from my github.

It may turn out that writing two streams of audio data in separate files on an SD card just takes too long and basically minces the audio - you may have to settle for just enjoying the fact that you can hear the driver a lot better and forego the ability to analyse how well the noise cancelling worked after the event.

If it is possible to record both the streams without mincing the audio and I don't figure it out then I hope someone who can figure it out turns up for you.
 
I am reasonably convinced that the noise is not electrical at all, there is no amount of changing power supply configuration or shielding that will help if I am right about what is causing it.

Please post your entire sketch (either attached or between [code]here[/code] tags) and confirm for me that you are using the copy of the audio library from my github.
I think i copyed from your git hub just the mixer4 file you say you have modifyed not all the lib.
It may turn out that writing two streams of audio data in separate files on an SD card just takes too long and basically minces the audio - you may have to settle for just enjoying the fact that you can hear the driver a lot better and forego the ability to analyse how well the noise cancelling worked after the event.
Well the timing i got from the serial are ok to write 2 files as i posted some days ago but it still maybe that it's not ok to record 2 streams, the recording is just for debug purposes, the final aim is to get clean driver voice i can live without recording.
If it is possible to record both the streams without mincing the audio and I don't figure it out then I hope someone who can figure it out turns up for you.
Well i still think is not a Sd speed thing becouse if i disconnect the line in the noise stops ( i don't know if it's related or not).
Code:
/*
 * A simple hardware test which receives audio from the audio shield
 * Line-In pins and send it to the Line-Out pins and headphone jack.
 *
 * This example code is in the public domain.
 */
#include <Bounce.h>
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>

// GUItool: begin automatically generated code
AudioInputI2S            i2s_in;           //xy=200,69
AudioMixer4              mix1;
AudioOutputI2S           i2s_out;           //xy=365,94
AudioRecordQueue         queue_raw;         //xy=462,208
AudioRecordQueue         queue_filtered;         //xy=652,225

AudioConnection          patchCord1(i2s_in, 0, mix1, 0);
AudioConnection          patchCord2(i2s_in, 0, queue_raw, 0);
AudioConnection          patchCord3(i2s_in, 1, mix1, 1);
AudioConnection          patchCord4(i2s_in, 0, i2s_out, 0);
AudioConnection          patchCord5(i2s_in, 1, i2s_out, 1);
AudioConnection          patchCord6(mix1, 0, queue_filtered, 0);


AudioControlSGTL5000     sgtl5000_1;     //xy=302,184

int pin_record=0;
int pin_stop=1;
//int pin_green=17;
//int pin_red=16;

Bounce buttonRecord = Bounce(pin_record, 8);
Bounce buttonStop =   Bounce(pin_stop, 8);  // 8 = 8 ms debounce time
File raw;
File filtered;
int mode = 0;
bool green,red=LOW;
// GUItool: end automatically generated code


const int myInput = AUDIO_INPUT_LINEIN;
//const int myInput = AUDIO_INPUT_MIC;


void setup() 
{
  //digitalWrite(pin_red, red);
  //digitalWrite(pin_green, green);
  delay(10000);
  Serial.begin(115200);
  Serial.println("Teensy Audio filter v01");
  pinMode(pin_record, INPUT_PULLUP);  //Start Record
  pinMode(pin_stop, INPUT_PULLUP);  //Stop Record
  //pinMode(pin_green, OUTPUT);
  //pinMode(pin_red, OUTPUT);
  // Audio connections require memory to work.  For more
  // detailed information, see the MemoryAndCpuUsage example
  AudioMemory(60);
  mix1.gain(0,1);
  mix1.gain(1,-1);
  // Enable the audio shield, select input, and enable output
  sgtl5000_1.enable();
  sgtl5000_1.inputSelect(myInput);
  sgtl5000_1.volume(0.6);
  
  SPI.setMOSI(7);
  SPI.setSCK(14);
  if (!(SD.begin(10))) 
   {
    // stop here if no SD card, but print a message
    while (1) 
       {
        Serial.println("Unable to access the SD card");
        delay(500);
       }
    }
 //   digitalWrite(pin_green, HIGH);
}



void loop() 
 {
   
   buttonRecord.update();
   buttonStop.update();         
 
   if (buttonRecord.fallingEdge()) {
    Serial.println("Record Button Press");    
    if (mode == 0) 
         {
          startRecording();
          //digitalWrite(pin_red, HIGH);
          //digitalWrite(pin_green, LOW);
         }
          
  }
  if (buttonStop.fallingEdge()) 
    {
    Serial.println("Stop Button Press");
    if (mode == 1) stopRecording(); 
    //digitalWrite(pin_red, LOW);
    //digitalWrite(pin_green, HIGH);  
    }
    
    if (mode == 1) 
    {
    continueRecording();
    }
    
   /*
   int temp1=millis()%500;
   if ((temp1==0)&(mode == 0))
      {
       green=!green;
       digitalWrite(pin_green, green);
      } 
      */
 }
 
 void startRecording() 
 {
  Serial.println("startRecording");
  if (SD.exists("RAW.RAW")) 
    {
      // The SD library writes new data to the end of the
      // file, so to start a new recording, the old file
      // must be deleted before new data is written.
      SD.remove("RAW.RAW");
    }
  if (SD.exists("FILTERED.RAW")) 
    {
      // The SD library writes new data to the end of the
      // file, so to start a new recording, the old file
      // must be deleted before new data is written.
      SD.remove("FILTERED.RAW");
    }
  raw = SD.open("RAW.RAW", FILE_WRITE);
   if (raw) 
     {
      queue_raw.begin();
      mode = 1;
     }
  filtered = SD.open("FILTERED.RAW", FILE_WRITE);
   if (filtered) 
     {
      queue_filtered.begin();
      mode = 1;
     }
 }
 
 void continueRecording() 
{
  if (queue_filtered.available() >= 2) 
  {
    byte buffer[512];
    memcpy(buffer, queue_filtered.readBuffer(), 256);
    queue_filtered.freeBuffer();
    memcpy(buffer+256, queue_filtered.readBuffer(), 256);
    queue_filtered.freeBuffer();
    elapsedMicros usec = 0;
    filtered.write(buffer, 512);
    Serial.print("SD write, us=");
    Serial.println(usec);
   }
   if (queue_raw.available() >= 2) 
   {
    byte buffer[512];
    memcpy(buffer, queue_raw.readBuffer(), 256);
    queue_raw.freeBuffer();
    memcpy(buffer+256, queue_raw.readBuffer(), 256);
    queue_raw.freeBuffer();
    elapsedMicros usec = 0;
    raw.write(buffer, 512);
    Serial.print("SD write, us=");
    Serial.println(usec);
   }
   /*
   int temp=millis()%1000;
   if (temp==0)
      {
       red=!red;
       digitalWrite(pin_red, red);
      } 
      */
   
}
 
void stopRecording() 
{
  Serial.println("stopRecording");
  queue_filtered.end();
  if (mode == 1) {
    while (queue_filtered.available() > 0) {
      filtered.write((byte*)queue_filtered.readBuffer(), 256);
      queue_filtered.freeBuffer();
    }
    filtered.close();
  }
  
  queue_raw.end();
  if (mode == 1) {
    while (queue_raw.available() > 0) {
      raw.write((byte*)queue_raw.readBuffer(), 256);
      queue_raw.freeBuffer();
    }
    raw.close();
  }
  mode = 0;
  
}
 
Status
Not open for further replies.
Back
Top