comparison projects/filter_FIR/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>
10 #include "../../include/RTAudio.h" // to schedule lower prio parallel process
11 #include <rtdk.h>
12 #include <cmath> 10 #include <cmath>
13 #include <stdio.h>
14 #include <NE10.h> // neon library 11 #include <NE10.h> // neon library
15 #include "SampleData.h" 12 #include "SampleData.h"
16 #include "FIRfilter.h" 13 #include "FIRfilter.h"
17 14
18 SampleData gSampleData; // User defined structure to get complex data from main 15 SampleData gSampleData; // User defined structure to get complex data from main
23 ne10_float32_t *gFIRfilterIn; 20 ne10_float32_t *gFIRfilterIn;
24 ne10_float32_t *gFIRfilterOut; 21 ne10_float32_t *gFIRfilterOut;
25 ne10_uint32_t blockSize; 22 ne10_uint32_t blockSize;
26 ne10_float32_t *gFIRfilterState; 23 ne10_float32_t *gFIRfilterState;
27 24
28 void initialise_filter(); 25 void initialise_filter(BeagleRTContext *context);
29
30 26
31 // Task for handling the update of the frequencies using the matrix 27 // Task for handling the update of the frequencies using the matrix
32 AuxiliaryTask gTriggerSamplesTask; 28 AuxiliaryTask gTriggerSamplesTask;
33 29
34 bool initialise_trigger(); 30 bool initialise_trigger();
35 void trigger_samples(); 31 void trigger_samples();
36 32
37 int gPeriodSize; // Period size in sensor frames 33 // setup() is called once before the audio rendering starts.
38
39
40 // initialise_render() is called once before the audio rendering starts.
41 // Use it to perform any initialisation and allocation which is dependent 34 // Use it to perform any initialisation and allocation which is dependent
42 // on the period size or sample rate. 35 // on the period size or sample rate.
43 // 36 //
44 // userData holds an opaque pointer to a data structure that was passed 37 // userData holds an opaque pointer to a data structure that was passed
45 // in from the call to initAudio(). 38 // in from the call to initAudio().
46 // 39 //
47 // Return true on success; returning false halts the program. 40 // Return true on success; returning false halts the program.
48 41
49 bool initialise_render(int numMatrixChannels, int numAudioChannels, 42 bool setup(BeagleRTContext *context, void *userData)
50 int numMatrixFramesPerPeriod,
51 int numAudioFramesPerPeriod,
52 float matrixSampleRate, float audioSampleRate,
53 void *userData)
54 { 43 {
55 44
56 // Retrieve a parameter passed in from the initAudio() call 45 // Retrieve a parameter passed in from the initAudio() call
57 gSampleData = *(SampleData *)userData; 46 gSampleData = *(SampleData *)userData;
58 47
59 gReadPtr = -1; 48 gReadPtr = -1;
60 gPeriodSize = numMatrixFramesPerPeriod;
61 49
62 initialise_filter(); 50 initialise_filter(context);
63 51
64 // Initialise auxiliary tasks 52 // Initialise auxiliary tasks
65 if(!initialise_trigger()) 53 if(!initialise_trigger())
66 return false; 54 return false;
67 55
71 // render() is called regularly at the highest priority by the audio engine. 59 // render() is called regularly at the highest priority by the audio engine.
72 // Input and output are given from the audio hardware and the other 60 // Input and output are given from the audio hardware and the other
73 // ADCs and DACs (if available). If only audio is available, numMatrixFrames 61 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
74 // will be 0. 62 // will be 0.
75 63
76 void render(int numMatrixFrames, int numAudioFrames, float *audioIn, float *audioOut, 64 void render(BeagleRTContext *context, void *userData)
77 uint16_t *matrixIn, uint16_t *matrixOut)
78 { 65 {
79 for(int n = 0; n < numAudioFrames; n++) { 66 for(unsigned int n = 0; n < context->audioFrames; n++) {
80 float in = 0; 67 float in = 0;
81 68
82 // If triggered... 69 // If triggered...
83 if(gReadPtr != -1) 70 if(gReadPtr != -1)
84 in += gSampleData.samples[gReadPtr++]; // ...read each sample... 71 in += gSampleData.samples[gReadPtr++]; // ...read each sample...
89 gFIRfilterIn[n] = in; 76 gFIRfilterIn[n] = in;
90 } 77 }
91 78
92 ne10_fir_float_neon(&gFIRfilter, gFIRfilterIn, gFIRfilterOut, blockSize); 79 ne10_fir_float_neon(&gFIRfilter, gFIRfilterIn, gFIRfilterOut, blockSize);
93 80
94 for(int n = 0; n < numAudioFrames; n++) { 81 for(unsigned int n = 0; n < context->audioFrames; n++) {
95 for(int channel = 0; channel < gNumAudioChannels; channel++) 82 for(unsigned int channel = 0; channel < context->audioChannels; channel++)
96 audioOut[n * gNumAudioChannels + channel] = gFIRfilterOut[n]; // ...and put it in both left and right channel 83 context->audioOut[n * context->audioChannels + channel] = gFIRfilterOut[n]; // ...and put it in both left and right channel
97 } 84 }
98 85
99 86
100 // Request that the lower-priority task run at next opportunity 87 // Request that the lower-priority task run at next opportunity
101 scheduleAuxiliaryTask(gTriggerSamplesTask); 88 BeagleRT_scheduleAuxiliaryTask(gTriggerSamplesTask);
102 } 89 }
103 90
104 // Initialise NE10 data structures to define FIR filter 91 // Initialise NE10 data structures to define FIR filter
105 92
106 void initialise_filter() 93 void initialise_filter(BeagleRTContext *context)
107 { 94 {
108 blockSize = 2*gPeriodSize; 95 blockSize = context->audioFrames;
109 gFIRfilterState = (ne10_float32_t *) NE10_MALLOC ((FILTER_TAP_NUM+blockSize-1) * sizeof (ne10_float32_t)); 96 gFIRfilterState = (ne10_float32_t *) NE10_MALLOC ((FILTER_TAP_NUM+blockSize-1) * sizeof (ne10_float32_t));
110 gFIRfilterIn = (ne10_float32_t *) NE10_MALLOC (blockSize * sizeof (ne10_float32_t)); 97 gFIRfilterIn = (ne10_float32_t *) NE10_MALLOC (blockSize * sizeof (ne10_float32_t));
111 gFIRfilterOut = (ne10_float32_t *) NE10_MALLOC (blockSize * sizeof (ne10_float32_t)); 98 gFIRfilterOut = (ne10_float32_t *) NE10_MALLOC (blockSize * sizeof (ne10_float32_t));
112 ne10_fir_init_float(&gFIRfilter, FILTER_TAP_NUM, filterTaps, gFIRfilterState, blockSize); 99 ne10_fir_init_float(&gFIRfilter, FILTER_TAP_NUM, filterTaps, gFIRfilterState, blockSize);
113 } 100 }
116 // Initialise the auxiliary task 103 // Initialise the auxiliary task
117 // and print info 104 // and print info
118 105
119 bool initialise_trigger() 106 bool initialise_trigger()
120 { 107 {
121 if((gTriggerSamplesTask = createAuxiliaryTaskLoop(&trigger_samples, 50, "beaglert-trigger-samples")) == 0) 108 if((gTriggerSamplesTask = BeagleRT_createAuxiliaryTask(&trigger_samples, 50, "beaglert-trigger-samples")) == 0)
122 return false; 109 return false;
123 110
124 rt_printf("Press 'a' to trigger sample, 's' to stop\n"); 111 rt_printf("Press 'a' to trigger sample, 's' to stop\n");
125 rt_printf("Press 'q' to quit\n"); 112 rt_printf("Press 'q' to quit\n");
126 113
159 } 146 }
160 } 147 }
161 148
162 149
163 150
164 // cleanup_render() is called once at the end, after the audio has stopped. 151 // cleanup() is called once at the end, after the audio has stopped.
165 // Release any resources that were allocated in initialise_render(). 152 // Release any resources that were allocated in setup().
166 153
167 void cleanup_render() 154 void cleanup(BeagleRTContext *context, void *userData)
168 { 155 {
169 delete[] gSampleData.samples; 156 delete[] gSampleData.samples;
170 157
171 NE10_FREE(gFIRfilterState); 158 NE10_FREE(gFIRfilterState);
172 NE10_FREE(gFIRfilterIn); 159 NE10_FREE(gFIRfilterIn);