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