robert@464
|
1 /*
|
robert@464
|
2 ____ _____ _ _
|
robert@464
|
3 | __ )| ____| | / \
|
robert@464
|
4 | _ \| _| | | / _ \
|
robert@464
|
5 | |_) | |___| |___ / ___ \
|
robert@464
|
6 |____/|_____|_____/_/ \_\
|
robert@464
|
7
|
robert@464
|
8 The platform for ultra-low latency audio and sensor processing
|
robert@464
|
9
|
robert@464
|
10 http://bela.io
|
robert@464
|
11
|
robert@464
|
12 A project of the Augmented Instruments Laboratory within the
|
robert@464
|
13 Centre for Digital Music at Queen Mary University of London.
|
robert@464
|
14 http://www.eecs.qmul.ac.uk/~andrewm
|
robert@464
|
15
|
robert@464
|
16 (c) 2016 Augmented Instruments Laboratory: Andrew McPherson,
|
robert@464
|
17 Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack,
|
robert@464
|
18 Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved.
|
robert@464
|
19
|
robert@464
|
20 The Bela software is distributed under the GNU Lesser General Public License
|
robert@464
|
21 (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt
|
robert@464
|
22 */
|
robert@464
|
23
|
robert@464
|
24
|
robert@464
|
25 #include <Bela.h>
|
robert@464
|
26 #include <cmath>
|
robert@464
|
27 #include "SampleData.h"
|
robert@464
|
28
|
robert@464
|
29 SampleData gSampleData; // User defined structure to get complex data from main
|
robert@464
|
30 int gReadPtr; // Position of last read sample from file
|
robert@464
|
31
|
robert@464
|
32 // Task for handling the update of the frequencies using the matrix
|
robert@464
|
33 AuxiliaryTask gTriggerSamplesTask;
|
robert@464
|
34
|
robert@464
|
35 bool initialise_trigger();
|
robert@464
|
36 void trigger_samples();
|
robert@464
|
37
|
robert@464
|
38 bool setup(BelaContext *context, void *userData)
|
robert@464
|
39 {
|
robert@464
|
40
|
robert@544
|
41 // Check that we have the same number of inputs and outputs.
|
robert@544
|
42 if(context->audioInChannels != context->audioOutChannels ||
|
robert@544
|
43 context->analogInChannels != context-> analogOutChannels){
|
robert@544
|
44 printf("Error: for this project, you need the same number of input and output channels.\n");
|
robert@544
|
45 return false;
|
robert@544
|
46 }
|
robert@544
|
47
|
robert@464
|
48 // Retrieve a parameter passed in from the initAudio() call
|
robert@464
|
49 gSampleData = *(SampleData *)userData;
|
robert@464
|
50
|
robert@464
|
51 gReadPtr = -1;
|
robert@464
|
52
|
robert@464
|
53 // Initialise auxiliary tasks
|
robert@464
|
54 if(!initialise_trigger())
|
robert@464
|
55 return false;
|
robert@464
|
56
|
robert@464
|
57 return true;
|
robert@464
|
58 }
|
robert@464
|
59
|
robert@464
|
60 void render(BelaContext *context, void *userData)
|
robert@464
|
61 {
|
robert@464
|
62 for(unsigned int n = 0; n < context->audioFrames; n++) {
|
robert@464
|
63 float out = 0;
|
robert@464
|
64
|
robert@464
|
65 // If triggered...
|
robert@464
|
66 if(gReadPtr != -1)
|
robert@464
|
67 out += gSampleData.samples[gReadPtr++]; // ...read each sample...
|
robert@464
|
68
|
robert@464
|
69 if(gReadPtr >= gSampleData.sampleLen)
|
robert@464
|
70 gReadPtr = -1;
|
robert@464
|
71
|
robert@544
|
72 for(unsigned int channel = 0; channel < context->audioOutChannels; channel++)
|
robert@544
|
73 context->audioOut[n * context->audioOutChannels + channel] = out; // ...and put it in both left and right channel
|
robert@464
|
74 }
|
robert@464
|
75
|
robert@464
|
76 // Request that the lower-priority task run at next opportunity
|
robert@464
|
77 Bela_scheduleAuxiliaryTask(gTriggerSamplesTask);
|
robert@464
|
78 }
|
robert@464
|
79
|
robert@464
|
80 // Initialise the auxiliary task
|
robert@464
|
81 // and print info
|
robert@464
|
82
|
robert@464
|
83 bool initialise_trigger()
|
robert@464
|
84 {
|
robert@464
|
85 if((gTriggerSamplesTask = Bela_createAuxiliaryTask(&trigger_samples, 50, "bela-trigger-samples")) == 0)
|
robert@464
|
86 return false;
|
robert@464
|
87
|
robert@464
|
88 rt_printf("Press 'a' to trigger sample, 's' to stop\n");
|
robert@464
|
89 rt_printf("Press 'q' to quit\n");
|
robert@464
|
90
|
robert@464
|
91 return true;
|
robert@464
|
92 }
|
robert@464
|
93
|
robert@464
|
94 // This is a lower-priority call to periodically read keyboard input
|
robert@464
|
95 // and trigger samples. By placing it at a lower priority,
|
robert@464
|
96 // it has minimal effect on the audio performance but it will take longer to
|
robert@464
|
97 // complete if the system is under heavy audio load.
|
robert@464
|
98
|
robert@464
|
99 void trigger_samples()
|
robert@464
|
100 {
|
robert@464
|
101 // This is not a real-time task!
|
robert@464
|
102 // Cos getchar is a system call, not handled by Xenomai.
|
robert@464
|
103 // This task will be automatically down graded.
|
robert@464
|
104
|
robert@464
|
105 char keyStroke = '.';
|
robert@464
|
106
|
robert@464
|
107 keyStroke = getchar();
|
robert@464
|
108 while(getchar()!='\n'); // to read the first stroke
|
robert@464
|
109
|
robert@464
|
110 switch (keyStroke)
|
robert@464
|
111 {
|
robert@464
|
112 case 'a':
|
robert@464
|
113 gReadPtr = 0;
|
robert@464
|
114 break;
|
robert@464
|
115 case 's':
|
robert@464
|
116 gReadPtr = -1;
|
robert@464
|
117 break;
|
robert@464
|
118 case 'q':
|
robert@464
|
119 gShouldStop = true;
|
robert@464
|
120 break;
|
robert@464
|
121 default:
|
robert@464
|
122 break;
|
robert@464
|
123 }
|
robert@464
|
124 }
|
robert@464
|
125
|
robert@464
|
126
|
robert@464
|
127 void cleanup(BelaContext *context, void *userData)
|
robert@464
|
128 {
|
robert@464
|
129 delete[] gSampleData.samples;
|
robert@464
|
130 }
|
robert@464
|
131
|
robert@464
|
132
|
robert@464
|
133 /**
|
robert@500
|
134 \example samples/render.cpp
|
robert@464
|
135
|
robert@464
|
136 Playback WAV files
|
robert@464
|
137 ------------------
|
robert@464
|
138
|
robert@464
|
139 This sketch shows how to playback audio samples from a buffer.
|
robert@464
|
140
|
robert@464
|
141 An audio file is loaded into a buffer `SampleData` as `gSampleData`. This is
|
robert@464
|
142 accessed with a read pointer that is incremented at audio rate within the render
|
robert@464
|
143 function: `out += gSampleData.samples[gReadPtr++]`.
|
robert@464
|
144
|
robert@464
|
145 Note that the read pointer is stopped from incrementing past the length of the
|
robert@464
|
146 `gSampleData`. This is achieved by comparing the read pointer value against the
|
robert@464
|
147 sample length which we can access as follows: `gSampleData.sampleLen`.
|
robert@464
|
148
|
robert@464
|
149 The sample is triggered by keyboard input: (a) starts sample playback, (s)
|
robert@464
|
150 stops sample playback. The triggering is treated as a lower priority task than
|
robert@464
|
151 the audio. You can see this at the bottom of the render function:
|
robert@464
|
152 `Bela_scheduleAuxiliaryTask(gTriggerSamplesTask)`;
|
robert@464
|
153 */
|