annotate examples/01-Basics/sinetone/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 1cec96845a23
children db3e1a08cdee
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 gInverseSampleRate = 1.0 / context->audioSampleRate;
chris@543 34 gPhase = 0.0;s
robert@464 35
robert@464 36 return true;
robert@464 37 }
robert@464 38
robert@464 39 void render(BelaContext *context, void *userData)
robert@464 40 {
robert@464 41 for(unsigned int n = 0; n < context->audioFrames; n++) {
robert@464 42 float out = 0.8f * sinf(gPhase);
robert@464 43 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate;
robert@464 44 if(gPhase > 2.0 * M_PI)
robert@464 45 gPhase -= 2.0 * M_PI;
robert@464 46
chris@543 47 for(unsigned int channel = 0; channel < context->audioOutChannels; channel++) {
robert@464 48 // Two equivalent ways to write this code
robert@464 49
robert@464 50 // The long way, using the buffers directly:
chris@543 51 // context->audioOut[n * context->audioOutChannels + channel] = out;
robert@464 52
robert@464 53 // Or using the macros:
robert@464 54 audioWrite(context, n, channel, out);
robert@464 55 }
robert@464 56 }
robert@464 57 }
robert@464 58
robert@464 59 void cleanup(BelaContext *context, void *userData)
robert@464 60 {
robert@464 61
robert@464 62 }
robert@464 63
robert@464 64
robert@464 65 /**
robert@500 66 \example sinetone/render.cpp
robert@464 67
robert@464 68 Producing your first bleep!
robert@464 69 ---------------------------
robert@464 70
robert@464 71 This sketch is the hello world of embedded interactive audio. Better known as bleep, it
robert@464 72 produces a sine tone.
robert@464 73
robert@464 74 The frequency of the sine tone is determined by a global variable, `gFrequency`
robert@464 75 (line 12). The sine tone is produced by incrementing the phase of a sin function
robert@464 76 on every audio frame.
robert@464 77
robert@464 78 In render() you'll see a nested for loop structure. You'll see this in all Bela projects.
robert@464 79 The first for loop cycles through 'audioFrames', the second through 'audioChannels' (in this case left 0 and right 1).
robert@464 80 It is good to familiarise yourself with this structure as it's fundamental to producing sound with the system.
robert@464 81 */