@@ -23,30 +23,27 @@ static final byte BEACON_KEY = 42;
23
23
// Serial baud rate. Should match desktop program.
24
24
static final int BAUD_RATE = 115200 ;
25
25
26
- Minim minim;
27
- AudioInput in;
28
- SelectInput si;
29
- BeatDetect beat;
30
- FFT fft;
31
- Serial serial_port;
32
- PluginHandler plugin;
26
+ Minim minim; // Needs global for stop() and setup()
27
+ AudioInput in; // Needs global for stop() and setup()
28
+ Serial serial_port; // Needs global for draw() and setup()
29
+ PluginHandler plugin; // Needs global for draw() and setup()
33
30
34
31
void setup ()
35
32
{
36
33
// Set up minim / audio input
37
34
minim = new Minim (this );
38
- autodetectInput();
35
+ in = autodetectInput();
39
36
40
37
// Set up BeatDetect
41
- beat = new BeatDetect (in. bufferSize(), in. sampleRate());
38
+ BeatDetect beat = new BeatDetect (in. bufferSize(), in. sampleRate());
42
39
new BeatListener (beat, in);
43
40
44
41
// Set up FFT
45
- fft = new FFT (in. bufferSize(), in. sampleRate());
42
+ FFT fft = new FFT (in. bufferSize(), in. sampleRate());
46
43
new FFTListener (fft, in);
47
44
48
45
// Set up Serial
49
- autodetectSerial();
46
+ serial_port = autodetectSerial();
50
47
51
48
// Set frame rate of draw()
52
49
frameRate (FRAME_RATE );
@@ -64,9 +61,9 @@ void setup()
64
61
}
65
62
66
63
// Trys to find and set the Soundflower (2ch) input
67
- void autodetectInput()
64
+ AudioInput autodetectInput()
68
65
{
69
- si = new SelectInput (Minim . STEREO , BUFFER_SIZE );
66
+ SelectInput si = new SelectInput (Minim . STEREO , BUFFER_SIZE );
70
67
Mixer . Info [] m = si. getInputs();
71
68
72
69
// First we will set a default index, in case we don't find it.
@@ -78,11 +75,11 @@ void autodetectInput()
78
75
current_input = i;
79
76
80
77
// Now we set the input. If soundflower was found it should be set.
81
- in = si. setInput(si. getInputs()[current_input]);
78
+ return si. setInput(si. getInputs()[current_input]);
82
79
}
83
80
84
81
// Finds the correct serial device to connect to
85
- void autodetectSerial()
82
+ Serial autodetectSerial()
86
83
{
87
84
// Run through each Serial device
88
85
for (int i = 0 ; i < Serial . list(). length; i++ )
@@ -103,9 +100,11 @@ void autodetectSerial()
103
100
delay (BEACON_PERIOD + 1 );
104
101
// If the device sends us a matching byte, we found it
105
102
if (serial_port. read() == BEACON_KEY )
106
- return ;
103
+ return serial_port ;
107
104
}
108
105
}
106
+ // If we've gotten this far, there are no more devices left
107
+ return null ;
109
108
}
110
109
111
110
void draw ()
@@ -135,4 +134,4 @@ public void stop() {
135
134
136
135
boolean displayable () {
137
136
return VISIBLE ;
138
- }
137
+ }
0 commit comments