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