Mercurial > hg > beaglert
comparison projects/filter_FIR/render.cpp @ 2:021ac8a1a4f9
_new FIR filter example
author | Victor Zappi <victor.zappi@qmul.ac.uk> |
---|---|
date | Thu, 06 Nov 2014 15:59:16 +0000 |
parents | |
children | 09f03ac40fcc |
comparison
equal
deleted
inserted
replaced
1:24fc8026ae8e | 2:021ac8a1a4f9 |
---|---|
1 /* | |
2 * render.cpp | |
3 * | |
4 * Created on: Oct 24, 2014 | |
5 * Author: Andrew McPherson and Victor Zappi | |
6 */ | |
7 | |
8 | |
9 #include "../../include/render.h" | |
10 #include "../../include/RTAudio.h" // to schedule lower prio parallel process | |
11 #include <rtdk.h> | |
12 #include <cmath> | |
13 #include <stdio.h> | |
14 #include <NE10.h> // neon library | |
15 #include "SampleData.h" | |
16 #include "FIRfilter.h" | |
17 | |
18 SampleData gSampleData; // User defined structure to get complex data from main | |
19 int gReadPtr; // Position of last read sample from file | |
20 int gNumChannels; | |
21 | |
22 | |
23 // filter vars | |
24 ne10_fir_instance_f32_t gFIRfilter; | |
25 ne10_float32_t *gFIRfilterIn; | |
26 ne10_float32_t *gFIRfilterOut; | |
27 ne10_uint32_t blockSize; | |
28 ne10_float32_t *gFIRfilterState; | |
29 | |
30 void initialise_filter(); | |
31 | |
32 | |
33 // Task for handling the update of the frequencies using the matrix | |
34 AuxiliaryTask gTriggerSamplesTask; | |
35 | |
36 bool initialise_trigger(); | |
37 void trigger_samples(); | |
38 | |
39 extern int gPeriodSize; // Period size in sensor frames | |
40 | |
41 | |
42 // initialise_render() is called once before the audio rendering starts. | |
43 // Use it to perform any initialisation and allocation which is dependent | |
44 // on the period size or sample rate. | |
45 // | |
46 // userData holds an opaque pointer to a data structure that was passed | |
47 // in from the call to initAudio(). | |
48 // | |
49 // Return true on success; returning false halts the program. | |
50 | |
51 bool initialise_render(int numChannels, int numMatrixFramesPerPeriod, | |
52 int numAudioFramesPerPeriod, float matrixSampleRate, | |
53 float audioSampleRate, void *userData) | |
54 { | |
55 | |
56 // Retrieve a parameter passed in from the initAudio() call | |
57 gSampleData = *(SampleData *)userData; | |
58 | |
59 gReadPtr = -1; | |
60 gNumChannels = numChannels; | |
61 | |
62 initialise_filter(); | |
63 | |
64 // Initialise auxiliary tasks | |
65 if(!initialise_trigger()) | |
66 return false; | |
67 | |
68 return true; | |
69 } | |
70 | |
71 // 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 | |
73 // ADCs and DACs (if available). If only audio is available, numMatrixFrames | |
74 // will be 0. | |
75 | |
76 void render(int numMatrixFrames, int numAudioFrames, float *audioIn, float *audioOut, | |
77 uint16_t *matrixIn, uint16_t *matrixOut) | |
78 { | |
79 for(int n = 0; n < numAudioFrames; n++) { | |
80 float in = 0; | |
81 | |
82 // If triggered... | |
83 if(gReadPtr != -1) | |
84 in += gSampleData.samples[gReadPtr++]; // ...read each sample... | |
85 | |
86 if(gReadPtr >= gSampleData.sampleLen) | |
87 gReadPtr = -1; | |
88 | |
89 gFIRfilterIn[n] = in; | |
90 } | |
91 | |
92 ne10_fir_float_neon(&gFIRfilter, gFIRfilterIn, gFIRfilterOut, blockSize); | |
93 | |
94 for(int n = 0; n < numAudioFrames; n++) { | |
95 for(int channel = 0; channel < gNumChannels; channel++) | |
96 audioOut[n * gNumChannels + channel] = gFIRfilterOut[n]; // ...and put it in both left and right channel | |
97 } | |
98 | |
99 | |
100 // Request that the lower-priority task run at next opportunity | |
101 scheduleAuxiliaryTask(gTriggerSamplesTask); | |
102 } | |
103 | |
104 // Initialise NE10 data structures to define FIR filter | |
105 | |
106 void initialise_filter() | |
107 { | |
108 blockSize = 2*gPeriodSize; | |
109 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)); | |
111 gFIRfilterOut = (ne10_float32_t *) NE10_MALLOC (blockSize * sizeof (ne10_float32_t)); | |
112 ne10_fir_init_float(&gFIRfilter, FILTER_TAP_NUM, filterTaps, gFIRfilterState, blockSize); | |
113 } | |
114 | |
115 | |
116 // Initialise the auxiliary task | |
117 // and print info | |
118 | |
119 bool initialise_trigger() | |
120 { | |
121 if((gTriggerSamplesTask = createAuxiliaryTaskLoop(&trigger_samples, 50, "beaglert-trigger-samples")) == 0) | |
122 return false; | |
123 | |
124 rt_printf("Press 'a' to trigger sample, 's' to stop\n"); | |
125 rt_printf("Press 'q' to quit\n"); | |
126 | |
127 return true; | |
128 } | |
129 | |
130 // This is a lower-priority call to periodically read keyboard input | |
131 // and trigger samples. By placing it at a lower priority, | |
132 // it has minimal effect on the audio performance but it will take longer to | |
133 // complete if the system is under heavy audio load. | |
134 | |
135 void trigger_samples() | |
136 { | |
137 // This is not a real-time task! | |
138 // Cos getchar is a system call, not handled by Xenomai. | |
139 // This task will be automatically down graded. | |
140 | |
141 char keyStroke = '.'; | |
142 | |
143 keyStroke = getchar(); | |
144 while(getchar()!='\n'); // to read the first stroke | |
145 | |
146 switch (keyStroke) | |
147 { | |
148 case 'a': | |
149 gReadPtr = 0; | |
150 break; | |
151 case 's': | |
152 gReadPtr = -1; | |
153 break; | |
154 case 'q': | |
155 gShouldStop = true; | |
156 break; | |
157 default: | |
158 break; | |
159 } | |
160 } | |
161 | |
162 | |
163 | |
164 // cleanup_render() is called once at the end, after the audio has stopped. | |
165 // Release any resources that were allocated in initialise_render(). | |
166 | |
167 void cleanup_render() | |
168 { | |
169 delete[] gSampleData.samples; | |
170 | |
171 NE10_FREE(gFIRfilterState); | |
172 NE10_FREE(gFIRfilterIn); | |
173 NE10_FREE(gFIRfilterOut); | |
174 } |