annotate examples/01-Basics/sinetone/render.cpp @ 507:1cec96845a23 prerelease

Explanted explantation
author Giulio Moro <giuliomoro@yahoo.it>
date Wed, 22 Jun 2016 01:51:17 +0100
parents b935f890e512
children 8f8809c77dda
rev   line source
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 #include <Bela.h>
robert@464 25 #include <cmath>
robert@464 26
robert@464 27 float gFrequency = 440.0;
robert@464 28 float gPhase;
robert@464 29 float gInverseSampleRate;
robert@464 30
robert@464 31 bool setup(BelaContext *context, void *userData)
robert@464 32 {
robert@464 33 // Retrieve a parameter passed in from the initAudio() call
robert@464 34 if(userData != 0)
robert@464 35 gFrequency = *(float *)userData;
robert@464 36
robert@464 37 gInverseSampleRate = 1.0 / context->audioSampleRate;
robert@464 38 gPhase = 0.0;
robert@464 39
robert@464 40 return true;
robert@464 41 }
robert@464 42
robert@464 43 void render(BelaContext *context, void *userData)
robert@464 44 {
robert@464 45 for(unsigned int n = 0; n < context->audioFrames; n++) {
robert@464 46 float out = 0.8f * sinf(gPhase);
robert@464 47 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate;
robert@464 48 if(gPhase > 2.0 * M_PI)
robert@464 49 gPhase -= 2.0 * M_PI;
robert@464 50
robert@464 51 for(unsigned int channel = 0; channel < context->audioChannels; channel++) {
robert@464 52 // Two equivalent ways to write this code
robert@464 53
robert@464 54 // The long way, using the buffers directly:
robert@464 55 // context->audioOut[n * context->audioChannels + channel] = out;
robert@464 56
robert@464 57 // Or using the macros:
robert@464 58 audioWrite(context, n, channel, out);
robert@464 59 }
robert@464 60 }
robert@464 61 }
robert@464 62
robert@464 63 void cleanup(BelaContext *context, void *userData)
robert@464 64 {
robert@464 65
robert@464 66 }
robert@464 67
robert@464 68
robert@464 69 /**
robert@500 70 \example sinetone/render.cpp
robert@464 71
robert@464 72 Producing your first bleep!
robert@464 73 ---------------------------
robert@464 74
robert@464 75 This sketch is the hello world of embedded interactive audio. Better known as bleep, it
robert@464 76 produces a sine tone.
robert@464 77
robert@464 78 The frequency of the sine tone is determined by a global variable, `gFrequency`
robert@464 79 (line 12). The sine tone is produced by incrementing the phase of a sin function
robert@464 80 on every audio frame.
robert@464 81
robert@464 82 In render() you'll see a nested for loop structure. You'll see this in all Bela projects.
robert@464 83 The first for loop cycles through 'audioFrames', the second through 'audioChannels' (in this case left 0 and right 1).
robert@464 84 It is good to familiarise yourself with this structure as it's fundamental to producing sound with the system.
robert@464 85 */