Mercurial > hg > beaglert
comparison projects/basic_network/render.cpp @ 92:a55dcdcebdcd
Recovered older basic_network project which got lost in the API update
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Tue, 21 Jul 2015 17:17:37 +0100 |
parents | 7cc79d46ecb9 |
children | 3068421c0737 dbff109f64c2 |
comparison
equal
deleted
inserted
replaced
91:241d4d5df929 | 92:a55dcdcebdcd |
---|---|
4 * Created on: Oct 24, 2014 | 4 * Created on: Oct 24, 2014 |
5 * Author: parallels | 5 * Author: parallels |
6 */ | 6 */ |
7 | 7 |
8 #include <BeagleRT.h> | 8 #include <BeagleRT.h> |
9 //#include <rtdk.h> | |
9 #include <cmath> | 10 #include <cmath> |
10 #include <client.h> | 11 #include <UdpClient.h> |
12 #include <Utilities.h> | |
13 | |
14 AuxiliaryTask transmitReceiveDataTask; | |
15 | |
16 #define NETWORK_AUDIO_BUFFER_SIZE 400 //1400/4 //maximum payload for a UDP datagram over ethernet is 1472 bytes, I leave some headroom and divide by 4 to get the number of floats | |
17 struct networkAudio{ | |
18 int timestamp; | |
19 int currentBuffer; | |
20 int index; | |
21 float buffers[2][NETWORK_AUDIO_BUFFER_SIZE]; | |
22 int doneOnTime; | |
23 bool toBeSent; | |
24 UdpClient udpClient; | |
25 }; | |
11 | 26 |
12 float gFrequency; | 27 float gFrequency; |
13 float gPhase; | 28 float gPhase; |
14 float gInverseSampleRate; | 29 float gInverseSampleRate; |
15 int gCount=0; | 30 int gCount=0; |
16 networkData networkObject; | 31 //networkData networkObject; |
17 AuxiliaryTask transmitReceiveDataTask; | 32 #define numNetAudio 3 |
33 networkAudio netAudio[numNetAudio]; | |
34 AuxiliaryTask printIntervalTask; | |
35 AuxiliaryTask transmitReceiveAudioTask; | |
18 | 36 |
19 void transmitReceiveData(){ | 37 void transmitReceiveAudio(){ //transmit and receive audio buffers |
20 printf("transmitReceiveData auxiliary task has started\n"); | 38 for(int n=0;n<numNetAudio; n++){ |
21 while(!gShouldStop){ | 39 if(netAudio[n].toBeSent){ |
22 sendMessage(networkObject); | 40 netAudio[n].toBeSent=false; |
23 receiveMessage(networkObject); | 41 netAudio[n].udpClient.send(netAudio[n].buffers[!netAudio[n].currentBuffer],NETWORK_AUDIO_BUFFER_SIZE*sizeof(float)); |
24 usleep(1000); | 42 netAudio[n].doneOnTime=1; |
43 } | |
25 } | 44 } |
26 closeSockets(); | |
27 } | 45 } |
28 | 46 |
29 // setup() is called once before the audio rendering starts. | 47 // setup() is called once before the audio rendering starts. |
30 // Use it to perform any initialisation and allocation which is dependent | 48 // Use it to perform any initialisation and allocation which is dependent |
31 // on the period size or sample rate. | 49 // on the period size or sample rate. |
40 gFrequency = *(float *)userData; | 58 gFrequency = *(float *)userData; |
41 | 59 |
42 gInverseSampleRate = 1.0 / context->audioSampleRate; | 60 gInverseSampleRate = 1.0 / context->audioSampleRate; |
43 gPhase = 0.0; | 61 gPhase = 0.0; |
44 | 62 |
45 networkObject.counter=&gCount; | 63 // networkObject.counter=&gCount; |
46 networkObject.variables[0]=&gFrequency; | 64 // networkObject.variables[0]=&gFrequency; |
47 networkObject.variables[1]=&gPhase; | 65 // networkObject.variables[1]=&gPhase; |
48 networkObject.numVariables=2; | 66 // networkObject.numVariables=2; |
49 // setupSockets(settings->receivePort, settings->transmitPort, settings->serverName); | 67 for(int n=0; n<numNetAudio; n++){ |
50 setupSockets(10000, 9999, "127.0.0.1"); | 68 netAudio[n].doneOnTime=1; |
51 transmitReceiveDataTask= BeagleRT_createAuxiliaryTask(*transmitReceiveData, 80, "transmit-receive-data"); | 69 netAudio[n].index=0; |
52 //scheduleAuxiliaryTask(transmitReceiveDataTask); //here it does not work | 70 netAudio[n].currentBuffer=0; |
71 netAudio[n].toBeSent=false; | |
72 // netAudio[n].udpClient.setPort(settings->transmitPort+n); | |
73 // netAudio[n].udpClient.setServer(settings->serverName); | |
74 netAudio[n].udpClient.setPort(9999+n); | |
75 netAudio[n].udpClient.setServer("192.168.7.1"); | |
76 } | |
77 // setupSockets(settings->receivePort, settings->transmitPort, settings->serverName); | |
78 | |
79 // transmitReceiveDataTask=createAuxiliaryTask(*transmitReceiveData, 10, "transmit-receive-data"); | |
80 // scheduleAuxiliaryTask(transmitReceiveDataTask); //here it does not work | |
81 transmitReceiveAudioTask=BeagleRT_createAuxiliaryTask(*transmitReceiveAudio, 98, "transmit-receive-audio"); | |
53 return true; | 82 return true; |
54 } | 83 } |
55 | 84 |
56 // render() is called regularly at the highest priority by the audio engine. | 85 // render() is called regularly at the highest priority by the audio engine. |
57 // Input and output are given from the audio hardware and the other | 86 // Input and output are given from the audio hardware and the other |
58 // ADCs and DACs (if available). If only audio is available, numMatrixFrames | 87 // ADCs and DACs (if available). If only audio is available, numMatrixFrames |
59 // will be 0. | 88 // will be 0. |
60 | 89 |
61 void render(BeagleRTContext *context, void *userData) | 90 void render(BeagleRTContext *context, void *userData) |
62 { | 91 {/* |
63 for(unsigned int n = 0; n < context->audioFrames; n++) { | 92 for(unsigned int n = 0; n < context->audioFrames; n++) { |
64 float out = 0.7f * sinf(gPhase); | 93 float out = 0.7f * sinf(gPhase); |
65 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate; | 94 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate; |
66 if(gPhase > 2.0 * M_PI) | 95 if(gPhase > 2.0 * M_PI) |
67 gPhase -= 2.0 * M_PI; | 96 gPhase -= 2.0 * M_PI; |
72 if(gCount == 0){ | 101 if(gCount == 0){ |
73 BeagleRT_scheduleAuxiliaryTask(transmitReceiveDataTask); | 102 BeagleRT_scheduleAuxiliaryTask(transmitReceiveDataTask); |
74 } | 103 } |
75 gCount++; | 104 gCount++; |
76 } | 105 } |
106 | |
107 | |
108 */ | |
109 for(int n = 0; n < context->audioFrames; n++) { | |
110 float out = 0.7f * sinf(gPhase); | |
111 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate; | |
112 if(gPhase > 2.0 * M_PI) | |
113 gPhase -= 2.0 * M_PI; | |
114 | |
115 // for(int channel = 0; channel < context->audioChannels; channel++) | |
116 // context->audioOut[n * context->audioChannels + channel] = context->audioIn[n * context->audioChannels + 0]+context->audioIn[n * context->audioChannels + 1]; | |
117 context->audioOut[n * context->audioChannels] = context->audioIn[n*context->audioChannels+0]; | |
118 context->audioOut[n * context->audioChannels+1]=out; | |
119 if(0==gCount){ | |
120 // scheduleAuxiliaryTask(transmitReceiveDataTask); | |
121 } | |
122 for(int j=0; j<numNetAudio; j++){ | |
123 if(netAudio[j].index==(NETWORK_AUDIO_BUFFER_SIZE)){ // when the buffer is ready ... | |
124 netAudio[j].toBeSent=true; | |
125 netAudio[j].index=0; //reset the counter | |
126 if(netAudio[j].doneOnTime==0) | |
127 rt_printf("Network buffer underrun :-{\n"); | |
128 netAudio[j].timestamp=gCount; | |
129 netAudio[j].currentBuffer=!netAudio[j].currentBuffer; //switch buffer | |
130 netAudio[j].doneOnTime=0; | |
131 BeagleRT_scheduleAuxiliaryTask(transmitReceiveAudioTask); //send the buffer | |
132 } | |
133 } | |
134 if((gCount&1)==0){ | |
135 netAudio[1].buffers[netAudio[1].currentBuffer][netAudio[1].index++]=analogReadFrame(context,n/2,0)+context->audioOut[n*context->audioChannels + 0]; | |
136 netAudio[2].buffers[netAudio[2].currentBuffer][netAudio[2].index++]=analogReadFrame(context,n/2,1)+context->audioOut[n*context->audioChannels + 0]; | |
137 } | |
138 netAudio[0].buffers[netAudio[0].currentBuffer][netAudio[0].index++]=0.5*(out+context->audioOut[n*context->audioChannels + 0]);//copy channel 0 to the buffer | |
139 // netAudio[1].buffers[netAudio[1].currentBuffer][netAudio[1].index++]=0.5*(out+context->audioOut[n*context->audioChannels + 0]); | |
140 // netAudio[2].buffers[netAudio[2].currentBuffer][netAudio[2].index++]=0.5*(out+context->audioOut[n*context->audioChannels + 0]); | |
141 gCount++; | |
142 } | |
77 } | 143 } |
78 | 144 |
79 // cleanup() is called once at the end, after the audio has stopped. | 145 // cleanup() is called once at the end, after the audio has stopped. |
80 // Release any resources that were allocated in setup(). | 146 // Release any resources that were allocated in setup(). |
81 | 147 |