Skip to content

Commit 10caac9

Browse files
committed
Audio guide content update
1 parent 7bb9eaa commit 10caac9

File tree

1 file changed

+65
-2
lines changed
  • content/hardware/08.mega/boards/giga-r1/tutorials/giga-audio

1 file changed

+65
-2
lines changed

content/hardware/08.mega/boards/giga-r1/tutorials/giga-audio/content.md

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ void loop(){
639639

640640
### Playback with the GIGA R1 DACs
641641

642-
The GIGA R1 12-bit DAC channels can also be used to create a simple mono or stereo audio playback. In the following example, we will use a USB drive as our source for a .WAV audio files and output them using custom libraries to help us process these files. A 3.5mm audio jack-compatible speaker is also required for the audio playback output.
642+
The GIGA R1 12-bit DAC channels can also be used to create a simple mono or stereo audio playback. In the following example, we will use a USB drive as our source for a .WAV audio files and output them using custom libraries to help us process them. A 3.5mm audio jack-compatible speaker is also required for the audio playback output.
643643

644644
The libraries we are going to use are the following:
645645

@@ -748,4 +748,67 @@ void setup() {
748748
void loop() {}
749749
```
750750

751-
Once you have the setup and the code ready, you can upload it to the GIGA R1 board and play the audio file of your choice. The example can playback 3 to 4 seconds of an audio file.
751+
Once you have the setup and the code ready, you can upload it to the GIGA R1 board and play the audio file of your choice. The example can playback 3 to 4 seconds of an audio file.
752+
753+
## Pulse Density Modulation Support
754+
755+
Pulse Density Support (PDM) is a form of modulation used to represent analog information into digital information; PDM uses a high-frequency stream of 1-bit digital samples. In PDM, a large cluster of ones represents a positive amplitude, while a large cluster of zeros represents a negative amplitude.
756+
757+
The GIGA R1 PDM support can be used with the [built-in PDM library](https://docs.arduino.cc/learn/built-in-libraries/pdm). Let's check an interesting example that shows of how to read a PDM microphone wwith the GIGA R1:
758+
759+
```arduino
760+
#include <PDM.h>
761+
762+
// Default number of output channels
763+
static const char channels = 1;
764+
765+
// Default PCM output frequency
766+
static const int frequency = 16000;
767+
768+
// Buffer to read samples into, each sample is 16-bits
769+
short sampleBuffer[128];
770+
771+
// Number of audio samples read
772+
volatile int samplesRead;
773+
774+
void setup() {
775+
Serial.begin(9600);
776+
while (!Serial);
777+
778+
// Configure the callback function and gain
779+
PDM.onReceive(onPDMdata);
780+
PDM.setGain(30);
781+
782+
// Initialize PDM microphone in mono mode, and a 16 kHz sample rate:
783+
if (!PDM.begin(channels, frequency)) {
784+
Serial.println("Failed to start PDM!");
785+
while (1);
786+
}
787+
}
788+
789+
void loop() {
790+
// Wait for samples to be read
791+
if (samplesRead) {
792+
793+
// Print samples to the Serial Monitor
794+
for (int i = 0; i < samplesRead; i++) {
795+
if(channels == 2) {
796+
Serial.print("L:");
797+
Serial.print(sampleBuffer[i]);
798+
Serial.print(" R:");
799+
i++;
800+
}
801+
Serial.println(sampleBuffer[i]);
802+
}
803+
804+
samplesRead = 0;
805+
}
806+
}
807+
808+
// Callback function
809+
void onPDMdata() {
810+
int bytesAvailable = PDM.available();
811+
PDM.read(sampleBuffer, bytesAvailable);
812+
samplesRead = bytesAvailable / 2;
813+
}
814+
```

0 commit comments

Comments
 (0)