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