annotate examples/04-Audio/tremolo/render.cpp @ 533:2ec36efb2c52 prerelease

Added tremolo to audio examples.
author Robert Jack <robert.h.jack@gmail.com>
date Thu, 23 Jun 2016 21:17:16 +0100
parents
children e2364e1711c2
rev   line source
robert@533 1 /*
robert@533 2 ____ _____ _ _
robert@533 3 | __ )| ____| | / \
robert@533 4 | _ \| _| | | / _ \
robert@533 5 | |_) | |___| |___ / ___ \
robert@533 6 |____/|_____|_____/_/ \_\
robert@533 7
robert@533 8 The platform for ultra-low latency audio and sensor processing
robert@533 9
robert@533 10 http://bela.io
robert@533 11
robert@533 12 A project of the Augmented Instruments Laboratory within the
robert@533 13 Centre for Digital Music at Queen Mary University of London.
robert@533 14 http://www.eecs.qmul.ac.uk/~andrewm
robert@533 15
robert@533 16 (c) 2016 Augmented Instruments Laboratory: Andrew McPherson,
robert@533 17 Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack,
robert@533 18 Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved.
robert@533 19
robert@533 20 The Bela software is distributed under the GNU Lesser General Public License
robert@533 21 (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt
robert@533 22 */
robert@533 23
robert@533 24 #include <Bela.h>
robert@533 25 #include <cmath>
robert@533 26
robert@533 27 float gFrequency = 4.0;
robert@533 28 float gPhase;
robert@533 29 float gInverseSampleRate;
robert@533 30
robert@533 31 bool setup(BelaContext *context, void *userData)
robert@533 32 {
robert@533 33
robert@533 34 gInverseSampleRate = 1.0 / context->audioSampleRate;
robert@533 35 gPhase = 0.0;
robert@533 36
robert@533 37 return true;
robert@533 38 }
robert@533 39
robert@533 40 void render(BelaContext *context, void *userData)
robert@533 41 {
robert@533 42 // Nested for loops for audio channels
robert@533 43 for(unsigned int n = 0; n < context->audioFrames; n++) {
robert@533 44
robert@533 45 // Generate a sinewave with frequency set by gFrequency
robert@533 46 // and amplitude from -0.5 to 0.5
robert@533 47 float lfo = sinf(gPhase) * 0.5;
robert@533 48 // Keep track and wrap the phase of the sinewave
robert@533 49 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate;
robert@533 50 if(gPhase > 2.0 * M_PI)
robert@533 51 gPhase -= 2.0 * M_PI;
robert@533 52
robert@533 53 for(unsigned int channel = 0; channel < context->audioChannels; channel++) {
robert@533 54 // Read the audio input and half the amplitude
robert@533 55 float input = audioRead(context, n, channel) * 0.5;
robert@533 56 // Write to audio output the audio input multiplied by the sinewave
robert@533 57 audioWrite(context, n, channel, (input*lfo));
robert@533 58
robert@533 59 }
robert@533 60 }
robert@533 61
robert@533 62 // Nested for loops for analog channels
robert@533 63 for(unsigned int n = 0; n < context->analogFrames; n++) {
robert@533 64 for(unsigned int ch = 0; ch < context->analogChannels; ch++) {
robert@533 65 // Read analog channel 0 and map the range from 0-1 to 0.25-20
robert@533 66 // use this to set the value of gFrequency
robert@533 67 gFrequency = map(analogRead(context, n, 0), 0.0, 1.0, 0.25, 20.0);
robert@533 68
robert@533 69 }
robert@533 70 }
robert@533 71
robert@533 72 }
robert@533 73
robert@533 74 void cleanup(BelaContext *context, void *userData)
robert@533 75 {
robert@533 76
robert@533 77 }
robert@533 78
robert@533 79
robert@533 80 /**
robert@533 81 \example tremolo/render.cpp
robert@533 82
robert@533 83 A simple tremolo effect
robert@533 84 -----------------------
robert@533 85
robert@533 86 This sketch demonstrates how to make a simple tremolo effect with one potiometer to
robert@533 87 control the rate of the effect. A tremolo effect is a simple type of amplitude modulation
robert@533 88 where the amplitude of one signal is continuous modulated by the amplitude of another.
robert@533 89 This is achieved by multiplying to signals together.
robert@533 90
robert@533 91 In this example we want to create a tremolo effect like that you would find in a guitar
robert@533 92 effects box so our first signal will be our audio input into which we could plug a guitar
robert@533 93 or external sound source. This will be our 'carrier' signal.
robert@533 94
robert@533 95 The second signal that we will use, the 'modulator', will be a low freqeuncy oscillator (LFO),
robert@533 96 in this case a sinetone which we will generate in the same way as the 01-Basic/sinetone example.
robert@533 97 The frequency of this sinetone is determined by a global variable, `gFrequency`. Again, the
robert@533 98 sinetone is produced by incrementing the phase of a sine function on every audio frame.
robert@533 99
robert@533 100 In `render()` you'll see two nested for loop structures, one for audio and the other for the
robert@533 101 analogs. You should be pretty familiar with this structure by now. In the first of these for loops
robert@533 102 we deal with all the audio -- in the second with reading the analog input channels. We read the
robert@533 103 value of analog input 0 and map it to an appropriate range for controlling the frequency
robert@533 104 of the sine tone.
robert@533 105
robert@533 106 The lfo is then mulitplied together with the audio input and sent to the audio output.
robert@533 107 */