arduino-audio-tools
AudioServerEx.h
1 #pragma once
2 
3 #include "AudioConfig.h"
4 #include "AudioTools/CoreAudio/AudioOutput.h"
5 #include "AudioTools/AudioCodecs/CodecWAV.h"
6 #include "AudioTools/CoreAudio/AudioBasic/StrView.h"
7 #include "HttpServer.h"
8 #include "HttpExtensions.h"
9 
10 namespace audio_tools {
11 
18 struct AudioServerExConfig : public AudioInfo {
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 
38 class AudioServerEx : public AudioOutput {
39  public:
40  // Default Constructor
41  AudioServerEx() = default;
42 
44  AudioServerEx(const char *ssid, const char* pwd){
45  info.ssid = ssid;
46  info.password = pwd;
47  }
48 
49  virtual AudioServerExConfig defaultConfig() {
51  return cfg;
52  }
53 
54  virtual bool begin(AudioServerExConfig cfg) {
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 {
71  p_stream = new ExtensionStream(info.path, info.mime, *info.input);
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:
111  AudioServerExConfig info;
112  WiFiServer wifi;
113  HttpServer *p_server;
114  ExtensionStream *p_stream=nullptr;
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 
139  AudioServerExConfig defaultConfig() override {
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 
155  bool begin(AudioServerExConfig cfg) override{
156  return AudioServerEx::begin(cfg);
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;
168  WAVAudioInfo wi;
169  wi.format = AudioFormat::PCM;
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 }
Abstract Audio Ouptut class.
Definition: AudioOutput.h:22
A powerfull Web server which is based on https://github.com/pschatzmann/TinyHttp. It supports multipl...
Definition: AudioServerEx.h:38
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
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
Definition: NoArduino.h:125
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:823
Basic Audio information which drives e.g. I2S.
Definition: AudioTypes.h:52
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition: AudioTypes.h:55
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition: AudioTypes.h:57
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition: AudioTypes.h:59
Config information for AudioServerEx.
Definition: AudioServerEx.h:18