Skip to content

Commit edb4971

Browse files
committed
URLStream corrections
1 parent 7103467 commit edb4971

File tree

7 files changed

+63
-44
lines changed

7 files changed

+63
-44
lines changed

sandbox/streams-url_wav-i2s/streams-url_wav-i2s.ino

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @file test-memory_wav-serial.ino
2+
* @file streams-url_wav-serial.ino
33
* @author Phil Schatzmann
44
* @brief decode WAV stream from url and output it on I2S
55
* @version 0.1
@@ -14,7 +14,7 @@
1414
using namespace audio_tools;
1515

1616
// UrlStream -copy-> AudioOutputStream -> WAVDecoder -> I2S
17-
URLStream url;
17+
URLStream url("ssid","password");
1818
I2SStream i2s; // I2S stream
1919
WAVDecoder decoder(i2s); // decode wav to pcm and send it to I2S
2020
AudioOutputStream out(decoder); // output to decoder
@@ -25,20 +25,24 @@ void setup(){
2525
Serial.begin(115200);
2626
AudioLogger::instance().begin(Serial, AudioLogger::Debug);
2727

28-
// url.begin("http://pi.local:5002//api/tts?text=hallo my name is sam")
29-
// url.begin("http://192.168.1.37:12101/api/text-to-speech?play=false", UrlStream::POST, "text/plain","Hallo, my name is Alice");
30-
31-
I2SConfig config = out.defaultConfig(TX_MODE);
32-
config.sample_rate = 16000;
28+
// setup i2s
29+
I2SConfig config = i2s.defaultConfig(TX_MODE);
30+
config.sample_rate = 16000; // Mozzilla 22050
3331
config.bits_per_sample = 32;
3432
config.channels = 1;
35-
3633
i2s.begin(config);
3734

35+
// Mozilla tts
36+
// url.begin("http://192.168.1.37:5002/api/tts", POST, "text/plain","Hallo, my name is Alice");
37+
// rhasspy
38+
url.begin("http://192.168.1.37:12101/api/text-to-speech?play=false", POST, "text/plain","Hallo, my name is Alice");
39+
40+
41+
3842
}
3943

