-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
129 lines (103 loc) · 3.33 KB
/
Copy pathmain.cpp
File metadata and controls
129 lines (103 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <Arduino.h>
#include <OneButton.h>
#include <AudioTools.h>
#include <AudioTools/Disk/AudioSourceLittleFS.h>
// https://github.com/pschatzmann/arduino-libhelix
#include <AudioTools/AudioCodecs/CodecMP3Helix.h>
// data sources: LittleFS
// output: I2S
// Decoder: MP3
// Volume Control
// start: play()
// Navigation: next(), previous()
// Metadata
// see https://github.com/pschatzmann/arduino-audio-tools/blob/main/examples/examples-player/player-littlefs-i2s/player-littlefs-i2s.ino
#define PIN_INPUT GPIO_NUM_21
const int pinBck = 45;
const int pinWs = 1;
const int pinData = 46;
const char *startFilePath="/";
const char* ext = "mp3";
float volume = 0.005f; // 0.005f is the minimum volume for the used amplifier board
OneButton button(PIN_INPUT, true);
AudioSourceLittleFS source(startFilePath, ext);
I2SStream i2s;
MP3DecoderHelix decoder;
AudioPlayer player(source, i2s, decoder);
// events
volatile bool toggledPlaying = false;
volatile bool stoppedPlaying = false;
volatile bool nextPlaying = false;
void printMetaData(MetaDataType type, const char* str, int len){
Serial.print("==> ");
Serial.print(toStr(type));
Serial.print(": ");
Serial.println(str);
}
void onClick() {
toggledPlaying = true;
}
void onDoubleClick() {
stoppedPlaying = true;
}
void onLongPressStop() {
nextPlaying = true;
}
void buttonTask(void *parameters)
{
for (;;)
{
button.tick();
vTaskDelay(pdMS_TO_TICKS(40));
}
}
void setup() {
Serial.begin(115200);
// init button and its interrupts
button.attachClick(onClick);
button.attachDoubleClick(onDoubleClick);
button.attachLongPressStop(onLongPressStop);
// setup logger
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Info);
// setup output
auto cfg = i2s.defaultConfig(TX_MODE);
cfg.pin_bck = pinBck;
cfg.pin_ws = pinWs;
cfg.pin_data = pinData;
// cfg.bits_per_sample = 24;
i2s.begin(cfg);
// setup player
//source.setFileFilter("*Bob Dylan*");
player.setMetadataCallback(printMetaData);
player.begin();
player.setAutoNext(true); // automatically move to the next stream when the current stream is finished
player.setVolume(volume);
// start tasks
xTaskCreatePinnedToCore(buttonTask, "button", 1024, NULL, 1, NULL, ARDUINO_RUNNING_CORE);
}
void loop() {
if (toggledPlaying) {
toggledPlaying = false;
if (player.isActive()) {
// player.stop(); // Halts playback; equivalent to setActive(false)
player.end(); // Ends playback and resets decoder/intermediate stages
} else {
player.play();
}
}
if (stoppedPlaying) {
stoppedPlaying = false;
player.begin(0, false); // Starts or restarts playback from the first or given stream index, but does not start playing
}
if (nextPlaying) {
nextPlaying = false;
player.next(); // Moves to the next stream
}
// copy data from source to output and do the decoding in between
// this will also trigger the metadata callback
// Note: the player will automatically move to the next stream when the current stream is finished or on timeout (if enabled)
// You have to call copy() in the loop to have more control over the copying and to be able to do other things in between
// put it into a separate task causes a problem with popping speaker when the player stops
player.copy();
delay(2); // add a small delay to give some time to the tasks to run
}