2015年7月24日金曜日

Processing Sound (day 2)

day2はday1を基本としてdelay、recording 機能を追加してみます。
サンプルの組み合わせです。これで他の既存フィルタ等々の追加にも対応できるようになったといえます。マウスカーソルをwindowのひだりうえ端に持っていくとロボット的な声になって面白いです。



以下コード
 //Tab1 name:delayAndRecording
import ddf.minim.*;
import ddf.minim.ugens.*;

Minim minim;
AudioInput in;
AudioOutput out;
MyAudioSocket socket;
Delay myDelay;
AudioRecorder recorder;

void setup(){
  size(1024,200);
  //initialize the minim and out objects
  int buffer_size = 1024;
  minim = new Minim(this);
  minim.debugOn();
  in = minim.getLineIn(Minim.STEREO,buffer_size);
  out = minim.getLineOut(Minim.STEREO,buffer_size);
  socket = new MyAudioSocket(buffer_size);
  myDelay = new Delay (1,1,true,true);
  recorder = minim.createRecorder(out, "Rec.wav");
  
  in.addListener(socket);
  socket.patch(myDelay).patch(out);
}

void draw(){
  // erase the window to dark grey
  background( 64 );
  // draw using a light gray stroke
  stroke( 192 );
  // draw the waveforms
  for( int i = 0; i < out.bufferSize() - 1; i++ )
  {
    // find the x position of each buffer value
    float x1  =  map( i, 0, out.bufferSize(), 0, width );
    float x2  =  map( i+1, 0, out.bufferSize(), 0, width );
    // draw a line from one buffer position to the next for both channels
    line( x1, 50 + out.left.get(i)*50, x2, 50 + out.left.get(i+1)*50);
    line( x1, 150 + out.right.get(i)*50, x2, 150 + out.right.get(i+1)*50);
  }
  
  text( "Delay time is " + myDelay.delTime.getLastValue(), 5, 15 );
  text( "Delay amplitude (feedback) is " + myDelay.delAmp.getLastValue(), 5, 30 );
  if ( recorder.isRecording() )
  {
    text("Currently recording...", 5, 45);
  }
  else
  {
    text("Not recording.", 5, 45);
  }
}

void mouseMoved()
{
  // set the delay time by the horizontal location
  float delayTime = map( mouseX, 0, width, 0.0001, 1 );
  myDelay.setDelTime( delayTime );
  // set the feedback factor by the vertical location
  float feedbackFactor = map( mouseY, 0, height, 0.99, 0.0 );
  myDelay.setDelAmp( feedbackFactor );
}

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
    println("Done saving.");
  }
}

0 件のコメント:

コメントを投稿