Skip to content

Commit e62899b

Browse files
committed
UDP
1 parent 6b6e2f1 commit e62899b

File tree

3 files changed

+138
-6
lines changed

3 files changed

+138
-6
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* @file example-udp-receive.ino
3+
* @author Phil Schatzmann
4+
* @brief Receiving audio via udp
5+
* @version 0.1
6+
* @date 2022-03-09
7+
*
8+
* @copyright Copyright (c) 2022
9+
*
10+
* ATTENTION: DRAFT - not tested yet
11+
*/
12+
13+
#include "AudioTools.h"
14+
#include "AudioLibs/Communication.h"
15+
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);
22+
const char* ssid="SSID";
23+
const char* password="password";
24+
25+
void setup() {
26+
Serial.begin(115200);
27+
AudioLogger::instance().begin(Serial, AudioLogger::Info);
28+
29+
30+
// connect to WIFI
31+
WiFi.begin(ssid, password);
32+
while (WiFi.status() != WL_CONNECTED) {
33+
delay(500);
34+
Serial.print(".");
35+
}
36+
Serial.println();
37+
38+
39+
udp.begin(port);
40+
41+
// start I2S
42+
Serial.println("starting I2S...");
43+
auto config = out.defaultConfig(TX_MODE);
44+
config.sample_rate = sample_rate;
45+
config.channels = channels;
46+
config.bits_per_sample = 16;
47+
out.begin(config);
48+
49+
Serial.println("started...");
50+
}
51+
52+
void loop() {
53+
copier.copy();
54+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @file example-udp-send.ino
3+
* @author Phil Schatzmann
4+
* @brief Sending audio over udp
5+
* @version 0.1
6+
* @date 2022-03-09
7+
*
8+
* @copyright Copyright (c) 2022
9+
*
10+
* ATTENTION: DRAFT - not tested yet
11+
*/
12+
13+
#include "AudioTools.h"
14+
#include "AudioLibs/Communication.h"
15+
16+
uint16_t sample_rate = 44100;
17+
uint8_t channels = 2; // The stream will have 2 channels
18+
SineWaveGenerator<int16_t> sineWave( 32000); // subclass of SoundGenerator with max amplitude of 32000
19+
GeneratedSoundStream<int16_t> sound( sineWave); // Stream generated from sine wave
20+
UDPStream udp;
21+
IPAddress udpAddress(192, 168, 1, 255);
22+
const int udpPort = 7000;
23+
StreamCopy copier(udp, sound); // copies sound into i2s
24+
const char *ssid = "ssid";
25+
const char *password = "password";
26+
27+
28+
void setup() {
29+
Serial.begin(115200);
30+
AudioLogger::instance().begin(Serial, AudioLogger::Info);
31+
32+
// connect to WIFI
33+
WiFi.begin(ssid, password);
34+
while (WiFi.status() != WL_CONNECTED) {
35+
delay(500);
36+
Serial.print(".");
37+
}
38+
Serial.println();
39+
Serial.println(WiFi.localIP());
40+
41+
42+
// Define udp address and port
43+
udp.begin(udpAddress, udpPort);
44+
45+
// Setup sine wave
46+
sineWave.begin(channels, sample_rate, N_B4);
47+
Serial.println("started...");
48+
}
49+
50+
void loop() {
51+
copier.copy();
52+
}

src/AudioLibs/Communication.h

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ 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>
36+
bool addPeers(const char *(&array)[size]) {
3737
bool result = true;
38-
for (int j=0;j<size;j++){
39-
if(!addPeer(array[j])){
38+
for (int j = 0; j < size; j++) {
39+
if (!addPeer(array[j])) {
4040
result = false;
4141
}
4242
}
@@ -150,10 +150,11 @@ class ESPNowStream : public AudioStreamX {
150150
// we use the first confirming mac_addr for further confirmations and ignore
151151
// others
152152
if (first_mac[0] == 0) {
153-
strcpy((char*)first_mac,(char*) mac_addr);
153+
strcpy((char *)first_mac, (char *)mac_addr);
154154
}
155155
LOGI("%s:%d", mac_addr, status);
156-
if (strcmp((char*)first_mac, (char*)mac_addr) == 0 && status == ESP_NOW_SEND_SUCCESS) {
156+
if (strcmp((char *)first_mac, (char *)mac_addr) == 0 &&
157+
status == ESP_NOW_SEND_SUCCESS) {
157158
ESPNowStreamSelf->available_to_write = ESP_NOW_MAX_DATA_LEN;
158159
}
159160
}
@@ -181,15 +182,40 @@ class UDPStream : public WiFiUDP {
181182
return size;
182183
}
183184

185+
/// Starts to send data to the indicated address / port
186+
uint8_t begin(IPAddress a, uint16_t port) {
187+
remote_address_ext = a;
188+
remote_port_ext = port;
189+
return WiFiUDP::begin(port);
190+
}
191+
192+
/// We use the same remote port as defined in begin for write
193+
uint16_t remotePort() {
194+
uint16_t result = WiFiUDP::remotePort();
195+
return result != 0 ? result : remote_port_ext;
196+
}
197+
198+
/// We use the same remote ip as defined in begin for write
199+
IPAddress remoteIP() {
200+
// IPAddress result = WiFiUDP::remoteIP();
201+
// LOGI("ip: %u", result);
202+
return remote_address_ext;
203+
}
204+
184205
/**
185206
* Replys will be sent to the initial remote caller
186207
*/
187208
size_t write(const uint8_t *buffer, size_t size) override {
209+
LOGD(LOG_METHOD);
188210
beginPacket(remoteIP(), remotePort());
189211
size_t result = WiFiUDP::write(buffer, size);
190212
endPacket();
191213
return result;
192214
}
215+
216+
protected:
217+
uint16_t remote_port_ext;
218+
IPAddress remote_address_ext;
193219
};
194220

195221
} // namespace audio_tools

0 commit comments

Comments
 (0)