comparison include/Scope.h @ 71:53e57276ac1a

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