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
16template <class Client, class Server>
17class AudioEncoderServerT : public AudioServerT<Client, Server> {
18 public:
24 this->encoder = encoder;
25 }
26
34 const char *password, int port = 80)
36 this->encoder = encoder;
37 }
38
42 virtual ~AudioEncoderServerT() = default;
43
52 bool begin(Stream &in, int sample_rate, int channels,
53 int bits_per_sample = 16, BaseConverter *converter = nullptr) {
54 TRACED();
55 this->in = &in;
57 audio_info.sample_rate = sample_rate;
58 audio_info.channels = channels;
59 audio_info.bits_per_sample = bits_per_sample;
61 // encoded_stream.begin(&client_obj, encoder);
66 }
67
76 bool begin(Stream &in, AudioInfo info, BaseConverter *converter = nullptr) {
77 TRACED();
78 this->in = &in;
79 this->audio_info = info;
85 LOGE("encoder begin failed");
86 // stop();
87 }
88
90 }
91
111
119 bool begin(AudioServerDataCallback cb, int sample_rate, int channels,
120 int bits_per_sample = 16) {
121 TRACED();
122 audio_info.sample_rate = sample_rate;
123 audio_info.channels = channels;
124 audio_info.bits_per_sample = bits_per_sample;
126
128 }
129
130 // provides a pointer to the encoder
132
133 protected:
134 // Sound Generation - use EncodedAudioOutput with is more efficient then
135 // EncodedAudioStream
139
140 // moved to be part of reply content to avoid timeout issues in Chrome
141 void sendReplyHeader() override {}
142
143 void sendReplyContent() override {
144 TRACED();
145 // restart encoder
146 if (encoder) {
147 encoder->end();
148 encoder->begin();
149 }
150
151 if (this->callback != nullptr) {
152 // encoded_stream.begin(out_ptr(), encoder);
153 encoded_stream.setOutput(this->out_ptr());
154 encoded_stream.setEncoder(encoder);
156
157 // provide data via Callback to encoded_stream
158 LOGI("sendReply - calling callback");
159 // Send delayed header
161 this->callback(&encoded_stream);
162 this->client_obj.stop();
163 } else if (this->in != nullptr) {
164 // provide data for stream: in -copy> encoded_stream -> out
165 LOGI("sendReply - Returning encoded stream...");
166 // encoded_stream.begin(out_ptr(), encoder);
167 encoded_stream.setOutput(this->out_ptr());
168 encoded_stream.setEncoder(encoder);
170
171 this->copier.begin(encoded_stream, *this->in);
172 if (!this->client_obj.connected()) {
173 LOGE("connection was closed");
174 }
175 // Send delayed header
177 }
178 }
179};
180
189template <class Client, class Server>
190class AudioWAVServerT : public AudioEncoderServerT<Client, Server> {
191 public:
197
204 AudioWAVServerT(const char *network, const char *password, int port = 80)
206
214
215 // provides a pointer to the encoder
217};
218
219
220
221} // namespace audio_tools
222
#define TRACED()
Definition AudioLoggerIDF.h:31
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define LOGE(...)
Definition AudioLoggerIDF.h:30
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 streams the audio using the indicated encoder.. This class is based ...
Definition AudioEncodedServerT.h:17
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:23
AudioEncoderServerT(AudioEncoder *encoder, const char *network, const char *password, int port=80)
Construct a new Audio Server object.
Definition AudioEncodedServerT.h:33
void sendReplyHeader() override
Definition AudioEncodedServerT.h:141
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:76
AudioEncoder * audioEncoder()
Definition AudioEncodedServerT.h:131
AudioInfo audio_info
Definition AudioEncodedServerT.h:137
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:119
AudioEncoder * encoder
Definition AudioEncodedServerT.h:138
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:52
void sendReplyContent() override
Definition AudioEncodedServerT.h:143
bool begin(AudioStream &in, BaseConverter *converter=nullptr)
Start the server. You need to be connected to WiFI before calling this method.
Definition AudioEncodedServerT.h:99
virtual ~AudioEncoderServerT()=default
Destructor release the memory.
EncodedAudioOutput encoded_stream
Definition AudioEncodedServerT.h:136
A simple Arduino Webserver which streams the result This class is based on the WiFiServer class....
Definition AudioServerT.h:37
Stream * in
Definition AudioServerT.h:191
void setConverter(BaseConverter *c)
defines a converter that will be used when the audio is rendered
Definition AudioServerT.h:161
char * password
Definition AudioServerT.h:183
Client * out_ptr()
Provides a pointer to the WiFiClient.
Definition AudioServerT.h:167
virtual void sendReplyHeader()
Definition AudioServerT.h:220
StreamCopy copier
Definition AudioServerT.h:192
char * network
Definition AudioServerT.h:184
Client client_obj
Definition AudioServerT.h:182
AudioServerDataCallback callback
Definition AudioServerT.h:190
bool begin(Stream &in, const char *contentType)
Start the server. You need to be connected to WiFI before calling this method.
Definition AudioServerT.h:70
Base class for all Audio Streams. It support the boolean operator to test if the object is ready with...
Definition BaseStream.h:123
virtual AudioInfo audioInfo() override
provides the actual input AudioInfo
Definition BaseStream.h:154
A simple Arduino Webserver which streams the audio as WAV data. This class is based on the AudioEncod...
Definition AudioEncodedServerT.h:190
AudioWAVServerT(const char *network, const char *password, int port=80)
Construct a new Audio WAV Server object.
Definition AudioEncodedServerT.h:204
WAVEncoder & wavEncoder()
Definition AudioEncodedServerT.h:216
~AudioWAVServerT()
Destructor: release the allocated encoder.
Definition AudioEncodedServerT.h:208
AudioWAVServerT(int port=80)
Construct a new Audio WAV Server object We assume that the WiFi is already connected.
Definition AudioEncodedServerT.h:196
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:24
Definition NoArduino.h:169
bool connected()
Definition NoArduino.h:174
void stop()
Definition NoArduino.h:171
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:128
bool begin() override
Starts the processing - sets the status to active.
Definition AudioEncoded.h:155
void setOutput(Print *outputStream)
Defines the output.
Definition AudioEncoded.h:104
void begin()
(Re)starts the processing
Definition StreamCopy.h:56
Definition NoArduino.h:142
A simple WAV file encoder. If no AudioEncoderExt is specified the WAV file contains PCM data,...
Definition CodecWAV.h:521
void(* AudioServerDataCallback)(Print *out)
Calback which writes the sound data to the stream.
Definition AudioServerT.h:11
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