记录/创建音频文件之前的minim loadFile

时间:2013-07-14 18:38:31

标签: audio processing record minim

我正在尝试创建一个允许用户发出声音的应用,然后以播放方式使用它。

我希望我的应用程序播放用户将记录的.wav文件。

我无法弄清楚如何对此进行编码,因为我一直收到错误。

    ==== JavaSound Minim Error ====
    ==== Error invoking createInput on the file loader object: null

代码片段:

   import ddf.minim.*;

   AudioInput in;
   AudioRecorder recorder;

   RadioButtons r;
   boolean showGUI = false;
   color bgCol = color(0);

   Minim minim;

   //Recording players
   AudioPlayer player1; 
   AudioPlayer player2;

   void newFile()
   {
      countname =(name+1);
      recorder = minim.createRecorder(in, "data/" + countname + ".wav", true);
   }
   ......

   void setup(){

       minim = new Minim(this);
       in = minim.getLineIn(Minim.MONO, 2048);
       newFile();
       player1 = minim.loadFile("data/" + countname + ".wav");// recording #1
       player2 = minim.loadFile("data/" + countname + ".wav");//recording #2


   void draw() {
   // Draw the image to the screen at coordinate (0,0)
       image(img,0,0);

       //recording button
       if(r.get() == 0)
       {
            for(int i = 0; i < in.left.size()-1; i++)
       }

            if ( recorder.isRecording() )
        {
            text("Currently recording...", 5, 15);
             }
            else
        {
            text("Not recording.", 5, 15);
          }
         }
     //play button
     if(r.get() == 1)
     {
     if(mousePressed){
     .......
     player_1.cue(0);
     player_1.play();
     }
     if(mousePressed){
     .......
     player_2.cue(0);
     player_2.play();
     }
     } 

我遇到问题的地方在这里:

       player1 = minim.loadFile("data/" + countname + ".wav");// recording #1
       player2 = minim.loadFile("data/" + countname + ".wav");//recording #2

将要录制的文件是1.wav,2.wav。但我不能把它放在

       player1.minim.loadFile ("1.wav");
       player2.mminim.loadFile("2.wav");

我该怎么做?

2 个答案:

答案 0 :(得分:2)

如AudioRecorder [1]的JavaDoc页面所示,需要调用beginRecord(),endRecord()和save(),以便实际记录您想要记录的内容,然后保存到磁盘。只要没有发生这种情况,loadFile()就无法加载,因此您将收到错误。所以问题出在你的程序流程中。只有当您的程序达到已经记录并保存文件的状态时,您才可以实际加载该文件。

当你到达音频输入缓冲区时,你可能还有一些方法可以回放正在录制的内容(人们通常称之为'监听'),但据我所知,这不是你想要什么。

除了这个一般的概念缺陷外,您的代码中似乎还存在其他问题,例如: countname没有在两个后续的loadFile调用之间进行迭代(我假设它应该迭代);在某些时候你也有“player_1.play();” (注意下划线),虽然你可能正在引用这个,用“player1 = minim.loadFile(...)”初始化的不同编写的变量? ......

[1] http://code.compartmental.net/minim/javadoc/ddf/minim/AudioRecorder.html

答案 1 :(得分:1)

这是从音频文件录制到AudioRecorder对象的方法。你加载一个文件,播放它,然后你选择保存到你可以使用的另一个文件的部分和你的操作系统提供的AudioPlayer对象或你最喜欢的声音播放器。

相关
  

我无法弄清楚如何对此进行编码,因为我一直在努力   错误。

尽管它说这是一个错误,但它并不影响执行你的程序。我会认为这是一个警告并忽略它。如果你想修复它,我相信你需要编辑文件的标签来正确设置它们的值。

说明:在代码中,定义要播放的文件。运行草图时,按r开始录制,再次r停止录制。不要忘记按s将文件保存到音频文件中,该文件将位于数据文件夹中。

注意:如果您需要播放wav文件,则需要 Sampler 对象而不是 FilePlayer 对象。

//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.");
  }
}