Skip to content

Commit 0f8d10d

Browse files
committed
support for 2 Maximilian
1 parent e62899b commit 0f8d10d

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed

src/AudioLibs/Communication.h

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ ESPNowStream *ESPNowStreamSelf = nullptr;
1919
*/
2020

2121
class ESPNowStream : public AudioStreamX {
22-
public:
22+
public:
2323
ESPNowStream() { ESPNowStreamSelf = this; };
2424

2525
/// Adds a peer to which we can send info or from which we can receive info
@@ -32,8 +32,7 @@ class ESPNowStream : public AudioStreamX {
3232
}
3333

3434
/// Adds an array of
35-
template <size_t size>
36-
bool addPeers(const char *(&array)[size]) {
35+
template <size_t size> bool addPeers(const char *(&array)[size]) {
3736
bool result = true;
3837
for (int j = 0; j < size; j++) {
3938
if (!addPeer(array[j])) {
@@ -133,7 +132,7 @@ class ESPNowStream : public AudioStreamX {
133132
/// Returns the mac address of the current ESP32
134133
const char *address() { return WiFi.macAddress().c_str(); }
135134

136-
protected:
135+
protected:
137136
RingBuffer<uint8_t> buffer{1024 * 5};
138137
esp_now_recv_cb_t receive = default_recv_cb;
139138
esp_now_send_cb_t send = default_send_cb;
@@ -168,7 +167,7 @@ class ESPNowStream : public AudioStreamX {
168167
*/
169168

170169
class UDPStream : public WiFiUDP {
171-
public:
170+
public:
172171
/**
173172
* Provides the available size of the current package and if this is used up
174173
* of the next package
@@ -213,9 +212,53 @@ class UDPStream : public WiFiUDP {
213212
return result;
214213
}
215214

216-
protected:
215+
protected:
217216
uint16_t remote_port_ext;
218217
IPAddress remote_address_ext;
219218
};
220219

221-
} // namespace audio_tools
220+
221+
struct ThrottleConfig : public AudioBaseInfo {
222+
ThrottleConfig(){
223+
sample_rate = 44100;
224+
bits_per_sample = 16;
225+
channels = 2;
226+
}
227+
int correction_ms = 0;
228+
}
229+
230+
/**
231+
* @brief Throttle Sending to follow the the indicated sample rate
232+
*
233+
*/
234+
class Throttle {
235+
public:
236+
Throttle() = default;
237+
238+
void begin(ThrottleConfig info) {
239+
this->info = info;
240+
bytesPerSample = info.bits_per_sample / 8 * info.channels;
241+
}
242+
243+
// starts the timing
244+
void start() { start_time = millis(); }
245+
246+
// delay
247+
void throttle(size_t bytes) { throttleSamples(bytes / bytesPerSample); }
248+
249+
// delay
250+
void throttleSamples(size_t samples) {
251+
int durationMsEff = millis() - start_time;
252+
int durationToBe = (samples * 1000) / info.sample_rate;
253+
int waitMs = durationToBe - durationMsEff + correction_ms;
254+
if (waitMs > 0) {
255+
delay(waitMs);
256+
}
257+
}
258+
259+
protected:
260+
unsigned long start_time;
261+
AudioBaseInfo info;
262+
int bytesPerSample;
263+
264+
} // namespace audio_tools

src/AudioLibs/MaximilianDSP.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
// Maximilian play function - return an array of 2 channels
77
void play(maxi_float_t *channels);//run dac!
8+
void play1(maxi_float_t *channels);//run dac!
89

910
namespace audio_tools {
1011

@@ -15,10 +16,11 @@ namespace audio_tools {
1516
class Maximilian {
1617
public:
1718

18-
Maximilian(Print &out, int bufferSize=DEFAULT_BUFFER_SIZE){
19+
Maximilian(Print &out, int bufferSize=DEFAULT_BUFFER_SIZE, void (*callback)(maxi_float_t *channels)=play){
1920
buffer_size = bufferSize;
2021
p_buffer = new uint8_t[bufferSize];
2122
p_sink = &out;
23+
this->callback = callback;
2224
}
2325

2426
~Maximilian() {
@@ -49,7 +51,7 @@ class Maximilian {
4951
uint16_t samples = buffer_size / sizeof(uint16_t);
5052
int16_t *p_samples = (int16_t *)p_buffer;
5153
for (uint16_t j=0;j<samples;j+=cfg.channels){
52-
play(out);
54+
callback(out);
5355
// convert all channels to int16
5456
for (int ch=0;ch<cfg.channels;ch++){
5557
p_samples[j+ch] = out[ch]*32767*volume;
@@ -66,6 +68,7 @@ class Maximilian {
6668
int buffer_size=256;
6769
Print *p_sink=nullptr;
6870
AudioBaseInfo cfg;
71+
void (*callback)(maxi_float_t *channels);
6972
};
7073

7174

0 commit comments

Comments
 (0)