LoginSignup
6
8

More than 5 years have passed since last update.

p5.jsでマイクインプットに合わせて般若心経の練習

Posted at

p5.jsではマイクのインプットができます。

インスタンスを作って・・・

  //マイクインプット開始
  mic = new p5.AudioIn();
  mic.start();

これだけでボリュームが拾えます。

  var vol = mic.getLevel();

これ英語のリーディングとかに使える?
と思って作ってみました般若心経

eee.gif

jsdoit : http://jsdo.it/hp0me_/872t

マイクの許可を忘れずに
スクリーンショット 2015-04-10 22.38.26.png

コード

sketch.js
//般若心経を用意     
var scripts = ["","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","fin.."];

var scIndex = 0;
var textObj;
var input;
var inputFlg = 0;
var analyzer;

function setup() {

  //キャンバスを作る
  createCanvas(500, 200);
  textObj = new Hannnya();

  //マイクインプットのインスタンス化&スタート
  mic = new p5.AudioIn();
  mic.start();
}

function draw() {

  background(255);

  var vol = mic.getLevel();

  //ボリュームが閾値を超えたら次のフェーズへ
  if(vol>0.1){
    inputFlg = 1;
  }

  //ボリュームが閾値以下に戻ったら次のフェーズへ
  if(vol<0.1 && inputFlg==1){
    inputFlg = 0;
    console.log("grounding");
    //次の文字を出す
    textObj.frameInc();
  }

  fill(127);
  noStroke();
  textObj.draw();
}

//class for label
function Hannnya(){

  this.currentFrame = 0;
  this.str = scripts[this.currentFrame];

  this.frameInc = function(){
    this.currentFrame++;
    if(this.currentFrame >= scripts.length)
      this.currentFrame = 0;
  };

  this.draw = function(){
    textSize(102);
    noStroke();
    //文字を出す
    text(scripts[this.currentFrame], 30, 100);
    this.guide(this.currentFrame);
  };

  this.guide = function(currentIndex){
      textSize(30);
      fill(127);
      var tx = "";

      //未来10個分の文字を出す
      for(var i = 1; i <= 10; i++){
        tx += scripts[this.currentFrame+i];
      }
      text(tx, 30, 150);
      fill(25,50,50,50);
  }
}

6
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
8