From 791d6bc62c8d609d7d358ffe9a5841f7bf2670b1 Mon Sep 17 00:00:00 2001 From: alfran Date: Tue, 14 Mar 2017 17:31:40 +0100 Subject: [PATCH 1/5] fixed microphones low volume issue --- libraries/Audio/src/Audio.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Audio/src/Audio.h b/libraries/Audio/src/Audio.h index 37f8588..c3461f6 100644 --- a/libraries/Audio/src/Audio.h +++ b/libraries/Audio/src/Audio.h @@ -75,7 +75,7 @@ class AudioClass : public Print { uint32_t bufferIn_fptr = 0; //fptr private: - uint32_t volume = 60; + uint32_t volume = 100; AudioMode mode; }; From 0318e84b45cd50c39c7f5c10c816f3fda705a67f Mon Sep 17 00:00:00 2001 From: alfran Date: Wed, 15 Mar 2017 12:40:28 +0100 Subject: [PATCH 2/5] Improved Volume management --- libraries/Audio/src/Audio.cpp | 72 ++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 34 deletions(-) diff --git a/libraries/Audio/src/Audio.cpp b/libraries/Audio/src/Audio.cpp index 7a2328c..33d06e3 100644 --- a/libraries/Audio/src/Audio.cpp +++ b/libraries/Audio/src/Audio.cpp @@ -11,7 +11,7 @@ #include "Audio.h" #include "otto_audio_inout.h" - +int audioVolume = 100; AudioClass Audio; @@ -53,7 +53,7 @@ void AudioClass::end() { BSP_AUDIO_IN_Stop(CODEC_PDWN_SW); } BSP_AUDIO_OUT_Stop(CODEC_PDWN_SW); - if (bufferOut) + if (bufferOut) { free(bufferOut); bufferOut = NULL; @@ -65,46 +65,50 @@ void AudioClass::prepare(int16_t *buffer, int S, int volume){ uint16_t *ubuffer = (uint16_t*) buffer; if(volume >= 100) - volume = 100; - if (volume <= 10) - volume = 10; + audioVolume = 100; + if (volume <= 0) + audioVolume = 0; + BSP_AUDIO_OUT_SetVolume(audioVolume); + BSP_AUDIO_IN_SetVolume(audioVolume); } size_t AudioClass::write(const uint32_t *data, size_t size) { int i; + BSP_AUDIO_OUT_SetVolume(audioVolume); + BSP_AUDIO_IN_SetVolume(audioVolume); + + if(size > (bufferOutSize / 2)) + return size; + + /* not running yet, need to fill-in full FIFO */ + if(running == NULL) { + memcpy(next, (uint8_t *) data, size); + /* First half FIFO */ + if(next == bufferOut) { + next = half; + /* Second half FIfO, when copied, start playing */ + } else { + next = bufferOut; + running = bufferOut; + BSP_AUDIO_OUT_Play((uint16_t*)bufferOut, bufferOutSize); + } + _receivedBytes += size; + return size; + } - if(size > (bufferOutSize / 2)) - return size; - - /* not running yet, need to fill-in full FIFO */ - if(running == NULL) { - memcpy(next, (uint8_t *) data, size); - /* First half FIFO */ - if(next == bufferOut) { - next = half; - /* Second half FIfO, when copied, start playing */ - } else { - next = bufferOut; - running = bufferOut; - BSP_AUDIO_OUT_Play((uint16_t*)bufferOut, bufferOutSize); - } - _receivedBytes += size; - return size; - } - - /* Wait for room in FIFO*/ - while((int)next == (int) running); + /* Wait for room in FIFO*/ + while((int)next == (int) running); - _receivedBytes += size; + _receivedBytes += size; - /* If running is not next there is room in fifo */ - memcpy(next,(uint8_t *) data, size); + /* If running is not next there is room in fifo */ + memcpy(next,(uint8_t *) data, size); - if(next == bufferOut) { - next = half; - } else { - next = bufferOut; - } + if(next == bufferOut) { + next = half; + } else { + next = bufferOut; + } return size; } From 14bfab4d00d72ac25204866eb6ef5d35f1097f45 Mon Sep 17 00:00:00 2001 From: alfran Date: Wed, 15 Mar 2017 12:41:47 +0100 Subject: [PATCH 3/5] Improved Simple Audio Recorder Example --- .../SimpleAudioRecorder.ino | 38 ++++++++++++++++--- 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/libraries/Audio/example/SimpleAudioRecorder/SimpleAudioRecorder.ino b/libraries/Audio/example/SimpleAudioRecorder/SimpleAudioRecorder.ino index 6bef58c..7345bbe 100644 --- a/libraries/Audio/example/SimpleAudioRecorder/SimpleAudioRecorder.ino +++ b/libraries/Audio/example/SimpleAudioRecorder/SimpleAudioRecorder.ino @@ -1,4 +1,4 @@ -/* +//* Simple Audio Recorder Demonstrates the use of the Audio library for the Arduino OTTO @@ -8,6 +8,7 @@ * Only 48kHz supported for now Original by Frederic Pillon November 09, 2016 + Modified by Francesco Alessi March 15 2017 This example code is in the public domain @@ -21,7 +22,7 @@ const char recFile[] = "record.wav"; #define AUDIO_IN_FREQUENCY BSP_AUDIO_FREQUENCY_48K -#define DEFAULT_TIME_REC 30 // Recording time in second (default: 30s) +#define DEFAULT_TIME_REC 3 // Recording time in second (default: 30s) #define REC_SAMPLE_LENGTH (DEFAULT_TIME_REC * AUDIO_IN_FREQUENCY * DEFAULT_AUDIO_IN_CHANNEL_NBR * 2) #define RECORD_5PERCENT ((5*REC_SAMPLE_LENGTH)/100) @@ -29,11 +30,23 @@ void setup() { // initialize SerialUSB communication at 115200 bits per second: SerialUSB.begin(115200); while(!SerialUSB); - + SerialUSB.print("Detecting SD"); + int counter = 0; while (SD.begin(SD_DETECT_PIN) != TRUE) { - delay(10); + SerialUSB.print("."); + delay(500); + if (counter == 10) + { + SerialUSB.println("."); + SerialUSB.println("SD Card not detected!"); + SerialUSB.print("Detecting SD"); + counter = 0; + } + counter++; } + //SerialUSB.println(""); + SerialUSB.println("done."); } void loop() { @@ -43,10 +56,22 @@ void loop() { const int S = 1024; // Number of samples to read in block uint32_t byteswritten = 0; uint32_t count = 0; - delay(4000); // delay for console + delay(1000); + // removing old file + while(SD.exists(recFile) == TRUE) + { + SerialUSB.print("Removing file "); + SerialUSB.print(recFile); + SerialUSB.print("..."); + SD.remove(recFile); + SerialUSB.println("done"); + } + File myFile = SD.open(recFile, FILE_WRITE); + if (!SD.exists(recFile)) { + // If the file didn't open, print an error and stop SerialUSB.print("Error: failed to create "); SerialUSB.println(recFile); @@ -70,7 +95,7 @@ void loop() { } delay(1000); - SerialUSB.println("Start AUDIO record"); + SerialUSB.println("Starting AUDIO recorder"); delay(1000); status = Audio.begin(WaveFormat.SampleRate, 100, AUDIO_IN); if (status != 0) { @@ -84,6 +109,7 @@ void loop() { delay(1000); // Prepare samples + SerialUSB.println("Recording..."); int volume = 100; Audio.prepare(NULL, S, volume); delay(1000); From 4aa96be4343798f9b33aeeaa22b46956a3d521f9 Mon Sep 17 00:00:00 2001 From: alfran Date: Wed, 15 Mar 2017 18:16:39 +0100 Subject: [PATCH 4/5] improved Volume management and audio related sketch --- .../SimpleAudioPlayer/SimpleAudioPlayer.ino | 28 ++++++++++++++----- .../SimpleAudioRecorder.ino | 5 ++-- libraries/Audio/src/Audio.cpp | 28 +++++++++++-------- 3 files changed, 40 insertions(+), 21 deletions(-) diff --git a/libraries/Audio/example/SimpleAudioPlayer/SimpleAudioPlayer.ino b/libraries/Audio/example/SimpleAudioPlayer/SimpleAudioPlayer.ino index 47a6e48..09e08ce 100644 --- a/libraries/Audio/example/SimpleAudioPlayer/SimpleAudioPlayer.ino +++ b/libraries/Audio/example/SimpleAudioPlayer/SimpleAudioPlayer.ino @@ -20,16 +20,28 @@ #include #include +const char recFile[] = "record.wav"; + void setup() { // initialize serial communication at 9600 bits per second: SerialUSB.begin(115200); while(!SerialUSB); - - /* Test begin() method */ + SerialUSB.print("Detecting SD"); + int counter = 0; while (SD.begin(SD_DETECT_PIN) != TRUE) { - delay(10); + SerialUSB.print("."); + delay(500); + if (counter == 10) + { + SerialUSB.println("."); + SerialUSB.println("SD Card not detected!"); + SerialUSB.print("Detecting SD"); + counter = 0; + } + counter++; } + SerialUSB.println("...done."); } void loop() { @@ -40,19 +52,21 @@ void loop() { int duration; delay(1000); // delay for console - File myFile = SD.open("test.wav"); + File myFile = SD.open(recFile); if (!myFile.available()) { // if the file didn't open, print an error and stop - SerialUSB.println("error opening test.wav"); + SerialUSB.print("error opening "); + SerialUSB.println(recFile); while (true); } else { - SerialUSB.println("test.wav open OK"); + SerialUSB.println(recFile); + SerialUSB.println(" open OK"); } myFile.read((void*) &WaveFormat, sizeof(WaveFormat)); delay(1000); - SerialUSB.println("STARTUP AUDIO\r\n"); + SerialUSB.println("Starting Playback"); delay(1000); Audio.begin(WaveFormat.SampleRate, 100); diff --git a/libraries/Audio/example/SimpleAudioRecorder/SimpleAudioRecorder.ino b/libraries/Audio/example/SimpleAudioRecorder/SimpleAudioRecorder.ino index 7345bbe..c3b11ac 100644 --- a/libraries/Audio/example/SimpleAudioRecorder/SimpleAudioRecorder.ino +++ b/libraries/Audio/example/SimpleAudioRecorder/SimpleAudioRecorder.ino @@ -1,4 +1,4 @@ -//* +/* Simple Audio Recorder Demonstrates the use of the Audio library for the Arduino OTTO @@ -45,8 +45,7 @@ void setup() { } counter++; } - //SerialUSB.println(""); - SerialUSB.println("done."); + SerialUSB.println("...done."); } void loop() { diff --git a/libraries/Audio/src/Audio.cpp b/libraries/Audio/src/Audio.cpp index 33d06e3..a3deaef 100644 --- a/libraries/Audio/src/Audio.cpp +++ b/libraries/Audio/src/Audio.cpp @@ -11,8 +11,6 @@ #include "Audio.h" #include "otto_audio_inout.h" -int audioVolume = 100; - AudioClass Audio; /* Begin class can be extended to support more options */ @@ -33,12 +31,12 @@ int AudioClass::begin(uint32_t sampleRate, uint32_t msPreBuffer, AudioMode audio next = bufferOut; } - ret = BSP_AUDIO_OUT_Init(OUTPUT_DEVICE_HEADPHONE, volume, sampleRate); + ret = BSP_AUDIO_OUT_Init(OUTPUT_DEVICE_HEADPHONE, 0, sampleRate); /* Audio In */ if(mode != AUDIO_OUT) { - ret += BSP_AUDIO_IN_Init(INPUT_DEVICE_DIGITAL_MICROPHONE_2, volume, sampleRate); + ret += BSP_AUDIO_IN_Init(INPUT_DEVICE_DIGITAL_MICROPHONE_2, 0, sampleRate); ret += BSP_AUDIO_IN_Record((uint16_t*)&bufferIn[0], bufferInSize); bufferIn_fptr += 44; } @@ -64,18 +62,26 @@ void AudioClass::end() { void AudioClass::prepare(int16_t *buffer, int S, int volume){ uint16_t *ubuffer = (uint16_t*) buffer; - if(volume >= 100) - audioVolume = 100; + if (volume >= 100) + volume = 100; if (volume <= 0) - audioVolume = 0; - BSP_AUDIO_OUT_SetVolume(audioVolume); - BSP_AUDIO_IN_SetVolume(audioVolume); + volume = 0; + + if (mode == AUDIO_OUT) + BSP_AUDIO_OUT_SetVolume(volume); + if (mode == AUDIO_IN) + BSP_AUDIO_IN_SetVolume(volume); + if (mode == AUDIO_BOTH) { + BSP_AUDIO_OUT_SetVolume(volume); + BSP_AUDIO_IN_SetVolume(volume); + } + + } size_t AudioClass::write(const uint32_t *data, size_t size) { + int i; - BSP_AUDIO_OUT_SetVolume(audioVolume); - BSP_AUDIO_IN_SetVolume(audioVolume); if(size > (bufferOutSize / 2)) return size; From e109f978285d42ff111f2f7f41ed40633cfa579d Mon Sep 17 00:00:00 2001 From: alfran Date: Wed, 15 Mar 2017 18:23:57 +0100 Subject: [PATCH 5/5] Fixed SimpleAudioRecorder Sketch --- .../Audio/example/SimpleAudioRecorder/SimpleAudioRecorder.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/Audio/example/SimpleAudioRecorder/SimpleAudioRecorder.ino b/libraries/Audio/example/SimpleAudioRecorder/SimpleAudioRecorder.ino index c3b11ac..bd00433 100644 --- a/libraries/Audio/example/SimpleAudioRecorder/SimpleAudioRecorder.ino +++ b/libraries/Audio/example/SimpleAudioRecorder/SimpleAudioRecorder.ino @@ -20,9 +20,9 @@ #include #include -const char recFile[] = "record.wav"; +const char recFile[] = "test.wav"; #define AUDIO_IN_FREQUENCY BSP_AUDIO_FREQUENCY_48K -#define DEFAULT_TIME_REC 3 // Recording time in second (default: 30s) +#define DEFAULT_TIME_REC 30 // Recording time in second (default: 30s) #define REC_SAMPLE_LENGTH (DEFAULT_TIME_REC * AUDIO_IN_FREQUENCY * DEFAULT_AUDIO_IN_CHANNEL_NBR * 2) #define RECORD_5PERCENT ((5*REC_SAMPLE_LENGTH)/100)