How do I record the audio input to a new audio render using minim

So, i´ve been tryng to make the minim recorder to record the audio from the Audioplayer object when is played without any succes.

Something like the combination of this 2 examples :

http://code.compartmental.net/minim/audioplayer_method_play.html http://code.compartmental.net/minim/minim_method_createrecorder.html

I´ve found similar threats but with no anwer to it :

https://forum.processing.org/one/topic/minim-how-to-record-minim-s-output.html

The audio plays just well but the recorder comes out empty. If I try the "createrecorder" example it works on recording the sounds created by minim, but I need it to record the sound getting them from the audio player. This is my greates attemp :

import ddf.minim.*;
import ddf.minim.ugens.*;

Minim         minim;
AudioOutput   out;
AudioRecorder recorder;
AudioPlayer sonido;
void setup()
{
  size(512, 200, P3D);

  minim = new Minim(this);

  out = minim.getLineOut();
  sonido = minim.loadFile("fijo2.wav");
  sonido.loop();

  recorder = minim.createRecorder(out, "myrecording.wav");  
  textFont(createFont("Arial", 12));
}

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

void keyReleased()
{
  if ( key == 'r' ) 
  {
    if ( recorder.isRecording() ) 
    {
      recorder.endRecord();
    }
    else 
    {
      recorder.beginRecord();
    }
  }
  if ( key == 's' )
  {
    recorder.save();
    println("Done saving.");
  }
}

