Skip to content

Commit 0e18b4f

Browse files
committed
MP3 examples
1 parent 597fde9 commit 0e18b4f

File tree

8 files changed

+887
-10
lines changed

8 files changed

+887
-10
lines changed

examples/examples-stream/streams-memory_mp3-analog/streams-memory_mp3-analog.ino

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,32 @@
1919

2020

2121
MemoryStream mp3(BabyElephantWalk60_mp3, BabyElephantWalk60_mp3_len);
22-
AnalogAudioStream out; // Analog output
23-
EncodedAudioStream decoded(&out, new MP3DecoderHelix()); // output to decoder
24-
StreamCopy copier(decoded, mp3); // copy in to out
22+
AnalogAudioStream analog; // Analog output
23+
EncodedAudioStream out(&analog, new MP3DecoderHelix()); // output to decoder
24+
StreamCopy copier(out, mp3); // copy in to analog
2525

2626
void setup(){
2727
Serial.begin(115200);
2828
AudioLogger::instance().begin(Serial, AudioLogger::Info);
2929

3030
// update audio info with info from decoder
31-
decoded.setNotifyAudioChange(out);
31+
out.setNotifyAudioChange(analog);
3232

3333
// begin processing
34-
auto cfg = out.defaultConfig();
35-
out.begin(cfg);
34+
auto cfg = analog.defaultConfig();
35+
analog.begin(cfg);
3636

37-
decoded.begin();
37+
out.begin();
3838
}
3939

4040
void loop(){
4141
if (mp3) {
4242
copier.copy();
4343
} else {
44-
auto info = decoded.decoder().audioInfo();
44+
auto info = out.decoder().audioInfo();
4545
LOGI("The audio rate from the mp3 file is %d", info.sample_rate);
4646
LOGI("The channels from the mp3 file is %d", info.channels);
47-
out.end();
47+
analog.end();
4848
stop();
4949
}
5050
}

examples/examples-stream/streams-memory_mp3-pwm/streams-memory_mp3-pwm.ino

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include "BabyElephantWalk60_mp3.h"
1818

1919

