1
- #include < WiFi.h>
2
1
#include " AudioTools.h"
2
+ #include " AudioWAVServer.h"
3
3
4
4
using namespace audio_tools ;
5
5
6
6
// WIFI
7
7
const char *ssid = " ssid" ;
8
8
const char *password = " password" ;
9
- WiFiServer server ( 80 );
10
- WiFiClient client ;
9
+
10
+ AudioWAVServer server (ssid, password) ;
11
11
12
12
// Sound Generation
13
13
const int sample_rate = 10000 ;
14
- const int data_length = 100000 ;
15
14
const int channels = 1 ;
16
15
17
16
SineWaveGenerator<int16_t > sineWave; // subclass of SoundGenerator with max amplitude of 32000
18
17
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
22
18
23
19
24
20
void setup () {
25
21
Serial.begin (115200 );
26
22
AudioLogger::instance ().begin (Serial,AudioLogger::Debug);
27
23
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
-
39
24
// start server
40
- server.begin ();
25
+ server.begin (in, sample_rate, channels );
41
26
42
27
// start generation of sound
43
28
sineWave.begin (sample_rate, B4);
44
29
in.begin ();
45
30
}
46
31
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
- }
91
32
92
33
// copy the data
93
34
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 ();
108
36
}
0 commit comments