Mercurial > hg > beaglert
comparison examples/samples/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/samples/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 "SampleData.h" | |
12 | |
13 SampleData gSampleData; // User defined structure to get complex data from main | |
14 int gReadPtr; // Position of last read sample from file | |
15 | |
16 // Task for handling the update of the frequencies using the matrix | |
17 AuxiliaryTask gTriggerSamplesTask; | |
18 | |
19 bool initialise_trigger(); | |
20 void trigger_samples(); | |
21 | |
22 // setup() is called once before the audio rendering starts. | |
23 // Use it to perform any initialisation and allocation which is dependent | |
24 // on the period size or sample rate. | |
25 // | |
26 // userData holds an opaque pointer to a data structure that was passed | |
27 // in from the call to initAudio(). | |
28 // | |
29 // Return true on success; returning false halts the program. | |
30 | |
31 bool setup(BeagleRTContext *context, void *userData) | |
32 { | |
33 | |
34 // Retrieve a parameter passed in from the initAudio() call | |
35 gSampleData = *(SampleData *)userData; | |
36 | |
37 gReadPtr = -1; | |
38 | |
39 // Initialise auxiliary tasks | |
40 if(!initialise_trigger()) | |
41 return false; | |
42 | |
43 return true; | |
44 } | |
45 | |
46 // render() is called regularly at the highest priority by the audio engine. | |
47 // Input and output are given from the audio hardware and the other | |
48 // ADCs and DACs (if available). If only audio is available, numMatrixFrames | |
49 // will be 0. | |
50 | |
51 void render(BeagleRTContext *context, void *userData) | |
52 { | |
53 for(unsigned int n = 0; n < context->audioFrames; n++) { | |
54 float out = 0; | |
55 | |
56 // If triggered... | |
57 if(gReadPtr != -1) | |
58 out += gSampleData.samples[gReadPtr++]; // ...read each sample... | |
59 | |
60 if(gReadPtr >= gSampleData.sampleLen) | |
61 gReadPtr = -1; | |
62 | |
63 for(unsigned int channel = 0; channel < context->audioChannels; channel++) | |
64 context->audioOut[n * context->audioChannels + channel] = out; // ...and put it in both left and right channel | |
65 } | |
66 | |
67 // Request that the lower-priority task run at next opportunity | |
68 BeagleRT_scheduleAuxiliaryTask(gTriggerSamplesTask); | |
69 } | |
70 | |
71 // Initialise the auxiliary task | |
72 // and print info | |
73 | |
74 bool initialise_trigger() | |
75 { | |
76 if((gTriggerSamplesTask = BeagleRT_createAuxiliaryTask(&trigger_samples, 50, "beaglert-trigger-samples")) == 0) | |
77 return false; | |
78 | |
79 rt_printf("Press 'a' to trigger sample, 's' to stop\n"); | |
80 rt_printf("Press 'q' to quit\n"); | |
81 | |
82 return true; | |
83 } | |
84 | |
85 // This is a lower-priority call to periodically read keyboard input | |
86 // and trigger samples. By placing it at a lower priority, | |
87 // it has minimal effect on the audio performance but it will take longer to | |
88 // complete if the system is under heavy audio load. | |
89 | |
90 void trigger_samples() | |
91 { | |
92 // This is not a real-time task! | |
93 // Cos getchar is a system call, not handled by Xenomai. | |
94 // This task will be automatically down graded. | |
95 | |
96 char keyStroke = '.'; | |
97 | |
98 keyStroke = getchar(); | |
99 while(getchar()!='\n'); // to read the first stroke | |
100 | |
101 switch (keyStroke) | |
102 { | |
103 case 'a': | |
104 gReadPtr = 0; | |
105 break; | |
106 case 's': | |
107 gReadPtr = -1; | |
108 break; | |
109 case 'q': | |
110 gShouldStop = true; | |
111 break; | |
112 default: | |
113 break; | |
114 } | |
115 } | |
116 | |
117 | |
118 | |
119 // cleanup() is called once at the end, after the audio has stopped. | |
120 // Release any resources that were allocated in setup(). | |
121 | |
122 void cleanup(BeagleRTContext *context, void *userData) | |
123 { | |
124 delete[] gSampleData.samples; | |
125 } |