comparison projects/basic_network/render.cpp @ 56:3c3a1357657d newapi

Further API update to name three primary functions setup(), render() and cleanup(). Changed include paths so now can #include <BeagleRT.h>. Removed stale pru_rtaudio.bin file as this is now done as pru_rtaudio_bin.h. Updated examples to new API and fixed minor compiler warnings along the way. Network example needs further attention to compile.
author andrewm
date Wed, 15 Jul 2015 12:10:51 +0100
parents 98aed580452a
children 7cc79d46ecb9
comparison
equal deleted inserted replaced
55:41d24dba6b74 56:3c3a1357657d
3 * 3 *
4 * Created on: Oct 24, 2014 4 * Created on: Oct 24, 2014
5 * Author: parallels 5 * Author: parallels
6 */ 6 */
7 7
8 #include "../../include/RTAudioSettings.h" 8 #include <BeagleRT.h>
9 #include "../../include/render.h"
10 #include <cmath> 9 #include <cmath>
11 #include "../../include/client.h" 10 #include <client.h>
12 #include "../../include/RTAudio.h" // to schedule lower prio parallel process 11
13 #include <rtdk.h>
14 float gFrequency; 12 float gFrequency;
15 float gPhase; 13 float gPhase;
16 float gInverseSampleRate; 14 float gInverseSampleRate;
17 int gCount=0; 15 int gCount=0;
18 networkData networkObject; 16 networkData networkObject;
26 usleep(1000); 24 usleep(1000);
27 } 25 }
28 closeSockets(); 26 closeSockets();
29 } 27 }
30 28
31 // initialise_render() is called once before the audio rendering starts. 29 // setup() is called once before the audio rendering starts.
32 // Use it to perform any initialisation and allocation which is dependent 30 // Use it to perform any initialisation and allocation which is dependent
33 // on the period size or sample rate. 31 // on the period size or sample rate.
34 // 32 //
35 // userData holds an opaque pointer to a data structure that was passed 33 // userData holds an opaque pointer to a data structure that was passed
36 // in from the call to initAudio(). 34 // in from the call to initAudio().
37 // 35 //
38 // Return true on success; returning false halts the program. 36 // Return true on success; returning false halts the program.
39 bool initialise_render(int numMatrixChannels, int numDigitalChannels, int numAudioChannels, 37 bool setup(BeagleRTContext *context, void *userData)
40 int numMatrixFramesPerPeriod,
41 int numAudioFramesPerPeriod,
42 float matrixSampleRate, float audioSampleRate,
43 void *userData, RTAudioSettings *settings)
44 { 38 {
45 // Retrieve a parameter passed in from the initAudio() call 39 // Retrieve a parameter passed in from the initAudio() call
46 gFrequency = *(float *)userData; 40 gFrequency = *(float *)userData;
47 41
48 gInverseSampleRate = 1.0 / audioSampleRate; 42 gInverseSampleRate = 1.0 / context->audioSampleRate;
49 gPhase = 0.0; 43 gPhase = 0.0;
50 44
51 networkObject.counter=&gCount; 45 networkObject.counter=&gCount;
52 networkObject.variables[0]=&gFrequency; 46 networkObject.variables[0]=&gFrequency;
53 networkObject.variables[1]=&gPhase; 47 networkObject.variables[1]=&gPhase;
54 networkObject.numVariables=2; 48 networkObject.numVariables=2;
49
55 setupSockets(settings->receivePort, settings->transmitPort, settings->serverName); 50 setupSockets(settings->receivePort, settings->transmitPort, settings->serverName);
56 transmitReceiveDataTask=createAuxiliaryTaskLoop(*transmitReceiveData, 80, "transmit-receive-data"); 51 transmitReceiveDataTask= BeagleRT_createAuxiliaryTask(*transmitReceiveData, 80, "transmit-receive-data");
57 //scheduleAuxiliaryTask(transmitReceiveDataTask); //here it does not work 52 //scheduleAuxiliaryTask(transmitReceiveDataTask); //here it does not work
58 return true; 53 return true;
59 } 54 }
60 55
61 // render() is called regularly at the highest priority by the audio engine. 56 // render() is called regularly at the highest priority by the audio engine.
62 // Input and output are given from the audio hardware and the other 57 // Input and output are given from the audio hardware and the other
63 // ADCs and DACs (if available). If only audio is available, numMatrixFrames 58 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
64 // will be 0. 59 // will be 0.
65 60
66 void render(int numAnalogFrames, int numAudioFrames, int numDigitalFrames, float *audioIn, float *audioOut, 61 void render(BeagleRTContext *context, void *userData)
67 float *analogIn, float *analogOut, uint32_t *digital)
68 { 62 {
69 for(int n = 0; n < numAudioFrames; n++) { 63 for(unsigned int n = 0; n < context->audioFrames; n++) {
70 float out = 0.7f * sinf(gPhase); 64 float out = 0.7f * sinf(gPhase);
71 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate; 65 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate;
72 if(gPhase > 2.0 * M_PI) 66 if(gPhase > 2.0 * M_PI)
73 gPhase -= 2.0 * M_PI; 67 gPhase -= 2.0 * M_PI;
74 68
75 for(int channel = 0; channel < gNumAudioChannels; channel++) 69 for(unsigned int channel = 0; channel < context->audioChannels; channel++)
76 audioOut[n * gNumAudioChannels + channel] = out; 70 context->audioOut[n * context->audioChannels + channel] = out;
77 if(gCount==0){ 71
78 scheduleAuxiliaryTask(transmitReceiveDataTask); 72 if(gCount == 0){
73 BeagleRT_scheduleAuxiliaryTask(transmitReceiveDataTask);
79 } 74 }
80 gCount++; 75 gCount++;
81 } 76 }
82 } 77 }
83 78
84 // cleanup_render() is called once at the end, after the audio has stopped. 79 // cleanup() is called once at the end, after the audio has stopped.
85 // Release any resources that were allocated in initialise_render(). 80 // Release any resources that were allocated in setup().
86 81
87 void cleanup_render() 82 void cleanup(BeagleRTContext *context, void *userData)
88 { 83 {
89 } 84 }