Skip to content

Commit 263f6b2

Browse files
committed
WAV encoding
1 parent b450bcf commit 263f6b2

File tree

19 files changed

+351733
-115
lines changed

19 files changed

+351733
-115
lines changed

.vscode/settings.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"files.associations": {
3+
"ostream": "cpp",
4+
"cullsettings": "cpp",
5+
"types": "cpp",
6+
"*.tcc": "cpp",
7+
"functional": "cpp",
8+
"string_view": "cpp",
9+
"quat": "cpp",
10+
"vec2d": "cpp",
11+
"vec2f": "cpp",
12+
"vec3d": "cpp",
13+
"vec3f": "cpp",
14+
"vec4d": "cpp",
15+
"vec4f": "cpp",
16+
"tuple": "cpp"
17+
}
18+
}

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ Here are a couple of simple test sketches to demo different output destinations:
9090
- [streams-generator-dac](/examples/streams-generator-dac) Output of generated sound on ESP32 internal DAC via I2S
9191
- [streams-generator-a2dp](/examples/streams-generator-a2dp) Output of generated sound on Bluetooth Speaker using A2DP
9292
- [streams-adc-serial](/examples/streams-adc-serial) Displaying input from analog microphone on the Serial Plotter
93+
- [streams-memory_wav-serial](/examples/streams-memory_wav-serial) Decoding of WAV from Flash memory and display on the Serial Plotter
94+
- [streams-generator-webserver_wav](/examples/streams-generator-webserver_wav) Encoding of WAV from generated sound returned via Webserver
9395

9496
And some more useful examples:
9597

@@ -149,7 +151,7 @@ This is currently work in progress:
149151
| I2S | tested |
150152
| Files (RAW, MP3...) | tested |
151153
| Streams | tested |
152-
| WAV encoding/deconding | open |
154+
| WAV encoding/deconding | tested |
153155
| AAC encoding/deconding | open |
154156
| int24_t | tested |
155157

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
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
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#include <WiFi.h>
2+
#include "AudioTools.h"
3+
4+
using namespace audio_tools;
5+
6+
const char *ssid = "ssid";
7+
const char *password = "password";
8+
WiFiServer server(80);
9+
WiFiClient client;
10+
11+
const int sample_rate = 10000;
12+
const int data_length = 100000;
13+
const int channels = 1;
14+
15+
SineWaveGenerator<int16_t> sineWave; // subclass of SoundGenerator with max amplitude of 32000
16+
GeneratedSoundStream<int16_t> in(sineWave, channels); // Stream generated from sine wave
17+
StreamCopy copier; // buffered copy
18+
WAVEncoder encoder;
19+
AudioOutputStream wav_stream(encoder); // WAV output stream
20+
21+
void setup()
22+
{
23+
Serial.begin(115200);
24+
AudioLogger::instance().begin(Serial,AudioLogger::Debug);
25+
26+
WiFi.begin(ssid, password);
27+
while (WiFi.status() != WL_CONNECTED)
28+
{
29+
delay(500);
30+
Serial.print(".");
31+
}
32+
33+
Serial.println("");
34+
Serial.println("WiFi connected.");
35+
Serial.println("IP address: ");
36+
Serial.println(WiFi.localIP());
37+
38+
server.begin();
39+
sineWave.begin(sample_rate, B4);
40+
41+
in.begin();
42+
}
43+
44+
45+
void processClient() {
46+
if (client) { // if you get a client,
47+
Serial.println("New Client."); // print a message out the serial port
48+
String currentLine = ""; // make a String to hold incoming data from the client
49+
while (client.connected()) { // loop while the client's connected
50+
if (client.available()) { // if there's bytes to read from the client,
51+
char c = client.read(); // read a byte, then
52+
Serial.write(c); // print it out the serial monitor
53+
if (c == '\n') { // if the byte is a newline character
54+
55+
// if the current line is blank, you got two newline characters in a row.
56+
// that's the end of the client HTTP request, so send a response:
57+
if (currentLine.length() == 0){
58+
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
59+
// and a content-type so the client knows what's coming, then a blank line:
60+
client.println("HTTP/1.1 200 OK");
61+
client.println("Content-type:audio/wav");
62+
client.println();
63+
64+
// set up wav encoder
65+
auto config = encoder.defaultConfig();
66+
config.channels = channels;
67+
config.sample_rate = sample_rate;
68+
//config.data_length = data_length;
69+
config.is_streamed = true;
70+
encoder.begin(client, config);
71+
72+
Serial.println("Returning WAV stream...");
73+
copier.begin(wav_stream, in);
74+
// break out of the while loop:
75+
break;
76+
} else { // if you got a newline, then clear currentLine:
77+
currentLine = "";
78+
}
79+
}
80+
else if (c != '\r')
81+
{ // if you got anything else but a carriage return character,
82+
currentLine += c; // add it to the end of the currentLine
83+
}
84+
}
85+
}
86+
}
87+
}
88+
89+
void loop() {
90+
if (!client.connected()) {
91+
client = server.available(); // listen for incoming clients
92+
processClient();
93+
} else {
94+
// We are connected: copy input from source to wav output
95+
if (encoder){
96+
copier.copy();
97+
if (!encoder) {
98+
Serial.println("stop client...");
99+
client.stop();
100+
}
101+
}
102+
}
103+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#Decoding a WAV file
2+
3+
In this example we decode a WAV file into RAW output and send it to the Serial output. The audio chain is:
4+
5+
MemoryStream -> AudioOutputStream -> WAVDecoder -> CsvStream

0 commit comments

Comments
 (0)