Mercurial > hg > beaglert
comparison 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 |
comparison
equal
deleted
inserted
replaced
131:ff28e56e5b7e | 132:e24c531220ee |
---|---|
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 <cmath> | 5 #include <cmath> |
5 | 6 |
6 float gPhase1, gPhase2; | 7 float gPhase1, gPhase2; |
7 float gFrequency1, gFrequency2; | 8 float gFrequency1, gFrequency2; |
8 float gInverseSampleRate; | 9 float gInverseSampleRate; |
18 // in from the call to initAudio(). | 19 // in from the call to initAudio(). |
19 // | 20 // |
20 // Return true on success; returning false halts the program. | 21 // Return true on success; returning false halts the program. |
21 ReceiveAudioThread receiveAudio0; | 22 ReceiveAudioThread receiveAudio0; |
22 //ReceiveAudioThread receiveAudio1; | 23 //ReceiveAudioThread receiveAudio1; |
24 ClockSynchronizer clockSynchronizer; | |
25 extern I2c_Codec* gAudioCodec; | |
23 bool setup(BeagleRTContext *context, void *userData) | 26 bool setup(BeagleRTContext *context, void *userData) |
24 { | 27 { |
25 receiveAudio0.init(10000, context->audioFrames, 0); | 28 receiveAudio0.init(10000, context->audioFrames, 0); |
26 // receiveAudio1.init(10000, context->audioFrames, 1); | 29 // receiveAudio1.init(10000, context->audioFrames, 1); |
27 | 30 |
28 // scope.setup(); //call this once in setup to initialise the scope | 31 // scope.setup(); //call this once in setup to initialise the scope |
29 // scope.setPort(0, 9999); | 32 // scope.setPort(0, 9999); |
30 // scope.setPort(1, 10000); | 33 // scope.setPort(1, 10000); |
31 networkSend.setup(context->audioSampleRate, context->audioFrames, 0, 9999, "192.168.7.1"); | 34 networkSend.setup(context->audioSampleRate, context->audioFrames, 0, 9999, "192.168.7.1"); |
32 | 35 clockSynchronizer.setup(); |
33 gInverseSampleRate = 1.0/context->audioSampleRate; | 36 gInverseSampleRate = 1.0/context->audioSampleRate; |
34 | 37 |
35 gPhase1 = 0.0; | 38 gPhase1 = 0.0; |
36 gPhase2 = 0.0; | 39 gPhase2 = 0.0; |
37 | 40 |
47 // will be 0. | 50 // will be 0. |
48 | 51 |
49 void render(BeagleRTContext *context, void *userData) | 52 void render(BeagleRTContext *context, void *userData) |
50 { | 53 { |
51 static int count=0; | 54 static int count=0; |
55 // if((count&262143)==0){ | |
56 // static int nextCall=160000; | |
57 if( ((count&(16384-1))==0 /*&& count>200000*/)){ | |
58 // rt_printf("b %d\n", count); | |
59 clockSynchronizer.update(networkSend.getTimestamp(), receiveAudio0.getTimestamp(), receiveAudio0.getLastTime()); | |
60 // nextCall=count+100000; | |
61 // rt_printf("a %d\n", count); | |
62 } | |
63 // if(count == nextCall){ | |
64 // clockSynchronizer.update(networkSend.getTimestamp(), receiveAudio0.getTimestamp(), receiveAudio0.getLastTime()); | |
65 // } | |
52 if(count==0){ | 66 if(count==0){ |
67 gAudioCodec->setAudioSamplingRate(44080); | |
53 printf("startHread\n"); | 68 printf("startHread\n"); |
54 ReceiveAudioThread::startThread(); | 69 ReceiveAudioThread::startThread(); |
55 } | 70 } |
56 for(unsigned int n = 0; n < context->audioFrames; n++) { | 71 for(unsigned int n = 0; n < context->audioFrames; n++) { |
57 | 72 |
61 // float chn2 = context->audioIn[n*2 + 0]; | 76 // float chn2 = context->audioIn[n*2 + 0]; |
62 // float chn3 = context->audioIn[n*2 + 1]; | 77 // float chn3 = context->audioIn[n*2 + 1]; |
63 | 78 |
64 // float chn4 = context->analogIn[(int)n/2*8 + 0]; | 79 // float chn4 = context->analogIn[(int)n/2*8 + 0]; |
65 // float chn5 = context->analogIn[(int)n/2*8 + 1]; | 80 // float chn5 = context->analogIn[(int)n/2*8 + 1]; |
66 networkSend.log(context->audioIn[n]); | 81 // networkSend.log(context->audioIn[n]); |
67 // networkSend.log(chn0); | 82 networkSend.log(chn0); |
68 // scope.log(0, chn0); | 83 // scope.log(0, chn0); |
69 // scope.log(1, chn1); | 84 // scope.log(1, chn1); |
70 // scope.log(2, chn2); | 85 // scope.log(2, chn2); |
71 // scope.log(3, chn3); | 86 // scope.log(3, chn3); |
72 // scope.log(4, chn4); | 87 // scope.log(4, chn4); |
77 //takes six or fewer floats as parameters | 92 //takes six or fewer floats as parameters |
78 //first parameter becomes channel 1 etc | 93 //first parameter becomes channel 1 etc |
79 //to view, click the 'oscilloscope' button on the toolbar while BeagleRT is NOT running | 94 //to view, click the 'oscilloscope' button on the toolbar while BeagleRT is NOT running |
80 //then click the big red button on the toolbar on this page | 95 //then click the big red button on the toolbar on this page |
81 | 96 |
82 gPhase1 += 2.0 * M_PI * gFrequency1 * gInverseSampleRate * ((count&4095)/4096.0+1); | 97 gPhase1 += 2.0 * M_PI * gFrequency1 * gInverseSampleRate * ((count&65535)/65535.0+1); |
83 gPhase2 += 2.0 * M_PI * gFrequency2 * gInverseSampleRate; | 98 gPhase2 += 2.0 * M_PI * gFrequency2 * gInverseSampleRate; |
84 if(gPhase1 > 2.0 * M_PI) | 99 if(gPhase1 > 2.0 * M_PI) |
85 gPhase1 -= 2.0 * M_PI; | 100 gPhase1 -= 2.0 * M_PI; |
86 if(gPhase2 > 2.0 * M_PI) | 101 if(gPhase2 > 2.0 * M_PI) |
87 gPhase2 -= 2.0 * M_PI; | 102 gPhase2 -= 2.0 * M_PI; |
88 | 103 count++; |
89 } | 104 } |
90 if(count>0){ | 105 if(count>0){ |
91 float samplingRateRatio=1; | 106 float samplingRateRatio=1; |
92 int channelsInDestinationBuffer=2; | 107 int channelsInDestinationBuffer=2; |
93 int channelToWriteTo=0; | 108 int channelToWriteTo=0; |
94 int length=receiveAudio0.getSamplesSrc(context->audioOut, context->audioFrames, | 109 int length=receiveAudio0.getSamplesSrc(context->audioOut, context->audioFrames, |
95 samplingRateRatio, channelsInDestinationBuffer, channelToWriteTo); | 110 samplingRateRatio, channelsInDestinationBuffer, channelToWriteTo); |
96 if(length!=context->audioFrames){ | 111 if((unsigned int)length!=context->audioFrames){ |
97 rt_printf("Length mismatch: %d\n", length); | 112 rt_printf("Length mismatch: %d\n", length); |
98 } | 113 } |
99 // int readPointer1=receiveAudio1.getSamplesSrc(context->audioOut, context->audioFrames, 1, 2, 1); | 114 // int readPointer1=receiveAudio1.getSamplesSrc(context->audioOut, context->audioFrames, 1, 2, 1); |
100 } | 115 } |
101 for(int n=0; n<context->audioFrames; n++){ | 116 for(unsigned int n=0; n<context->audioFrames; n++){ |
102 context->audioOut[n*2+1]=context->audioOut[n*2]; | 117 context->audioOut[n*2+1]=context->audioOut[n*2]; |
103 } | 118 } |
104 count++; | |
105 } | 119 } |
106 | 120 |
107 // cleanup_render() is called once at the end, after the audio has stopped. | 121 // cleanup_render() is called once at the end, after the audio has stopped. |
108 // Release any resources that were allocated in initialise_render(). | 122 // Release any resources that were allocated in initialise_render(). |
109 | 123 |