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