@@ -25,7 +25,7 @@ class VBANConfig : public AudioInfo {
25
25
const char * ssid = nullptr ;
26
26
// / password for wifi connection
27
27
const char * password = nullptr ;
28
- int rx_buffer_count = 10 ;
28
+ int rx_buffer_count = 30 ;
29
29
// set to true if samples are generated faster then sample rate
30
30
bool throttle_active = false ;
31
31
// when negative the number of ms that are subtracted from the calculated wait
@@ -34,6 +34,7 @@ class VBANConfig : public AudioInfo {
34
34
// defines the max write size
35
35
int max_write_size =
36
36
DEFAULT_BUFFER_SIZE * 2 ; // just good enough for 44100 stereo
37
+ uint8_t format = 0 ;
37
38
};
38
39
39
40
/* *
@@ -54,13 +55,16 @@ class VBANStream : public AudioStream {
54
55
return def;
55
56
}
56
57
58
+ void setOutput (Print &out){
59
+ p_out = &out;
60
+ }
61
+
57
62
void setAudioInfo (AudioInfo info) override {
58
63
cfg.copyFrom (info);
59
64
AudioStream::setAudioInfo (info);
60
65
auto thc = throttle.defaultConfig ();
61
66
thc.copyFrom (info);
62
- thc.correction_us = cfg.throttle_correction_us ;
63
- ;
67
+ thc.correction_us = cfg.throttle_correction_us ;
64
68
throttle.begin (thc);
65
69
if (cfg.mode == TX_MODE) {
66
70
configure_tx ();
@@ -82,7 +86,12 @@ class VBANStream : public AudioStream {
82
86
tx_buffer.resize (VBAN_PACKET_NUM_SAMPLES);
83
87
return begin_tx ();
84
88
} else {
85
- rx_buffer.resize (VBAN_PACKET_MAX_LEN_BYTES, cfg.rx_buffer_count );
89
+ #ifdef ESP32
90
+ rx_buffer.resize (DEFAULT_BUFFER_SIZE * cfg.rx_buffer_count );
91
+ rx_buffer.setReadMaxWait (10 );
92
+ #else
93
+ rx_buffer.resize (DEFAULT_BUFFER_SIZE, cfg.rx_buffer_count );
94
+ #endif
86
95
return begin_rx ();
87
96
}
88
97
}
@@ -91,8 +100,7 @@ class VBANStream : public AudioStream {
91
100
if (!udp_connected) return 0 ;
92
101
93
102
int16_t * adc_data = (int16_t *)data;
94
- ;
95
- size_t samples = byteCount / 2 ;
103
+ size_t samples = byteCount / (cfg.bits_per_sample /8 );
96
104
97
105
// limit output speed
98
106
if (cfg.throttle_active ) {
@@ -123,6 +131,11 @@ class VBANStream : public AudioStream {
123
131
int availableForWrite () { return cfg.max_write_size ; }
124
132
125
133
size_t readBytes (uint8_t * data, size_t byteCount) override {
134
+ TRACED ();
135
+ size_t samples = byteCount / (cfg.bits_per_sample /8 );
136
+ if (cfg.throttle_active ) {
137
+ throttle.delayFrames (samples / cfg.channels );
138
+ }
126
139
return rx_buffer.readArray (data, byteCount);
127
140
}
128
141
@@ -134,12 +147,17 @@ class VBANStream : public AudioStream {
134
147
VBan vban;
135
148
VBANConfig cfg;
136
149
SingleBuffer<int16_t > tx_buffer{0 };
150
+ #ifdef ESP32
151
+ SynchronizedBufferRTOS<uint8_t > rx_buffer{ 0 };
152
+ #else
137
153
NBuffer<uint8_t > rx_buffer{DEFAULT_BUFFER_SIZE, 0 };
154
+ #endif
138
155
bool udp_connected = false ;
139
156
uint32_t packet_counter = 0 ;
140
157
Throttle throttle;
141
158
size_t bytes_received = 0 ;
142
159
bool available_active = false ;
160
+ Print *p_out = nullptr ;
143
161
144
162
bool begin_tx () {
145
163
if (!configure_tx ()) {
@@ -332,19 +350,21 @@ class VBANStream : public AudioStream {
332
350
333
351
int len = packet.length ();
334
352
if (len > 0 ) {
353
+ LOGD (" receive_udp %d" , len);
335
354
uint8_t * udpIncomingPacket = packet.data ();
336
355
337
356
// receive incoming UDP packet
338
357
// Check if packet length meets VBAN specification:
339
358
if (len <= (VBAN_PACKET_HEADER_BYTES + VBAN_PACKET_COUNTER_BYTES) ||
340
359
len > VBAN_PACKET_MAX_LEN_BYTES) {
341
- LOGE (" Error: packet length %u bytes\n " , len);
360
+ LOGE (" Packet length %u bytes" , len);
361
+ rx_buffer.reset ();
342
362
return ;
343
363
}
344
364
345
365
// Check if preamble matches VBAN format:
346
366
if (strncmp (" VBAN" , (const char *)udpIncomingPacket, 4 ) != 0 ) {
347
- LOGE (" Unrecognized preamble %.4s\n " , udpIncomingPacket);
367
+ LOGE (" Unrecognized preamble %.4s" , udpIncomingPacket);
348
368
return ;
349
369
}
350
370
@@ -353,15 +373,26 @@ class VBANStream : public AudioStream {
353
373
vban_rx_pkt_nbr = (uint32_t *)&udpIncomingPacket[VBAN_PACKET_HEADER_BYTES];
354
374
vban_rx_data = (int16_t *)&udpIncomingPacket[VBAN_PACKET_HEADER_BYTES +
355
375
VBAN_PACKET_COUNTER_BYTES];
356
- vban_rx_sample_count = vban_rx_data_bytes / 2 ;
376
+ vban_rx_sample_count = vban_rx_data_bytes / (cfg. bits_per_sample / 8 ) ;
357
377
uint8_t vbanSampleRateIdx = udpIncomingPacket[4 ] & VBAN_SR_MASK;
358
378
uint8_t vbchannels = udpIncomingPacket[6 ] + 1 ;
379
+ uint8_t vbframes = udpIncomingPacket[5 ] + 1 ;
380
+ uint8_t vbformat = udpIncomingPacket[4 ] & VBAN_PROTOCOL_MASK;;
359
381
uint32_t vbanSampleRate = VBanSRList[vbanSampleRateIdx];
382
+
383
+ LOGD (" sample_count: %d - frames: %d" , vban_rx_sample_count, vbframes);
384
+ assert (vban_rx_sample_count == vbframes*vbchannels);
385
+
386
+
387
+ if (vbformat != cfg.format ){
388
+ LOGE (" Format ignored: 0x%x" , vbformat);
389
+ return ;
390
+ }
360
391
361
392
// Just to be safe, re-check sample count against max sample count to
362
393
// avoid overrunning outBuf later
363
394
if (vban_rx_sample_count > VBAN_PACKET_MAX_SAMPLES) {
364
- LOGE (" error: unexpected packet size: %u\n " , vban_rx_sample_count);
395
+ LOGE (" unexpected packet size: %u" , vban_rx_sample_count);
365
396
return ;
366
397
}
367
398
@@ -372,17 +403,28 @@ class VBANStream : public AudioStream {
372
403
setAudioInfo (cfg);
373
404
}
374
405
406
+ if (p_out!=nullptr ){
407
+ int size_written = p_out->write ((uint8_t *)vban_rx_data, vban_rx_data_bytes);
408
+ if (size_written != vban_rx_data_bytes) {
409
+ LOGE (" buffer overflow %d -> %d" , vban_rx_data_bytes, size_written);
410
+ }
411
+ return ;
412
+ }
413
+
375
414
// write data to buffer
376
- int size = vban_rx_sample_count * sizeof (uint16_t );
377
- if (rx_buffer.writeArray ((uint8_t *)&vban_rx_data, size) != size) {
378
- LOGE (" buffer overflow" );
415
+ TRACED ();
416
+ int size_written = rx_buffer.writeArray ((uint8_t *)vban_rx_data, vban_rx_data_bytes);
417
+ if (size_written != vban_rx_data_bytes) {
418
+ LOGE (" buffer overflow %d -> %d" , vban_rx_data_bytes, size_written);
379
419
}
380
420
381
421
// report available bytes only when buffer is 50% full
382
422
if (!available_active) {
383
- bytes_received += size ;
384
- if (bytes_received >= cfg.rx_buffer_count * DEFAULT_BUFFER_SIZE * 0.5 )
423
+ bytes_received += vban_rx_data_bytes ;
424
+ if (bytes_received >= cfg.rx_buffer_count * DEFAULT_BUFFER_SIZE * 0.75 ){
385
425
available_active = true ;
426
+ LOGI (" Activating vban" );
427
+ }
386
428
}
387
429
}
388
430
}
0 commit comments