comparison projects/filter_IIR/render.cpp @ 108:3068421c0737 ultra-staging

Merged default into ultra-staging
author Giulio Moro <giuliomoro@yahoo.it>
date Tue, 18 Aug 2015 00:35:15 +0100
parents 3c3a1357657d
children 5433c83ce04e
comparison
equal deleted inserted replaced
54:d3f869b98147 108:3068421c0737
4 * Created on: Oct 24, 2014 4 * Created on: Oct 24, 2014
5 * Author: Andrew McPherson and Victor Zappi 5 * Author: Andrew McPherson and Victor Zappi
6 */ 6 */
7 7
8 8
9 #include "../../include/render.h" 9 #include <BeagleRT.h> // to schedule lower prio parallel process
10 #include "../../include/RTAudio.h" // to schedule lower prio parallel process
11 #include <rtdk.h> 10 #include <rtdk.h>
12 #include <cmath> 11 #include <cmath>
13 #include <stdio.h> 12 #include <stdio.h>
14 #include "SampleData.h" 13 #include "SampleData.h"
15 14
43 42
44 43
45 extern float gCutFreq; 44 extern float gCutFreq;
46 45
47 46
48 // initialise_render() is called once before the audio rendering starts. 47 // setup() is called once before the audio rendering starts.
49 // Use it to perform any initialisation and allocation which is dependent 48 // Use it to perform any initialisation and allocation which is dependent
50 // on the period size or sample rate. 49 // on the period size or sample rate.
51 // 50 //
52 // userData holds an opaque pointer to a data structure that was passed 51 // userData holds an opaque pointer to a data structure that was passed
53 // in from the call to initAudio(). 52 // in from the call to initAudio().
54 // 53 //
55 // Return true on success; returning false halts the program. 54 // Return true on success; returning false halts the program.
56 55
57 bool initialise_render(int numMatrixChannels, int numAudioChannels, 56 bool setup(BeagleRTContext *context, void *userData)
58 int numMatrixFramesPerPeriod,
59 int numAudioFramesPerPeriod,
60 float matrixSampleRate, float audioSampleRate,
61 void *userData)
62 { 57 {
63 58
64 // Retrieve a parameter passed in from the initAudio() call 59 // Retrieve a parameter passed in from the initAudio() call
65 gSampleData = *(SampleData *)userData; 60 gSampleData = *(SampleData *)userData;
66 61
78 // render() is called regularly at the highest priority by the audio engine. 73 // render() is called regularly at the highest priority by the audio engine.
79 // Input and output are given from the audio hardware and the other 74 // Input and output are given from the audio hardware and the other
80 // ADCs and DACs (if available). If only audio is available, numMatrixFrames 75 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
81 // will be 0. 76 // will be 0.
82 77
83 void render(int numMatrixFrames, int numAudioFrames, float *audioIn, float *audioOut, 78 void render(BeagleRTContext *context, void *userData)
84 uint16_t *matrixIn, uint16_t *matrixOut) 79 {
85 { 80 for(unsigned int n = 0; n < context->audioFrames; n++) {
86 for(int n = 0; n < numAudioFrames; n++) {
87 float sample = 0; 81 float sample = 0;
88 float out = 0; 82 float out = 0;
89 83
90 // If triggered... 84 // If triggered...
91 if(gReadPtr != -1) 85 if(gReadPtr != -1)
99 gLastX[1] = gLastX[0]; 93 gLastX[1] = gLastX[0];
100 gLastX[0] = out; 94 gLastX[0] = out;
101 gLastY[1] = gLastY[0]; 95 gLastY[1] = gLastY[0];
102 gLastY[0] = out; 96 gLastY[0] = out;
103 97
104 for(int channel = 0; channel < gNumAudioChannels; channel++) 98 for(unsigned int channel = 0; channel < context->audioChannels; channel++)
105 audioOut[n * gNumAudioChannels + channel] = out; // ...and put it in both left and right channel 99 context->audioOut[n * context->audioChannels + channel] = out; // ...and put it in both left and right channel
106 100
107 } 101 }
108 102
109 // Request that the lower-priority tasks run at next opportunity 103 // Request that the lower-priority tasks run at next opportunity
110 scheduleAuxiliaryTask(gChangeCoeffTask); 104 BeagleRT_scheduleAuxiliaryTask(gChangeCoeffTask);
111 scheduleAuxiliaryTask(gInputTask); 105 BeagleRT_scheduleAuxiliaryTask(gInputTask);
112 } 106 }
113 107
114 // First calculation of coefficients 108 // First calculation of coefficients
115 109
116 void initialise_filter(float freq) 110 void initialise_filter(float freq)
143 // Initialise the auxiliary tasks 137 // Initialise the auxiliary tasks
144 // and print info 138 // and print info
145 139
146 bool initialise_aux_tasks() 140 bool initialise_aux_tasks()
147 { 141 {
148 if((gChangeCoeffTask = createAuxiliaryTaskLoop(&check_coeff, 90, "beaglert-check-coeff")) == 0) 142 if((gChangeCoeffTask = BeagleRT_createAuxiliaryTask(&check_coeff, 90, "beaglert-check-coeff")) == 0)
149 return false; 143 return false;
150 144
151 if((gInputTask = createAuxiliaryTaskLoop(&read_input, 50, "beaglert-read-input")) == 0) 145 if((gInputTask = BeagleRT_createAuxiliaryTask(&read_input, 50, "beaglert-read-input")) == 0)
152 return false; 146 return false;
153 147
154 rt_printf("Press 'a' to trigger sample, 's' to stop\n"); 148 rt_printf("Press 'a' to trigger sample, 's' to stop\n");
155 rt_printf("Press 'z' to low down cut-off freq of 100 Hz, 'x' to raise it up\n"); 149 rt_printf("Press 'z' to low down cut-off freq of 100 Hz, 'x' to raise it up\n");
156 rt_printf("Press 'q' to quit\n"); 150 rt_printf("Press 'q' to quit\n");
216 } 210 }
217 } 211 }
218 212
219 213
220 214
221 // cleanup_render() is called once at the end, after the audio has stopped. 215 // cleanup() is called once at the end, after the audio has stopped.
222 // Release any resources that were allocated in initialise_render(). 216 // Release any resources that were allocated in setup().
223 217
224 void cleanup_render() 218 void cleanup(BeagleRTContext *context, void *userData)
225 { 219 {
226 delete[] gSampleData.samples; 220 delete[] gSampleData.samples;
227 } 221 }