Skip to content

Commit 1003f1a

Browse files
committed
Multiuser server / final bugs
1 parent 273e9b2 commit 1003f1a

File tree

8 files changed

+97
-63
lines changed

8 files changed

+97
-63
lines changed

examples/examples-webserver/player-sd-webserverex_mp3/player-sd-webserverex_mp3.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ AudioPlayer player(source, out, *new CopyDecoder());
2929
void setup() {
3030
Serial.begin(115200);
3131
AudioLogger::instance().begin(Serial, AudioLogger::Warning);
32+
HttpLogger.setLevel(tinyhttp::Warning);
3233

3334
// setup SPI for SD card
3435
SPI.begin(PIN_AUDIO_KIT_SD_CARD_CLK, PIN_AUDIO_KIT_SD_CARD_MISO, PIN_AUDIO_KIT_SD_CARD_MOSI, PIN_AUDIO_KIT_SD_CARD_CS);

examples/examples-webserver/streams-generator-webserverex_wav/streams-generator-webserverex_wav.ino

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
#include "AudioLibs/AudioServerEx.h"
1313

1414
// WIFI
15-
const char *ssid = "ssid";
16-
const char *password = "password";
15+
const char *ssid = "Phil Schatzmann";
16+
const char *password = "sabrina01";
1717
AudioWAVServerEx server;
1818
// Sound Generation
1919
const int sample_rate = 10000;
@@ -25,6 +25,7 @@ GeneratedSoundStream<int16_t> in(sineWave); // Stream generated from sine wa
2525
void setup() {
2626
Serial.begin(115200);
2727
AudioLogger::instance().begin(Serial,AudioLogger::Info);
28+
HttpLogger.setLevel(tinyhttp::Info);
2829

2930
// start server
3031
auto cfg = server.defaultConfig();
@@ -44,4 +45,4 @@ void setup() {
4445
// copy the data
4546
void loop() {
4647
server.copy();
47-
}
48+
}

src/AudioBasic/Debouncer.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#pragma once
2+
3+
namespace audio_tools {
4+
5+
/**
6+
* @brief Helper class to debounce user input from a push button
7+
*
8+
*/
9+
class Debouncer {
10+
11+
public:
12+
Debouncer(uint16_t timeoutMs = 5000, void* ref = nullptr) {
13+
setDebounceTimeout(timeoutMs);
14+
p_ref = ref;
15+
}
16+
17+
void setDebounceTimeout(uint16_t timeoutMs) {
18+
ms = timeoutMs;
19+
}
20+
21+
/// Prevents that the same method is executed multiple times within the indicated time limit
22+
bool debounce(void(*cb)(void* ref) = nullptr) {
23+
bool result = false;
24+
if (millis() > debounce_ms) {
25+
LOGI("accpted");
26+
if (cb != nullptr) cb(p_ref);
27+
// new time limit
28+
debounce_ms = millis() + ms;
29+
result = true;
30+
}
31+
else {
32+
LOGI("rejected");
33+
}
34+
return result;
35+
}
36+
37+
protected:
38+
unsigned long debounce_ms = 0; // Debounce sensitive touch
39+
uint16_t ms;
40+
void* p_ref = nullptr;
41+
42+
};
43+
44+
}

src/AudioBasic/StrExt.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ class StrExt : public Str {
9999
grow(newLen);
100100
}
101101

102+
// make sure that the max size is allocated
103+
void allocate(int len=-1) {
104+
int new_size = len<0?maxlen:len;
105+
grow(new_size);
106+
this->len = new_size;
107+
}
108+
102109
protected:
103110

104111
bool grow(int newMaxLen){

src/AudioCodecs/CodecWAV.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ struct WAVAudioInfo : AudioBaseInfo {
2222
bits_per_sample=from.bits_per_sample;
2323
}
2424

25-
int format;
26-
int byte_rate;
27-
int block_align;
28-
bool is_streamed;
29-
bool is_valid;
30-
uint32_t data_length;
31-
uint32_t file_size;
25+
int format=WAV_FORMAT_PCM;
26+
int byte_rate=0;
27+
int block_align=0;
28+
bool is_streamed=true;
29+
bool is_valid=false;
30+
uint32_t data_length=0;
31+
uint32_t file_size=0;
3232
};
3333

3434
const char* wav_mime = "audio/wav";

src/AudioLibs/AudioServerEx.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,16 @@ class AudioServerEx : public AudioPrint {
6262

6363
virtual bool begin() {
6464
end(); // we (re) start with a clean state
65+
6566
if (info.input==nullptr){
6667
p_stream = new ExtensionStream(info.path,tinyhttp::GET, info.mime );
6768
} else {
6869
p_stream = new ExtensionStream(info.path, info.mime, *info.input);
6970
}
7071
p_stream->setReplyHeader(*getReplyHeader());
71-
p_server = new HttpServer(wifi);
72+
p_server = new tinyhttp::HttpServer(wifi);
73+
74+
// handling of WAV
7275
p_server->addExtension(*p_stream);
7376
return p_server->begin(info.port, info.ssid, info.password);
7477
}
@@ -153,21 +156,25 @@ class AudioWAVServerEx : public AudioServerEx {
153156
protected:
154157
// wav files start with a 44 bytes header
155158
virtual tinyhttp::Str* getReplyHeader() {
156-
MemoryPrint mp{(uint8_t*)header.c_str(), header.length()};
159+
header.allocate(44);
160+
MemoryPrint mp{(uint8_t*)header.c_str(), 44};
157161
WAVEncoder enc;
158162
WAVAudioInfo wi;
163+
wi.format = WAV_FORMAT_PCM;
159164
wi.sample_rate = info.sample_rate;
160165
wi.bits_per_sample = info.bits_per_sample;
161166
wi.channels = info.channels;
162167
enc.setAudioInfo(wi);
163168
// fill header with data
164169
enc.writeHeader(&mp);
170+
// make sure that the length is 44
171+
assert(header.length() == 44);
165172

166173
return &header;
167174
}
168175

169-
// Allocate memory for 44 bytes header
170-
tinyhttp::StrExt header{44};
176+
// Dynamic memory
177+
tinyhttp::StrExt header;
171178

172179
};
173180

src/AudioTools/AudioOutput.h

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -671,30 +671,38 @@ class MemoryPrint : public AudioPrint {
671671
MemoryPrint(uint8_t*start, int len){
672672
p_start = start;
673673
p_next = start;
674-
size = len;
674+
max_size = len;
675+
if (p_next==nullptr){
676+
LOGE("start must not be null");
677+
}
675678
}
676679

677680
size_t write(const uint8_t *buffer, size_t len) override {
678-
if (pos+len<=size){
681+
if (p_next==nullptr) return 0;
682+
if (pos+len<=max_size){
679683
memcpy(p_next, buffer,len);
680684
pos+=len;
681685
p_next+=len;
682686
return len;
683687
} else {
684-
LOGE("Buffer too small");
688+
LOGE("Buffer too small: pos:%d, size: %lu ", pos, max_size);
685689
return 0;
686690
}
687691
}
688692

689693
int availableForWrite() override {
690-
return size-pos;
694+
return max_size-pos;
695+
}
696+
697+
int size() {
698+
return max_size;
691699
}
692700

693701
protected:
694-
int pos;
695-
uint8_t* p_start;
696-
uint8_t *p_next;
697-
size_t size;
702+
int pos=0;
703+
uint8_t* p_start=nullptr;
704+
uint8_t *p_next=nullptr;
705+
size_t max_size;
698706

699707
};
700708

src/AudioTools/AudioPlayer.h

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#pragma once
22

33
#include "AudioConfig.h"
4+
#include "AudioBasic/Str.h"
5+
#include "AudioBasic/Debouncer.h"
46
#include "AudioTools/AudioTypes.h"
57
#include "AudioTools/Buffers.h"
68
#include "AudioTools/Converter.h"
79
#include "AudioTools/AudioLogger.h"
810
#include "AudioTools/AudioStreams.h"
911
#include "AudioTools/AudioCopy.h"
1012
#include "AudioHttp/AudioHttp.h"
11-
#include "AudioBasic/Str.h"
1213
#include "AudioTools/AudioSource.h"
1314
// support for legacy USE_SDFAT
1415
#ifdef USE_SDFAT
@@ -18,46 +19,6 @@
1819

1920
namespace audio_tools {
2021

21-
22-
/**
23-
* @brief Helper class to debounce user input from a push button
24-
*
25-
*/
26-
class Debouncer {
27-
28-
public:
29-
Debouncer(uint16_t timeoutMs = 5000, void* ref = nullptr) {
30-
setDebounceTimeout(timeoutMs);
31-
p_ref = ref;
32-
}
33-
34-
void setDebounceTimeout(uint16_t timeoutMs) {
35-
ms = timeoutMs;
36-
}
37-
38-
/// Prevents that the same method is executed multiple times within the indicated time limit
39-
bool debounce(void(*cb)(void* ref) = nullptr) {
40-
bool result = false;
41-
if (millis() > debounce_ms) {
42-
LOGI("accpted");
43-
if (cb != nullptr) cb(p_ref);
44-
// new time limit
45-
debounce_ms = millis() + ms;
46-
result = true;
47-
}
48-
else {
49-
LOGI("rejected");
50-
}
51-
return result;
52-
}
53-
54-
protected:
55-
unsigned long debounce_ms = 0; // Debounce sensitive touch
56-
uint16_t ms;
57-
void* p_ref = nullptr;
58-
59-
};
60-
6122
/**
6223
* @brief Implements a simple audio player which supports the following commands:
6324
* - begin
@@ -331,6 +292,11 @@ namespace audio_tools {
331292
virtual void copy() {
332293
if (active) {
333294
LOGD(LOG_METHOD);
295+
if (p_final_print!=nullptr && p_final_print->availableForWrite()==0){
296+
// not ready to do anything - so we wait a bit
297+
delay(100);
298+
return;
299+
}
334300
// handle sound
335301
if (copier.copy() || timeout == 0) {
336302
// reset timeout

0 commit comments

Comments
 (0)