小菜与老鸟 我用了两种思路来修改程序,但是都没有达到预期效果,不知道是不是我的逻辑上有疏漏?
第一种是我在程序播放了第一次录音之后,将recorded
设置为false
,就回到了初始状态,应该是可以重来录一遍的。代码如下:
* This sketch demonstrates how to use an AudioRecorder to record audio to disk
* and then immediately play it back by creating a new FilePlayer using the AudioRecordingStream
* returned by the save method.
* <p>
* To use this sketch you need to have something plugged into the line-in on your computer.<br/>
* Press 'r' to toggle recording on and off and the press 's' to save to disk.<br/>
* The recorded file will be placed in the main 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;
// 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);
// get a stereo line-in: sample buffer length of 2048
// default sample rate is 44100, default bit depth is 16
in = minim.getLineIn(Minim.STEREO, 2048);
// create an AudioRecorder that will record from in to the filename specified.
// the file will be located in the sketch's main folder.
recorder = minim.createRecorder(in, "myrecording.wav");
// get an output we can playback the recording on
out = minim.getLineOut( Minim.STEREO );
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.left.size()-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("Now recording, press the r key to stop recording.", 5, 15);
}
//else if ( !recorded )
else if (!recorder.isRecording() )
{
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);
}
}
void keyReleased()
{
//if ( !recorded && key == 'r' )
if ( key == 'r' )
{
// to indicate that you want to start or stop capturing audio data,
// you must callstartRecording() and stopRecording() 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 to the end of the file.
if ( recorder.isRecording() )
{
recorder.endRecord();
// recorded = true;
println("recorded",recorded);
}
else
{
print("begin");
recorder.beginRecord();
//revise
}
}
// if ( recorded && key == 's' )
if ( key == 's' )
{ println("save1");
// we've filled the file out buffer,
// now write it to a file of the type we specified in setup
// in the case of buffered recording,
// this will appear to freeze the sketch for sometime, if the buffer is large
// 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.
// save returns the recorded audio in an AudioRecordingStream,
// which we can then play with a FilePlayer
if ( player != null )
{
player.unpatch( out );
player.close();
}
println("save2");
player = new FilePlayer( recorder.save() );
player.patch( out );
player.play();
// println("save3");
recorded = false;
}
}