Skip to content

Commit 61f5f36

Browse files
committed
AudioServerEx examples DRAFT
1 parent 7ad0a41 commit 61f5f36

File tree

5 files changed

+140
-10
lines changed

5 files changed

+140
-10
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# A Simple SdFat Audio Player
2+
3+
The example demonstrates how to implement an __MP3 Player__: which provides the data from a SD drive and provides the audio via a Webserver!
4+
5+
## SD Card
6+
7+
Here is the information how to wire the SD card to the ESP32
8+
9+
| SD | ESP32
10+
|-------|-----------------------
11+
| CS | VSPI-CS0 (GPIO 05)
12+
| SCK | VSPI-CLK (GPIO 18)
13+
| MOSI | VSPI-MOSI (GPIO 23)
14+
| MISO | VSPI-MISO (GPIO 19)
15+
| VCC | VIN (5V)
16+
| GND | GND
17+
18+
![SD](https://www.pschatzmann.ch/wp-content/uploads/2021/04/sd-module.jpeg)
19+
20+
21+
## Dependencies
22+
23+
- https://github.com/pschatzmann/arduino-audio-tools
24+
- https://github.com/pschatzmann/TinyHttp.git
25+
- Arduino SD library
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @file player-sdfat-a2dp.ino
3+
* @brief see https://github.com/pschatzmann/arduino-audio-tools/blob/main/examples/examples-player/player-sdfat-a2dp/README.md
4+
*
5+
* @author Phil Schatzmann
6+
* @copyright GPLv3
7+
*/
8+
9+
10+
#include "AudioTools.h"
11+
#include "AudioLibs/AudioSourceSD.h"
12+
#include "AudioLibs/AudioServerEx.h"
13+
#include "AudioCodecs/CodecCopy.h"
14+
15+
const char *ssid = "SSID";
16+
const char *password = "PWD";
17+
const char *startFilePath="/";
18+
const char* ext="mp3";
19+
AudioSourceSD source(startFilePath, ext);
20+
AudioServerEx out;
21+
AudioPlayer player(source, out, *new CopyDecoder());
22+
23+
void setup() {
24+
Serial.begin(115200);
25+
AudioLogger::instance().begin(Serial, AudioLogger::Warning);
26+
27+
// setup output - We need to login and serve the data as audio/mp3
28+
auto cfg = out.defaultConfig();
29+
cfg.password = password;
30+
cfg.ssid = ssid;
31+
cfg.mime = "audio/mp3";
32+
out.begin(cfg);
33+
34+
// setup player
35+
player.setVolume(1.0);
36+
player.begin();
37+
38+
}
39+
40+
void loop() {
41+
player.copy();
42+
out.copy();
43+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Webserver
2+
3+
With the help of the ESP32 WIFI functionality we can implement a simple web server.
4+
In the example we use a Sine Wave generator as sound source and return the result as an WAV file
5+
6+
Multiple users can connect to the server!
7+
8+
## Dependencies
9+
10+
- https://github.com/pschatzmann/arduino-audio-tools
11+
- https://github.com/pschatzmann/TinyHttp.git
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @file streams-generator-server_wav.ino
3+
*
4+
* This sketch generates a test sine wave. The result is provided as WAV stream which can be listened to in a Web Browser
5+
*
6+
* @author Phil Schatzmann
7+
* @copyright GPLv3
8+
*
9+
*/
10+
11+
#include "AudioTools.h"
12+
#include "AudioLibs/AudioServerEx.h"
13+
14+
// WIFI
15+
const char *ssid = "ssid";
16+
const char *password = "password";
17+
AudioWAVServerEx server;
18+
// Sound Generation
19+
const int sample_rate = 10000;
20+
const int channels = 1;
21+
SineWaveGenerator<int16_t> sineWave; // Subclass of SoundGenerator with max amplitude of 32000
22+
GeneratedSoundStream<int16_t> in(sineWave); // Stream generated from sine wave
23+
24+
25+
void setup() {
26+
Serial.begin(115200);
27+
AudioLogger::instance().begin(Serial,AudioLogger::Info);
28+
29+
// start server
30+
auto cfg = server.defaultConfig();
31+
cfg.sample_rate = sample_rate;
32+
cfg.channels = channels;
33+
cfg.ssid = ssid;
34+
cfg.password = password;
35+
cfg.input = &in;
36+
server.begin(cfg);
37+
38+
// start generation of sound
39+
sineWave.begin(channels, sample_rate, N_B4);
40+
in.begin();
41+
}
42+
43+
44+
// copy the data
45+
void loop() {
46+
server.copy();
47+
}

src/AudioLibs/AudioServerEx.h

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,23 @@ class AudioServerEx : public AudioPrint {
4444
info.password = pwd;
4545
}
4646

47-
AudioServerExConfig defaultConfig() {
47+
virtual AudioServerExConfig defaultConfig() {
4848
AudioServerExConfig cfg;
4949
return cfg;
5050
}
5151

52-
bool begin(AudioServerExConfig cfg) {
52+
virtual bool begin(AudioServerExConfig cfg) {
5353
info = cfg;
5454
return begin();
5555
}
5656

57-
bool begin(Stream &in, const char* contentType) {
57+
virtual bool begin(Stream &in, const char* contentType) {
5858
info.input = &in;
5959
info.mime = contentType;
6060
return begin();
6161
}
6262

63-
bool begin() {
63+
virtual bool begin() {
6464
end(); // we (re) start with a clean state
6565
if (info.input==nullptr){
6666
p_stream = new ExtensionStream(info.path,tinyhttp::GET, info.mime );
@@ -73,7 +73,7 @@ class AudioServerEx : public AudioPrint {
7373
return p_server->begin(info.port, info.ssid, info.password);
7474
}
7575

76-
void end() {
76+
virtual void end() {
7777
if (p_stream!=nullptr) {
7878
delete p_stream;
7979
p_stream = nullptr;
@@ -96,7 +96,7 @@ class AudioServerEx : public AudioPrint {
9696
}
9797

9898
/// Needs to be called if the data was provided as input Stream in the AudioServerExConfig
99-
void copy() {
99+
virtual void copy() {
100100
if (p_server!=nullptr){
101101
p_server->copy();
102102
}
@@ -130,6 +130,12 @@ class AudioWAVServerEx : public AudioServerEx {
130130
/// To be compatible with legacy API
131131
AudioWAVServerEx(const char *ssid, const char* pwd):AudioServerEx(ssid, pwd){}
132132

133+
AudioServerExConfig defaultConfig() override {
134+
AudioServerExConfig cfg;
135+
cfg.mime = "audio/wav";
136+
return cfg;
137+
}
138+
133139
/// Legacy API support
134140
bool begin(Stream &in, int sample_rate, int channels, int bits_per_sample=16) {
135141
info.input = &in;
@@ -140,10 +146,8 @@ class AudioWAVServerEx : public AudioServerEx {
140146
return AudioServerEx::begin();
141147
}
142148

143-
AudioServerExConfig defaultConfig() {
144-
AudioServerExConfig cfg;
145-
cfg.mime = "audio/wav";
146-
return cfg;
149+
bool begin(AudioServerExConfig cfg) override{
150+
return AudioServerEx::begin(cfg);
147151
}
148152

149153
protected:

0 commit comments

Comments
 (0)