Answers

  • edited June 2

    Tested with both wav and mp3. Data should be placed in data folder.

    Kf

    //REFERENCE:  https:// forum.processing.org/one/topic/how-can-i-detect-sound-with-my-mic-in-my-computer.html
    
    
    /**
      * This sketch demonstrates how to use an <code>AudioRecorder</code> to record audio to disk. 
      * Press 'r' to toggle recording on and off and the press 's' to save to disk. 
      * The recorded file will be placed in the sketch folder of the sketch.
      * <p>
      * For more information about Minim and additional features, 
      * visit http://code.compartmental.net/minim/
      */
    
    import ddf.minim.*;
    import ddf.minim.ugens.*;
    
    Minim         minim;
    AudioPlayer player;
    AudioInput in;
    AudioRecorder recorder;
    
    void setup()
    {
      size(512, 200, P3D);
    
      minim = new Minim(this);  
      //player = minim.loadFile("energeticDJ.mp3");
      player = minim.loadFile("fair1939.wav");
    
      delay(800);   //Needed (?)
      player.play();
      in = minim.getLineIn(Minim.STEREO, 512);
    
      recorder = minim.createRecorder(in, dataPath("myrecording.wav"));
    
    
      textFont(createFont("Arial", 12));
    }
    
    void draw()
    {
      background(0); 
      stroke(255);
    
      for(int i = 0; i < player.bufferSize() - 1; i++)
      {
        float x1 = map( i, 0, player.bufferSize(), 0, width );
        float x2 = map( i+1, 0, player.bufferSize(), 0, width );
        line( x1, 50 + player.left.get(i)*50, x2, 50 + player.left.get(i+1)*50 );
        line( x1, 150 + player.right.get(i)*50, x2, 150 + player.right.get(i+1)*50 );
      }
    
      // draw a line to show where in the song playback is currently located
      float posx = map(player.position(), 0, player.length(), 0, width);
      stroke(0,200,0);
      line(posx, 0, posx, height);
    
    
    
      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.");
      }
    }
    
  • edited June 1

    Hi ! Thanks for taking your time for anwering me !.

    The code that you posted records the input from the mic NOT from sounds that are being played in processing

  • that's only because of line 33.

    this example purports to record the output, see lines 23 and 27:

    https://github.com/ddf/Minim/blob/master/examples/Basics/RecordAudioOutput/RecordAudioOutput.pde

  • Answer ✓

    Ok, so implementing @koogs changes are not enough, as you get back to the business of empty recorded files. However, that gave me an idea (Thxs @koogs) which I tested and sort of works. I mean, it only works for mp3 files but not for wav files. However, I tried a second idea, and it might work for you although it doesn't seem to have much control over audio when it is being played. That is what I labeled second solution using sampler objects. It works for both mp3 and wav files (tested).

    INSTRUCTIONS: In the code, define your file to play. When you run the sketch, press r to begin recording, r again to stop recording. Don't forget to press s to save the file to an audio file which will be located in the data folder.

    Kf


    FIRST solution: Only mp3

    //REFERENCE: https:// forum.processing.org/one/topic/how-can-i-detect-sound-with-my-mic-in-my-computer.html
    //REFERENCE: https:// forum.processing.org/two/discussion/21842/is-it-possible-to-perform-fft-with-fileplayer-object-minim
    
    /**
     * This sketch demonstrates how to use an <code>AudioRecorder</code> to record audio to disk. 
     * Press 'r' to toggle recording on and off and the press 's' to save to disk. 
     * The recorded file will be placed in the sketch folder of the sketch.
     * <p>
     * For more information about Minim and additional features, 
     * visit <a href="http://code.compartmental.net/minim/" target="_blank" rel="nofollow">http://code.compartmental.net/minim/</a>;
     */
    
    import ddf.minim.*;
    import ddf.minim.ugens.*;
    import ddf.minim.analysis.*;
    
    Minim         minim;
    FilePlayer player;
    AudioOutput out;
    AudioRecorder recorder;
    
    void setup()
    {
      size(512, 200, P3D);
      textFont(createFont("Arial", 12));
    
      minim = new Minim(this);  
      player = new FilePlayer(minim.loadFileStream("energeticDJ.mp3"));
      // IT DOESN'T WORK FOR WAV files  ====> player = new FilePlayer(minim.loadFileStream("fair1939.wav"));
      out = minim.getLineOut();
      TickRate rateControl = new TickRate(1.f);
      player.patch(rateControl).patch(out);
      recorder = minim.createRecorder(out, dataPath("myrecording.wav"),true);
    
      player.loop(0);
    
    }
    
    void draw()
    {
      background(0); 
      stroke(255);
    
      // draw a line to show where in the song playback is currently located
      float posx = map(player.position(), 0, player.length(), 0, width);
      stroke(0, 200, 0);
      line(posx, 0, posx, height);
    
    
    
      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.");
      }
    }
    

    SECOND solution: Works for both wav and mp3

    //REFERENCE: https:// forum.processing.org/one/topic/how-can-i-detect-sound-with-my-mic-in-my-computer.html
    //REFERENCE: https:// forum.processing.org/two/discussion/21842/is-it-possible-to-perform-fft-with-fileplayer-object-minim
    //REFERENCE: https:// forum.processing.org/two/discussion/21953/why-can-i-only-load-four-audio-files-in-minum
    /**
     * This sketch demonstrates how to use an <code>AudioRecorder</code> to record audio to disk. 
     * Press 'r' to toggle recording on and off and the press 's' to save to disk. 
     * The recorded file will be placed in the sketch folder of the sketch.
     * <p>
     * For more information about Minim and additional features, 
     * visit <a href="http://code.compartmental.net/minim/" target="_blank" rel="nofollow">http://code.compartmental.net/minim/</a>;
     */
    
    import ddf.minim.*;
    import ddf.minim.ugens.*;
    import ddf.minim.analysis.*;
    
    Minim         minim;
    AudioRecorder recorder;
    AudioOutput out;
    Sampler  note;
    
    void setup()
    {
      size(512, 200, P3D);
      textFont(createFont("Arial", 12));
    
      minim = new Minim(this);  
      out = minim.getLineOut();
      note = new Sampler( "energeticDJ.mp3", 4, minim );
      //note = new Sampler( "fair1939.wav", 4, minim );
      note.patch( out );
    
      recorder = minim.createRecorder(out, dataPath("myrecording.wav"), true);
    
      note.trigger();
    }
    
    void draw()
    {
      background(0); 
      stroke(255);
    
      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.");
      }
    }
    

    Keyword: kf_keyword minim sound recording from wav mp3 files

  • edited June 4

    @kafrager thanks for anwering me !

    Both of your solutions work. The first one even works with .wav files that I was using.

    Now im having another problem that technically is out of the scope of this thread, but is related.

    The objetive of my program is to record the panning and movement of sounds that I placed.

    Here is an image showing the problem:

    espacialización

    Here i´ve uploaded the code of the program : https://github.com/Aurelian24/espacializacion

    The problem is that if I use the "Sampler" class or " FilePlayer " class i can´t accoplish panning or turning up and down the gain .

  • Not much experience here:

    • Start a new thread in the new forum preferably
    • I have limited knowledge about features of each class or missing features. I have a feeling you need to explore the soundbeats library. I can get you the manual (if you cannot find it online) but first, I suggest you explore previous posts.

    With panning, you mean advancing/jumping in your sound track?

    Kf

  • panning generally means where in the stereo image the sound is. -1 for far left, +1 for far right.

Sign In or Register to comment.