comparison examples/filter_FIR/render.cpp @ 300:dbeed520b014 prerelease

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