Skip to content

Commit d80613a

Browse files
committed
udp examples
1 parent ff52e9b commit d80613a

File tree

3 files changed

+31
-39
lines changed

3 files changed

+31
-39
lines changed

examples/sandbox/communication/udp/communication-udp-receive/communication-udp-receive.ino renamed to examples/examples-communication/udp/communication-udp-receive/communication-udp-receive.ino

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/**
22
* @file example-udp-receive.ino
33
* @author Phil Schatzmann
4-
* @brief Receiving audio via udp
4+
* @brief Receiving audio via udp.
5+
* Because the clocks of the 2 processors are not synchronized, we might get some
6+
* buffer overruns or underruns.
57
* @version 0.1
68
* @date 2022-03-09
79
*
@@ -12,37 +14,29 @@
1214

1315
#include "AudioTools.h"
1416
#include "AudioLibs/Communication.h"
17+
// #include "AudioLibs/AudioKit.h"
1518

16-
uint16_t sample_rate = 44100;
17-
uint8_t channels = 2; // The stream will have 2 channels
18-
I2SStream out;
19-
UDPStream udp;
20-
const int udpPort = 7000;
21-
StreamCopy copier(out, udp);
2219
const char* ssid="SSID";
2320
const char* password="password";
21+
AudioInfo info(22000, 1, 16);
22+
UDPStream udp(ssid, password);
23+
const int udpPort = 7000;
24+
I2SStream out; // or ony other e.g. AudioKitStream
25+
StreamCopy copier(out, udp);
2426

2527
void setup() {
2628
Serial.begin(115200);
2729
AudioLogger::instance().begin(Serial, AudioLogger::Info);
2830

29-
// connect to WIFI
30-
WiFi.begin(ssid, password);
31-
while (WiFi.status() != WL_CONNECTED) {
32-
delay(500);
33-
Serial.print(".");
34-
}
35-
Serial.println();
36-
37-
38-
udp.begin(port);
31+
// start UDP receive
32+
udp.begin(udpPort);
3933

4034
// start I2S
4135
Serial.println("starting I2S...");
4236
auto config = out.defaultConfig(TX_MODE);
43-
config.sample_rate = sample_rate;
44-
config.channels = channels;
45-
config.bits_per_sample = 16;
37+
config.copyFrom(info);
38+
config.buffer_size = 1024;
39+
config.buffer_count = 20;
4640
out.begin(config);
4741

4842
Serial.println("started...");

examples/sandbox/communication/udp/communication-udp-send/communication-udp-send.ino renamed to examples/examples-communication/udp/communication-udp-send/communication-udp-send.ino

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/**
22
* @file example-udp-send.ino
33
* @author Phil Schatzmann
4-
* @brief Sending audio over udp
4+
* @brief Sending audio over udp. Because the SineWaveGenerator is providing the data as fast
5+
* as possible we need to throttle the speed.
56
* @version 0.1
67
* @date 2022-03-09
78
*
@@ -13,42 +14,33 @@
1314
#include "AudioTools.h"
1415
#include "AudioLibs/Communication.h"
1516

16-
uint16_t sample_rate = 44100;
17-
uint8_t channels = 2; // The stream will have 2 channels
17+
const char *ssid = "ssid";
18+
const char *password = "password";
19+
AudioInfo info(22000, 1, 16);
1820
SineWaveGenerator<int16_t> sineWave( 32000); // subclass of SoundGenerator with max amplitude of 32000
1921
GeneratedSoundStream<int16_t> sound( sineWave); // Stream generated from sine wave
20-
UDPStream udp;
22+
UDPStream udp(ssid, password);
2123
Throttle throttle;
2224
IPAddress udpAddress(192, 168, 1, 255);
2325
const int udpPort = 7000;
2426
StreamCopy copier(udp, sound); // copies sound into i2s
25-
const char *ssid = "ssid";
26-
const char *password = "password";
2727

2828

2929
void setup() {
3030
Serial.begin(115200);
3131
AudioLogger::instance().begin(Serial, AudioLogger::Info);
3232

33-
// connect to WIFI
34-
WiFi.begin(ssid, password);
35-
while (WiFi.status() != WL_CONNECTED) {
36-
delay(500);
37-
Serial.print(".");
38-
}
39-
Serial.println();
40-
Serial.println(WiFi.localIP());
41-
42-
4333
// Define udp address and port
4434
udp.begin(udpAddress, udpPort);
35+
36+
// Define Throttle
4537
auto cfg = throttle.defaultConfig();
46-
cfg.channels = channels;
47-
cfg.sample_rate = sample_rate;
38+
cfg.copyFrom(info);
39+
//cfg.correction_ms = 0;
4840
throttle.begin(cfg);
4941

5042
// Setup sine wave
51-
sineWave.begin(channels, sample_rate, N_B4);
43+
sineWave.begin(info, N_B4);
5244
Serial.println("started...");
5345
}
5446

src/AudioLibs/Communication.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,12 @@ class Throttle {
753753
return c;
754754
}
755755

756+
void begin(AudioInfo info) {
757+
ThrottleConfig cfg;
758+
cfg.copyFrom(info);
759+
begin(cfg);
760+
}
761+
756762
void begin(ThrottleConfig info) {
757763
this->info = info;
758764
bytesPerSample = info.bits_per_sample / 8 * info.channels;

0 commit comments

Comments
 (0)