Skip to content

Commit 6c98e62

Browse files
committed
SAM
1 parent 1e06daf commit 6c98e62

File tree

9 files changed

+125
-386
lines changed

9 files changed

+125
-386
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ Here are a couple of simple test sketches to demo different output destinations:
9595

9696
And some more useful examples:
9797

98-
- [streams-memory_raw-i2s](examples/streams-memory_raw-i2s) - Play music form Flash Memory via I2S to External DAC
98+
- [streams-memory_raw-i2s](/examples/streams-memory_raw-i2s) - Play music form Flash Memory via I2S to External DAC
9999
- [streams-url_raw-serial](/examples/streams-url_raw-serial) Displaying a music file from the internet on the Serial Plotter
100100
- [streams-url_raw-I2S.ino](/examples/streams-url_raw-i2s) Streaming a File from the Internet to on external DAC via I2S
101101
- [streams-mozzi-a2dp](/examples/streams-mozzi-a2dp) Use Mozzi to generate Sound to be sent to a Bluetooth Speaker
@@ -112,6 +112,12 @@ And some more useful examples:
112112
- [base-i2s-serial](basic-api/base-i2s-serial) - Sample digital sound and write it to Serial
113113
- [base-i2s-a2dp](basic-api/base-i2s-a2dp) - Sample analog sound and write it to a A2DP Bluetooth source
114114

115+
#### Listening to the Result with a Webbrowser
116+
117+
I am providing a simple webserver which can render the audio data as wav result. Here are some examples
118+
119+
- [streams-generator-webserver](/examples/streams-generator-webserver) A Webserver which renders some generated sound
120+
- [streams-sam-webserver](/examples/streams-sam-webserver) A Webserver which renders the result from the SAM TTS engine
115121

116122

117123
## Optional Libraries
@@ -123,7 +129,7 @@ Dependent on the example you might need to install some of the following librari
123129
- [SD Library](https://www.arduino.cc/en/reference/SD) to read and write files.
124130
- [arduino-fdk-aac](https://github.com/pschatzmann/arduino-fdk-aac) to encode or decode AAC
125131
- [Mozzi](https://github.com/pschatzmann/Mozzi) A sound synthesis library for Arduino
126-
132+
- [SAM](https://github.com/pschatzmann/SAM) A Text to Speach Engine
127133

128134
## Installation
129135

Lines changed: 5 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,36 @@
1-
#include <WiFi.h>
21
#include "AudioTools.h"
2+
#include "AudioWAVServer.h"
33

44
using namespace audio_tools;
55

66
// WIFI
77
const char *ssid = "ssid";
88
const char *password = "password";
9-
WiFiServer server(80);
10-
WiFiClient client;
9+
10+
AudioWAVServer server(ssid, password);
1111

1212
// Sound Generation
1313
const int sample_rate = 10000;
14-
const int data_length = 100000;
1514
const int channels = 1;
1615

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

2319

2420
void setup() {
2521
Serial.begin(115200);
2622
AudioLogger::instance().begin(Serial,AudioLogger::Debug);
2723

28-
WiFi.begin(ssid, password);
29-
while (WiFi.status() != WL_CONNECTED) {
30-
delay(500);
31-
Serial.print(".");
32-
}
33-
34-
Serial.println("");
35-
Serial.println("WiFi connected.");
36-
Serial.println("IP address: ");
37-
Serial.println(WiFi.localIP());
38-
3924
// start server
40-
server.begin();
25+
server.begin(in, sample_rate, channels);
4126

4227
// start generation of sound
4328
sineWave.begin(sample_rate, B4);
4429
in.begin();
4530
}
4631

47-
// Handle an new client connection and return the data
48-
void processClient() {
49-
if (client) { // if you get a client,
50-
Serial.println("New Client."); // print a message out the serial port
51-
String currentLine = ""; // make a String to hold incoming data from the client
52-
while (client.connected()) { // loop while the client's connected
53-
if (client.available()) { // if there's bytes to read from the client,
54-
char c = client.read(); // read a byte, then
55-
Serial.write(c); // print it out the serial monitor
56-
if (c == '\n') { // if the byte is a newline character
57-
58-
// if the current line is blank, you got two newline characters in a row.
59-
// that's the end of the client HTTP request, so send a response:
60-
if (currentLine.length() == 0){
61-
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
62-
// and a content-type so the client knows what's coming, then a blank line:
63-
client.println("HTTP/1.1 200 OK");
64-
client.println("Content-type:audio/wav");
65-
client.println();
66-
67-
// set up wav encoder
68-
auto config = encoder.defaultConfig();
69-
config.channels = channels;
70-
config.sample_rate = sample_rate;
71-
//config.data_length = data_length;
72-
config.is_streamed = true;
73-
encoder.begin(client, config);
74-
75-
Serial.println("Returning WAV stream...");
76-
copier.begin(wav_stream, in);
77-
// break out of the while loop:
78-
break;
79-
} else { // if you got a newline, then clear currentLine:
80-
currentLine = "";
81-
}
82-
}
83-
else if (c != '\r')
84-
{ // if you got anything else but a carriage return character,
85-
currentLine += c; // add it to the end of the currentLine
86-
}
87-
}
88-
}
89-
}
90-
}
9132

9233
// copy the data
9334
void loop() {
94-
if (!client.connected()) {
95-
client = server.available(); // listen for incoming clients
96-
processClient();
97-
} else {
98-
// We are connected: copy input from source to wav output
99-
if (encoder){
100-
copier.copy();
101-
// if we limit the size of the WAV the encoder gets automatically closed when all has been sent
102-
if (!encoder) {
103-
Serial.println("stop client...");
104-
client.stop();
105-
}
106-
}
107-
}
35+
server.doLoop();
10836
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Using SAM Speach to Text
2+
3+
I am providing a simple sketch which generates sound data with the SAM text to speach engine.
4+
You need to install https://github.com/pschatzmann/SAM
5+
6+
In this demo we provide the result as WAV stream which can be listened to in a Web Browser
7+

sandbox/experiment-sam-webserver/experiment-sam-webserver.ino renamed to examples/streams-sam-webserver_wav/streams-sam-webserver_wav.ino

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,28 @@
66
* @copyright GPLv3
77
*
88
*/
9-
#include "AudioTools.h"
10-
#include "AudioWAVServer.h"
119
#include "sam_arduino.h"
1210

1311
using namespace audio_tools;
1412

15-
extern const char *alice;
16-
AudioWAVServer server("network", "password");
13+
AudioWAVServer server("ssid","password");
1714
int channels = 1;
18-
int audio_rate = 22050;
19-
15+
int bits_per_sample = 8;
2016

2117
// Callback which provides the audio data
2218
void outputData(Stream &out){
23-
Serial.println("providing audio data...");
24-
SAM sam(out, false, channels);
25-
sam.say(alice);
19+
Serial.print("providing data...");
20+
SAM sam(out, false);
21+
sam.setOutputChannels(channels);
22+
sam.setBitsPerSample(bits_per_sample);
23+
24+
sam.say("hallo, I am SAM");
2625
}
2726

2827
void setup(){
2928
Serial.begin(115200);
30-
3129
// start data sink
32-
server.begin(outputData, audio_rate, channels);
30+
server.begin(outputData, SAM::sampleRate(), channels, bits_per_sample);
3331
}
3432

3533

sandbox/experiment-sam-webserver/README.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)