arduino-audio-tools
AudioServerEx.h
1 #pragma once
2 
3 #include "AudioConfig.h"
4 #include "AudioTools/AudioOutput.h"
5 #include "AudioCodecs/CodecWAV.h"
6 #include "HttpServer.h"
7 #include "HttpExtensions.h"
8 
9 namespace audio_tools {
10 
17 struct AudioServerExConfig : public AudioInfo {
18  const char* mime = nullptr;
19  const char* ssid = nullptr;
20  const char* password = nullptr;
21  const char* path = "/";
22  // optional input; if not used use write methods to push data
23  Stream *input=nullptr;
24  int port = 80;
25 };
26 
37 class AudioServerEx : public AudioOutput {
38  public:
39  // Default Constructor
40  AudioServerEx() = default;
41 
43  AudioServerEx(const char *ssid, const char* pwd){
44  info.ssid = ssid;
45  info.password = pwd;
46  }
47 
48  virtual AudioServerExConfig defaultConfig() {
50  return cfg;
51  }
52 
53  virtual bool begin(AudioServerExConfig cfg) {
54  info = cfg;
55  return begin();
56  }
57 
58  virtual bool begin(Stream &in, const char* contentType) {
59  info.input = ∈
60  info.mime = contentType;
61  return begin();
62  }
63 
64  virtual bool begin() {
65  end(); // we (re) start with a clean state
66 
67  if (info.input==nullptr){
68  p_stream = new ExtensionStream(info.path,tinyhttp::T_GET, info.mime );
69  } else {
70  p_stream = new ExtensionStream(info.path, info.mime, *info.input);
71  }
72  p_stream->setReplyHeader(*getReplyHeader());
73  p_server = new tinyhttp::HttpServer(wifi);
74 
75  // handling of WAV
76  p_server->addExtension(*p_stream);
77  return p_server->begin(info.port, info.ssid, info.password);
78  }
79 
80  virtual void end() {
81  if (p_stream!=nullptr) {
82  delete p_stream;
83  p_stream = nullptr;
84  }
85  if (p_server!=nullptr) {
86  delete p_server;
87  p_server = nullptr;
88  }
89  }
90 
92  size_t write(const uint8_t* data, size_t len) override {
93  if (p_stream==nullptr) return 0;
94  return p_stream->write((uint8_t*)data, len);
95  }
96 
97  int availableForWrite() override {
98  if (p_stream==nullptr) return 0;
99  return p_stream->availableForWrite();
100  }
101 
103  virtual void copy() {
104  if (p_server!=nullptr){
105  p_server->copy();
106  }
107  }
108 
109  protected:
110  AudioServerExConfig info;
111  WiFiServer wifi;
112  HttpServer *p_server;
113  ExtensionStream *p_stream=nullptr;
114 
115  virtual tinyhttp::Str* getReplyHeader() {
116  return nullptr;
117  }
118 
119 };
120 
131  public:
132  // Default Constructor
133  AudioWAVServerEx() = default;
134 
136  AudioWAVServerEx(const char *ssid, const char* pwd):AudioServerEx(ssid, pwd){}
137 
138  AudioServerExConfig defaultConfig() override {
140  cfg.mime = "audio/wav";
141  return cfg;
142  }
143 
145  bool begin(Stream &in, int sample_rate, int channels, int bits_per_sample=16) {
146  info.input = ∈
147  info.sample_rate = sample_rate;
148  info.channels = channels;
149  info. bits_per_sample = bits_per_sample;
150  info.mime = "audio/wav";
151  return AudioServerEx::begin();
152  }
153 
154  bool begin(AudioServerExConfig cfg) override{
155  return AudioServerEx::begin(cfg);
156  }
157 
158  protected:
159  // Dynamic memory
160  tinyhttp::StrExt header;
161 
162  // wav files start with a 44 bytes header
163  virtual tinyhttp::Str* getReplyHeader() {
164  header.allocate(44);
165  MemoryOutput mp{(uint8_t*)header.c_str(), 44};
166  WAVHeader enc;
167  WAVAudioInfo wi;
168  wi.format = AudioFormat::PCM;
169  wi.sample_rate = info.sample_rate;
170  wi.bits_per_sample = info.bits_per_sample;
171  wi.channels = info.channels;
172  enc.setAudioInfo(wi);
173  // fill header with data
174  enc.writeHeader(&mp);
175  // make sure that the length is 44
176  assert(header.length() == 44);
177 
178  return &header;
179  }
180 };
181 
182 }
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:37
virtual void copy()
Needs to be called if the data was provided as input Stream in the AudioServerExConfig.
Definition: AudioServerEx.h:103
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:92
AudioServerEx(const char *ssid, const char *pwd)
To be compatible with legacy API.
Definition: AudioServerEx.h:43
A powerfull WAV Web server which is based on https://github.com/pschatzmann/TinyHttp....
Definition: AudioServerEx.h:130
bool begin(Stream &in, int sample_rate, int channels, int bits_per_sample=16)
Legacy API support.
Definition: AudioServerEx.h:145
AudioWAVServerEx(const char *ssid, const char *pwd)
To be compatible with legacy API.
Definition: AudioServerEx.h:136
Definition: NoArduino.h:125
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AnalogAudio.h:10
Basic Audio information which drives e.g. I2S.
Definition: AudioTypes.h:50
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:17