comparison projects/scope/render.cpp @ 152:8f98b32d0e23 ClockSync

Last commit on this branch for a while. Overall not very succesful
author Giulio Moro <giuliomoro@yahoo.it>
date Mon, 05 Oct 2015 13:06:14 +0100
parents e9c9404e3d1f
children
comparison
equal deleted inserted replaced
151:e9c9404e3d1f 152:8f98b32d0e23
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 #include <ClockSyncThread.h>
7 #include <Pid.h>
7 8
8 float gPhase1, gPhase2; 9 float gPhase1, gPhase2;
9 float gFrequency1, gFrequency2; 10 float gFrequency1, gFrequency2;
10 float gInverseSampleRate; 11 float gInverseSampleRate;
11 12
54 gInverseSampleRate = 1.0/context->audioSampleRate; 55 gInverseSampleRate = 1.0/context->audioSampleRate;
55 56
56 gPhase1 = 0.0; 57 gPhase1 = 0.0;
57 gPhase2 = 0.0; 58 gPhase2 = 0.0;
58 59
59 gFrequency1 = 200.0; 60 gFrequency1 = 441.0;
60 gFrequency2 = 201.0; 61 gFrequency2 = 201.0;
61 62
62 // testTime=BeagleRT_createAuxiliaryTask(testTimeFunction, 80, "testTimeTask"); 63 // testTime=BeagleRT_createAuxiliaryTask(testTimeFunction, 80, "testTimeTask");
63 return true; 64 return true;
64 } 65 }
65 66
66 // render() is called regularly at the highest priority by the audio engine. 67 // render() is called regularly at the highest priority by the audio engine.
67 // Input and output are given from the audio hardware and the other 68 // Input and output are given from the audio hardware and the other
68 // ADCs and DACs (if available). If only audio is available, numMatrixFrames 69 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
69 // will be 0. 70 // will be 0.
70 71 extern Pid* gClockSyncPid;
72 extern ClockSync* gClockSync;
71 void render(BeagleRTContext *context, void *userData) 73 void render(BeagleRTContext *context, void *userData)
72 { 74 {
73 virtualClock.sync(context->audioFrames); 75 virtualClock.sync(context->audioFrames);
74 static int count=0; 76 static int count=0;
75 if(count==0){ 77 if(count==0){
99 */ 101 */
100 static float phase=0; 102 static float phase=0;
101 float phaseInc=gFrequency1/44100.0*2*M_PI; 103 float phaseInc=gFrequency1/44100.0*2*M_PI;
102 // rt_printf("phaseInc: %f, phase: %f\n",phaseInc,phase); 104 // rt_printf("phaseInc: %f, phase: %f\n",phaseInc,phase);
103 for(unsigned int n=0; n<context->audioFrames; n++){ 105 for(unsigned int n=0; n<context->audioFrames; n++){
104 context->audioOut[n*2]=sinf(phase);//context->audioIn[n*2]; 106 context->audioOut[n*2]=sinf(phase) > 0 ? 1 : -1;
105 phase+=200.0/44100.0*2*M_PI; 107 context->audioOut[n*2+1]=sinf(phase) > 0 ? 1 : -1;
108 // context->audioOut[n*2]= context->audioIn[n*2];
109 // context->audioOut[n*2+1]= context->audioIn[n*2+1];
110 phase+=phaseInc;
106 if(phase>=2*M_PI) 111 if(phase>=2*M_PI)
107 phase-=2*M_PI; 112 phase-=2*M_PI;
108 context->audioOut[n*2+1]=rand()/(float)RAND_MAX;//context->audioIn[n*2]; 113 // context->audioOut[n*2+1]=rand()/(float)RAND_MAX;//context->audioIn[n*2];
109 } 114 }
115 gClockSyncPid->setProportionalGain(context->analogIn[0]*10);
116 // gClockSyncPid->setIntegralGain(context->analogIn[1]);
117 gClockSyncPid->setIntegralGain(0);
118 // gClockSyncPid->setDerivativeGain(context->analogIn[2]);
119 gClockSyncPid->setDerivativeGain(0);
120 gClockSyncPid->setGlobalGain(context->analogIn[3]*1/0.828);
121 for (int n = 0; n < context->analogFrames; n++){
122 context->analogOut[n*context->analogChannels + 0] = (gClockSync->getOffset() )/ 100 + 0.5;
123 context->analogOut[n*context->analogChannels + 1] = (gAudioCodec->getAudioSamplingRate()-44100)/20 + 0.5;
124 }
125 static bool pastSwitch = false;
126 bool thisSwitch = context->analogIn[4]>0.5;
127 if(thisSwitch && pastSwitch == false){
128 rt_printf("--------------reset offset\n");
129 virtualClock.addOffset(gClockSync->getIirOffset());
130 }
131 pastSwitch = thisSwitch;
110 count++; 132 count++;
111 /* 133 /*
112 // if((count&262143)==0){ 134 // if((count&262143)==0){
113 // static int nextCall=160000; 135 // static int nextCall=160000;
114 if( ((count&(2047))==0)){ 136 if( ((count&(2047))==0)){