Mercurial > hg > beaglert
comparison examples/11-Extras/userdata/render.cpp @ 543:8f8809c77dda prerelease
updated basics, digital, instruments, extras examples
author | chnrx <chris.heinrichs@gmail.com> |
---|---|
date | Fri, 24 Jun 2016 13:19:52 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
542:3016638b4da2 | 543:8f8809c77dda |
---|---|
1 /* | |
2 ____ _____ _ _ | |
3 | __ )| ____| | / \ | |
4 | _ \| _| | | / _ \ | |
5 | |_) | |___| |___ / ___ \ | |
6 |____/|_____|_____/_/ \_\ | |
7 | |
8 The platform for ultra-low latency audio and sensor processing | |
9 | |
10 http://bela.io | |
11 | |
12 A project of the Augmented Instruments Laboratory within the | |
13 Centre for Digital Music at Queen Mary University of London. | |
14 http://www.eecs.qmul.ac.uk/~andrewm | |
15 | |
16 (c) 2016 Augmented Instruments Laboratory: Andrew McPherson, | |
17 Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack, | |
18 Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved. | |
19 | |
20 The Bela software is distributed under the GNU Lesser General Public License | |
21 (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt | |
22 */ | |
23 | |
24 #include <Bela.h> | |
25 #include <cmath> | |
26 | |
27 float gFrequency = 440.0; | |
28 float gPhase; | |
29 float gInverseSampleRate; | |
30 | |
31 bool setup(BelaContext *context, void *userData) | |
32 { | |
33 /* | |
34 * Retrieve the parameter passed in from the Bela_initAudio() call in main.cpp | |
35 */ | |
36 if(userData != 0) | |
37 gFrequency = *(float *)userData; | |
38 | |
39 gInverseSampleRate = 1.0 / context->audioSampleRate; | |
40 gPhase = 0.0; | |
41 | |
42 return true; | |
43 } | |
44 | |
45 void render(BelaContext *context, void *userData) | |
46 { | |
47 for(unsigned int n = 0; n < context->audioFrames; n++) { | |
48 float out = 0.8f * sinf(gPhase); | |
49 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate; | |
50 if(gPhase > 2.0 * M_PI) | |
51 gPhase -= 2.0 * M_PI; | |
52 | |
53 for(unsigned int channel = 0; channel < context->audioOutChannels; channel++) { | |
54 // Two equivalent ways to write this code | |
55 | |
56 // The long way, using the buffers directly: | |
57 // context->audioOut[n * context->audioOutChannels + channel] = out; | |
58 | |
59 // Or using the macros: | |
60 audioWrite(context, n, channel, out); | |
61 } | |
62 } | |
63 } | |
64 | |
65 void cleanup(BelaContext *context, void *userData) | |
66 { | |
67 | |
68 } | |
69 | |
70 | |
71 /** | |
72 \example userdata/render.cpp | |
73 | |
74 Passing parameters using the `*userData` argument | |
75 ------------------------------------------------- | |
76 | |
77 This sketch demonstrates how to pass command line arguments using the `*userData` argument inside the `setup()` function. | |
78 | |
79 In main.cpp we first parse a command line argument `-f` and allocate its value to the variable `frequency`. | |
80 We then pass the address of this variable when we call `Bela_initAudio()`. The variable can now be accessed from the | |
81 `setup()` and `render()` functions inside render.cpp. | |
82 | |
83 */ |