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