annotate examples/03-Analog/analog-output/render.cpp @ 542:3016638b4da2 prerelease

Analog examples updated
author Robert Jack <robert.h.jack@gmail.com>
date Fri, 24 Jun 2016 13:00:31 +0100
parents 1cec96845a23
children
rev   line source
robert@464 1 /*
robert@464 2 ____ _____ _ _
robert@464 3 | __ )| ____| | / \
robert@464 4 | _ \| _| | | / _ \
robert@464 5 | |_) | |___| |___ / ___ \
robert@464 6 |____/|_____|_____/_/ \_\
robert@464 7
robert@464 8 The platform for ultra-low latency audio and sensor processing
robert@464 9
robert@464 10 http://bela.io
robert@464 11
robert@464 12 A project of the Augmented Instruments Laboratory within the
robert@464 13 Centre for Digital Music at Queen Mary University of London.
robert@464 14 http://www.eecs.qmul.ac.uk/~andrewm
robert@464 15
robert@464 16 (c) 2016 Augmented Instruments Laboratory: Andrew McPherson,
robert@464 17 Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack,
robert@464 18 Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved.
robert@464 19
robert@464 20 The Bela software is distributed under the GNU Lesser General Public License
robert@464 21 (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt
robert@464 22 */
robert@464 23
robert@464 24
robert@464 25 #include <Bela.h>
robert@464 26 #include <cmath>
robert@464 27
robert@464 28 // Set range for analog outputs designed for driving LEDs
robert@464 29 const float kMinimumAmplitude = (1.5 / 5.0);
robert@464 30 const float kAmplitudeRange = 1.0 - kMinimumAmplitude;
robert@464 31
robert@542 32 float gFrequency = 3.0;
robert@464 33 float gPhase;
robert@464 34 float gInverseSampleRate;
robert@464 35
robert@464 36 bool setup(BelaContext *context, void *userData)
robert@464 37 {
robert@464 38
robert@542 39 // Check if analog channels are enabled
robert@542 40 if(context->analogFrames == 0 || context->analogFrames > context->audioFrames) {
robert@542 41 rt_printf("Error: this example needs analog enabled, with 4 or 8 channels\n");
robert@542 42 return false;
robert@542 43 }
robert@542 44
robert@542 45 // Check that we have the same number of inputs and outputs.
robert@542 46 if(context->audioInChannels != context->audioOutChannels ||
robert@542 47 context->analogInChannels != context-> analogOutChannels){
robert@542 48 printf("Error: for this project, you need the same number of input and output channels.\n");
robert@464 49 return false;
robert@464 50 }
robert@464 51
robert@464 52 gInverseSampleRate = 1.0 / context->analogSampleRate;
robert@464 53 gPhase = 0.0;
robert@464 54
robert@464 55 return true;
robert@464 56 }
robert@464 57
robert@464 58 void render(BelaContext *context, void *userData)
robert@464 59 {
robert@464 60 for(unsigned int n = 0; n < context->analogFrames; n++) {
robert@464 61 // Set LED to different phase for each matrix channel
robert@464 62 float relativePhase = 0.0;
robert@542 63 for(unsigned int channel = 0; channel < context->analogOutChannels; channel++) {
robert@464 64 float out = kMinimumAmplitude + kAmplitudeRange * 0.5f * (1.0f + sinf(gPhase + relativePhase));
robert@464 65
robert@464 66 analogWrite(context, n, channel, out);
robert@464 67
robert@464 68 // Advance by pi/4 (1/8 of a full rotation) for each channel
robert@464 69 relativePhase += M_PI * 0.25;
robert@464 70 }
robert@464 71
robert@542 72 // Update and wrap phase of sine tone
robert@464 73 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate;
robert@464 74 if(gPhase > 2.0 * M_PI)
robert@464 75 gPhase -= 2.0 * M_PI;
robert@464 76 }
robert@464 77 }
robert@464 78
robert@464 79 void cleanup(BelaContext *context, void *userData)
robert@464 80 {
robert@464 81
robert@464 82 }
robert@464 83
robert@464 84
robert@464 85 /**
robert@500 86 \example analog-output/render.cpp
robert@464 87
robert@464 88 Fading LEDs
robert@464 89 -----------
robert@464 90
robert@464 91 This sketch uses a sine wave to drive the brightness of a series of LEDs
robert@464 92 connected to the eight analog out pins. Again you can see the nested `for` loop
robert@464 93 structure but this time for the analog output channels rather than the audio.
robert@464 94
robert@464 95 - connect an LED in series with a 470ohm resistor between each of the analogOut pins and ground.
robert@464 96
robert@464 97 Within the first for loop in render we cycle through each frame in the analog
robert@464 98 output matrix. At each frame we then cycle through the analog output channels
robert@464 99 with another for loop and set the output voltage according to the phase of a
robert@464 100 sine tone that acts as an LFO. The analog output pins can provide a voltage of
robert@464 101 ~4.092V.
robert@464 102
robert@464 103 The output on each pin is set with `analogWrite()` within the for loop that
robert@464 104 cycles through the analog output channels. This needs to be provided with
robert@464 105 arguments as follows `analogWrite(context, n, channel, out)`. Channel is
robert@464 106 where the you give the address of the analog output pin (in this case we cycle
robert@464 107 through each pin address in the for loop), out is the variable that holds the
robert@542 108 desired output (in this case set by the sine wave) and `n` is the frame number.
robert@464 109
robert@464 110 Notice that the phase of the brightness cycle for each led is different. This
robert@464 111 is achieved by updating a variable that stores a relative phase value. This
robert@464 112 variable is advanced by pi/4 (1/8 of a full rotation) for each channel giving
robert@464 113 each of the eight LEDs a different phase.
robert@464 114 */