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
+ }
0 commit comments