Mercurial > hg > beaglert
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/01-Basics/sinetone/render.cpp Mon Jun 20 16:20:38 2016 +0100 @@ -0,0 +1,86 @@ +/* + ____ _____ _ _ +| __ )| ____| | / \ +| _ \| _| | | / _ \ +| |_) | |___| |___ / ___ \ +|____/|_____|_____/_/ \_\ + +The platform for ultra-low latency audio and sensor processing + +http://bela.io + +A project of the Augmented Instruments Laboratory within the +Centre for Digital Music at Queen Mary University of London. +http://www.eecs.qmul.ac.uk/~andrewm + +(c) 2016 Augmented Instruments Laboratory: Andrew McPherson, + Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack, + Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved. + +The Bela software is distributed under the GNU Lesser General Public License +(LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt +*/ + +#include <Bela.h> +#include <cmath> + +float gFrequency = 440.0; +float gPhase; +float gInverseSampleRate; + +bool setup(BelaContext *context, void *userData) +{ + // Retrieve a parameter passed in from the initAudio() call + if(userData != 0) + gFrequency = *(float *)userData; + + gInverseSampleRate = 1.0 / context->audioSampleRate; + gPhase = 0.0; + + return true; +} + +void render(BelaContext *context, void *userData) +{ + for(unsigned int n = 0; n < context->audioFrames; n++) { + float out = 0.8f * sinf(gPhase); + gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate; + if(gPhase > 2.0 * M_PI) + gPhase -= 2.0 * M_PI; + + for(unsigned int channel = 0; channel < context->audioChannels; channel++) { + // Two equivalent ways to write this code + + // The long way, using the buffers directly: + // context->audioOut[n * context->audioChannels + channel] = out; + + // Or using the macros: + audioWrite(context, n, channel, out); + } + } +} + +void cleanup(BelaContext *context, void *userData) +{ + +} + +/* ------------ Project Explantation ------------ */ + +/** +\example 01-sinetone + +Producing your first bleep! +--------------------------- + +This sketch is the hello world of embedded interactive audio. Better known as bleep, it +produces a sine tone. + +The frequency of the sine tone is determined by a global variable, `gFrequency` +(line 12). The sine tone is produced by incrementing the phase of a sin function +on every audio frame. + +In render() you'll see a nested for loop structure. You'll see this in all Bela projects. +The first for loop cycles through 'audioFrames', the second through 'audioChannels' (in this case left 0 and right 1). +It is good to familiarise yourself with this structure as it's fundamental to producing sound with the system. +*/