Skip to content

Commit

Permalink
WAV encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
pschatzmann committed May 13, 2021
1 parent b450bcf commit 263f6b2
Show file tree
Hide file tree
Showing 19 changed files with 351,733 additions and 115 deletions.
18 changes: 18 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"files.associations": {
"ostream": "cpp",
"cullsettings": "cpp",
"types": "cpp",
"*.tcc": "cpp",
"functional": "cpp",
"string_view": "cpp",
"quat": "cpp",
"vec2d": "cpp",
"vec2f": "cpp",
"vec3d": "cpp",
"vec3f": "cpp",
"vec4d": "cpp",
"vec4f": "cpp",
"tuple": "cpp"
}
}
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ Here are a couple of simple test sketches to demo different output destinations:
- [streams-generator-dac](/examples/streams-generator-dac) Output of generated sound on ESP32 internal DAC via I2S
- [streams-generator-a2dp](/examples/streams-generator-a2dp) Output of generated sound on Bluetooth Speaker using A2DP
- [streams-adc-serial](/examples/streams-adc-serial) Displaying input from analog microphone on the Serial Plotter
- [streams-memory_wav-serial](/examples/streams-memory_wav-serial) Decoding of WAV from Flash memory and display on the Serial Plotter
- [streams-generator-webserver_wav](/examples/streams-generator-webserver_wav) Encoding of WAV from generated sound returned via Webserver

And some more useful examples:

Expand Down Expand Up @@ -149,7 +151,7 @@ This is currently work in progress:
| I2S | tested |
| Files (RAW, MP3...) | tested |
| Streams | tested |
| WAV encoding/deconding | open |
| WAV encoding/deconding | tested |
| AAC encoding/deconding | open |
| int24_t | tested |

4 changes: 4 additions & 0 deletions examples/streams-generator-webserver_wav/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Webserver

With the help of the ESP32 WIFI functionality we can implement a simple web server.
In the example we use a Sine Wave generator as sound source and return the result as an WAV file
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#include <WiFi.h>
#include "AudioTools.h"

using namespace audio_tools;

const char *ssid = "ssid";
const char *password = "password";
WiFiServer server(80);
WiFiClient client;

const int sample_rate = 10000;
const int data_length = 100000;
const int channels = 1;

SineWaveGenerator<int16_t> sineWave; // subclass of SoundGenerator with max amplitude of 32000
GeneratedSoundStream<int16_t> in(sineWave, channels); // Stream generated from sine wave
StreamCopy copier; // buffered copy
WAVEncoder encoder;
AudioOutputStream wav_stream(encoder); // WAV output stream

void setup()
{
Serial.begin(115200);
AudioLogger::instance().begin(Serial,AudioLogger::Debug);

WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());

server.begin();
sineWave.begin(sample_rate, B4);

in.begin();
}


void processClient() {
if (client) { // if you get a client,
Serial.println("New Client."); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character

// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0){
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:audio/wav");
client.println();

// set up wav encoder
auto config = encoder.defaultConfig();
config.channels = channels;
config.sample_rate = sample_rate;
//config.data_length = data_length;
config.is_streamed = true;
encoder.begin(client, config);

Serial.println("Returning WAV stream...");
copier.begin(wav_stream, in);
// break out of the while loop:
break;
} else { // if you got a newline, then clear currentLine:
currentLine = "";
}
}
else if (c != '\r')
{ // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
}
}

void loop() {
if (!client.connected()) {
client = server.available(); // listen for incoming clients
processClient();
} else {
// We are connected: copy input from source to wav output
if (encoder){
copier.copy();
if (!encoder) {
Serial.println("stop client...");
client.stop();
}
}
}
}
5 changes: 5 additions & 0 deletions examples/streams-memory_wav-serial/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#Decoding a WAV file

In this example we decode a WAV file into RAW output and send it to the Serial output. The audio chain is:

MemoryStream -> AudioOutputStream -> WAVDecoder -> CsvStream
Loading

0 comments on commit 263f6b2

Please sign in to comment.