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
42class AudioServerEx : public AudioOutput {
43 public:
44 // Default Constructor
45 AudioServerEx() = default;
46
48 AudioServerEx(const char *ssid, const char* pwd){
49 info.ssid = ssid;
51 }
52
57
59 info = cfg;
60 return begin();
61 }
62
63 virtual bool begin(Stream &in, const char* contentType) {
64 info.input = ∈
65 info.mime = contentType;
66 return begin();
67 }
68
69 virtual bool begin() {
70 end(); // we (re) start with a clean state
71
72 if (info.input==nullptr){
73 p_stream = new ExtensionStream(info.path,tinyhttp::T_GET, info.mime );
74 } else {
76 }
77 p_stream->setReplyHeader(*getReplyHeader());
78 p_server = new tinyhttp::HttpServer(wifi);
79
80 // handling of WAV
81 p_server->addExtension(*p_stream);
82 return p_server->begin(info.port, info.ssid, info.password);
83 }
84
85 virtual void end() {
86 if (p_stream!=nullptr) {
87 delete p_stream;
88 p_stream = nullptr;
89 }
90 if (p_server!=nullptr) {
91 delete p_server;
92 p_server = nullptr;
93 }
94 }
95
97 size_t write(const uint8_t* data, size_t len) override {
98 if (p_stream==nullptr) return 0;
99 return p_stream->write((uint8_t*)data, len);
100 }
101
102 int availableForWrite() override {
103 if (p_stream==nullptr) return 0;
104 return p_stream->availableForWrite();
105 }
106
108 virtual void copy() {
109 if (p_server!=nullptr){
110 p_server->copy();
111 }
112 }
113
114 protected:
119
120 virtual tinyhttp::StrView* getReplyHeader() {
121 return nullptr;
122 }
123
124};
125
136 public:
137 // Default Constructor
138 AudioWAVServerEx() = default;
139
141 AudioWAVServerEx(const char *ssid, const char* pwd):AudioServerEx(ssid, pwd){}
142
145 cfg.mime = "audio/wav";
146 return cfg;
147 }
148
150 bool begin(Stream &in, int sample_rate, int channels, int bits_per_sample=16) {
151 info.input = ∈
152 info.sample_rate = sample_rate;
153 info.channels = channels;
154 info. bits_per_sample = bits_per_sample;
155 info.mime = "audio/wav";
156 return AudioServerEx::begin();
157 }
158
161 }
162
163 protected:
164 // Dynamic memory
165 tinyhttp::Str header;
166
167 // wav files start with a 44 bytes header
168 virtual tinyhttp::StrView* getReplyHeader() {
169 header.allocate(44);
170 MemoryOutput mp{(uint8_t*)header.c_str(), 44};
171 WAVHeader enc;
174 wi.sample_rate = info.sample_rate;
175 wi.bits_per_sample = info.bits_per_sample;
176 wi.channels = info.channels;
177 enc.setAudioInfo(wi);
178 // fill header with data
179 enc.writeHeader(&mp);
180 // make sure that the length is 44
181 assert(header.length() == 44);
182
183 return &header;
184 }
185};
186
187}
#define assert(T)
Definition avr.h:10
Definition Arduino.h:136
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:42
virtual bool begin(AudioServerExConfig cfg)
Definition AudioServerEx.h:58
virtual bool begin(Stream &in, const char *contentType)
Definition AudioServerEx.h:63
virtual bool begin()
Definition AudioServerEx.h:69
virtual void copy()
Needs to be called if the data was provided as input Stream in the AudioServerExConfig.
Definition AudioServerEx.h:108
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:97
int availableForWrite() override
Definition AudioServerEx.h:102
HttpServer * p_server
Definition AudioServerEx.h:117
ExtensionStream * p_stream
Definition AudioServerEx.h:118
virtual AudioServerExConfig defaultConfig()
Definition AudioServerEx.h:53
AudioServerExConfig info
Definition AudioServerEx.h:115
virtual tinyhttp::StrView * getReplyHeader()
Definition AudioServerEx.h:120
virtual void end()
Definition AudioServerEx.h:85
WiFiServer wifi
Definition AudioServerEx.h:116
AudioServerEx(const char *ssid, const char *pwd)
To be compatible with legacy API.
Definition AudioServerEx.h:48
A powerfull WAV Web server which is based on https://github.com/pschatzmann/TinyHttp....
Definition AudioServerEx.h:135
bool begin(Stream &in, int sample_rate, int channels, int bits_per_sample=16)
Legacy API support.
Definition AudioServerEx.h:150
AudioWAVServerEx(const char *ssid, const char *pwd)
To be compatible with legacy API.
Definition AudioServerEx.h:141
tinyhttp::Str header
Definition AudioServerEx.h:165
bool begin(AudioServerExConfig cfg) override
Definition AudioServerEx.h:159
AudioServerExConfig defaultConfig() override
Definition AudioServerEx.h:143
virtual tinyhttp::StrView * getReplyHeader()
Definition AudioServerEx.h:168
Writes to a preallocated memory.
Definition AudioOutput.h:683
Parser for Wav header data for details see https://de.wikipedia.org/wiki/RIFF_WAVE.
Definition CodecWAV.h:67
void setAudioInfo(WAVAudioInfo info)
Sets the info in the header.
Definition CodecWAV.h:128
bool writeHeader(Print *out)
Just write a wav header to the indicated outputbu.
Definition CodecWAV.h:131
WiFiServer (Zephyr zsock version) This class provides a TCP server interface using Zephyr's socket AP...
Definition WiFiServerZephyr.h:17
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:508
Basic Audio information which drives e.g. I2S.
Definition AudioTypes.h:51
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:53
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:55
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:57
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