changeset 54:d3f869b98147 ultra-staging

Udp transmission working (enough) for demo in Nottingham
author Giulio Moro <giuliomoro@yahoo.it>
date Mon, 08 Jun 2015 01:07:48 +0100
parents 6907e2177eb8
children 3068421c0737
files core/UdpServer.cpp include/client.h projects/basic_network/render.cpp
diffstat 3 files changed, 59 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/core/UdpServer.cpp	Sun Jun 07 14:58:34 2015 +0100
+++ b/core/UdpServer.cpp	Mon Jun 08 01:07:48 2015 +0100
@@ -13,7 +13,7 @@
 	init(0);
 }
 UdpServer::~UdpServer(){
-
+	//TODO: unbind from port. AFAIK, this involves closing the socket, therefore creating the socket should become part of bindToPort
 };
 bool UdpServer::init(int aPort){
 	enabled=true;
--- a/include/client.h	Sun Jun 07 14:58:34 2015 +0100
+++ b/include/client.h	Mon Jun 08 01:07:48 2015 +0100
@@ -21,6 +21,8 @@
 	int index;
 	float buffers[2][NETWORK_AUDIO_BUFFER_SIZE];
 	int doneOnTime;
+	bool toBeSent;
+	UdpClient udpClient;
 };
 
 void error(const char *);
--- a/projects/basic_network/render.cpp	Sun Jun 07 14:58:34 2015 +0100
+++ b/projects/basic_network/render.cpp	Mon Jun 08 01:07:48 2015 +0100
@@ -8,17 +8,31 @@
 #include "../../include/RTAudioSettings.h"
 #include "../../include/render.h"
 #include <cmath>
-#include "../../include/client.h"
+//#include "../../include/client.h"
 #include "../../include/RTAudio.h"	// to schedule lower prio parallel process
 #include <rtdk.h>
 #include <native/timer.h>
 #include "../../include/PRU.h"
+#include "../../include/UdpClient.h"
+
+#define NETWORK_AUDIO_BUFFER_SIZE 400 //1400/4 //maximum payload for a UDP datagram over ethernet is 1472 bytes, I leave some headroom and divide by 4 to get the number of floats
+struct networkAudio{
+	int timestamp;
+	int currentBuffer;
+	int index;
+	float buffers[2][NETWORK_AUDIO_BUFFER_SIZE];
+	int doneOnTime;
+	bool toBeSent;
+	UdpClient udpClient;
+};
+
 float gFrequency;
 float gPhase;
 float gInverseSampleRate;
 int gCount=0;
-networkData networkObject;
-networkAudio netAudio;
+//networkData networkObject;
+#define numNetAudio 3
+networkAudio netAudio[numNetAudio];
 extern PRU *gPRU;
 AuxiliaryTask printIntervalTask;
 AuxiliaryTask transmitReceiveAudioTask;
@@ -33,7 +47,13 @@
 }
 
 void transmitReceiveAudio(){ //transmit and receive audio buffers
-	sendAudio(&netAudio);
+	for(int n=0;n<numNetAudio; n++){
+		if(netAudio[n].toBeSent){
+			netAudio[n].toBeSent=false;
+			netAudio[n].udpClient.send(netAudio[n].buffers[!netAudio[n].currentBuffer],NETWORK_AUDIO_BUFFER_SIZE*sizeof(float));
+			netAudio[n].doneOnTime=1;
+		}
+	}
 }
 
 
@@ -61,13 +81,19 @@
 //	networkObject.variables[0]=&gFrequency;
 //	networkObject.variables[1]=&gPhase;
 //	networkObject.numVariables=2;
-//	netAudio.doneOnTime=1;
-//	netAudio.index=0;
-//	netAudio.currentBuffer=0;
+	for(int n=0; n<numNetAudio; n++){
+		netAudio[n].doneOnTime=1;
+		netAudio[n].index=0;
+		netAudio[n].currentBuffer=0;
+		netAudio[n].toBeSent=false;
+		netAudio[n].udpClient.setPort(settings->transmitPort+n);
+		netAudio[n].udpClient.setServer(settings->serverName);
+	}
 //	setupSockets(settings->receivePort, settings->transmitPort, settings->serverName);
