1

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

我想让我的应用程序播放用户将录制的 .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");

我该怎么做?

4

2 回答 2

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

于 2013-07-24T11:56:58.630 回答
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.");
  }
}
于 2018-06-02T23:53:50.287 回答