Skip to content

Commit 48e87e0

Browse files
committed
New Minim for 2.0
1 parent b76a1d9 commit 48e87e0

36 files changed

Lines changed: 1625 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* This sketch demonstrates how to use an FFT to analyze
3+
* the audio being generated by an AudioPlayer.
4+
* <p>
5+
* FFT stands for Fast Fourier Transform, which is a
6+
* method of analyzing audio that allows you to visualize
7+
* the frequency content of a signal. You've seen
8+
* visualizations like this before in music players
9+
* and car stereos.
10+
*/
11+
12+
import ddf.minim.analysis.*;
13+
import ddf.minim.*;
14+
15+
Minim minim;
16+
AudioPlayer jingle;
17+
FFT fft;
18+
19+
void setup()
20+
{
21+
size(512, 200, P3D);
22+
23+
minim = new Minim(this);
24+
25+
// specify that we want the audio buffers of the AudioPlayer
26+
// to be 1024 samples long because our FFT needs to have
27+
// a power-of-two buffer size and this is a good size.
28+
jingle = minim.loadFile("jingle.mp3", 1024);
29+
30+
// loop the file indefinitely
31+
jingle.loop();
32+
33+
// create an FFT object that has a time-domain buffer
34+
// the same size as jingle's sample buffer
35+
// note that this needs to be a power of two
36+
// and that it means the size of the spectrum will be half as large.
37+
fft = new FFT( jingle.bufferSize(), jingle.sampleRate() );
38+
39+
}
40+
41+
void draw()
42+
{
43+
background(0);
44+
stroke(255);
45+
46+
// perform a forward FFT on the samples in jingle's mix buffer,
47+
// which contains the mix of both the left and right channels of the file
48+
fft.forward( jingle.mix );
49+
50+
for(int i = 0; i < fft.specSize(); i++)
51+
{
52+
// draw the line for frequency band i, scaling it up a bit so we can see it
53+
line( i, height, i, height - fft.getBand(i)*8 );
54+
}
55+
}
151 KB
Binary file not shown.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Get Meta Data
3+
* by Damien Di Fede.
4+
*
5+
* This sketch demonstrates how to use the <code>getMetaData</code>
6+
* method of <code>AudioPlayer</code>. This method is also available
7+
* for <code>AudioSnippet</code> and <code>AudioSample</code>.
8+
* You should use this method when you want to retrieve metadata
9+
* about a file that you have loaded, like ID3 tags from an mp3 file.
10+
* If you load WAV file or other non-tagged file, most of the metadata
11+
* will be empty, but you will still have information like the filename
12+
* and the length.
13+
*/
14+
15+
import ddf.minim.*;
16+
17+
Minim minim;
18+
AudioPlayer groove;
19+
AudioMetaData meta;
20+
21+
void setup()
22+
{
23+
size(512, 256, P2D);
24+
25+
minim = new Minim(this);
26+
groove = minim.loadFile("groove.mp3");
27+
meta = groove.getMetaData();
28+
29+
textFont(createFont("Serif", 12));
30+
}
31+
32+
int ys = 25;
33+
int yi = 15;
34+
35+
void draw()
36+
{
37+
background(0);
38+
int y = ys;
39+
text("File Name: " + meta.fileName(), 5, y);
40+
text("Length (in milliseconds): " + meta.length(), 5, y+=yi);
41+
text("Title: " + meta.title(), 5, y+=yi);
42+
text("Author: " + meta.author(), 5, y+=yi);
43+
text("Album: " + meta.album(), 5, y+=yi);
44+
text("Date: " + meta.date(), 5, y+=yi);
45+
text("Comment: " + meta.comment(), 5, y+=yi);
46+
text("Track: " + meta.track(), 5, y+=yi);
47+
text("Genre: " + meta.genre(), 5, y+=yi);
48+
text("Copyright: " + meta.copyright(), 5, y+=yi);
49+
text("Disc: " + meta.disc(), 5, y+=yi);
50+
text("Composer: " + meta.composer(), 5, y+=yi);
51+
text("Orchestra: " + meta.orchestra(), 5, y+=yi);
52+
text("Publisher: " + meta.publisher(), 5, y+=yi);
53+
text("Encoded: " + meta.encoded(), 5, y+=yi);
54+
}
55+
56+
57+
void stop()
58+
{
59+
// always close Minim audio classes when you are done with them
60+
groove.close();
61+
// always stop Minim before exiting
62+
minim.stop();
63+
64+
super.stop();
65+
}
424 KB
Binary file not shown.
22 KB
Binary file not shown.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* This sketch demonstrates how to monitor the currently active audio input
3+
* of the computer using an <code>AudioInput</code>. What you will actually
4+
* be monitoring depends on the current settings of the machine the sketch is running on.
5+
* Typically, you will be monitoring the built-in microphone, but if running on a desktop
6+
* its feasible that the user may have the actual audio output of the computer
7+
* as the active audio input, or something else entirely.
8+
* <p>
9+
* When you run your sketch as an applet you will need to sign it in order to get an input.
10+
*/
11+
12+
import ddf.minim.*;
13+
14+
Minim minim;
15+
AudioInput in;
16+
17+
void setup()
18+
{
19+
size(512, 200, P3D);
20+
21+
minim = new Minim(this);
22+
23+
// use the getLineIn method of the Minim object to get an AudioInput
24+
in = minim.getLineIn();
25+
26+
// uncomment this line to *hear* what is being monitored, in addition to seeing it
27+
in.enableMonitoring();
28+
}
29+
30+
void draw()
31+
{
32+
background(0);
33+
stroke(255);
34+
35+
// draw the waveforms so we can see what we are monitoring
36+
for(int i = 0; i < in.bufferSize() - 1; i++)
37+
{
38+
line( i, 50 + in.left.get(i)*50, i+1, 50 + in.left.get(i+1)*50 );
39+
line( i, 150 + in.right.get(i)*50, i+1, 150 + in.right.get(i+1)*50 );
40+
}
41+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* This sketch demonstrates how to create a simple synthesis chain that
3+
* involves controlling the value of a UGenInput with the output of
4+
* a UGen. In this case, we patch an Oscil generating a sine wave into
5+
* the amplitude input of an Oscil generating a square wave. The result
6+
* is known as amplitude modulation.
7+
*/
8+
9+
import ddf.minim.*;
10+
import ddf.minim.ugens.*;
11+
12+
Minim minim;
13+
AudioOutput out;
14+
Oscil wave;
15+
Oscil mod;
16+
17+
void setup()
18+
{
19+
size(512, 200, P3D);
20+
21+
minim = new Minim(this);
22+
23+
// use the getLineOut method of the Minim object to get an AudioOutput object
24+
out = minim.getLineOut();
25+
26+
// create a triangle wave Oscil, set to 440 Hz, at 1.0 amplitude
27+
// in this case, the amplitude we construct the Oscil with
28+
// doesn't matter because we will be patching something to
29+
// its amplitude input.
30+
wave = new Oscil( 440, 1.0f, Waves.TRIANGLE );
31+
32+
// create a sine wave Oscil for modulating the amplitude of wave
33+
mod = new Oscil( 2, 0.4f, Waves.SINE );
34+
35+
// connect up the modulator
36+
mod.patch( wave.amplitude );
37+
38+
// patch wave to the output
39+
wave.patch( out );
40+
}
41+
42+
void draw()
43+
{
44+
background(0);
45+
stroke(255);
46+
47+
// draw the waveforms
48+
for(int i = 0; i < out.bufferSize() - 1; i++)
49+
{
50+
line( i, 50 + out.left.get(i)*50, i+1, 50 + out.left.get(i+1)*50 );
51+
line( i, 150 + out.right.get(i)*50, i+1, 150 + out.right.get(i+1)*50 );
52+
}
53+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* This sketch demonstrates how to play a file with Minim using an AudioPlayer. <br />
3+
* It's also a good example of how to draw the waveform of the audio.
4+
*/
5+
6+
import ddf.minim.*;
7+
8+
Minim minim;
9+
AudioPlayer player;
10+
11+
void setup()
12+
{
13+
size(512, 200, P3D);
14+
15+
// we pass this to Minim so that it can load files from the data directory
16+
minim = new Minim(this);
17+
18+
// loadFile will look in all the same places as loadImage does.
19+
// this means you can find files that are in the data folder and the
20+
// sketch folder. you can also pass an absolute path, or a URL.
21+
player = minim.loadFile("marcus_kellis_theme.mp3");
22+
23+
// play the file
24+
player.play();
25+
}
26+
27+
void draw()
28+
{
29+
background(0);
30+
stroke(255);
31+
32+
// draw the waveforms
33+
// the values returned by left.get() and right.get() will be between -1 and 1,
34+
// so we need to scale them up to see the waveform
35+
// note that if the file is MONO, left.get() and right.get() will return the same value
36+
for(int i = 0; i < player.bufferSize() - 1; i++)
37+
{
38+
float x1 = map( i, 0, player.bufferSize(), 0, width );
39+
float x2 = map( i+1, 0, player.bufferSize(), 0, width );
40+
line( x1, 50 + player.left.get(i)*50, x2, 50 + player.left.get(i+1)*50 );
41+
line( x1, 150 + player.right.get(i)*50, x2, 150 + player.right.get(i+1)*50 );
42+
}
43+
}
Binary file not shown.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* This sketch demonstrates how to an <code>AudioRecorder</code> to record audio to disk.
3+
* To use this sketch you need to have something plugged into the line-in on your computer, or else be working on a
4+
* laptop with an active built-in microphone. Press 'r' to toggle recording on and off and the press 's' to save to disk.
5+
* The recorded file will be placed in the sketch folder of the sketch.
6+
*/
7+
8+
import ddf.minim.*;
9+
10+
Minim minim;
11+
AudioInput in;
12+
AudioRecorder recorder;
13+
14+
void setup()
15+
{
16+
size(512, 200, P3D);
17+
18+
minim = new Minim(this);
19+
20+
in = minim.getLineIn();
21+
// create a recorder that will record from the input to the filename specified, using buffered recording
22+
// buffered recording means that all captured audio will be written into a sample buffer
23+
// then when save() is called, the contents of the buffer will actually be written to a file
24+
// the file will be located in the sketch's root folder.
25+
recorder = minim.createRecorder(in, "myrecording.wav", true);
26+
27+
textFont(createFont("Arial", 12));
28+
}
29+
30+
void draw()
31+
{
32+
background(0);
33+
stroke(255);
34+
// draw the waveforms
35+
// the values returned by left.get() and right.get() will be between -1 and 1,
36+
// so we need to scale them up to see the waveform
37+
for(int i = 0; i < in.bufferSize() - 1; i++)
38+
{
39+
line(i, 50 + in.left.get(i)*50, i+1, 50 + in.left.get(i+1)*50);
40+
line(i, 150 + in.right.get(i)*50, i+1, 150 + in.right.get(i+1)*50);
41+
}
42+
43+
if ( recorder.isRecording() )
44+
{
45+
text("Currently recording...", 5, 15);
46+
}
47+
else
48+
{
49+
text("Not recording.", 5, 15);
50+
}
51+
}
52+
53+
void keyReleased()
54+
{
55+
if ( key == 'r' )
56+
{
57+
// to indicate that you want to start or stop capturing audio data, you must call
58+
// beginRecord() and endRecord() on the AudioRecorder object. You can start and stop
59+
// as many times as you like, the audio data will be appended to the end of the buffer
60+
// (in the case of buffered recording) or to the end of the file (in the case of streamed recording).
61+
if ( recorder.isRecording() )
62+
{
63+
recorder.endRecord();
64+
}
65+
else
66+
{
67+
recorder.beginRecord();
68+
}
69+
}
70+
if ( key == 's' )
71+
{
72+
// we've filled the file out buffer,
73+
// now write it to the file we specified in createRecorder
74+
// in the case of buffered recording, if the buffer is large,
75+
// this will appear to freeze the sketch for sometime
76+
// in the case of streamed recording,
77+
// it will not freeze as the data is already in the file and all that is being done
78+
// is closing the file.
79+
// the method returns the recorded audio as an AudioRecording,
80+
// see the example AudioRecorder >> RecordAndPlayback for more about that
81+
recorder.save();
82+
println("Done saving.");
83+
}
84+
}

0 commit comments

Comments
 (0)