comparison projects/scope/render.cpp @ 135:e77e2e712fbc ClockSync

To work with the ClockSync plugin
author Giulio Moro <giuliomoro@yahoo.it>
date Sat, 12 Sep 2015 20:05:55 +0100
parents 04b1678614c9
children 44d07fa9bd03
comparison
equal deleted inserted replaced
133:04b1678614c9 135:e77e2e712fbc
1 #include <BeagleRT.h> 1 #include <BeagleRT.h>
2 #include <NetworkSend.h> 2 #include <NetworkSend.h>
3 #include <ReceiveAudioThread.h> 3 #include <ReceiveAudioThread.h>
4 #include <ClockSynchronizer.h> 4 #include <ClockSynchronizer.h>
5 #include <cmath> 5 #include <cmath>
6 #include <ClockSyncThread.h>
6 7
7 float gPhase1, gPhase2; 8 float gPhase1, gPhase2;
8 float gFrequency1, gFrequency2; 9 float gFrequency1, gFrequency2;
9 float gInverseSampleRate; 10 float gInverseSampleRate;
10 11
11 //Scope scope(2); //create a scope object with 2 channels 12 //Scope scope(2); //create a scope object with 2 channels
12 NetworkSend networkSend; 13 //NetworkSend networkSend;
13 14
14 // initialise_render() is called once before the audio rendering starts. 15 // initialise_render() is called once before the audio rendering starts.
15 // Use it to perform any initialisation and allocation which is dependent 16 // Use it to perform any initialisation and allocation which is dependent
16 // on the period size or sample rate. 17 // on the period size or sample rate.
17 // 18 //
18 // userData holds an opaque pointer to a data structure that was passed 19 // userData holds an opaque pointer to a data structure that was passed
19 // in from the call to initAudio(). 20 // in from the call to initAudio().
20 // 21 //
21 // Return true on success; returning false halts the program. 22 // Return true on success; returning false halts the program.
22 ReceiveAudioThread receiveAudio0; 23 //ReceiveAudioThread receiveAudio0;
23 //ReceiveAudioThread receiveAudio1; 24 //ReceiveAudioThread receiveAudio1;
24 ClockSynchronizer clockSynchronizer; 25 ClockSynchronizer clockSynchronizer;
25 extern I2c_Codec* gAudioCodec; 26 extern I2c_Codec* gAudioCodec;
27 VirtualClock virtualClock;
28 ClockSyncThread clockSyncThread;
26 bool setup(BeagleRTContext *context, void *userData) 29 bool setup(BeagleRTContext *context, void *userData)
27 { 30 {
28 receiveAudio0.init(10000, context->audioFrames, 0); 31 // receiveAudio0.init(10000, context->audioFrames, 0);
29 // receiveAudio1.init(10000, context->audioFrames, 1); 32 // receiveAudio1.init(10000, context->audioFrames, 1);
30 33
31 // scope.setup(); //call this once in setup to initialise the scope 34 // scope.setup(); //call this once in setup to initialise the scope
32 // scope.setPort(0, 9999); 35 // scope.setPort(0, 9999);
33 // scope.setPort(1, 10000); 36 // scope.setPort(1, 10000);
34 networkSend.setup(context->audioSampleRate, context->audioFrames, 0, 9999, "192.168.7.1"); 37 // networkSend.setup(context->audioSampleRate, context->audioFrames, 0, 9999, "192.168.7.1");
35 clockSynchronizer.setup(); 38 clockSynchronizer.setup();
39 virtualClock.init();
40 clockSyncThread.init(true, 5000, virtualClock); //start as slave
36 gInverseSampleRate = 1.0/context->audioSampleRate; 41 gInverseSampleRate = 1.0/context->audioSampleRate;
37 42
38 gPhase1 = 0.0; 43 gPhase1 = 0.0;
39 gPhase2 = 0.0; 44 gPhase2 = 0.0;
40 45
49 // ADCs and DACs (if available). If only audio is available, numMatrixFrames 54 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
50 // will be 0. 55 // will be 0.
51 56
52 void render(BeagleRTContext *context, void *userData) 57 void render(BeagleRTContext *context, void *userData)
53 { 58 {
59 virtualClock.sync(context->audioFrames);
54 static int count=0; 60 static int count=0;
61 if(count==0)
62 clockSyncThread.startThread();
63 static float phase=0;
64 float phaseInc=200.0/44100.0*2*M_PI;
65 // rt_printf("phaseInc: %f, phase: %f\n",phaseInc,phase);
66 for(unsigned int n=0; n<context->audioFrames; n++){
67 context->audioOut[n*2]=sinf(phaseInc);//context->audioIn[n*2];
68 context->audioOut[n*2+1]=sinf(phaseInc);//context->audioIn[n*2];
69 phase+=200.0/44100.0*2*M_PI;
70 if(phase>=2*M_PI)
71 phase-=2*M_PI;
72 // context->audioOut[n*2+1]=rand()/(float)RAND_MAX;context->audioIn[n*2];
73 }
74 count++;
75 /*
55 // if((count&262143)==0){ 76 // if((count&262143)==0){
56 // static int nextCall=160000; 77 // static int nextCall=160000;
57 if( ((count&(2047-1))==0 /*&& count>200000*/)){ 78 if( ((count&(2047))==0)){
58 // rt_printf("b %d\n", count); 79 // rt_printf("b %d\n", count);
59 clockSynchronizer.update(networkSend.getTimestamp(), receiveAudio0.getTimestamp(), receiveAudio0.getLastTime()); 80 clockSynchronizer.update(networkSend.getTimestamp(), receiveAudio0.getTimestamp(), receiveAudio0.getLastTime());
60 // nextCall=count+100000; 81 // nextCall=count+100000;
61 // rt_printf("a %d\n", count); 82 // rt_printf("a %d\n", count);
62 } 83 }
63 // if(count == nextCall){ 84 // if(count == nextCall){
64 // clockSynchronizer.update(networkSend.getTimestamp(), receiveAudio0.getTimestamp(), receiveAudio0.getLastTime()); 85 // clockSynchronizer.update(networkSend.getTimestamp(), receiveAudio0.getTimestamp(), receiveAudio0.getLastTime());
65 // } 86 // }
66 if(count==0){ 87 if(count==0){
67 gAudioCodec->setAudioSamplingRate( 44101); 88 gAudioCodec->setAudioSamplingRate( 44100);
68 rt_printf("startHread\n"); 89 rt_printf("startHread\n");
69 ReceiveAudioThread::startThread(); 90 ReceiveAudioThread::startThread();
70 } 91 }
71 for(unsigned int n = 0; n < context->audioFrames; n++) { 92 for(unsigned int n = 0; n < context->audioFrames; n++) {
72 93
98 gPhase2 += 2.0 * M_PI * gFrequency2 * gInverseSampleRate; 119 gPhase2 += 2.0 * M_PI * gFrequency2 * gInverseSampleRate;
99 if(gPhase1 > 2.0 * M_PI) 120 if(gPhase1 > 2.0 * M_PI)
100 gPhase1 -= 2.0 * M_PI; 121 gPhase1 -= 2.0 * M_PI;
101 if(gPhase2 > 2.0 * M_PI) 122 if(gPhase2 > 2.0 * M_PI)
102 gPhase2 -= 2.0 * M_PI; 123 gPhase2 -= 2.0 * M_PI;
124 int value=count%1000;
125 context->audioOut[n*2]=value>=500 ? 1 : -1;
126 context->audioOut[n*2+1]=context->audioOut[n*2];
103 count++; 127 count++;
104 } 128 }
105 if(count>0){ 129 if(count>0){
106 float samplingRateRatio=1; 130 float samplingRateRatio=1;
107 int channelsInDestinationBuffer=2; 131 int channelsInDestinationBuffer=2;
108 int channelToWriteTo=0; 132 int channelToWriteTo=1;
109 int length=receiveAudio0.getSamplesSrc(context->audioOut, context->audioFrames, 133 int length=receiveAudio0.getSamplesSrc(context->audioOut, context->audioFrames,
110 samplingRateRatio, channelsInDestinationBuffer, channelToWriteTo); 134 samplingRateRatio, channelsInDestinationBuffer, channelToWriteTo);
111 if((unsigned int)length!=context->audioFrames){ 135 if((unsigned int)length!=context->audioFrames){
112 rt_printf("Length mismatch: %d\n", length); 136 rt_printf("Length mismatch: %d\n", length);
113 } 137 }
114 // int readPointer1=receiveAudio1.getSamplesSrc(context->audioOut, context->audioFrames, 1, 2, 1); 138 // int readPointer1=receiveAudio1.getSamplesSrc(context->audioOut, context->audioFrames, 1, 2, 1);
115 } 139 }
116 for(unsigned int n=0; n<context->audioFrames; n++){ 140 for(unsigned int n=0; n<context->audioFrames; n++){
117 context->audioOut[n*2+1]=context->audioOut[n*2]; 141 // context->audioOut[n*2+1]=context->audioOut[n*2];
118 } 142 }
143 */
144
119 } 145 }
120 146
121 // cleanup_render() is called once at the end, after the audio has stopped. 147 // cleanup_render() is called once at the end, after the audio has stopped.
122 // Release any resources that were allocated in initialise_render(). 148 // Release any resources that were allocated in initialise_render().
123 149