2015年7月25日土曜日

Processing Sound (day 3)


Day1 にて紹介したライン入力にpatch()メソッドを適用する方法としてもっとシンプルな方法がわかったので紹介します。

minimのreferencesのVocoderの部分にあったやり方です。
Vocoder reference in -> http://code.compartmental.net/minim/vocoder_class_vocoder.html

以下ソース
import ddf.minim.*;
import ddf.minim.ugens.*;
import ddf.minim.spi.*;

Minim minim;
LiveInput in;
AudioOutput out;
AudioRecorder recorder;

void setup(){
  size(1024,200);
  //initialize the minim and out objects
  int buffer_size = 512;
  minim = new Minim(this);
  minim.debugOn();
  out = minim.getLineOut(Minim.STEREO,buffer_size);

  AudioStream inputStream = minim.getInputStream( Minim.STEREO, 
                                                  out.bufferSize(), 
                                                  out.sampleRate(), 
                                                  out.getFormat().getSampleSizeInBits()
                                                 );
  in = new LiveInput(inputStream);
  out = minim.getLineOut(Minim.STEREO,buffer_size);
  in.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);
  }
}

この方法でスマートにpatch()を適用することができ、Day2のようなエフェクトの追加が可能です。

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

2015年7月23日木曜日

Processing Sound (day 1)



質より量を重視する選択をしましたどうもご無沙汰しております。

Processingの開発環境には音のライブラリとしてminimが標準で入っています。
このminimライブラリを使えばラインインから集めた音の周波数解析(FFT)やオーディオリアクティブなビジュアルソフト、自作の楽器を簡単に作成することができるようです。

ですが、標準のままですとラインインになんらかの処理を施し、ラインアウトへ。という処理ができません。すなわちラインインにつないだエレキギターにエフェクトをかけてスピーカーからかっこいい音を出したい。というのはそのままでは難しいということになります。一番の障害となっているのはAudioInputのインスタンスにはpatch() というメソッドが用意されていないことです。
patch() reference in -> http://code.compartmental.net/minim/ugen_method_patch.html

ソフトウエア的に生成した音声情報(ファイルからの読み込みを含む)はpatch()メソッドによって様々な既存のフィルター等を経由させて最終的に出力が可能です。

このpatch()メソッドをAudioInputのインスタンスに適用するためのひとつの方法は、新たにクラスを追加することです。processing forum (http://forum.processing.org/two/discussion/7172/echo-effect-with-minim)において紹介されている方法にて先ほど不可能であったラインインからラインアウトへ。が可能になったのでここに記述しておきます。

//Tab1 name:lineInTolineOut
Minim minim;
AudioInput in;
AudioOutput out;
MyAudioSocket socket;

void setup(){
  //initialize the minim and out objects
  int buffer_size = 512;
  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);
  in.addListener(socket);
  socket.patch(out);
}

void draw(){
}

//Tab2 name:MyAudioSocket
class MyAudioSocket extends UGen implements AudioListener
{
 
  private float[] left;
  private float[] right;
  private int buffer_max;
  private int inpos, outpos;
  private int count;
 
  MyAudioSocket(int buffer_size)
  {
     int n_buffers = 4;
     buffer_max = n_buffers * buffer_size;
     left = new float[buffer_max];
     right = new float[buffer_max];
     inpos = 0;
     outpos = 0;
     count = 0;
  }
 
  // The AudioListener:samples method accepts new input samples
  synchronized void samples(float[] samp)
  {
    // handle mono by writing samples to both left and right
    samples(samp, samp);
  }
 
  synchronized void samples(float[] sampL, float[] sampR)
  {
    System.arraycopy(sampL, 0, left, inpos, sampL.length);
    System.arraycopy(sampR, 0, right, inpos, sampR.length);
    inpos += sampL.length;
    if (inpos == buffer_max) {
      inpos = 0;
    }
    count += sampL.length;
  }
 
 
  //if I understand this correctly, UGens operate on a per sample basis
  //Override
  protected void uGenerate(float[] channels) 
  {
    if (count > 0) {
      for(int n = 0; n < channels.length; n++){
       channels[n] = ((n&1) == 0)?left[outpos]:right[outpos]; 
      }
      outpos++;
      if (outpos == buffer_max) {
         outpos = 0;
       }
      count--;
    }
  }
}