annotate examples/11-Extras/userdata/render.cpp @ 556:ce391098f321 prerelease tip

THIS PROJECT HAS MOVED TO https://github.com/BelaPlatform/bela
author Giulio Moro <giuliomoro@yahoo.it>
date Sat, 25 Jun 2016 20:21:00 +0100
parents 8f8809c77dda
children
rev   line source
chris@543 1 /*
chris@543 2 ____ _____ _ _
chris@543 3 | __ )| ____| | / \
chris@543 4 | _ \| _| | | / _ \
chris@543 5 | |_) | |___| |___ / ___ \
chris@543 6 |____/|_____|_____/_/ \_\
chris@543 7
chris@543 8 The platform for ultra-low latency audio and sensor processing
chris@543 9
chris@543 10 http://bela.io
chris@543 11
chris@543 12 A project of the Augmented Instruments Laboratory within the
chris@543 13 Centre for Digital Music at Queen Mary University of London.
chris@543 14 http://www.eecs.qmul.ac.uk/~andrewm
chris@543 15
chris@543 16 (c) 2016 Augmented Instruments Laboratory: Andrew McPherson,
chris@543 17 Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack,
chris@543 18 Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved.
chris@543 19
chris@543 20 The Bela software is distributed under the GNU Lesser General Public License
chris@543 21 (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt
chris@543 22 */
chris@543 23
chris@543 24 #include <Bela.h>
chris@543 25 #include <cmath>
chris@543 26
chris@543 27 float gFrequency = 440.0;
chris@543 28 float gPhase;
chris@543 29 float gInverseSampleRate;
chris@543 30
chris@543 31 bool setup(BelaContext *context, void *userData)
chris@543 32 {
chris@543 33 /*
chris@543 34 * Retrieve the parameter passed in from the Bela_initAudio() call in main.cpp
chris@543 35 */
chris@543 36 if(userData != 0)
chris@543 37 gFrequency = *(float *)userData;
chris@543 38
chris@543 39 gInverseSampleRate = 1.0 / context->audioSampleRate;
chris@543 40 gPhase = 0.0;
chris@543 41
chris@543 42 return true;
chris@543 43 }
chris@543 44
chris@543 45 void render(BelaContext *context, void *userData)
chris@543 46 {
chris@543 47 for(unsigned int n = 0; n < context->audioFrames; n++) {
chris@543 48 float out = 0.8f * sinf(gPhase);
chris@543 49 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate;
chris@543 50 if(gPhase > 2.0 * M_PI)
chris@543 51 gPhase -= 2.0 * M_PI;
chris@543 52
chris@543 53 for(unsigned int channel = 0; channel < context->audioOutChannels; channel++) {
chris@543 54 // Two equivalent ways to write this code
chris@543 55
chris@543 56 // The long way, using the buffers directly:
chris@543 57 // context->audioOut[n * context->audioOutChannels + channel] = out;
chris@543 58
chris@543 59 // Or using the macros:
chris@543 60 audioWrite(context, n, channel, out);
chris@543 61 }
chris@543 62 }
chris@543 63 }
chris@543 64
chris@543 65 void cleanup(BelaContext *context, void *userData)
chris@543 66 {
chris@543 67
chris@543 68 }
chris@543 69
chris@543 70
chris@543 71 /**
chris@543 72 \example userdata/render.cpp
chris@543 73
chris@543 74 Passing parameters using the `*userData` argument
chris@543 75 -------------------------------------------------
chris@543 76
chris@543 77 This sketch demonstrates how to pass command line arguments using the `*userData` argument inside the `setup()` function.
chris@543 78
chris@543 79 In main.cpp we first parse a command line argument `-f` and allocate its value to the variable `frequency`.
chris@543 80 We then pass the address of this variable when we call `Bela_initAudio()`. The variable can now be accessed from the
chris@543 81 `setup()` and `render()` functions inside render.cpp.
chris@543 82
chris@543 83 */