viernes, 30 de octubre de 2020

Record audio from speakers

 https://forum.processing.org/two/discussion/28028/how-do-i-record-the-audio-input-to-a-new-audio-render-using-minim

 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.

al processing fem:

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="<a href="http://code.compartmental.net/minim/" target="_blank" rel="nofollow">http://code.compartmental.net/minim/</a>" target="_blank" rel="nofollow"><a href="http://code.compartmental.net/minim/</a>" target="_blank" rel="nofollow">http://code.compartmental.net/minim/</a></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="<a href="http://code.compartmental.net/minim/" target="_blank" rel="nofollow">http://code.compartmental.net/minim/</a>" target="_blank" rel="nofollow"><a href="http://code.compartmental.net/minim/</a>" target="_blank" rel="nofollow">http://code.compartmental.net/minim/</a></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.");
  }
}