Hello,
I am trying to get my photon to stream audio data over UDP or similar. I think a good solution is to stream over UDP to VLC media player but cannot get this to work.
I have followed this example :https://github.com/dmiddlecamp/particle-photon-audio-stream
Which allowed me to stream the audio and save the files onto my laptop. I now want (close to) live or ‘real-time’ playback. I have modified the audio-stream main file to:
#include "SparkIntervalTimer.h"
#include "SimpleRingBuffer.h"
#include <math.h>
#define MICROPHONE_PIN A0
#define AUDIO_BUFFER_MAX 8192
#define SINGLE_PACKET_MIN 512
#define SINGLE_PACKET_MAX 1024
#define SERIAL_DEBUG_ON true
#define AUDIO_TIMING_VAL 125 /* 8,000 hz */
uint8_t txBuffer[SINGLE_PACKET_MAX + 1];
SimpleRingBuffer audio_buffer;
//SimpleRingBuffer recv_buffer;
UDP Udp;
unsigned int localPort = 8888;
// version without timers
unsigned long lastRead = micros();
unsigned long lastSend = millis();
char myIpAddress[24];
IntervalTimer readMicTimer;
//float _volumeRatio = 0.50;
int _sendBufferLength = 0;
unsigned int lastPublished = 0;
bool _isRecording = false;
volatile int counter = 0;
void setup() {
digitalWrite(D2, HIGH);
#if SERIAL_DEBUG_ON
Serial.begin(115200);
#endif
setADCSampleTime(ADC_SampleTime_3Cycles);
pinMode(MICROPHONE_PIN, INPUT);
pinMode(D7, OUTPUT);
int mySampleRate = AUDIO_TIMING_VAL;
Particle.variable("ipAddress", myIpAddress, STRING);
Particle.variable("sample_rate", &mySampleRate, INT);
Particle.publish("sample_rate", " my sample rate is: " + String(AUDIO_TIMING_VAL));
IPAddress myIp = WiFi.localIP();
sprintf(myIpAddress, "%d.%d.%d.%d", myIp[0], myIp[1], myIp[2], myIp[3]);
audio_buffer.init(AUDIO_BUFFER_MAX);
lastRead = micros();
readMicTimer.begin(readMic, AUDIO_TIMING_VAL, uSec);
}
unsigned int lastLog = 0;
unsigned int lastClientCheck = 0;
void loop() {
unsigned int now = millis();
if ((now - lastClientCheck) > 100) {
lastClientCheck = now;
checkClient = audioServer.available();
if (checkClient.connected()) {
audioClient = checkClient;
}
}
#if SERIAL_DEBUG_ON
if ((now - lastLog) > 1000) {
lastLog = now;
Serial.println("counter was " + String(counter));
//Serial.println("audio buffer size is now " + String(audio_buffer.getSize()));
counter = 0;
}
#endif
sendEvery(100);
}
// Callback for Timer 1
void readMic(void) {
uint16_t value = analogRead(MICROPHONE_PIN);
value = map(value, 0, 4095, 0, 255);
audio_buffer.put(value);
counter++;
}
void startRecording() {
if (!_isRecording) {
readMicTimer.begin(readMic, AUDIO_TIMING_VAL, uSec);
}
_isRecording = true;
}
void stopRecording() {
if (_isRecording) {
readMicTimer.end();
}
_isRecording = false;
}
void sendEvery(int delay) {
// if it's been longer than 100ms since our last broadcast, then broadcast.
if ((millis() - lastSend) >= delay) {
sendAudio();
lastSend = millis();
}
}
// Callback for Timer 1
void sendAudio(void) {
int count = 0;
int storedSoundBytes = audio_buffer.getSize();
while (count < storedSoundBytes) {
if (audio_buffer.getSize() < SINGLE_PACKET_MIN) {
break;
}
int size = min(audio_buffer.getSize(), SINGLE_PACKET_MAX);
int c = 0;
for(int c = 0; c < size; c++) {
txBuffer[c] = audio_buffer.get();
}
count += size;
// send it!
Udp.sendPacket(txBuffer, size, IPAddress(192,168,0,198),localPort);
//audioClient.write(txBuffer, size);
}
}
Running this and then opening the stream on VLC results in:
main error: connection error: Connection timed out
ftp error: connection failed
main debug: no access modules matched
main debug: dead input
qt debug: IM: Deleting the input
main debug: changing item without a request (current 16/17)
main debug: nothing to play
Can anyone point me in right direction? Sorry this is my first project of this kind and I’m struggling with it.