20-
2120
MemoryStream mp3(BabyElephantWalk60_mp3, BabyElephantWalk60_mp3_len);
2221
PWMAudioStream out; // PWM output
2322
EncodedAudioStream decoded(&out, new MP3DecoderHelix()); // output to decoder
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Decoding a MP3 file
2+
3+
In this example we decode a very short MP3 file using lebhelix
4+
5+
### External DAC:
6+
7+
![DAC](https://pschatzmann.github.io/arduino-audio-tools/resources/dac.jpeg)
8+
9+
For my tests I am using the 24-bit PCM5102 PCM5102A Stereo DAC Digital-to-analog Converter PLL Voice Module pHAT
10+
11+
I am just using the default pins defined by the framework. However I could change them with the help of the config object. The mute pin can be defined in the constructor of the I2SStream - by not defining anything we use the default which is GPIO23
12+
13+
14+
DAC | ESP32
15+
-----|----------------
16+
VCC | 5V
17+
GND | GND
18+
BCK | BCK (GPIO14)
19+
DIN | OUT (GPIO22)
20+
LCK | BCK (GPIO15)
21+
FMT | GND
22+
XMT | 3V (or another GPIO PIN which is set to high)
23+
24+
- DMP - De-emphasis control for 44.1kHz sampling rate(1): Off (Low) / On (High)
25+
- FLT - Filter select : Normal latency (Low) / Low latency (High)
26+
- SCL - System clock input (probably SCL on your board).
27+
- FMT - Audio format selection : I2S (Low) / Left justified (High)
28+
- XMT - Soft mute control(1): Soft mute (Low) / soft un-mute (High)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @file streams-memory_mp3_short-i2s-2.ino
3+
* @author Phil Schatzmann
4+
* @brief decode MP3 stream and output as i2s signal
5+
* @version 0.1
6+
* @date 2021-01-24
7+
*
8+
* @copyright Copyright (c) 2021
9+
10+
*/
11+
12+
#include "AudioTools.h"
13+
#include "AudioCodecs/CodecMP3Helix.h"
14+
#include "zero.h"
15+
16+
17+
MemoryStream mp3(zero_mp3, zero_mp3_len);
18+
I2SStream i2s; // PWM output
19+
MP3DecoderHelix helix;
20+
EncodedAudioStream out(&i2s, &helix); // output to decoder
21+
StreamCopy copier(out, mp3); // copy in to out
22+
23+
void setup(){
24+
Serial.begin(115200);
25+
AudioLogger::instance().begin(Serial, AudioLogger::Info);
26+
27+
// begin processing
28+
auto cfg = i2s.defaultConfig();
29+
cfg.sample_rate = 24000;
30+
cfg.channels = 1;
31+
i2s.begin(cfg);
32+
out.begin();
33+
34+
sayWord();
35+
36+
}
37+
38+
void sayWord() {
39+
Serial.println("Saying word...");
40+
helix.begin(); // so that we can repeatedly call this method
41+
copier.copyAll();
42+
helix.end(); // flush output
43+
}
44+
45+
void loop(){
46+
}
47+

examples/examples-stream/streams-memory_mp3_short-i2s-2/zero.h

Lines changed: 365 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Decoding a MP3 file
2+
3+
In this example we decode a very short MP3 file using lebhelix
4+
5+
### External DAC:
6+
7+
![DAC](https://pschatzmann.github.io/arduino-audio-tools/resources/dac.jpeg)
8+
9+
For my tests I am using the 24-bit PCM5102 PCM5102A Stereo DAC Digital-to-analog Converter PLL Voice Module pHAT
10+
11+
I am just using the default pins defined by the framework. However I could change them with the help of the config object. The mute pin can be defined in the constructor of the I2SStream - by not defining anything we use the default which is GPIO23
12+
13+
14+
DAC | ESP32
15+
-----|----------------
16+
VCC | 5V
17+
GND | GND
18+
BCK | BCK (GPIO14)
19+
DIN | OUT (GPIO22)
20+
LCK | BCK (GPIO15)
21+
FMT | GND
22+
XMT | 3V (or another GPIO PIN which is set to high)
23+
24+
- DMP - De-emphasis control for 44.1kHz sampling rate(1): Off (Low) / On (High)
25+
- FLT - Filter select : Normal latency (Low) / Low latency (High)
26+
- SCL - System clock input (probably SCL on your board).
27+
- FMT - Audio format selection : I2S (Low) / Left justified (High)
28+
- XMT - Soft mute control(1): Soft mute (Low) / soft un-mute (High)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @file streams-memory_mp3_short-i2s.ino
3+
* @author Phil Schatzmann
4+
* @brief decode MP3 stream and output as i2s signal
5+
* @version 0.1
6+
* @date 2021-01-24
7+
*
8+
* @copyright Copyright (c) 2021
9+
10+
*/
11+
12+
#include "AudioTools.h"
13+
#include "AudioCodecs/CodecMP3Helix.h"
14+
#include "zero.h"
15+
16+
MemoryStream mp3(zero_mp3, zero_mp3_len);
17+
I2SStream i2s;
18+
MP3DecoderHelix helix;
19+
EncodedAudioStream out(&i2s, &helix); // output to decoder
20+
StreamCopy copier(out, mp3); // copy in to i2s
21+
22+
void setup(){
23+
Serial.begin(115200);
24+
AudioLogger::instance().begin(Serial, AudioLogger::Info);
25+
26+
// begin processing
27+
auto cfg = i2s.defaultConfig();
28+
cfg.sample_rate = 24000;
29+
cfg.channels = 1;
30+
i2s.begin(cfg);
31+
out.begin();
32+
}
33+
34+
void loop(){
35+
if (mp3.available()) {
36+
copier.copy();
37+
} else {
38+
helix.end(); // flush output
39+
auto info = out.decoder().audioInfo();
40+
LOGI("The audio rate from the mp3 file is %d", info.sample_rate);
41+
LOGI("The channels from the mp3 file is %d", info.channels);
42+
i2s.end();
43+
stop();
44+
}
45+
}

0 commit comments

Comments
 (0)