Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //here are the libraries that we need
- #include <Tympan_Library.h>
- #include "AudioEffectLowpass_FD_F32.h"
- const static float sample_rate_Hz = 16000.f; ; //24000 or 44117 (or other frequencies in the table in AudioOutputI2S_F32)
- const int audio_block_samples = 128; //for freq domain processing choose a power of 2 (16, 32, 64, 128) but no higher than 128
- AudioSettings_F32 audio_settings(sample_rate_Hz, audio_block_samples);
- //create audio library objects for handling the audio
- Tympan myTympan(TympanRev::E); //do TympanRev::D or TympanRev::E
- AudioInputI2S_F32 i2s_in(audio_settings); //Digital audio *from* the Tympan AIC.
- //the modules that do the work in this program
- AudioEffectLowpass_FD_F32 audioEffectLowpassFD(audio_settings);
- AudioSDWriter_F32 audioSDWriter(audio_settings); //this is stereo by default but can do 4 channels
- AudioOutputI2S_F32 i2s_out(audio_settings); //Digital audio *to* the Tympan AIC.
- //Make all of the audio connections
- AudioConnection_F32 patchCord1(i2s_in, 0, audioEffectLowpassFD, 0); //connect the Left input to our algorithm
- AudioConnection_F32 patchCord2(audioEffectLowpassFD, 0, i2s_out, 0); //connect the algorithm to the left output
- AudioConnection_F32 patchCord3(audioEffectLowpassFD, 0, i2s_out, 1); //connect the algorithm to the right output
- AudioConnection_F32 patchCord4(audioEffectLowpassFD, 0, audioSDWriter, 0); //connect audio to left channel of SD writer
- AudioConnection_F32 patchCord5(audioEffectLowpassFD, 0, audioSDWriter, 1); //connect audio to right channel of SD writer
- // define the setup() function, the function that is called once when the device is booting
- float input_gain_dB = 15.0f; //gain on the microphone
- float vol_knob_gain_dB = 0.0; //will be overridden by volume knob
- void setup() {
- // put your setup code here, to run once:
- //begin the serial comms (for debugging)
- myTympan.beginBothSerial();
- delay(10000);
- Serial.println("LowPassAndSDwrite: starting setup() (wait for debug delay)...");
- Serial.print(" : sample rate (Hz) = "); Serial.println(audio_settings.sample_rate_Hz);
- Serial.print(" : block size (samples) = "); Serial.println(audio_settings.audio_block_samples);
- // Allocate working memory for audio
- //First argument:
- //20 works for N_FFT equal to 128 or 256
- //40 works for 512
- //80 works for 1024
- AudioMemory_F32(40, audio_settings);
- // Configure the frequency-domain algorithm
- int N_FFT = 512;
- Serial.print(" : frequency resolution (Hz) = "); Serial.println(audio_settings.sample_rate_Hz / ((float)N_FFT));
- audioEffectLowpassFD.setup(audio_settings,N_FFT); //do after AudioMemory_F32();
- audioEffectLowpassFD.setCutoff_Hz(900.);
- //Enable the Tympan to start the audio flowing!
- myTympan.enable(); // activate AIC
- //Choose the desired audio input on the Typman...this will be overridden by the serviceMicDetect() in loop()
- //myTympan.inputSelect(TYMPAN_INPUT_ON_BOARD_MIC); // use the on-board microphones
- //myTympan.inputSelect(TYMPAN_INPUT_JACK_AS_MIC); // use the microphone jack - defaults to mic bias 2.5V
- myTympan.inputSelect(TYMPAN_INPUT_JACK_AS_LINEIN); // use the microphone jack - defaults to mic bias OFF
- //Set the desired volume levels
- myTympan.volume_dB(0); // headphone amplifier. -63.6 to +24 dB in 0.5dB steps.
- myTympan.setInputGain_dB(input_gain_dB); // set input volume, 0-47.5dB in 0.5dB setps
- //prepare the SD writer for the format that we want and any error statements
- audioSDWriter.setSerial(&myTympan); //the library will print any error info to this serial stream (note that myTympan is also a serial stream)
- audioSDWriter.setWriteDataType(AudioSDWriter::WriteDataType::INT16); //this is the built-in default, but here you could change it to FLOAT32
- audioSDWriter.setNumWriteChannels(2); //this is also the built-in defaullt, but you could change it to 4 (maybe?), if you wanted 4 channels.
- Serial.println("Setup complete.");
- }
- void loop() {
- auto time_ms = millis();
- //check the potentiometer
- servicePotentiometer(time_ms, 100); //service the potentiometer every 100 msec
- //service the SD recording
- serviceSD();
- //getSDstatus(millis(), 1000);
- //service the LEDs
- serviceLEDs();
- getSDstatus(time_ms, 1000);
- //check to see whether to print the CPU and Memory Usage
- myTympan.printCPUandMemory(time_ms,3000); //print every 3000 msec
- }
- //servicePotentiometer: listens to the blue potentiometer and sends the new pot value
- // to the audio processing algorithm as a control parameter
- void servicePotentiometer(unsigned long curTime_millis, unsigned long updatePeriod_millis) {
- //static unsigned long updatePeriod_millis = 100; //how many milliseconds between updating the potentiometer reading?
- static unsigned long lastUpdate_millis = 0;
- //has enough time passed to update everything?
- if (curTime_millis < lastUpdate_millis) lastUpdate_millis = 0; //handle wrap-around of the clock
- if ((curTime_millis - lastUpdate_millis) > updatePeriod_millis) { //is it time to update the user interface?
- float potentiometer_value = ((float32_t)(myTympan.readPotentiometer())) / 1023.0;
- startOrStopSDRecording(potentiometer_value);
- lastUpdate_millis = curTime_millis;
- } // end if
- } //end servicePotentiometer();
- void startOrStopSDRecording(float potentiometer_value) {
- //are we already recording?
- if (audioSDWriter.getState() == AudioSDWriter::STATE::RECORDING) {
- //check to see if potentiometer is set to turn off recording
- if (potentiometer_value < 0.45) audioSDWriter.stopRecording();
- } else { //we are not already recording
- //check to see if potentiometer has been set to start recording
- if (potentiometer_value > 0.55) audioSDWriter.startRecording();
- }
- }
- //for debugging sd state
- void getSDstatus(unsigned long curTime_millis, unsigned long updatePeriod_millis) {
- //static unsigned long updatePeriod_millis = 100; //how many milliseconds between updating the potentiometer reading?
- static unsigned long lastStatusUpdate = 0;
- //has enough time passed to update everything?
- if (curTime_millis < lastStatusUpdate) lastStatusUpdate = 0; //handle wrap-around of the clock
- if ((curTime_millis - lastStatusUpdate) > updatePeriod_millis) { //is it time to update the user interface?
- if (audioSDWriter.getState() == AudioSDWriter::STATE::RECORDING) {
- Serial.println(" == SD RECORDING == ");
- } else if (audioSDWriter.getState() == AudioSDWriter::STATE::STOPPED) {
- Serial.println(" == SD STOPPED == ");
- } else if (audioSDWriter.getState() == AudioSDWriter::STATE::UNPREPARED) {
- Serial.println(" == SD UNPREPARED == ");
- } else {
- Serial.println(" == SD Something Else???? == ");
- }
- lastStatusUpdate = curTime_millis;
- } // end if
- } //end servicePotentiometer();
- #define PRINT_OVERRUN_WARNING 1 //set to 1 to print a warning that the there's been a hiccup in the writing to the SD.
- void serviceSD(void) {
- static int max_max_bytes_written = 0; //for timing diagnotstics
- static int max_bytes_written = 0; //for timing diagnotstics
- static int max_dT_micros = 0; //for timing diagnotstics
- static int max_max_dT_micros = 0; //for timing diagnotstics
- unsigned long dT_micros = micros(); //for timing diagnotstics
- int bytes_written = audioSDWriter.serviceSD();
- dT_micros = micros() - dT_micros; //timing calculation
- if ( bytes_written > 0 ) {
- max_bytes_written = max(max_bytes_written, bytes_written);
- max_dT_micros = max((int)max_dT_micros, (int)dT_micros);
- if (dT_micros > 10000) { //if the write took a while, print some diagnostic info
- max_max_bytes_written = max(max_bytes_written,max_max_bytes_written);
- max_max_dT_micros = max(max_dT_micros, max_max_dT_micros);
- Serial.print("serviceSD: bytes written = ");
- Serial.print(bytes_written); Serial.print(", ");
- Serial.print(max_bytes_written); Serial.print(", ");
- Serial.print(max_max_bytes_written); Serial.print(", ");
- Serial.print("dT millis = ");
- Serial.print((float)dT_micros/1000.0,1); Serial.print(", ");
- Serial.print((float)max_dT_micros/1000.0,1); Serial.print(", ");
- Serial.print((float)max_max_dT_micros/1000.0,1);Serial.print(", ");
- Serial.println();
- max_bytes_written = 0;
- max_dT_micros = 0;
- }
- //print a warning if there has been an SD writing hiccup
- /*
- if (PRINT_OVERRUN_WARNING) {
- //if (audioSDWriter.getQueueOverrun() || i2s_in.get_isOutOfMemory()) {
- if (i2s_in.get_isOutOfMemory()) {
- float approx_time_sec = ((float)(millis()-audioSDWriter.getStartTimeMillis()))/1000.0;
- if (approx_time_sec > 0.1) {
- myTympan.print("SD Write Warning: there was a hiccup in the writing.");// Approx Time (sec): ");
- myTympan.println(approx_time_sec );
- }
- }
- }
- i2s_in.clear_isOutOfMemory();
- */
- }
- }
- void serviceLEDs(void) {
- static int loop_count = 0;
- loop_count++;
- if (audioSDWriter.getState() == AudioSDWriter::STATE::UNPREPARED) {
- if (loop_count > 200000) { //slow toggle
- loop_count = 0;
- toggleLEDs(true,true); //blink both
- }
- } else if (audioSDWriter.getState() == AudioSDWriter::STATE::RECORDING) {
- //let's flicker the LEDs while writing
- loop_count++;
- if (loop_count > 20000) { //fast toggle
- loop_count = 0;
- toggleLEDs(true,true); //blink both
- }
- } else {
- //myTympan.setRedLED(HIGH); myTympan.setAmberLED(LOW); //Go Red
- if (loop_count > 200000) { //slow toggle
- loop_count = 0;
- toggleLEDs(false,true); //just blink the red
- }
- }
- }
- void toggleLEDs(void) {
- toggleLEDs(true,true); //toggle both
- }
- void toggleLEDs(const bool &useAmber, const bool &useRed) {
- static bool LED = false;
- LED = !LED;
- if (LED) {
- if (useAmber) myTympan.setAmberLED(true);
- if (useRed) myTympan.setRedLED(false);
- } else {
- if (useAmber) myTympan.setAmberLED(false);
- if (useRed) myTympan.setRedLED(true);
- }
- if (!useAmber) myTympan.setAmberLED(false);
- if (!useRed) myTympan.setRedLED(false);
- }
Add Comment
Please, Sign In to add comment