处理中的微小 - 从麦克风录制但无法关闭并进行新录制

时间:2017-06-08 09:08:44

标签: processing audio-recording minim

使用来自processing.org的示例文件(在Win8上),我得到了“从麦克风录音”并播放()声音正常工作。我只需要一次记录10到30秒。但现在找不到任何方法来关闭现有录音并录制新录音。 我已经尝试过各种各样的方法。我希望能够点击另一个键,再次按“r”并再记录几秒钟,如语言词汇练习等。
我正在使用compartmental.net/minim中的minim示例代码。官方文档只列出了beginRecord / endRecord,但是没有方法可以关闭现有的录制并启动另一个录制。没有记录如recorder.close()或.reset / restart等。

 import ddf.minim.*;
import ddf.minim.ugens.*;
Minim minim;
// for recording
   AudioInput in;
   AudioRecorder recorder;
   boolean recorded;
 // for playing back
AudioOutput out;
FilePlayer player;
void setup()
{   size(512, 200, P3D);
    minim = new Minim(this);
    in = minim.getLineIn(Minim.STEREO, 2048);
  // create an AudioRecorder
  recorder = minim.createRecorder(in, "myrecording.wav");
    // get an output
  out = minim.getLineOut( Minim.STEREO );
  textFont(createFont("Arial", 24));
}
void draw()
{   background(255,240,128); 
     stroke(32);
  if ( recorder.isRecording() )
  {   text("Now recording, press the r key to stop recording.", 5, 15);   }
  else if ( !recorded )
  {   text("Press the R key to start recording.", 5, 15);     }
  else
  {  text("Press the S key to save the recording to disk and play it back in the sketch.", 5, 15);    }
} 
//end draw
void keyReleased()
{
     if ( !recorded && key == 'r' ) 
     {   // to indicate that you want to start or stop capturing audio data, 
           if ( recorder.isRecording()    ) 
           {    recorder.endRecord();
                 recorded = true;             }
    else 
    {  recorder.beginRecord();       }
  }
  if ( recorded && key == 's' )
  {  // now write it to file
    // case of buffered recording, will freeze sketch for a bit if buffer is large
    // case of streamed recording, will not freeze all that is being done
    // all that is being done is closing the file.
    // save returns the recorded audio in an AudioRecordingStream, 
    // which we can then play with a FilePlayer
    if ( player != null )
        {    player.unpatch( out );
              player.close();           }
    player = new FilePlayer( recorder.save() );
    player.patch( out );
    player.play();
   }
  //  my addition — this works for play-again
  if ( recorded && key == 'p' )
  {  player.rewind();
     player.play();
  }

我尝试关闭minim并重新启动它,但随后它抱怨“本地变量xxx未被使用”,如此:不起作用。

  if ( key == 'x' )
  { minim.stop();
    minim = new Minim(this);   
    AudioInput in;
    AudioRecorder recorder;  
    AudioOutput out;
    FilePlayer player;    }

1 个答案:

答案 0 :(得分:0)

你可以简单地重新初始化录音机:

recorder = minim.createRecorder(in, "myrecording.wav");

我建议使用时间戳或文件计数器,这样就不会覆盖以前的录音。

这是一个非常微调的Creating an AudioRecorder示例(也出现在示例> Contributed Libraries> Minim> Basics> RecordAudioInput )中:

/**
  * This sketch demonstrates how to an <code>AudioRecorder</code> to record audio to disk. 
  * To use this sketch you need to have something plugged into the line-in on your computer, 
  * or else be working on a laptop with an active built-in microphone. 
  * <p>
  * 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.*;

Minim minim;
AudioInput in;
AudioRecorder recorder;

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

  minim = new Minim(this);

  in = minim.getLineIn();
  // 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 - "+new java.util.Date()+".wav");

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

void draw()
{
  background(0); 
  stroke(255);
  // draw the waveforms
  // the values returned by left.get() and right.get() will be between -1 and 1,
  // so we need to scale them up to see the waveform
  for(int i = 0; i < in.bufferSize() - 1; i++)
  {
    line(i, 50 + in.left.get(i)*50, i+1, 50 + in.left.get(i+1)*50);
    line(i, 150 + in.right.get(i)*50, i+1, 150 + in.right.get(i+1)*50);
  }

  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 = minim.createRecorder(in, "myrecording - "+new java.util.Date()+".wav");
      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.");
  }
}

此外,如果您更愿意使用保存对话框而不是为.wav文件生成唯一名称,则可能需要签出selectInput()