|
6 | 6 | */
|
7 | 7 |
|
8 | 8 | #include <Arduino_AdvancedAnalog.h>
|
9 |
| - |
10 | 9 | #include <Arduino_USBHostMbed5.h>
|
11 |
| - |
12 | 10 | #include <DigitalOut.h>
|
13 | 11 | #include <FATFileSystem.h>
|
14 | 12 |
|
15 |
| -AdvancedDAC dac1(A12); |
16 |
| - |
17 | 13 | USBHostMSD msd;
|
| 14 | +WavReader wavreader; |
| 15 | +AdvancedDAC dac1(A12); |
18 | 16 | mbed::FATFileSystem usb("USB_DRIVE");
|
19 | 17 |
|
20 |
| -FILE * file = nullptr; |
21 |
| -int sample_size = 0; |
22 |
| -int samples_count = 0; |
| 18 | +#define N_SAMPLES (512) |
23 | 19 |
|
24 |
| - |
25 |
| -void setup() |
26 |
| -{ |
| 20 | +void setup() { |
27 | 21 | Serial.begin(115200);
|
28 |
| - while (!Serial); |
| 22 | + while (!Serial) { |
| 23 | + } |
29 | 24 |
|
30 |
| - /* Enable power for HOST USB connector. */ |
| 25 | + // Enable power for HOST USB connector. |
31 | 26 | pinMode(PA_15, OUTPUT);
|
32 | 27 | digitalWrite(PA_15, HIGH);
|
33 | 28 |
|
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 | + } |
36 | 33 |
|
37 | 34 | Serial.println("Mounting USB device ...");
|
38 | 35 | int const rc_mount = usb.mount(&msd);
|
39 |
| - if (rc_mount) |
40 |
| - { |
| 36 | + if (rc_mount) { |
41 | 37 | Serial.print("Error mounting USB device ");
|
42 | 38 | Serial.println(rc_mount);
|
43 |
| - return; |
| 39 | + while (1); |
44 | 40 | }
|
45 | 41 |
|
46 | 42 | 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); |
55 | 46 | }
|
56 | 47 |
|
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:"); |
77 | 48 | 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); |
122 | 53 |
|
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)) { |
132 | 56 | Serial.println("Failed to start DAC1 !");
|
133 |
| - return; |
| 57 | + while (1); |
134 | 58 | }
|
135 | 59 | }
|
136 | 60 |
|
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 | + } |
154 | 78 | }
|
155 |
| - |
156 |
| - /* Write the buffer to DAC. */ |
157 |
| - dac1.write(buf); |
| 79 | + // Write the buffer to DAC. |
| 80 | + dac1.write(dacbuf); |
| 81 | + pcmbuf.release(); |
158 | 82 | }
|
159 | 83 | }
|
0 commit comments