Skip to content

Commit dd84027

Browse files
committed
Microphone section totally updated and tested
1 parent fed782d commit dd84027

File tree

2 files changed

+49
-24
lines changed

2 files changed

+49
-24
lines changed
1.66 MB
Loading

content/hardware/06.nicla/boards/nicla-voice/tutorials/user-manual/content.md

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -441,26 +441,26 @@ The example code shown below captures audio from the onboard microphone of the N
441441
Keep in mind that this example code requires the following libraries:
442442
443443
- [arduino-libg722](https://github.com/pschatzmann/arduino-libg722)
444-
- [arduino-audio-tools](https://github.com/pschatzmann/arduino-audio-tools)
444+
- [arduino-audio-tools __v0.9.6__](https://github.com/pschatzmann/arduino-audio-tools/releases/tag/v0.9.6)
445445
446-
***If you encounter compilation or build errors with the `Record_and_stream` example, please try using the [`arduino-audio-tools` library version __0.9.6__](https://github.com/pschatzmann/arduino-audio-tools/releases/tag/v0.9.6).***
446+
***__Important:__ This code requires `arduino-audio-tools` version __v0.9.6__ to compile.***
447447
448-
Make sure to install these libraries in the Arduino IDE before uploading the code to your Nicla Voice board.
448+
Make sure that you have updated the NDP120 firmware. See the steps [here](#ndp120-processor-firmware-update).
449449
450450
```arduino
451451
// Include necessary libraries for the Nicla Voice board, audio processing, and G722 codec support
452452
#include "Arduino.h"
453453
#include "NDP.h"
454+
455+
#undef abs
456+
#define USE_INT24_FROM_INT
454457
#include "AudioTools.h"
455458
#include "AudioCodecs/CodecG722.h"
456459
457-
// Create an instance of the G722Encoder class for handling audio encoding
458460
G722Encoder encoder;
459461
460-
// Declare a buffer to temporarily store audio data
461462
uint8_t data[2048];
462463
463-
// Define a function to turn on the green LED for a short duration as an event indicator
464464
void ledGreenOn() {
465465
nicla::leds.begin();
466466
nicla::leds.setColor(green);
@@ -470,49 +470,37 @@ void ledGreenOn() {
470470
}
471471
472472
void setup() {
473-
// Start UART communication at 115200 baud
474-
Serial.begin(115200);
475473
476-
// Initialize the Nicla Voice board, disable the LDO
474+
Serial.begin(115200);
477475
nicla::begin();
478476
nicla::disableLDO();
479-
480-
// Initialize the built-in RGB LED, set up as an event indicator
481477
nicla::leds.begin();
478+
482479
NDP.onEvent(ledGreenOn);
483480
484-
// Set up the G722 encoder (1 channel, 16 kHz sample rate)
485481
AudioBaseInfo bi;
486482
bi.channels = 1;
487483
bi.sample_rate = 16000;
484+
488485
encoder.setOptions(0);
489486
encoder.begin(bi);
490487
491-
// Set the output stream for the encoder to the serial port
492488
encoder.setOutputStream(Serial);
493489
494-
// Load the required firmware packages for the NDP processor, turn on the onboard microphone
495490
NDP.begin("mcu_fw_120_v91.synpkg");
496491
NDP.load("dsp_firmware_v91.synpkg");
497492
NDP.load("alexa_334_NDP120_B0_v11_v91.synpkg");
498493
NDP.turnOnMicrophone();
499-
500-
// Check the audio chunk size to ensure it doesn't exceed the buffer size
501494
int chunk_size = NDP.getAudioChunkSize();
502495
if (chunk_size >= sizeof(data)) {
503496
for(;;);
504497
}
505498
}
506499
507-
// Continuously read audio data from the microphone, encode it using the G722 codec, send it to the serial port
508500
void loop() {
509-
// Declare a variable to store the length of the extracted audio data
510501
unsigned int len = 0;
511502
512-
// Extract audio data from the NDP and store it in the data buffer
513503
NDP.extractData(data, &len);
514-
515-
// Pass the extracted audio data to the G722 encoder and send it to the serial port
516504
encoder.write(data, len);
517505
}
518506
```
@@ -541,17 +529,54 @@ In the `loop()` function:
541529
- The length of the extracted audio data is stored in the `len` variable.
542530
- The extracted audio data is passed to the G722 encoder, which compresses the audio and sends it to the serial port.
543531
544-
To extract the audio data on a **Linux computer**, you will need to set up the serial port as raw:
532+
#### Linux Workflow
533+
534+
To extract the audio data on a **Linux computer**, follow the steps below:
535+
536+
- Set up the serial port as raw:
545537
546538
```bash
547539
stty -F /dev/ttyACM0 115200 raw
548540
```
549-
Dump the data to a file (e.g., test.g722):
541+
- Dump the data to a file (e.g., test.g722):
550542
551543
```bash
552544
cat /dev/ttyACM0 > test.g722
553545
```
554-
Then, you can open the file with a software like [Audacity](https://www.audacityteam.org/) to play back the audio.
546+
- Install **ffmpeg** with `sudo apt install ffmpeg` to convert the `.g722` file to `.wav`:
547+
548+
```bash
549+
ffmpeg -f g722 -i test.g722 -ar 16000 test.wav
550+
```
551+
Then, you can open the `.wav` file with a software like [Audacity](https://www.audacityteam.org/) to play back the audio.
552+
553+
#### Windows Workflow
554+
555+
To extract the audio data on a **Windows computer**, follow the steps below:
556+
557+
Download **PuTTY** for free [here](https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html), install it and open it.
558+
559+
1. Select "Serial" as connection type
560+
2. Enter the serial port where your board is connected
561+
3. Set the baud rate to `115200`
562+
4. In the left menu, navigate to "Logging"
563+
5. Select "All session output"
564+
6. Define the directory to save the recorded file `.g722`
565+
7. Click on "Open" to start recording audio
566+
567+
![PuTTY steps](assets/serial-g722.png)
568+
569+
- To stop recording, simply close the terminal window
570+
- Navigate to your `.g722` file directory, and now it is time to convert it to `.wav`
571+
- Download **FFmpeg** for Windows [here](https://www.gyan.dev/ffmpeg/builds/)
572+
- From the Command Prompt (CMD), navigate to the download directory to use it, or add it to your system PATH
573+
- Run the following command to convert the `.g722` file into `.wav`
574+
575+
```bash
576+
ffmpeg -f g722 -i <update-with-directory>\test.g722 -ar 16000 test.wav
577+
```
578+
Then, you can open the `.wav` file with a software like [Audacity](https://www.audacityteam.org/) or any media player to play back the audio.
579+
555580
556581
#### Machine Learning and Audio Analysis
557582

0 commit comments

Comments
 (0)