Permalink
Newer
Older
100644 77 lines (71 sloc) 2.75 KB
Johan
Jul 31, 2011
1
// This class is a very simple implementation of AudioListener. By implementing this interface,
2
// you can add instances of this class to any class in Minim that implements Recordable and receive
3
// buffers of samples in a callback fashion. In other words, every time that a Recordable object has
4
// a new buffer of samples, it will send a copy to all of its AudioListeners. You can add an instance of
5
// an AudioListener to a Recordable by using the addListener method of the Recordable. If you want to
6
// remove a listener that you previously added, you call the removeListener method of Recordable, passing
7
// the listener you want to remove.
8
//
9
// Although possible, it is not advised that you add the same listener to more than one Recordable.
10
// Your listener will be called any time any of the Recordables you've added it have new samples. This
11
// means that the stream of samples the listener sees will likely be interleaved buffers of samples from
12
// all of the Recordables it is listening to, which is probably not what you want.
13
//
14
// You'll notice that the three methods of this class are synchronized. This is because the samples methods
15
// will be called from a different thread than the one instances of this class will be created in. That thread
16
// might try to send samples to an instance of this class while the instance is in the middle of drawing the
17
// waveform, which would result in a waveform made up of samples from two different buffers. Synchronizing
18
// all the methods means that while the main thread of execution is inside draw, the thread that calls
19
// samples will block until draw is complete. Likewise, a call to draw will block if the sample thread is inside
20
// one of the samples methods. Hope that's not too confusing!
21
22
class WaveformRenderer implements AudioListener
23
{
24
private float[] left;
25
private float[] right;
26
27
WaveformRenderer()
28
{
29
left = null;
30
right = null;
31
}
32
33
synchronized void samples(float[] samp)
34
{
35
left = samp;
36
}
37
38
synchronized void samples(float[] sampL, float[] sampR)
39
{
40
left = sampL;
41
right = sampR;
42
}
43
44
synchronized void draw()
45
{
46
// we've got a stereo signal if right or left are not null
47
if ( left != null && right != null )
48
{
49
noFill();
50
stroke(255);
51
beginShape();
52
for ( int i = 0; i < left.length; i++ )
53
{
54
vertex(i, height/4 + left[i]*50);
55
}
56
endShape();
57
beginShape();
58
for ( int i = 0; i < right.length; i++ )
59
{
60
vertex(i, 3*(height/4) + right[i]*50);
61
}
62
endShape();
63
}
64
else if ( left != null )
65
{
66
noFill();
67
stroke(255);
68
beginShape();
69
for ( int i = 0; i < left.length; i++ )
70
{
71
vertex(i, height/2 + left[i]*50);
72
}
73
endShape();
74
}
75
}
76
}