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.*;
 
Minim minim;
AudioInput in;
AudioRecorder recorder;
 
void setup()
{
  size(512, 200);
 
  minim = new Minim(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.

8 thoughts on “Manual: AudioRecorder

  1. first of I really like this library!,
    Is there a way i can overwrite the data in my file instead of having the new recordings append to the audio file?

  2. Just delete the file (File.delete()) before saving.
    @ddf: you should indicate clearly what are the audio file formats supported here (only .wav?). Thanks.

  3. Love the library, find it very useful. Is it possible to change the file name that is being saved to without creating numerous instances of AudioRecorder objects? I want to have one audiorecorder saving a variety of mp3s. Is this feasible?

  4. Thanks a lot for this library! I’d like to record voices within a sound installation.
    How can I control the sound quality ? (something like compressor or just volume adaptation) Is there a tool I can associate to the Audiorecorder?

  5. If you are using an AudioInput as your Recordable source, you should be able to add AudioEffects to it and have them modify the sound before it is recorded. There isn’t a compressor AudioEffect in the library presently, though you could probably dig up code for one on the web.

  6. Any ideas to why when I add multiple AudioRecorder objects I can only record onto the first object defined, any others output a file when I save but there is no audio data stored within the files.

  7. I don’t know why that would be the case. Taking a quick look at the code, it should be the case that you can have multiple recorders recording the same source, so sounds like a bug. Would you be so kind as to use the contact form to send me your code, so I can have a test case?

Comments are closed.