4044
void loop(){
41-
if (wav) {
45+
if (decoder) {
4246
copier.copy();
4347
} else {
4448
stop();

src/AudioHttp/HttpHeader.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class HttpHeader {
8585
}
8686

8787
HttpHeader& put(const char* key, const char* value){
88-
if (value!=nullptr){
88+
if (value!=nullptr && strlen(value)>0){
8989
HttpHeaderLine *hl = headerLine(key);
9090
if (hl==nullptr){
9191
LOGE("HttpHeader::put - did not add HttpHeaderLine for %s", key);
@@ -296,7 +296,7 @@ class HttpHeader {
296296
}
297297
if (create_new_lines){
298298
HttpHeaderLine *newLine = new HttpHeaderLine();
299-
LOGI("HttpHeader::headerLine - new line created for %s", key);
299+
LOGD("HttpHeader::headerLine - new line created for %s", key);
300300
newLine->active = true;
301301
newLine->key = key;
302302
lines.push_back(newLine);

src/AudioHttp/HttpRequest.h

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,25 @@ class HttpRequest {
4545
}
4646

4747
operator bool() {
48-
return (bool)*client_ptr;
48+
return client_ptr != nullptr ? (bool)*client_ptr : false;
4949
}
5050

5151
virtual bool connected(){
52-
return client_ptr->connected();
52+
return client_ptr != nullptr ? (bool)*client_ptr && client_ptr->connected() : false;
5353
}
5454

5555
virtual int available() {
5656
if (reply_header.isChunked()){
5757
return chunk_reader.available();
5858
}
59-
return client_ptr->available();
59+
return client_ptr != nullptr ? client_ptr->available() : 0;
6060
}
6161

6262
virtual void stop(){
6363
LOGI("stop");
64-
client_ptr->stop();
64+
if (client_ptr!=nullptr){
65+
client_ptr->stop();
66+
}
6567
}
6668

6769
virtual int post(Url &url, const char* mime, const char *data, int len=-1){
@@ -158,14 +160,19 @@ class HttpRequest {
158160

159161
// sends request and reads the reply_header from the server
160162
virtual int process(MethodID action, Url &url, const char* mime, const char *data, int len=-1){
161-
if (!connected()){
163+
if (client_ptr==nullptr){
164+
LOGE("The client has not been defined");
165+
return -1;
166+
}
167+
if (!this->connected()){
162168
char msg[1024];
163-
164169
LOGI("process connecting to host %s port %d", url.host(), url.port());
165-
bool connected = connect(url.host(), url.port());
166-
if (!connected){
170+
bool is_connected = connect(url.host(), url.port());
171+
if (!is_connected){
167172
LOGE("Connect failed");
168173
}
174+
} else {
175+
LOGI("process is already connected");
169176
}
170177

171178
host_name = url.host();
@@ -184,12 +191,13 @@ class HttpRequest {
184191
request_header.write(*client_ptr);
185192

186193
if (len>0){
187-
LOGI("process - writing data: %d bytes", len);
194+
LOGI("Writing data: %d bytes", len);
188195
client_ptr->write((const uint8_t*)data,len);
196+
LOGD(data);
189197
}
190-
client_ptr->flush();
198+
191199
LOGI("Request written ... waiting for reply")
192-
200+
client_ptr->flush();
193201
reply_header.read(*client_ptr);
194202

195203
// if we use chunked tranfer we need to read the first chunked length

src/AudioHttp/URLStreamArduino.h

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,9 @@ class URLStream : public Stream {
2626
}
2727

2828
URLStream(const char* network, const char *password, int readBufferSize=DEFAULT_BUFFER_SIZE) {
29-
read_buffer = new uint8_t[readBufferSize];
30-
LOGD("connectWiFi");
31-
if (WiFi.status() != WL_CONNECTED && network!=nullptr && password != nullptr){
32-
WiFi.begin(network, password);
33-
while (WiFi.status() != WL_CONNECTED){
34-
Serial.print(".");
35-
delay(500);
36-
}
37-
Serial.println();
38-
}
39-
WiFiClient client;
40-
request.setClient(client);
41-
Serial.print("IP address: ");
42-
Serial.println(WiFi.localIP());
29+
read_buffer = new uint8_t[readBufferSize];
30+
this->network = network;
31+
this->password = password;
4332
}
4433

4534
~URLStream(){
@@ -48,6 +37,7 @@ class URLStream : public Stream {
4837
}
4938

5039
bool begin(const char* urlStr, MethodID action = GET, const char* reqMime="", const char*reqData="") {
40+
login();
5141
Url url(urlStr);
5242
int result = -1;
5343

@@ -101,6 +91,9 @@ class URLStream : public Stream {
10191
uint16_t read_buffer_size;
10292
uint16_t read_pos;
10393
uint16_t read_size;
94+
const char* network;
95+
const char* password;
96+
WiFiClient client;
10497

10598
inline void fillBuffer() {
10699
if (isEOS()){
@@ -114,6 +107,20 @@ class URLStream : public Stream {
114107
return read_pos>=read_size;
115108
}
116109

110+
void login(){
111+
LOGD("connectWiFi");
112+
if (WiFi.status() != WL_CONNECTED && network!=nullptr && password != nullptr){
113+
WiFi.begin(network, password);
114+
while (WiFi.status() != WL_CONNECTED){
115+
Serial.print(".");
116+
delay(500);
117+
}
118+
Serial.println();
119+
}
120+
request.setClient(client);
121+
delay(500);
122+
}
123+
117124
};
118125

119126
}

src/AudioTools/I2S_ESP32.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,12 @@ class I2SBase {
101101

102102
// update the cfg.i2s.channel_format based on the number of channels
103103
void setChannels(int channels){
104-
if (channels==2){
105-
i2s_config.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT;
106-
} else if (channels==1){
107-
i2s_config.channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT;
108-
}
109-
cfg.channels = channels;
110-
104+
if (channels==2){
105+
i2s_config.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT;
106+
} else if (channels==1){
107+
i2s_config.channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT;
108+
}
109+
cfg.channels = channels;
111110
}
112111

113112
/// writes the data to the I2S interface

src/AudioTools/TimerAlarmRepeating.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ class TimerAlarmRepeating {
148148

149149
// stops the timer and if necessary the task
150150
bool stop(){
151+
LOGW("stop");
151152
timerEnd(adc_timer);
152153
if (handler_task!=nullptr){
153154
vTaskDelete(handler_task);

src/CodecWAV.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ class WAVDecoder : public AudioWriter {
277277
LOGI("WAV sample_rate: %d", header.audioInfo().sample_rate);
278278
LOGI("WAV data_length: %d", header.audioInfo().data_length);
279279
LOGI("WAV is_streamed: %d", header.audioInfo().is_streamed);
280-
LOGI("WAVis_valid: %d", header.audioInfo().is_valid);
280+
LOGI("WAV is_valid: %s", header.audioInfo().is_valid ? "true" : "false");
281281

282282
// check format
283283
int format = header.audioInfo().format;

0 commit comments

Comments
 (0)