changeset 92:a55dcdcebdcd

Recovered older basic_network project which got lost in the API update
author Giulio Moro <giuliomoro@yahoo.it>
date Tue, 21 Jul 2015 17:17:37 +0100
parents 241d4d5df929
children 8c7f537d0a21
files .cproject projects/basic_network/render.cpp
diffstat 2 files changed, 86 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/.cproject	Sun Jul 19 23:19:27 2015 +0100
+++ b/.cproject	Tue Jul 21 17:17:37 2015 +0100
@@ -95,7 +95,6 @@
 					<sourceEntries>
 						<entry excluding="default_main.cpp|audio_routines_old.S" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="core"/>
 						<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="include"/>
-						<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="projects/basic"/>
 						<entry flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="source"/>
 					</sourceEntries>
 				</configuration>
@@ -185,7 +184,7 @@
 					<sourceEntries>
 						<entry excluding="default_main.cpp|audio_routines_old.S" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="core"/>
 						<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="include"/>
-						<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="projects/basic"/>
+						<entry flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="projects/basic_networks"/>
 						<entry flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="source"/>
 					</sourceEntries>
 				</configuration>
--- a/projects/basic_network/render.cpp	Sun Jul 19 23:19:27 2015 +0100
+++ b/projects/basic_network/render.cpp	Tue Jul 21 17:17:37 2015 +0100
@@ -6,24 +6,42 @@
  */
 
 #include <BeagleRT.h>
+//#include <rtdk.h>
 #include <cmath>
-#include <client.h>
+#include <UdpClient.h>
+#include <Utilities.h>
+
+AuxiliaryTask transmitReceiveDataTask;
+
+#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;
-AuxiliaryTask transmitReceiveDataTask;
+//networkData networkObject;
+#define numNetAudio 3
+networkAudio netAudio[numNetAudio];
+AuxiliaryTask printIntervalTask;
+AuxiliaryTask transmitReceiveAudioTask;
 
-void transmitReceiveData(){
-   	printf("transmitReceiveData auxiliary task has started\n");
-	while(!gShouldStop){
-		sendMessage(networkObject);
-		receiveMessage(networkObject);
-		usleep(1000);
+void transmitReceiveAudio(){ //transmit and receive audio buffers
+	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;
+		}
 	}
-	closeSockets();
 }
 
 // setup() is called once before the audio rendering starts.
@@ -42,14 +60,25 @@
 	gInverseSampleRate = 1.0 / context->audioSampleRate;
 	gPhase = 0.0;
 
-	networkObject.counter=&gCount;
-	networkObject.variables[0]=&gFrequency;
-	networkObject.variables[1]=&gPhase;
-	networkObject.numVariables=2;
-// setupSockets(settings->receivePort, settings->transmitPort, settings->serverName);
-	setupSockets(10000, 9999, "127.0.0.1");
-	transmitReceiveDataTask= BeagleRT_createAuxiliaryTask(*transmitReceiveData, 80, "transmit-receive-data");
-	//scheduleAuxiliaryTask(transmitReceiveDataTask); //here it does not work
+//	networkObject.counter=&gCount;
+//	networkObject.variables[0]=&gFrequency;
+//	networkObject.variables[1]=&gPhase;
+//	networkObject.numVariables=2;
+	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);
+		netAudio[n].udpClient.setPort(9999+n);
+		netAudio[n].udpClient.setServer("192.168.7.1");
+	}
+//	setupSockets(settings->receivePort, settings->transmitPort, settings->serverName);
+
+//	transmitReceiveDataTask=createAuxiliaryTask(*transmitReceiveData, 10, "transmit-receive-data");
+//	scheduleAuxiliaryTask(transmitReceiveDataTask); //here it does not work
+	transmitReceiveAudioTask=BeagleRT_createAuxiliaryTask(*transmitReceiveAudio, 98, "transmit-receive-audio");
 	return true;
 }
 
@@ -59,7 +88,7 @@
 // will be 0.
 
 void render(BeagleRTContext *context, void *userData)
-{
+{/*
 	for(unsigned int n = 0; n < context->audioFrames; n++) {
 		float out = 0.7f * sinf(gPhase);
 		gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate;
@@ -74,6 +103,43 @@
 		}
 		gCount++;
 	}
+
+
+*/
+	for(int n = 0; n < context->audioFrames; n++) {
+		float out = 0.7f * sinf(gPhase);
+		gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate;
+		if(gPhase > 2.0 * M_PI)
+			gPhase -= 2.0 * M_PI;
+
+//		for(int channel = 0; channel < context->audioChannels; channel++)
+//			context->audioOut[n * context->audioChannels + channel] = context->audioIn[n * context->audioChannels + 0]+context->audioIn[n * context->audioChannels + 1];
+		context->audioOut[n * context->audioChannels] = context->audioIn[n*context->audioChannels+0];
+		context->audioOut[n * context->audioChannels+1]=out;
+		if(0==gCount){
+//			scheduleAuxiliaryTask(transmitReceiveDataTask);
+		}
+		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;
+				BeagleRT_scheduleAuxiliaryTask(transmitReceiveAudioTask); //send the buffer
+			}
+		}
+		if((gCount&1)==0){
+			netAudio[1].buffers[netAudio[1].currentBuffer][netAudio[1].index++]=analogReadFrame(context,n/2,0)+context->audioOut[n*context->audioChannels + 0];
+			netAudio[2].buffers[netAudio[2].currentBuffer][netAudio[2].index++]=analogReadFrame(context,n/2,1)+context->audioOut[n*context->audioChannels + 0];
+		}
+		netAudio[0].buffers[netAudio[0].currentBuffer][netAudio[0].index++]=0.5*(out+context->audioOut[n*context->audioChannels + 0]);//copy channel 0 to the buffer
+//		netAudio[1].buffers[netAudio[1].currentBuffer][netAudio[1].index++]=0.5*(out+context->audioOut[n*context->audioChannels + 0]);
+//		netAudio[2].buffers[netAudio[2].currentBuffer][netAudio[2].index++]=0.5*(out+context->audioOut[n*context->audioChannels + 0]);
+		gCount++;
+	}
 }
 
 // cleanup() is called once at the end, after the audio has stopped.