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 <rtdk.h>
|
robert@464
|
27 #include <cmath>
|
robert@464
|
28
|
robert@464
|
29 // Set range for analog outputs designed for driving LEDs
|
robert@464
|
30 const float kMinimumAmplitude = (1.5 / 5.0);
|
robert@464
|
31 const float kAmplitudeRange = 1.0 - kMinimumAmplitude;
|
robert@464
|
32
|
robert@464
|
33 float gFrequency;
|
robert@464
|
34 float gPhase;
|
robert@464
|
35 float gInverseSampleRate;
|
robert@464
|
36
|
robert@464
|
37 bool setup(BelaContext *context, void *userData)
|
robert@464
|
38 {
|
robert@464
|
39 // Retrieve a parameter passed in from the initAudio() call
|
robert@464
|
40 gFrequency = *(float *)userData;
|
robert@464
|
41
|
robert@464
|
42 if(context->analogFrames == 0) {
|
robert@464
|
43 rt_printf("Error: this example needs the matrix enabled\n");
|
robert@464
|
44 return false;
|
robert@464
|
45 }
|
robert@464
|
46
|
robert@464
|
47 gInverseSampleRate = 1.0 / context->analogSampleRate;
|
robert@464
|
48 gPhase = 0.0;
|
robert@464
|
49
|
robert@464
|
50 return true;
|
robert@464
|
51 }
|
robert@464
|
52
|
robert@464
|
53 void render(BelaContext *context, void *userData)
|
robert@464
|
54 {
|
robert@464
|
55 for(unsigned int n = 0; n < context->analogFrames; n++) {
|
robert@464
|
56 // Set LED to different phase for each matrix channel
|
robert@464
|
57 float relativePhase = 0.0;
|
robert@464
|
58 for(unsigned int channel = 0; channel < context->analogChannels; channel++) {
|
robert@464
|
59 float out = kMinimumAmplitude + kAmplitudeRange * 0.5f * (1.0f + sinf(gPhase + relativePhase));
|
robert@464
|
60
|
robert@464
|
61 analogWrite(context, n, channel, out);
|
robert@464
|
62
|
robert@464
|
63 // Advance by pi/4 (1/8 of a full rotation) for each channel
|
robert@464
|
64 relativePhase += M_PI * 0.25;
|
robert@464
|
65 }
|
robert@464
|
66
|
robert@464
|
67 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate;
|
robert@464
|
68 if(gPhase > 2.0 * M_PI)
|
robert@464
|
69 gPhase -= 2.0 * M_PI;
|
robert@464
|
70 }
|
robert@464
|
71 }
|
robert@464
|
72
|
robert@464
|
73 void cleanup(BelaContext *context, void *userData)
|
robert@464
|
74 {
|
robert@464
|
75
|
robert@464
|
76 }
|
robert@464
|
77
|
robert@464
|
78 /* ------------ Project Explantation ------------ */
|
robert@464
|
79
|
robert@464
|
80 /**
|
robert@500
|
81 \example analog-output/render.cpp
|
robert@464
|
82
|
robert@464
|
83 Fading LEDs
|
robert@464
|
84 -----------
|
robert@464
|
85
|
robert@464
|
86 This sketch uses a sine wave to drive the brightness of a series of LEDs
|
robert@464
|
87 connected to the eight analog out pins. Again you can see the nested `for` loop
|
robert@464
|
88 structure but this time for the analog output channels rather than the audio.
|
robert@464
|
89
|
robert@464
|
90 - connect an LED in series with a 470ohm resistor between each of the analogOut pins and ground.
|
robert@464
|
91
|
robert@464
|
92 Within the first for loop in render we cycle through each frame in the analog
|
robert@464
|
93 output matrix. At each frame we then cycle through the analog output channels
|
robert@464
|
94 with another for loop and set the output voltage according to the phase of a
|
robert@464
|
95 sine tone that acts as an LFO. The analog output pins can provide a voltage of
|
robert@464
|
96 ~4.092V.
|
robert@464
|
97
|
robert@464
|
98 The output on each pin is set with `analogWrite()` within the for loop that
|
robert@464
|
99 cycles through the analog output channels. This needs to be provided with
|
robert@464
|
100 arguments as follows `analogWrite(context, n, channel, out)`. Channel is
|
robert@464
|
101 where the you give the address of the analog output pin (in this case we cycle
|
robert@464
|
102 through each pin address in the for loop), out is the variable that holds the
|
robert@464
|
103 desired output (in this case set by the sine wave).
|
robert@464
|
104
|
robert@464
|
105 Notice that the phase of the brightness cycle for each led is different. This
|
robert@464
|
106 is achieved by updating a variable that stores a relative phase value. This
|
robert@464
|
107 variable is advanced by pi/4 (1/8 of a full rotation) for each channel giving
|
robert@464
|
108 each of the eight LEDs a different phase.
|
robert@464
|
109 */
|