arduino-audio-tools
Loading...
Searching...
No Matches
AudioEncodedServerT.h
Go to the documentation of this file.
1#pragma once
2
3#include "AudioServerT.h"
4
5namespace audio_tools {
6
22template <class Client, class Server>
23class AudioEncoderServerT : public AudioServerT<Client, Server> {
24 public:
29 AudioEncoderServerT(AudioEncoder *encoder, int port = 80) : AudioServerT<Client, Server>(port) {
30 this->encoder = encoder;
31 }
32
40 const char *password, int port = 80)
41 : AudioServerT<Client, Server>(network, password, port) {
42 this->encoder = encoder;
43 }
44
48 virtual ~AudioEncoderServerT() = default;
49
58 bool begin(Stream &in, int sample_rate, int channels,
59 int bits_per_sample = 16, BaseConverter *converter = nullptr) {
60 TRACED();
61 this->in = &in;
63 audio_info.sample_rate = sample_rate;
64 audio_info.channels = channels;
65 audio_info.bits_per_sample = bits_per_sample;
67 // encoded_stream.begin(&client_obj, encoder);
72 }
73
82 bool begin(Stream &in, AudioInfo info, BaseConverter *converter = nullptr) {
83 TRACED();
84 this->in = &in;
85 this->audio_info = info;
91 LOGE("encoder begin failed");
92 // stop();
93 }
94
96 }
97
117
125 bool begin(AudioServerDataCallback cb, int sample_rate, int channels,
126 int bits_per_sample = 16) {
127 TRACED();
128 audio_info.sample_rate = sample_rate;
129 audio_info.channels = channels;
130 audio_info.bits_per_sample = bits_per_sample;
133
135 }
136
151
152 // provides a pointer to the encoder
154
155 protected:
156 // Sound Generation - use EncodedAudioOutput with is more efficient then
157 // EncodedAudioStream
161
162 // moved to be part of reply content to avoid timeout issues in Chrome
163 void sendReplyHeader() override {}
164
165 void sendReplyContent() override {
166 TRACED();
167 // restart encoder
168 if (encoder) {
169 encoder->end();
170 encoder->begin();
171 }
172
173 // if chunked transfer is active, the encoder must write into the
174 // chunked framing wrapper instead of directly into the client
175 Print *p_out = this->out_ptr();
176 if (this->isChunked()) {
177 this->chunked_print.begin(*this->out_ptr());
178 p_out = &this->chunked_print;
179 }
180
181 if (this->callback != nullptr) {
182 // encoded_stream.begin(out_ptr(), encoder);
183 encoded_stream.setOutput(p_out);
186
187 // provide data via Callback to encoded_stream
188 LOGI("sendReply - calling callback");
189 // Send delayed header
191 this->callback(&encoded_stream);
192 this->endChunked();
193 this->client_obj.stop();
194 } else if (this->in != nullptr) {
195 // provide data for stream: in -copy> encoded_stream -> out
196 LOGI("sendReply - Returning encoded stream...");
197 // encoded_stream.begin(out_ptr(), encoder);
198 encoded_stream.setOutput(p_out);
201
202 this->copier.begin(encoded_stream, *this->in);
203 if (!this->client_obj.connected()) {
204 LOGE("connection was closed");
205 }
206 // Send delayed header
208 }
209 }
210};
211
220template <class Client, class Server>
221class AudioWAVServerT : public AudioEncoderServerT<Client, Server> {
222 public:
227 AudioWAVServerT(int port = 80) : AudioEncoderServerT<Client, Server>(new WAVEncoder(), port) {}
228
235 AudioWAVServerT(const char *network, const char *password, int port = 80)
236 : AudioEncoderServerT<Client, Server>(new WAVEncoder(), network, password, port) {}
237
245
246 // provides a pointer to the encoder
248};
249
250
251
252} // namespace audio_tools
253
#define TRACED()
Definition AudioLoggerIDF.h:31
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define LOGE(...)
Definition AudioLoggerIDF.h:30
Definition Arduino.h:162
bool connected()
Definition Arduino.h:167
void stop()
Definition Arduino.h:164
Definition Arduino.h:56
Definition Arduino.h:136
Encoding of PCM data.
Definition AudioCodecsBase.h:97
void setAudioInfo(AudioInfo from) override
Defines the sample rate, number of channels and bits per sample.
Definition AudioCodecsBase.h:106
virtual const char * mime()=0
Provides the mime type of the encoded result.
A simple Arduino Webserver which encodes the provided PCM audio (from a Stream or a callback) with th...
Definition AudioEncodedServerT.h:23
AudioEncoderServerT(AudioEncoder *encoder, int port=80)
Construct a new Audio Server object that supports an AudioEncoder We assume that the WiFi is already ...
Definition AudioEncodedServerT.h:29
AudioEncoderServerT(AudioEncoder *encoder, const char *network, const char *password, int port=80)
Construct a new Audio Server object.
Definition AudioEncodedServerT.h:39
void sendReplyHeader() override
Definition AudioEncodedServerT.h:163
bool begin(Stream &in, AudioInfo info, BaseConverter *converter=nullptr)
Start the server. You need to be connected to WiFI before calling this method.
Definition AudioEncodedServerT.h:82
AudioEncoder * audioEncoder()
Definition AudioEncodedServerT.h:153
AudioInfo audio_info
Definition AudioEncodedServerT.h:159
bool begin(AudioServerDataCallback cb, int sample_rate, int channels, int bits_per_sample=16)
Start the server. The data must be provided by a callback method.
Definition AudioEncodedServerT.h:125
AudioEncoder * encoder
Definition AudioEncodedServerT.h:160
bool begin(Stream &in, int sample_rate, int channels, int bits_per_sample=16, BaseConverter *converter=nullptr)
Start the server. You need to be connected to WiFI before calling this method.
Definition AudioEncodedServerT.h:58
void sendReplyContent() override
Definition AudioEncodedServerT.h:165
bool begin(AudioStream &in, BaseConverter *converter=nullptr)
Start the server. You need to be connected to WiFI before calling this method.
Definition AudioEncodedServerT.h:105
virtual ~AudioEncoderServerT()=default
Destructor release the memory.
EncodedAudioOutput encoded_stream
Definition AudioEncodedServerT.h:158
bool begin(AudioServerDataCallback cb, AudioInfo info)
Start the server. The data must be provided by a callback method.
Definition AudioEncodedServerT.h:143
A minimal, single-client HTTP server that streams audio (or any other data) to a browser or media pla...
Definition AudioServerT.h:80
std::string network
Definition AudioServerT.h:272
bool isChunked()
Determines if chunked transfer encoding is active.
Definition AudioServerT.h:233
Stream * in
Definition AudioServerT.h:281
void endChunked()
Sends the terminating 0-length chunk when chunked transfer is active.
Definition AudioServerT.h:393
void setConverter(BaseConverter *c)
defines a converter that will be used when the audio is rendered
Definition AudioServerT.h:210
std::string password
Definition AudioServerT.h:271
bool begin()
Just starts the server.
Definition AudioServerT.h:141
Client * out_ptr()
Provides a pointer to the WiFiClient.
Definition AudioServerT.h:216
virtual void sendReplyHeader()
Definition AudioServerT.h:332
StreamCopy copier
Definition AudioServerT.h:283
Client client_obj
Definition AudioServerT.h:269
AudioServerDataCallback callback
Definition AudioServerT.h:280
ChunkedPrint chunked_print
Definition AudioServerT.h:288
Base class for all Audio Streams. It support the boolean operator to test if the object is ready with...
Definition BaseStream.h:120
virtual AudioInfo audioInfo() override
provides the actual input AudioInfo
Definition BaseStream.h:151
A simple Arduino Webserver which streams the audio as WAV data. This class is based on the AudioEncod...
Definition AudioEncodedServerT.h:221
AudioWAVServerT(const char *network, const char *password, int port=80)
Construct a new Audio WAV Server object.
Definition AudioEncodedServerT.h:235
WAVEncoder & wavEncoder()
Definition AudioEncodedServerT.h:247
~AudioWAVServerT()
Destructor: release the allocated encoder.
Definition AudioEncodedServerT.h:239
AudioWAVServerT(int port=80)
Construct a new Audio WAV Server object We assume that the WiFi is already connected.
Definition AudioEncodedServerT.h:227
virtual void end()=0
virtual bool begin()=0
Abstract Base class for Converters A converter is processing the data in the indicated array.
Definition BaseConverter.h:25
void begin(Print &out)
(re-)starts the chunked encoding for the indicated target
Definition AudioServerT.h:30
A more natural Print class to process encoded data (aac, wav, mp3...). Just define the output and the...
Definition AudioEncoded.h:21
void setEncoder(AudioEncoder *encoder)
Definition AudioEncoded.h:131
bool begin() override
Starts the processing - sets the status to active.
Definition AudioEncoded.h:161
virtual void setAudioInfo(AudioInfo newInfo) override
Defines the input AudioInfo.
Definition AudioEncoded.h:87
void setOutput(Print *outputStream)
Defines the output.
Definition AudioEncoded.h:107
void begin()
(Re)starts the processing
Definition StreamCopy.h:56
A simple WAV file encoder. If no AudioEncoderExt is specified the WAV file contains PCM data,...
Definition CodecWAV.h:653
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6
void(* AudioServerDataCallback)(Print *out)
Callback which writes the sound data to the stream.
Definition AudioServerT.h:13
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