Manual: AudioRecorder
[ javadoc ]
An AudioRecorder associates a Recordable object with a SampleRecorder and acts as a proxy to the SampleRecorder methods. You can get an AudioRecorder from Minim by using:
AudioRecorder createRecorder(Recordable source, String fileName, boolean buffered)
source is the Recordable object you want to record, fileName is the name of the file that you want to save to, and buffered is whether or not you want to use a SampleRecorder that buffers all audio in memory before writing to disk, or one that streams to a file on disk. The reason this option exists is that not all file formats can be streamed by the default Javasound implementation. Minim will ask the implementation it is using for a SampleRecorder of the type requested and if doesn’t get one will report an error and return null.
AudioRecorder has methods with pretty self-explanatory names:
// Begins recording audio from the current record source. void beginRecord() // Halts the recording of audio from the current record source. void endRecord() // Returns the current record state. boolean isRecording() // Requests the current SampleRecorder to save and // returns the recorded audio as an AudioRecording AudioRecording save() // Sets the record source for this recorder. void setRecordSource(Recordable recordSource) // Sets the SampleRecorder for this recorder. void setSampleRecorder(SampleRecorder recorder)
Code Sample
import ddf.minim.*; AudioInput in; AudioRecorder recorder; void setup() { size(512, 200); // always start Minim before you do anything with it Minim.start(this); // get a stereo line-in: sample buffer length of 512 // default sample rate is 44100, default bit depth is 16 in = Minim.getLineIn(Minim.STEREO, 512); // create a recorder that will record from the input // to the filename specified, using buffered recording // buffered recording means that all captured audio // will be written into a sample buffer // then when save() is called, the contents of the buffer // will actually be written to a file // the file will be located in the sketch's root folder. recorder = Minim.createRecorder(in, "myrecording.wav", true); textFont(createFont("Arial", 12)); } void draw() { background(0); stroke(255); for(int i = 0; i < in.left.size()-1; i++) { line(i, 50 + in.left.get(i)*50, i+1, 50 + in.left.get(i+1)*50); line(i, 150 + in.right.get(i)*50, i+1, 150 + in.right.get(i+1)*50); } if ( recorder.isRecording() ) { text("Currently recording...", 5, 15); } else { text("Not recording.", 5, 15); } } void keyReleased() { if ( key == 'r' ) { // to indicate that you want to start or stop capturing audio data, // you must call beginRecord() and endRecord() on the AudioRecorder object. // You can start and stop as many times as you like, the audio data // will be appended to the end of the buffer // (in the case of buffered recording) // or to the end of the file (in the case of streamed recording). if ( recorder.isRecording() ) { recorder.endRecord(); } else { recorder.beginRecord(); } } if ( key == 's' ) { // we've filled the file out buffer, // now write it to the file we specified in createRecorder // in the case of buffered recording, if the buffer is large, // this will appear to freeze the sketch for sometime // in the case of streamed recording, // it will not freeze as the data is already // in the file and all that is being done // is closing the file. // the method returns the recorded audio as an AudioRecording, // see the example AudioRecorder >> RecordAndPlayback for more about that recorder.save(); println("Done saving."); } } void stop() { // always close Minim audio classes when you are done with them in.close(); // always stop Minim before exiting Minim.stop; super.stop(); }
Explicit Construction
It is possible to create an AudioRecorder using its constructor:
AudioRecorder(Recordable source, SampleRecorder recorder)
To do this, you need to already have an object of a class that implements SampleRecorder. Maybe you have written a class that can encode ogg files, you need only make it implement the SampleRecorder interface and then you could use an instance of it to create an AudioRecorder.