andrewm@71
|
1 //scope.cpp
|
andrewm@71
|
2 #include <BeagleRT.h>
|
andrewm@71
|
3 #include <rtdk.h>
|
andrewm@71
|
4 #include <cmath>
|
andrewm@71
|
5 #include <UdpClient.h>
|
andrewm@71
|
6
|
giuliomoro@107
|
7 #define BUILD_FOR_UDPRECEIVE_PLUGIN
|
giuliomoro@107
|
8 #define NETWORK_AUDIO_BUFFER_SIZE 302
|
andrewm@71
|
9 struct networkAudio{
|
andrewm@71
|
10 int timestamp;
|
andrewm@71
|
11 int currentBuffer;
|
andrewm@71
|
12 int index;
|
andrewm@71
|
13 float buffers[2][NETWORK_AUDIO_BUFFER_SIZE];
|
andrewm@71
|
14 int doneOnTime;
|
andrewm@71
|
15 bool toBeSent;
|
andrewm@71
|
16 UdpClient udpClient;
|
andrewm@71
|
17 };
|
andrewm@71
|
18
|
andrewm@71
|
19 #define NUM_SCOPE_CHANNELS 6
|
andrewm@71
|
20
|
andrewm@71
|
21 static void SendScopeData();
|
andrewm@71
|
22
|
andrewm@71
|
23 class Scope {
|
andrewm@71
|
24 int sampleCount;
|
andrewm@71
|
25 float sampleRate;
|
andrewm@71
|
26 AuxiliaryTask scopeTask;
|
andrewm@71
|
27 public:
|
andrewm@71
|
28 int numChannels;
|
andrewm@71
|
29 networkAudio channel[NUM_SCOPE_CHANNELS];
|
andrewm@71
|
30 Scope(){
|
andrewm@71
|
31 numChannels = NUM_SCOPE_CHANNELS;
|
andrewm@71
|
32 sampleCount = 0;
|
giuliomoro@107
|
33 #ifdef BUILD_FOR_UDPRECEIVE_PLUGIN
|
giuliomoro@107
|
34 char server[]="192.168.7.1";
|
giuliomoro@107
|
35 #else
|
giuliomoro@107
|
36 char server[]="127.0.0.1";
|
giuliomoro@107
|
37 #endif /* BUILD_FOR_UDPRECEIVE_PLUGIN */
|
giuliomoro@107
|
38 printf("Sending messages to : %s\n", server);
|
andrewm@71
|
39 for(int n=0; n<numChannels; n++){
|
andrewm@71
|
40 channel[n].doneOnTime=1;
|
giuliomoro@93
|
41 channel[n].index=2; //leave space for the heading message (channel, timestamp)
|
andrewm@71
|
42 channel[n].timestamp=0;
|
andrewm@71
|
43 channel[n].currentBuffer=0;
|
andrewm@71
|
44 channel[n].toBeSent=false;
|
giuliomoro@107
|
45 channel[n].udpClient.setServer(server);
|
giuliomoro@93
|
46 #ifdef BUILD_FOR_UDPRECEIVE_PLUGIN
|
giuliomoro@93
|
47 channel[n].udpClient.setPort(9999+n);
|
giuliomoro@93
|
48 #else
|
andrewm@71
|
49 channel[n].udpClient.setPort(9999);
|
giuliomoro@93
|
50 #endif /* BUILD_FOR_UDPRECEIVE_PLUGIN */
|
andrewm@71
|
51 }
|
andrewm@71
|
52 }
|
andrewm@71
|
53 void setup(float _sampleRate);
|
andrewm@71
|
54 void log(float channel1=0.0, float channel2=0.0, float channel3=0.0, float channel4=0.0, float channel5=0.0, float channel6=0.0){
|
andrewm@71
|
55
|
andrewm@71
|
56 for(int j=0; j<numChannels; j++){
|
andrewm@71
|
57 if(channel[j].index==(NETWORK_AUDIO_BUFFER_SIZE)){ // when the buffer is ready ...
|
andrewm@71
|
58 channel[j].buffers[channel[j].currentBuffer][0] = (float)j;
|
andrewm@71
|
59 channel[j].buffers[channel[j].currentBuffer][1] = (float)channel[j].timestamp;
|
andrewm@71
|
60 channel[j].toBeSent=true;
|
andrewm@71
|
61 channel[j].index=2; //reset the counter
|
andrewm@71
|
62 if(channel[j].doneOnTime==0)
|
andrewm@71
|
63 rt_printf("Network buffer underrun :-{\n");
|
andrewm@71
|
64 channel[j].timestamp=sampleCount;
|
andrewm@71
|
65 channel[j].currentBuffer=!channel[j].currentBuffer; //switch buffer
|
andrewm@71
|
66 channel[j].doneOnTime=0;
|
andrewm@71
|
67 BeagleRT_scheduleAuxiliaryTask(scopeTask); //send the buffer
|
andrewm@71
|
68 }
|
andrewm@71
|
69 }
|
andrewm@71
|
70
|
andrewm@71
|
71 channel[0].buffers[channel[0].currentBuffer][channel[0].index++]=channel1;
|
andrewm@71
|
72 channel[1].buffers[channel[1].currentBuffer][channel[1].index++]=channel2;
|
andrewm@71
|
73 channel[2].buffers[channel[2].currentBuffer][channel[2].index++]=channel3;
|
andrewm@71
|
74 channel[3].buffers[channel[3].currentBuffer][channel[3].index++]=channel4;
|
andrewm@71
|
75 channel[4].buffers[channel[4].currentBuffer][channel[4].index++]=channel5;
|
andrewm@71
|
76 channel[5].buffers[channel[5].currentBuffer][channel[5].index++]=channel6;
|
andrewm@71
|
77
|
andrewm@71
|
78 sampleCount++;
|
andrewm@71
|
79 }
|
andrewm@71
|
80 };
|
andrewm@71
|
81
|
andrewm@71
|
82 Scope* gOscilloscopeInstance;
|
andrewm@71
|
83
|
andrewm@71
|
84 void Scope::setup(float _sampleRate){
|
andrewm@71
|
85 sampleRate = _sampleRate;
|
andrewm@71
|
86 gOscilloscopeInstance = this;
|
andrewm@71
|
87 scopeTask = BeagleRT_createAuxiliaryTask(*SendScopeData, 98, "transmit-receive-audio");
|
andrewm@71
|
88 }
|
andrewm@71
|
89
|
andrewm@71
|
90 //Scope scope;
|
andrewm@71
|
91
|
andrewm@71
|
92 static void SendScopeData(){
|
andrewm@71
|
93 for(int n=0; n<gOscilloscopeInstance->numChannels; n++){
|
andrewm@71
|
94 if(gOscilloscopeInstance->channel[n].toBeSent){
|
andrewm@71
|
95 gOscilloscopeInstance->channel[n].toBeSent=false;
|
giuliomoro@93
|
96 gOscilloscopeInstance->channel[n].udpClient.send(
|
giuliomoro@93
|
97 gOscilloscopeInstance->channel[n].buffers[!gOscilloscopeInstance->channel[n].currentBuffer],
|
giuliomoro@93
|
98 NETWORK_AUDIO_BUFFER_SIZE*sizeof(float)
|
giuliomoro@93
|
99 );
|
andrewm@71
|
100 gOscilloscopeInstance->channel[n].doneOnTime=1;
|
andrewm@71
|
101 }
|
andrewm@71
|
102 }
|
andrewm@71
|
103 }
|