-////	transmitReceiveDataTask=createAuxiliaryTaskLoop(*transmitReceiveData, 10, "transmit-receive-data");
-////	scheduleAuxiliaryTask(transmitReceiveDataTask); //here it does not work
-//	transmitReceiveAudioTask=createAuxiliaryTaskLoop(*transmitReceiveAudio, 98, "transmit-receive-audio");
+
+//	transmitReceiveDataTask=createAuxiliaryTaskLoop(*transmitReceiveData, 10, "transmit-receive-data");
+//	scheduleAuxiliaryTask(transmitReceiveDataTask); //here it does not work
+	transmitReceiveAudioTask=createAuxiliaryTaskLoop(*transmitReceiveAudio, 98, "transmit-receive-audio");
 	return true;
 }
 
@@ -81,32 +107,36 @@
 {
 	for(int n = 0; n < numAudioFrames; n++) {
 		float out = 0.7f * sinf(gPhase);
-		float fake=0.1;
-		for(int a=0; a<24; a++){
-			fake = 0.7f * sinf(fake+out);
-		}
-		fake/=1000000000000000;
 		gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate;
 		if(gPhase > 2.0 * M_PI)
 			gPhase -= 2.0 * M_PI;
 
 //		for(int channel = 0; channel < gNumAudioChannels; channel++)
 //			audioOut[n * gNumAudioChannels + channel] = audioIn[n * gNumAudioChannels + 0]+audioIn[n * gNumAudioChannels + 1];
-		audioOut[n * gNumAudioChannels] = fake*0.0000000001;
+		audioOut[n * gNumAudioChannels] = audioIn[n*gNumAudioChannels+0];
 		audioOut[n * gNumAudioChannels+1]=out;
 		if(0==gCount){
 //			scheduleAuxiliaryTask(transmitReceiveDataTask);
 		}
-//		if(netAudio.index==(NETWORK_AUDIO_BUFFER_SIZE)){ // when the buffer is ready ...
-//			netAudio.index=0; //reset the counter
-//			if(netAudio.doneOnTime==0)
-//				rt_printf("Network buffer underrun :-{\n");
-//			netAudio.timestamp=gCount;
-//			netAudio.currentBuffer=!netAudio.currentBuffer; //switch buffer
-//			netAudio.doneOnTime=0;
-//			scheduleAuxiliaryTask(transmitReceiveAudioTask); //send the buffer
-//		}
-//		netAudio.buffers[netAudio.currentBuffer][netAudio.index++]=audioOut[n*gNumAudioChannels + 0];//copy channel 0 to the buffer
+		for(int j=0; j<numNetAudio; j++){
+			if(netAudio[j].index==(NETWORK_AUDIO_BUFFER_SIZE)){ // when the buffer is ready ...
+				netAudio[j].toBeSent=true;
+				netAudio[j].index=0; //reset the counter
+				if(netAudio[j].doneOnTime==0)
+					rt_printf("Network buffer underrun :-{\n");
+				netAudio[j].timestamp=gCount;
+				netAudio[j].currentBuffer=!netAudio[j].currentBuffer; //switch buffer
+				netAudio[j].doneOnTime=0;
+				scheduleAuxiliaryTask(transmitReceiveAudioTask); //send the buffer
+			}
+		}
+		if((gCount&1)==0){
+			netAudio[1].buffers[netAudio[1].currentBuffer][netAudio[1].index++]=analogRead(0,n/2)+audioOut[n*gNumAudioChannels + 0];
+			netAudio[2].buffers[netAudio[2].currentBuffer][netAudio[2].index++]=analogRead(1,n/2)+audioOut[n*gNumAudioChannels + 0];
+		}
+		netAudio[0].buffers[netAudio[0].currentBuffer][netAudio[0].index++]=0.5*(out+audioOut[n*gNumAudioChannels + 0]);//copy channel 0 to the buffer
+//		netAudio[1].buffers[netAudio[1].currentBuffer][netAudio[1].index++]=0.5*(out+audioOut[n*gNumAudioChannels + 0]);
+//		netAudio[2].buffers[netAudio[2].currentBuffer][netAudio[2].index++]=0.5*(out+audioOut[n*gNumAudioChannels + 0]);
 		gCount++;
 	}
 }