Skip to content

Commit 38a0d93

Browse files
committed
examples: Update Audio_Playback example.
- Switch to WavReader. - Fix the handling of multi-channels. - Simplify the example code. Signed-off-by: iabdalkader <[email protected]>
1 parent a4421bd commit 38a0d93

File tree

1 file changed

+43
-119
lines changed

1 file changed

+43
-119
lines changed

examples/Beginner/Audio_Playback/Audio_Playback.ino

+43-119
Original file line numberDiff line numberDiff line change
@@ -6,154 +6,78 @@
66
*/
77

88
#include <Arduino_AdvancedAnalog.h>
9-
109
#include <Arduino_USBHostMbed5.h>
11-
1210
#include <DigitalOut.h>
1311
#include <FATFileSystem.h>
1412

15-
AdvancedDAC dac1(A12);
16-
1713
USBHostMSD msd;
14+
WavReader wavreader;
15+
AdvancedDAC dac1(A12);
1816
mbed::FATFileSystem usb("USB_DRIVE");
1917

20-
FILE * file = nullptr;
21-
int sample_size = 0;
22-
int samples_count = 0;
18+
#define N_SAMPLES (512)
2319

24-
25-
void setup()
26-
{
20+
void setup() {
2721
Serial.begin(115200);
28-
while (!Serial);
22+
while (!Serial) {
23+
}
2924

30-
/* Enable power for HOST USB connector. */
25+
// Enable power for HOST USB connector.
3126
pinMode(PA_15, OUTPUT);
3227
digitalWrite(PA_15, HIGH);
3328

34-
Serial.println("Please connect a USB stick to the GIGA's USB port ...");
35-
while (!msd.connect()) delay(100);
29+
Serial.println("Please connect a USB stick to the USB host port ...");
30+
while (!msd.connect()) {
31+
delay(100);
32+
}
3633

3734
Serial.println("Mounting USB device ...");
3835
int const rc_mount = usb.mount(&msd);
39-
if (rc_mount)
40-
{
36+
if (rc_mount) {
4137
Serial.print("Error mounting USB device ");
4238
Serial.println(rc_mount);
43-
return;
39+
while (1);
4440
}
4541

4642
Serial.println("Opening audio file ...");
47-
48-
/* 16-bit PCM Mono 16kHz realigned noise reduction */
49-
file = fopen("/USB_DRIVE/AUDIO_SAMPLE.wav", "rb");
50-
if (file == nullptr)
51-
{
52-
Serial.print("Error opening audio file: ");
53-
Serial.println(strerror(errno));
54-
return;
43+
if (!wavreader.begin("/USB_DRIVE/AUDIO_SAMPLE.wav", N_SAMPLES, 1, false)) {
44+
Serial.print("Error opening audio file: ");
45+
while (1);
5546
}
5647

57-
Serial.println("Reading audio header ...");
58-
struct wav_header_t
59-
{
60-
char chunkID[4]; //"RIFF" = 0x46464952
61-
unsigned long chunkSize; //28 [+ sizeof(wExtraFormatBytes) + wExtraFormatBytes] + sum(sizeof(chunk.id) + sizeof(chunk.size) + chunk.size)
62-
char format[4]; //"WAVE" = 0x45564157
63-
char subchunk1ID[4]; //"fmt " = 0x20746D66
64-
unsigned long subchunk1Size; //16 [+ sizeof(wExtraFormatBytes) + wExtraFormatBytes]
65-
unsigned short audioFormat;
66-
unsigned short numChannels;
67-
unsigned long sampleRate;
68-
unsigned long byteRate;
69-
unsigned short blockAlign;
70-
unsigned short bitsPerSample;
71-
};
72-
73-
wav_header_t header;
74-
fread(&header, sizeof(header), 1, file);
75-
76-
Serial.println("WAV File Header read:");
7748
char msg[64] = {0};
78-
snprintf(msg, sizeof(msg), "File Type: %s", header.chunkID);
79-
Serial.println(msg);
80-
snprintf(msg, sizeof(msg), "File Size: %ld", header.chunkSize);
81-
Serial.println(msg);
82-
snprintf(msg, sizeof(msg), "WAV Marker: %s", header.format);
83-
Serial.println(msg);
84-
snprintf(msg, sizeof(msg), "Format Name: %s", header.subchunk1ID);
85-
Serial.println(msg);
86-
snprintf(msg, sizeof(msg), "Format Length: %ld", header.subchunk1Size);
87-
Serial.println(msg);
88-
snprintf(msg, sizeof(msg), "Format Type: %hd", header.audioFormat);
89-
Serial.println(msg);
90-
snprintf(msg, sizeof(msg), "Number of Channels: %hd", header.numChannels);
91-
Serial.println(msg);
92-
snprintf(msg, sizeof(msg), "Sample Rate: %ld", header.sampleRate);
93-
Serial.println(msg);
94-
snprintf(msg, sizeof(msg), "Sample Rate * Bits/Sample * Channels / 8: %ld", header.byteRate);
95-
Serial.println(msg);
96-
snprintf(msg, sizeof(msg), "Bits per Sample * Channels / 8: %hd", header.blockAlign);
97-
Serial.println(msg);
98-
snprintf(msg, sizeof(msg), "Bits per Sample: %hd", header.bitsPerSample);
99-
Serial.println(msg);
100-
101-
/* Find the data section of the WAV file. */
102-
struct chunk_t
103-
{
104-
char ID[4];
105-
unsigned long size;
106-
};
107-
108-
chunk_t chunk;
109-
snprintf(msg, sizeof(msg), "id\t" "size");
110-
Serial.println(msg);
111-
/* Find data chunk. */
112-
while (true)
113-
{
114-
fread(&chunk, sizeof(chunk), 1, file);
115-
snprintf(msg, sizeof(msg), "%c%c%c%c\t" "%li", chunk.ID[0], chunk.ID[1], chunk.ID[2], chunk.ID[3], chunk.size);
116-
Serial.println(msg);
117-
if (*(unsigned int *) &chunk.ID == 0x61746164)
118-
break;
119-
/* Skip chunk data bytes. */
120-
fseek(file, chunk.size, SEEK_CUR);
121-
}
49+
snprintf(msg, sizeof(msg), "Number of Channels: %hd", wavreader.channels()); Serial.println(msg);
50+
snprintf(msg, sizeof(msg), "Sample Rate: %ld", wavreader.sample_rate()); Serial.println(msg);
51+
snprintf(msg, sizeof(msg), "Bits per Sample: %hd", wavreader.resolution()); Serial.println(msg);
52+
snprintf(msg, sizeof(msg), "Number of Samples = %i", wavreader.sample_count()); Serial.println(msg);
12253

123-
/* Determine number of samples. */
124-
sample_size = header.bitsPerSample / 8;
125-
samples_count = chunk.size * 8 / header.bitsPerSample;
126-
snprintf(msg, sizeof(msg), "Sample size = %i", sample_size); Serial.println(msg);
127-
snprintf(msg, sizeof(msg), "Samples count = %i", samples_count); Serial.println(msg);
128-
129-
/* Configure the advanced DAC. */
130-
if (!dac1.begin(AN_RESOLUTION_12, header.sampleRate, 256, 16))
131-
{
54+
// Configure and start the DAC. Note that the DAC is single-channel.
55+
if (!dac1.begin(AN_RESOLUTION_12, wavreader.sample_rate(), N_SAMPLES, 32)) {
13256
Serial.println("Failed to start DAC1 !");
133-
return;
57+
while (1);
13458
}
13559
}
13660

137-
void loop()
138-
{
139-
if (dac1.available() && !feof(file))
140-
{
141-
/* Read data from file. */
142-
uint16_t sample_data[256] = {0};
143-
fread(sample_data, sample_size, 256, file);
144-
145-
/* Get a free buffer for writing. */
146-
SampleBuffer buf = dac1.dequeue();
147-
148-
/* Write data to buffer. */
149-
for (size_t i = 0; i < buf.size(); i++)
150-
{
151-
/* Scale down to 12 bit. */
152-
uint16_t const dac_val = ((static_cast<unsigned int>(sample_data[i])+32768)>>4) & 0x0fff;
153-
buf[i] = dac_val;
61+
void loop() {
62+
if (dac1.available() && wavreader.available()) {
63+
// Get a free buffer for writing.
64+
SampleBuffer dacbuf = dac1.dequeue();
65+
66+
// Read a samples buffer from the wav file.
67+
SampleBuffer pcmbuf = wavreader.read();
68+
69+
// Process and write samples to the DAC buffer.
70+
for (size_t i=0; i<N_SAMPLES; i++) {
71+
// Map the samples to positive range and down scale to 12-bit.
72+
if (wavreader.channels() == 1) {
73+
dacbuf[i] = ((unsigned int) ((int16_t) pcmbuf[i] + 32768)) >> 4;
74+
} else {
75+
// Average the two channels, if the file has two channels.
76+
dacbuf[i] = ((unsigned int) ((((int16_t) pcmbuf[(i * 2)] + (int16_t) pcmbuf[(i * 2) + 1]) / 2) + 32768)) >> 4;
77+
}
15478
}
155-
156-
/* Write the buffer to DAC. */
157-
dac1.write(buf);
79+
// Write the buffer to DAC.
80+
dac1.write(dacbuf);
81+
pcmbuf.release();
15882
}
15983
}

0 commit comments

Comments
 (0)