arduino-audio-tools
Loading...
Searching...
No Matches
AudioEncodedServerT.h
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:
23 AudioEncoderServerT(AudioEncoder *encoder, int port = 80) : AudioServerT<Client, Server>(port) {
24 this->encoder = encoder;
25 }
26
33 AudioEncoderServerT(AudioEncoder *encoder, const char *network,
34 const char *password, int port = 80)
35 : AudioServerT<Client, Server>(network, password, port) {
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;
60 encoder->setAudioInfo(audio_info);
61 // encoded_stream.begin(&client_obj, encoder);
62 encoded_stream.setOutput(&this->client_obj);
63 encoded_stream.setEncoder(encoder);
64 encoded_stream.begin(audio_info);
65 return AudioServerT<Client, Server>::begin(in, encoder->mime());
66 }
67
76 bool begin(Stream &in, AudioInfo info, BaseConverter *converter = nullptr) {
77 TRACED();
78 this->in = &in;
79 this->audio_info = info;
81 encoder->setAudioInfo(audio_info);
82 encoded_stream.setOutput(&this->client_obj);
83 encoded_stream.setEncoder(encoder);
84 if (!encoded_stream.begin(audio_info)) {
85 LOGE("encoder begin failed");
86 // stop();
87 }
88
89 return AudioServerT<Client, Server>::begin(in, encoder->mime());
90 }
91
99 bool begin(AudioStream &in, BaseConverter *converter = nullptr) {
100 TRACED();
101 this->in = &in;
102 this->audio_info = in.audioInfo();
104 encoder->setAudioInfo(audio_info);
105 encoded_stream.setOutput(&this->client_obj);
106 encoded_stream.setEncoder(encoder);
107 encoded_stream.begin(audio_info);
108
109 return AudioServerT<Client, Server>::begin(in, encoder->mime());
110 }
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;
125 encoder->setAudioInfo(audio_info);
126
127 return AudioServerT<Client, Server>::begin(cb, encoder->mime());
128 }
129
130 // provides a pointer to the encoder
131 AudioEncoder *audioEncoder() { return encoder; }
132
133 protected:
134 // Sound Generation - use EncodedAudioOutput with is more efficient then
135 // EncodedAudioStream
136 EncodedAudioOutput encoded_stream;
137 AudioInfo audio_info;
138 AudioEncoder *encoder = nullptr;
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);
155 encoded_stream.begin();
156
157 // provide data via Callback to encoded_stream
158 LOGI("sendReply - calling callback");
159 // Send delayed header
160 AudioServerT<Client, Server>::sendReplyHeader();
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);
169 encoded_stream.begin();
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
176 AudioServerT<Client, Server>::sendReplyHeader();
177 }
178 }
179};
180
189template <class Client, class Server>
190class AudioWAVServerT : public AudioEncoderServerT<Client, Server> {
191 public:
196 AudioWAVServerT(int port = 80) : AudioEncoderServerT<Client, Server>(new WAVEncoder(), port) {}
197
204 AudioWAVServerT(const char *network, const char *password, int port = 80)
205 : AudioEncoderServerT<Client, Server>(new WAVEncoder(), network, password, port) {}
206
210 if (encoder != nullptr) {
211 delete encoder;
212 }
213 }
214
215 // provides a pointer to the encoder
216 WAVEncoder &wavEncoder() { return *static_cast<WAVEncoder *>(AudioEncoderServerT<Client, Server>::encoder); }
217};
218
219
220
221} // namespace audio_tools
222
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
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
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
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
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.
A simple Arduino Webserver which streams the result This class is based on the WiFiServer class....
Definition AudioServerT.h:37
void setConverter(BaseConverter *c)
defines a converter that will be used when the audio is rendered
Definition AudioServerT.h:161
Client * out_ptr()
Provides a pointer to the WiFiClient.
Definition AudioServerT.h:167
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:122
virtual AudioInfo audioInfo() override
provides the actual input AudioInfo
Definition BaseStream.h:153
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
~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
Abstract Base class for Converters A converter is processing the data in the indicated array.
Definition BaseConverter.h:24
Definition NoArduino.h:169
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
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