record past 15 seconds of microphone with Minim

Hi everyone,

I'd like to be able to record from the microphone using minim, but with a caveat. When the user presses the record button I want to be able to save the past 15 seconds (before the user pressed a button) and the next 15 seconds (after the user pressed a button) to a file (total=30 seconds).

I thought I might be able to do it by setting the buffer to 15 seconds and then have a timer that starts when the user presses the button and when the timer finishes (after 15 seconds) the buffer is saved to a file. I've implemented it below, but I get unexpected results.

Any idea/suggestions how I can record the last 15 seconds of an input stream. I've seen it referred on the next as a "ring buffer" or a "circular buffer" because it keeps recording the last 15 seconds and overwriting things older than 15 in memory. Any ideas?

Thanks. Below is my code.

    import ddf.minim.*;
    Minim minim;
    AudioInput in;
    AudioRecorder recorder;
    Timer timer;

    void setup()
    {
      size(512, 200, P3D);

      minim = new Minim(this);

      int totalDuration = 15; //total buffer size in seconds
      in = minim.getLineIn(Minim.STEREO, 44100 * totalDuration);
      // create a recorder that will record from the input to the filename specified
      // the file will be located in the sketch's root folder.
      recorder = minim.createRecorder(in, "myrecording.wav");

      textFont(createFont("Arial", 12));
    }

    void draw()
    {
      background(0); 
      stroke(255);

      if (recorder.isRecording() && timer.isFinished()) 
      {
        recorder.endRecord();
        recorder.save();
        println("Done saving.");
      }

      if ( recorder.isRecording() )
      {
        text("Currently recording...", 5, 15);
      } else
      {
        text("Not recording.", 5, 15);
      }
    }

    void keyPressed()
    {
      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). 
        recorder.beginRecord();
        timer = new Timer(15000);
        timer.start();
      }
    }

class Timer {

  int savedTime; // When Timer started
  int totalTime; // How long Timer should last

  Timer(int tempTotalTime) {
    totalTime = tempTotalTime;
  }

  void start() {
    savedTime = millis(); 
  }

  boolean isFinished() { 
    int passedTime = millis()- savedTime;
    if (passedTime > totalTime) {
      return true;
    } else {
      return false;
    }
  }
}
Tagged:

Answers

  • Bumping thread to see what answers come up.

  • Answer ✓

    I asked the original question and I finally resorted to using Python to do all the audio stuff I recorded audio using a python audio library, saved audio "frames" in a cyclical buffer (called deque in python/c++) and then saved the audio to a file when I needed too.

    Minim was a nightmare to work with and the documentation abysmal.

Sign In or Register to comment.