Mercurial > hg > beaglert
comparison examples/01-Basics/sinetone/render.cpp @ 464:8fcfbfb32aa0 prerelease
Examples reorder with subdirectories. Added header to each project. Moved Doxygen to bottom of render.cpp.
author | Robert Jack <robert.h.jack@gmail.com> |
---|---|
date | Mon, 20 Jun 2016 16:20:38 +0100 |
parents | |
children | b935f890e512 |
comparison
equal
deleted
inserted
replaced
463:c47709e8b5c9 | 464:8fcfbfb32aa0 |
---|---|
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 // Retrieve a parameter passed in from the initAudio() call | |
34 if(userData != 0) | |
35 gFrequency = *(float *)userData; | |
36 | |
37 gInverseSampleRate = 1.0 / context->audioSampleRate; | |
38 gPhase = 0.0; | |
39 | |
40 return true; | |
41 } | |
42 | |
43 void render(BelaContext *context, void *userData) | |
44 { | |
45 for(unsigned int n = 0; n < context->audioFrames; n++) { | |
46 float out = 0.8f * sinf(gPhase); | |
47 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate; | |
48 if(gPhase > 2.0 * M_PI) | |
49 gPhase -= 2.0 * M_PI; | |
50 | |
51 for(unsigned int channel = 0; channel < context->audioChannels; channel++) { | |
52 // Two equivalent ways to write this code | |
53 | |
54 // The long way, using the buffers directly: | |
55 // context->audioOut[n * context->audioChannels + channel] = out; | |
56 | |
57 // Or using the macros: | |
58 audioWrite(context, n, channel, out); | |
59 } | |
60 } | |
61 } | |
62 | |
63 void cleanup(BelaContext *context, void *userData) | |
64 { | |
65 | |
66 } | |
67 | |
68 /* ------------ Project Explantation ------------ */ | |
69 | |
70 /** | |
71 \example 01-sinetone | |
72 | |
73 Producing your first bleep! | |
74 --------------------------- | |
75 | |
76 This sketch is the hello world of embedded interactive audio. Better known as bleep, it | |
77 produces a sine tone. | |
78 | |
79 The frequency of the sine tone is determined by a global variable, `gFrequency` | |
80 (line 12). The sine tone is produced by incrementing the phase of a sin function | |
81 on every audio frame. | |
82 | |
83 In render() you'll see a nested for loop structure. You'll see this in all Bela projects. | |
84 The first for loop cycles through 'audioFrames', the second through 'audioChannels' (in this case left 0 and right 1). | |
85 It is good to familiarise yourself with this structure as it's fundamental to producing sound with the system. | |
86 */ |