chris@543: /* chris@543: ____ _____ _ _ chris@543: | __ )| ____| | / \ chris@543: | _ \| _| | | / _ \ chris@543: | |_) | |___| |___ / ___ \ chris@543: |____/|_____|_____/_/ \_\ chris@543: chris@543: The platform for ultra-low latency audio and sensor processing chris@543: chris@543: http://bela.io chris@543: chris@543: A project of the Augmented Instruments Laboratory within the chris@543: Centre for Digital Music at Queen Mary University of London. chris@543: http://www.eecs.qmul.ac.uk/~andrewm chris@543: chris@543: (c) 2016 Augmented Instruments Laboratory: Andrew McPherson, chris@543: Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack, chris@543: Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved. chris@543: chris@543: The Bela software is distributed under the GNU Lesser General Public License chris@543: (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt chris@543: */ chris@543: chris@543: #include chris@543: #include chris@543: chris@543: float gFrequency = 440.0; chris@543: float gPhase; chris@543: float gInverseSampleRate; chris@543: chris@543: bool setup(BelaContext *context, void *userData) chris@543: { chris@543: /* chris@543: * Retrieve the parameter passed in from the Bela_initAudio() call in main.cpp chris@543: */ chris@543: if(userData != 0) chris@543: gFrequency = *(float *)userData; chris@543: chris@543: gInverseSampleRate = 1.0 / context->audioSampleRate; chris@543: gPhase = 0.0; chris@543: chris@543: return true; chris@543: } chris@543: chris@543: void render(BelaContext *context, void *userData) chris@543: { chris@543: for(unsigned int n = 0; n < context->audioFrames; n++) { chris@543: float out = 0.8f * sinf(gPhase); chris@543: gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate; chris@543: if(gPhase > 2.0 * M_PI) chris@543: gPhase -= 2.0 * M_PI; chris@543: chris@543: for(unsigned int channel = 0; channel < context->audioOutChannels; channel++) { chris@543: // Two equivalent ways to write this code chris@543: chris@543: // The long way, using the buffers directly: chris@543: // context->audioOut[n * context->audioOutChannels + channel] = out; chris@543: chris@543: // Or using the macros: chris@543: audioWrite(context, n, channel, out); chris@543: } chris@543: } chris@543: } chris@543: chris@543: void cleanup(BelaContext *context, void *userData) chris@543: { chris@543: chris@543: } chris@543: chris@543: chris@543: /** chris@543: \example userdata/render.cpp chris@543: chris@543: Passing parameters using the `*userData` argument chris@543: ------------------------------------------------- chris@543: chris@543: This sketch demonstrates how to pass command line arguments using the `*userData` argument inside the `setup()` function. chris@543: chris@543: In main.cpp we first parse a command line argument `-f` and allocate its value to the variable `frequency`. chris@543: We then pass the address of this variable when we call `Bela_initAudio()`. The variable can now be accessed from the chris@543: `setup()` and `render()` functions inside render.cpp. chris@543: chris@543: */