URLStream corrections

This commit is contained in:
Phil Schatzmann 2021-06-25 17:17:16 +02:00
parent 7103467c6f
commit edb4971f35
7 changed files with 63 additions and 44 deletions

View File

@ -1,5 +1,5 @@
/**
* @file test-memory_wav-serial.ino
* @file streams-url_wav-serial.ino
* @author Phil Schatzmann
* @brief decode WAV stream from url and output it on I2S
* @version 0.1
@ -14,7 +14,7 @@
using namespace audio_tools;
// UrlStream -copy-> AudioOutputStream -> WAVDecoder -> I2S
URLStream url;
URLStream url("ssid","password");
I2SStream i2s; // I2S stream
WAVDecoder decoder(i2s); // decode wav to pcm and send it to I2S
AudioOutputStream out(decoder); // output to decoder
@ -25,20 +25,24 @@ void setup(){
Serial.begin(115200);
AudioLogger::instance().begin(Serial, AudioLogger::Debug);
// url.begin("http://pi.local:5002//api/tts?text=hallo my name is sam")
// url.begin("http://192.168.1.37:12101/api/text-to-speech?play=false", UrlStream::POST, "text/plain","Hallo, my name is Alice");
I2SConfig config = out.defaultConfig(TX_MODE);
config.sample_rate = 16000;
// setup i2s
I2SConfig config = i2s.defaultConfig(TX_MODE);
config.sample_rate = 16000; // Mozzilla 22050
config.bits_per_sample = 32;
config.channels = 1;
i2s.begin(config);
// Mozilla tts
// url.begin("http://192.168.1.37:5002/api/tts", POST, "text/plain","Hallo, my name is Alice");
// rhasspy
url.begin("http://192.168.1.37:12101/api/text-to-speech?play=false", POST, "text/plain","Hallo, my name is Alice");
}
void loop(){
if (wav) {
if (decoder) {
copier.copy();
} else {
stop();

View File

@ -85,7 +85,7 @@ class HttpHeader {
}
HttpHeader& put(const char* key, const char* value){
if (value!=nullptr){
if (value!=nullptr && strlen(value)>0){
HttpHeaderLine *hl = headerLine(key);
if (hl==nullptr){
LOGE("HttpHeader::put - did not add HttpHeaderLine for %s", key);
@ -296,7 +296,7 @@ class HttpHeader {
}
if (create_new_lines){
HttpHeaderLine *newLine = new HttpHeaderLine();
LOGI("HttpHeader::headerLine - new line created for %s", key);
LOGD("HttpHeader::headerLine - new line created for %s", key);
newLine->active = true;
newLine->key = key;
lines.push_back(newLine);

View File

@ -45,23 +45,25 @@ class HttpRequest {
}
operator bool() {
return (bool)*client_ptr;
return client_ptr != nullptr ? (bool)*client_ptr : false;
}
virtual bool connected(){
return client_ptr->connected();
return client_ptr != nullptr ? (bool)*client_ptr && client_ptr->connected() : false;
}
virtual int available() {
if (reply_header.isChunked()){
return chunk_reader.available();
}
return client_ptr->available();
return client_ptr != nullptr ? client_ptr->available() : 0;
}
virtual void stop(){
LOGI("stop");
client_ptr->stop();
if (client_ptr!=nullptr){
client_ptr->stop();
}
}
virtual int post(Url &url, const char* mime, const char *data, int len=-1){
@ -158,14 +160,19 @@ class HttpRequest {
// sends request and reads the reply_header from the server
virtual int process(MethodID action, Url &url, const char* mime, const char *data, int len=-1){
if (!connected()){
if (client_ptr==nullptr){
LOGE("The client has not been defined");
return -1;
}
if (!this->connected()){
char msg[1024];
LOGI("process connecting to host %s port %d", url.host(), url.port());
bool connected = connect(url.host(), url.port());
if (!connected){
bool is_connected = connect(url.host(), url.port());
if (!is_connected){
LOGE("Connect failed");
}
} else {
LOGI("process is already connected");
}
host_name = url.host();
@ -184,12 +191,13 @@ class HttpRequest {
request_header.write(*client_ptr);
if (len>0){
LOGI("process - writing data: %d bytes", len);
LOGI("Writing data: %d bytes", len);
client_ptr->write((const uint8_t*)data,len);
LOGD(data);
}
client_ptr->flush();
LOGI("Request written ... waiting for reply")
client_ptr->flush();
reply_header.read(*client_ptr);
// if we use chunked tranfer we need to read the first chunked length

View File

@ -26,20 +26,9 @@ class URLStream : public Stream {
}
URLStream(const char* network, const char *password, int readBufferSize=DEFAULT_BUFFER_SIZE) {
read_buffer = new uint8_t[readBufferSize];
LOGD("connectWiFi");
if (WiFi.status() != WL_CONNECTED && network!=nullptr && password != nullptr){
WiFi.begin(network, password);
while (WiFi.status() != WL_CONNECTED){
Serial.print(".");
delay(500);
}
Serial.println();
}
WiFiClient client;
request.setClient(client);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
read_buffer = new uint8_t[readBufferSize];
this->network = network;
this->password = password;
}
~URLStream(){
@ -48,6 +37,7 @@ class URLStream : public Stream {
}
bool begin(const char* urlStr, MethodID action = GET, const char* reqMime="", const char*reqData="") {
login();
Url url(urlStr);
int result = -1;
@ -101,6 +91,9 @@ class URLStream : public Stream {
uint16_t read_buffer_size;
uint16_t read_pos;
uint16_t read_size;
const char* network;
const char* password;
WiFiClient client;
inline void fillBuffer() {
if (isEOS()){
@ -114,6 +107,20 @@ class URLStream : public Stream {
return read_pos>=read_size;
}
void login(){
LOGD("connectWiFi");
if (WiFi.status() != WL_CONNECTED && network!=nullptr && password != nullptr){
WiFi.begin(network, password);
while (WiFi.status() != WL_CONNECTED){
Serial.print(".");
delay(500);
}
Serial.println();
}
request.setClient(client);
delay(500);
}
};
}

View File

@ -101,13 +101,12 @@ class I2SBase {
// update the cfg.i2s.channel_format based on the number of channels
void setChannels(int channels){
if (channels==2){
i2s_config.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT;
} else if (channels==1){
i2s_config.channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT;
}
cfg.channels = channels;
if (channels==2){
i2s_config.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT;
} else if (channels==1){
i2s_config.channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT;
}
cfg.channels = channels;
}
/// writes the data to the I2S interface

View File

@ -148,6 +148,7 @@ class TimerAlarmRepeating {
// stops the timer and if necessary the task
bool stop(){
LOGW("stop");
timerEnd(adc_timer);
if (handler_task!=nullptr){
vTaskDelete(handler_task);

View File

@ -277,7 +277,7 @@ class WAVDecoder : public AudioWriter {
LOGI("WAV sample_rate: %d", header.audioInfo().sample_rate);
LOGI("WAV data_length: %d", header.audioInfo().data_length);
LOGI("WAV is_streamed: %d", header.audioInfo().is_streamed);
LOGI("WAVis_valid: %d", header.audioInfo().is_valid);
LOGI("WAV is_valid: %s", header.audioInfo().is_valid ? "true" : "false");
// check format
int format = header.audioInfo().format;