annotate projects/basic/render.cpp @ 284:7bfb25a2e158 Doxy prerelease

Merge
author Robert Jack <robert.h.jack@gmail.com>
date Tue, 17 May 2016 15:53:24 +0100
parents ac8eb07afcf5
children 5433c83ce04e
rev   line source
robert@269 1 /*
robert@269 2 ____ _____ _ _
robert@269 3 | __ )| ____| | / \
robert@269 4 | _ \| _| | | / _ \
robert@269 5 | |_) | |___| |___ / ___ \
robert@269 6 |____/|_____|_____/_/ \_\.io
robert@269 7
robert@269 8 */
robert@269 9
andrewm@0 10 /*
andrewm@0 11 * render.cpp
andrewm@0 12 *
andrewm@0 13 * Created on: Oct 24, 2014
andrewm@0 14 * Author: parallels
andrewm@0 15 */
andrewm@0 16
robert@269 17 /**
robert@269 18 \example 1_basic_helloworld
robert@269 19
robert@269 20 Producing your first bleep!
robert@269 21 ---------------------------
robert@269 22
robert@269 23 This sketch is the hello world of embedded interactive audio. Better known as bleep, it
robert@269 24 produces a sine tone.
robert@269 25
robert@269 26 The frequency of the sine tone is determined by a global variable, `gFrequency`
robert@269 27 (line 12). The sine tone is produced by incrementing the phase of a sin function
robert@269 28 on every audio frame.
robert@269 29
robert@269 30 The important thing to notice is the nested `for` loop structure. You will see
robert@269 31 this in all Bela projects and in most digital audio applications. The first `for`
robert@269 32 loop cycles through the audio frames, the second through each of the audio
robert@269 33 channels (in this case left 0 and right 1). It is good to familiarise yourself
robert@269 34 with this structure as it is fundamental to producing sound with the system.
robert@269 35 */
robert@269 36
andrewm@0 37
andrewm@56 38 #include <BeagleRT.h>
andrewm@0 39 #include <cmath>
giuliomoro@180 40 #include <Utilities.h>
andrewm@0 41
andrewm@56 42 float gFrequency = 440.0;
andrewm@0 43 float gPhase;
andrewm@0 44 float gInverseSampleRate;
andrewm@0 45
andrewm@56 46 // setup() is called once before the audio rendering starts.
andrewm@0 47 // Use it to perform any initialisation and allocation which is dependent
andrewm@0 48 // on the period size or sample rate.
andrewm@0 49 //
andrewm@0 50 // userData holds an opaque pointer to a data structure that was passed
andrewm@0 51 // in from the call to initAudio().
andrewm@0 52 //
andrewm@0 53 // Return true on success; returning false halts the program.
andrewm@56 54 bool setup(BeagleRTContext *context, void *userData)
andrewm@0 55 {
andrewm@0 56 // Retrieve a parameter passed in from the initAudio() call
andrewm@56 57 if(userData != 0)
andrewm@56 58 gFrequency = *(float *)userData;
andrewm@0 59
andrewm@45 60 gInverseSampleRate = 1.0 / context->audioSampleRate;
andrewm@0 61 gPhase = 0.0;
andrewm@0 62
andrewm@0 63 return true;
andrewm@0 64 }
andrewm@0 65
andrewm@0 66 // render() is called regularly at the highest priority by the audio engine.
andrewm@0 67 // Input and output are given from the audio hardware and the other
andrewm@0 68 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
andrewm@0 69 // will be 0.
andrewm@0 70
andrewm@45 71 void render(BeagleRTContext *context, void *userData)
andrewm@0 72 {
andrewm@45 73 for(unsigned int n = 0; n < context->audioFrames; n++) {
andrewm@0 74 float out = 0.8f * sinf(gPhase);
andrewm@0 75 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate;
andrewm@0 76 if(gPhase > 2.0 * M_PI)
andrewm@0 77 gPhase -= 2.0 * M_PI;
andrewm@0 78
giuliomoro@180 79 for(unsigned int channel = 0; channel < context->audioChannels; channel++) {
giuliomoro@180 80 // Two equivalent ways to write this code
giuliomoro@180 81
giuliomoro@180 82 // The long way, using the buffers directly:
giuliomoro@180 83 // context->audioOut[n * context->audioChannels + channel] = out;
giuliomoro@180 84
giuliomoro@180 85 // Or using the macros:
giuliomoro@180 86 audioWriteFrame(context, n, channel, out);
giuliomoro@180 87 }
andrewm@0 88 }
andrewm@0 89 }
andrewm@0 90
andrewm@56 91 // cleanup() is called once at the end, after the audio has stopped.
andrewm@56 92 // Release any resources that were allocated in setup().
andrewm@0 93
andrewm@56 94 void cleanup(BeagleRTContext *context, void *userData)
andrewm@0 95 {
andrewm@0 96
andrewm@0 97 }