giuliomoro@94
|
1 /*
|
giuliomoro@94
|
2 * render.cpp
|
giuliomoro@94
|
3 *
|
giuliomoro@94
|
4 * Created on: Oct 24, 2014
|
giuliomoro@94
|
5 * Author: parallels
|
giuliomoro@94
|
6 */
|
giuliomoro@94
|
7
|
giuliomoro@94
|
8 #include <BeagleRT.h>
|
giuliomoro@94
|
9 #include <cmath>
|
giuliomoro@94
|
10 #include <client.h>
|
giuliomoro@94
|
11
|
giuliomoro@94
|
12 float gFrequency;
|
giuliomoro@94
|
13 float gPhase;
|
giuliomoro@94
|
14 float gInverseSampleRate;
|
giuliomoro@94
|
15 int gCount=0;
|
giuliomoro@94
|
16 networkData networkObject;
|
giuliomoro@94
|
17 AuxiliaryTask transmitReceiveDataTask;
|
giuliomoro@94
|
18
|
giuliomoro@94
|
19 void transmitReceiveData(){
|
giuliomoro@94
|
20 printf("transmitReceiveData auxiliary task has started\n");
|
giuliomoro@94
|
21 while(!gShouldStop){
|
giuliomoro@94
|
22 sendMessage(networkObject);
|
giuliomoro@94
|
23 receiveMessage(networkObject);
|
giuliomoro@94
|
24 usleep(1000);
|
giuliomoro@94
|
25 }
|
giuliomoro@94
|
26 closeSockets();
|
giuliomoro@94
|
27 }
|
giuliomoro@94
|
28
|
giuliomoro@94
|
29 // setup() is called once before the audio rendering starts.
|
giuliomoro@94
|
30 // Use it to perform any initialisation and allocation which is dependent
|
giuliomoro@94
|
31 // on the period size or sample rate.
|
giuliomoro@94
|
32 //
|
giuliomoro@94
|
33 // userData holds an opaque pointer to a data structure that was passed
|
giuliomoro@94
|
34 // in from the call to initAudio().
|
giuliomoro@94
|
35 //
|
giuliomoro@94
|
36 // Return true on success; returning false halts the program.
|
giuliomoro@94
|
37 bool setup(BeagleRTContext *context, void *userData)
|
giuliomoro@94
|
38 {
|
giuliomoro@94
|
39 // Retrieve a parameter passed in from the initAudio() call
|
giuliomoro@94
|
40 gFrequency = *(float *)userData;
|
giuliomoro@94
|
41
|
giuliomoro@94
|
42 gInverseSampleRate = 1.0 / context->audioSampleRate;
|
giuliomoro@94
|
43 gPhase = 0.0;
|
giuliomoro@94
|
44
|
giuliomoro@94
|
45 networkObject.counter=&gCount;
|
giuliomoro@94
|
46 networkObject.variables[0]=&gFrequency;
|
giuliomoro@94
|
47 networkObject.variables[1]=&gPhase;
|
giuliomoro@94
|
48 networkObject.numVariables=2;
|
giuliomoro@94
|
49
|
giuliomoro@94
|
50 setupSockets(settings->receivePort, settings->transmitPort, settings->serverName);
|
giuliomoro@94
|
51 transmitReceiveDataTask= BeagleRT_createAuxiliaryTask(*transmitReceiveData, 80, "transmit-receive-data");
|
giuliomoro@94
|
52 //scheduleAuxiliaryTask(transmitReceiveDataTask); //here it does not work
|
giuliomoro@94
|
53 return true;
|
giuliomoro@94
|
54 }
|
giuliomoro@94
|
55
|
giuliomoro@94
|
56 // render() is called regularly at the highest priority by the audio engine.
|
giuliomoro@94
|
57 // Input and output are given from the audio hardware and the other
|
giuliomoro@94
|
58 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
|
giuliomoro@94
|
59 // will be 0.
|
giuliomoro@94
|
60
|
giuliomoro@94
|
61 void render(BeagleRTContext *context, void *userData)
|
giuliomoro@94
|
62 {
|
giuliomoro@94
|
63 for(unsigned int n = 0; n < context->audioFrames; n++) {
|
giuliomoro@94
|
64 float out = 0.7f * sinf(gPhase);
|
giuliomoro@94
|
65 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate;
|
giuliomoro@94
|
66 if(gPhase > 2.0 * M_PI)
|
giuliomoro@94
|
67 gPhase -= 2.0 * M_PI;
|
giuliomoro@94
|
68
|
giuliomoro@94
|
69 for(unsigned int channel = 0; channel < context->audioChannels; channel++)
|
giuliomoro@94
|
70 context->audioOut[n * context->audioChannels + channel] = out;
|
giuliomoro@94
|
71
|
giuliomoro@94
|
72 if(gCount == 0){
|
giuliomoro@94
|
73 BeagleRT_scheduleAuxiliaryTask(transmitReceiveDataTask);
|
giuliomoro@94
|
74 }
|
giuliomoro@94
|
75 gCount++;
|
giuliomoro@94
|
76 }
|
giuliomoro@94
|
77 }
|
giuliomoro@94
|
78
|
giuliomoro@94
|
79 // cleanup() is called once at the end, after the audio has stopped.
|
giuliomoro@94
|
80 // Release any resources that were allocated in setup().
|
giuliomoro@94
|
81
|
giuliomoro@94
|
82 void cleanup(BeagleRTContext *context, void *userData)
|
giuliomoro@94
|
83 {
|
giuliomoro@94
|
84 }
|