diff projects/scope/render.cpp @ 132:e24c531220ee scope-refactoring

Added some sort of synchronization, not working great though
author Giulio Moro <giuliomoro@yahoo.it>
date Thu, 27 Aug 2015 01:42:04 +0100
parents ff28e56e5b7e
children 04b1678614c9
line wrap: on
line diff
--- a/projects/scope/render.cpp	Wed Aug 26 02:02:10 2015 +0100
+++ b/projects/scope/render.cpp	Thu Aug 27 01:42:04 2015 +0100
@@ -1,6 +1,7 @@
 #include <BeagleRT.h> 
 #include <NetworkSend.h>
 #include <ReceiveAudioThread.h>
+#include <ClockSynchronizer.h>
 #include <cmath>
 
 float gPhase1, gPhase2;
@@ -20,6 +21,8 @@
 // Return true on success; returning false halts the program.
 ReceiveAudioThread receiveAudio0;
 //ReceiveAudioThread receiveAudio1;
+ClockSynchronizer clockSynchronizer;
+extern I2c_Codec* gAudioCodec;
 bool setup(BeagleRTContext *context, void *userData)
 {
 	receiveAudio0.init(10000, context->audioFrames, 0);
@@ -29,7 +32,7 @@
 //	scope.setPort(0, 9999);
 //	scope.setPort(1, 10000);
 	networkSend.setup(context->audioSampleRate, context->audioFrames, 0, 9999, "192.168.7.1");
-	 
+	clockSynchronizer.setup();
 	gInverseSampleRate = 1.0/context->audioSampleRate;
 	
 	gPhase1 = 0.0;
@@ -49,7 +52,19 @@
 void render(BeagleRTContext *context, void *userData)
 {
 	static int count=0;
+//	if((count&262143)==0){
+//	static int nextCall=160000;
+	if( ((count&(16384-1))==0 /*&& count>200000*/)){
+//		rt_printf("b %d\n", count);
+		clockSynchronizer.update(networkSend.getTimestamp(), receiveAudio0.getTimestamp(), receiveAudio0.getLastTime());
+//		nextCall=count+100000;
+//		rt_printf("a %d\n", count);
+	}
+//	if(count == nextCall){
+//		clockSynchronizer.update(networkSend.getTimestamp(), receiveAudio0.getTimestamp(), receiveAudio0.getLastTime());
+//	}
 	if(count==0){
+		gAudioCodec->setAudioSamplingRate(44080);
 		printf("startHread\n");
 		ReceiveAudioThread::startThread();
 	}
@@ -63,8 +78,8 @@
 
     //  float chn4 = context->analogIn[(int)n/2*8 + 0];
     //  float chn5 = context->analogIn[(int)n/2*8 + 1];
-		networkSend.log(context->audioIn[n]);
-//		networkSend.log(chn0);
+//		networkSend.log(context->audioIn[n]);
+		networkSend.log(chn0);
 //		scope.log(0, chn0);
 //		scope.log(1, chn1);
 		//  scope.log(2, chn2);
@@ -79,13 +94,13 @@
 	    //to view, click the 'oscilloscope' button on the toolbar while BeagleRT is NOT running
 	    //then click the big red button on the toolbar on this page
 	    
-	    gPhase1 += 2.0 * M_PI * gFrequency1 * gInverseSampleRate * ((count&4095)/4096.0+1);
+	    gPhase1 += 2.0 * M_PI * gFrequency1 * gInverseSampleRate * ((count&65535)/65535.0+1);
 			gPhase2 += 2.0 * M_PI * gFrequency2 * gInverseSampleRate;
 		if(gPhase1 > 2.0 * M_PI)
 			gPhase1 -= 2.0 * M_PI;
 		if(gPhase2 > 2.0 * M_PI)
 			gPhase2 -= 2.0 * M_PI;
-		
+		count++;
 	}
 	if(count>0){
 		float samplingRateRatio=1;
@@ -93,15 +108,14 @@
 		int channelToWriteTo=0;
 		int length=receiveAudio0.getSamplesSrc(context->audioOut, context->audioFrames,
 				samplingRateRatio, channelsInDestinationBuffer, channelToWriteTo);
-		if(length!=context->audioFrames){
+		if((unsigned int)length!=context->audioFrames){
 			rt_printf("Length mismatch: %d\n", length);
 		}
 //		int readPointer1=receiveAudio1.getSamplesSrc(context->audioOut, context->audioFrames, 1, 2, 1);
 	}
-	for(int n=0; n<context->audioFrames; n++){
+	for(unsigned int n=0; n<context->audioFrames; n++){
 		context->audioOut[n*2+1]=context->audioOut[n*2];
 	}
-	count++;
 }
 
 // cleanup_render() is called once at the end, after the audio has stopped.