arduino-audio-tools
Loading...
Searching...
No Matches
AudioServerEx.h
Go to the documentation of this file.
1#pragma once
2
3#include "AudioToolsConfig.h"
7#include "HttpServer.h"
8#include "HttpExtensions.h"
9
10namespace audio_tools {
11
19 const char* mime = nullptr;
20 const char* ssid = nullptr;
21 const char* password = nullptr;
22 const char* path = "/";
23 // optional input; if not used use write methods to push data
24 Stream *input=nullptr;
25 int port = 80;
26};
27
38class AudioServerEx : public AudioOutput {
39 public:
40 // Default Constructor
41 AudioServerEx() = default;
42
44 AudioServerEx(const char *ssid, const char* pwd){
45 info.ssid = ssid;
47 }
48
53
55 info = cfg;
56 return begin();
57 }
58
59 virtual bool begin(Stream &in, const char* contentType) {
60 info.input = ∈
61 info.mime = contentType;
62 return begin();
63 }
64
65 virtual bool begin() {
66 end(); // we (re) start with a clean state
67
68 if (info.input==nullptr){
69 p_stream = new ExtensionStream(info.path,tinyhttp::T_GET, info.mime );
70 } else {
72 }
73 p_stream->setReplyHeader(*getReplyHeader());
74 p_server = new tinyhttp::HttpServer(wifi);
75
76 // handling of WAV
77 p_server->addExtension(*p_stream);
78 return p_server->begin(info.port, info.ssid, info.password);
79 }
80
81 virtual void end() {
82 if (p_stream!=nullptr) {
83 delete p_stream;
84 p_stream = nullptr;
85 }
86 if (p_server!=nullptr) {
87 delete p_server;
88 p_server = nullptr;
89 }
90 }
91
93 size_t write(const uint8_t* data, size_t len) override {
94 if (p_stream==nullptr) return 0;
95 return p_stream->write((uint8_t*)data, len);
96 }
97
98 int availableForWrite() override {
99 if (p_stream==nullptr) return 0;
100 return p_stream->availableForWrite();
101 }
102
104 virtual void copy() {
105 if (p_server!=nullptr){
106 p_server->copy();
107 }
108 }
109
110 protected:
115
116 virtual tinyhttp::StrView* getReplyHeader() {
117 return nullptr;
118 }
119
120};
121
132 public:
133 // Default Constructor
134 AudioWAVServerEx() = default;
135
137 AudioWAVServerEx(const char *ssid, const char* pwd):AudioServerEx(ssid, pwd){}
138
141 cfg.mime = "audio/wav";
142 return cfg;
143 }
144
146 bool begin(Stream &in, int sample_rate, int channels, int bits_per_sample=16) {
147 info.input = ∈
148 info.sample_rate = sample_rate;
149 info.channels = channels;
150 info. bits_per_sample = bits_per_sample;
151 info.mime = "audio/wav";
152 return AudioServerEx::begin();
153 }
154
157 }
158
159 protected:
160 // Dynamic memory
161 tinyhttp::Str header;
162
163 // wav files start with a 44 bytes header
164 virtual tinyhttp::StrView* getReplyHeader() {
165 header.allocate(44);
166 MemoryOutput mp{(uint8_t*)header.c_str(), 44};
167 WAVHeader enc;
170 wi.sample_rate = info.sample_rate;
171 wi.bits_per_sample = info.bits_per_sample;
172 wi.channels = info.channels;
173 enc.setAudioInfo(wi);
174 // fill header with data
175 enc.writeHeader(&mp);
176 // make sure that the length is 44
177 assert(header.length() == 44);
178
179 return &header;
180 }
181};
182
183}
#define assert(T)
Definition avr.h:10
Abstract Audio Ouptut class.
Definition AudioOutput.h:25
AudioInfo cfg
Definition AudioOutput.h:88
A powerfull Web server which is based on https://github.com/pschatzmann/TinyHttp. It supports multipl...
Definition AudioServerEx.h:38
virtual bool begin(AudioServerExConfig cfg)
Definition AudioServerEx.h:54
virtual bool begin(Stream &in, const char *contentType)
Definition AudioServerEx.h:59
virtual bool begin()
Definition AudioServerEx.h:65
virtual void copy()
Needs to be called if the data was provided as input Stream in the AudioServerExConfig.
Definition AudioServerEx.h:104
size_t write(const uint8_t *data, size_t len) override
Web server supports write so that we can e.g. use is as destination for the audio player.
Definition AudioServerEx.h:93
int availableForWrite() override
Definition AudioServerEx.h:98
HttpServer * p_server
Definition AudioServerEx.h:113
ExtensionStream * p_stream
Definition AudioServerEx.h:114
virtual AudioServerExConfig defaultConfig()
Definition AudioServerEx.h:49
AudioServerExConfig info
Definition AudioServerEx.h:111
virtual tinyhttp::StrView * getReplyHeader()
Definition AudioServerEx.h:116
virtual void end()
Definition AudioServerEx.h:81
WiFiServer wifi
Definition AudioServerEx.h:112
AudioServerEx(const char *ssid, const char *pwd)
To be compatible with legacy API.
Definition AudioServerEx.h:44
A powerfull WAV Web server which is based on https://github.com/pschatzmann/TinyHttp....
Definition AudioServerEx.h:131
bool begin(Stream &in, int sample_rate, int channels, int bits_per_sample=16)
Legacy API support.
Definition AudioServerEx.h:146
AudioWAVServerEx(const char *ssid, const char *pwd)
To be compatible with legacy API.
Definition AudioServerEx.h:137
tinyhttp::Str header
Definition AudioServerEx.h:161
bool begin(AudioServerExConfig cfg) override
Definition AudioServerEx.h:155
AudioServerExConfig defaultConfig() override
Definition AudioServerEx.h:139
virtual tinyhttp::StrView * getReplyHeader()
Definition AudioServerEx.h:164
Writes to a preallocated memory.
Definition AudioOutput.h:674
Definition NoArduino.h:142
Parser for Wav header data for details see https://de.wikipedia.org/wiki/RIFF_WAVE.
Definition CodecWAV.h:46
void setAudioInfo(WAVAudioInfo info)
Sets the info in the header.
Definition CodecWAV.h:106
int writeHeader(Print *out)
Just write a wav header to the indicated outputbu.
Definition CodecWAV.h:109
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:512
Basic Audio information which drives e.g. I2S.
Definition AudioTypes.h:55
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:57
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:59
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:61
Config information for AudioServerEx.
Definition AudioServerEx.h:18
const char * path
Definition AudioServerEx.h:22
const char * ssid
Definition AudioServerEx.h:20
int port
Definition AudioServerEx.h:25
const char * password
Definition AudioServerEx.h:21
Stream * input
Definition AudioServerEx.h:24
const char * mime
Definition AudioServerEx.h:19
Sound information which is available in the WAV header.
Definition CodecWAV.h:19
AudioFormat format
Definition CodecWAV.h:27