環境/macOS Montery Apple Silicon Mac

Sonic Pi にて事前に準備した簡易音楽を、ProcessingのSoundライブラリーで読み込み、音声の振幅(音量)を利用して、サークルを枠内をランダムに這いずります。
Simple music prepared in advance with Sonic Pi is loaded in Processing’s Sound library, and the amplitude (volume) of the sound is used to make the circle crawl randomly around the frame.

sound worm

Processing-Sound library

コードは、Processingのadd moreよりSoundライブラリーをインストールが必要になります。
The following source code requires the Sound library to be installed from Processing’s add more.

Source code of sound worm

下記コード内の soundfile = new SoundFile(this, “音源ファイル“); 適宜変更します。
soundfile = new SoundFile(this, “sound file”) in the code below; modify as appropriate.


/**
 *Sound-Worm
 *This code made by barbe_generative_diary
 *URL https://barbegenerativediary.com/
 *date 3.15.2022Sound-Worm
 */
//sound
import processing.sound.*;
SoundFile soundfile;
Amplitude vol;

freeLine frPos;

void setup(){
  size(1300,1300);
  background(10);
  frameRate(12);
  
  soundfile = new SoundFile(this, "211123_ambient_04.wav");
  soundfile.loop();
  vol = new Amplitude(this);
  vol.input(soundfile);
  
  frPos = new freeLine();
  
}

void draw(){
  fill(10,2);
  noStroke();
  rect(0,0,width,height);
  soundfile.rate(1.1);
  float ampLength = vol.analyze();

  stroke(255);
  frPos.update(ampLength);
}

サークルを枠内でランダムに動かすクラス
Class to move the circle randomly within the frame


/**The free line class
  *This code is for random motion.
  */
class freeLine extends PVector{
  PVector frpos1, frpos2;
  PVector flpos1, flpos2;
  PVector fcenter;
  float _angle, _angle2;
  float _angnoise, _angnoise2;
  float rad, rad2;
  float _radius1, _radius2;
  float _radiusnoise, _radiusnoise2;

  soundCircle circlePos;
  
  freeLine(){
   frpos1 = new PVector();
   frpos2 = new PVector();
   fcenter = new PVector(width/2, height/2);
   _angle = -PI/2;
   _angle2 = -PI/2;  
   _angnoise = random(10);
   _angnoise2 = random(10);  
   rad = _angle*DEG_TO_RAD;
   rad2 = _angle2*DEG_TO_RAD;
   _radius1 = 100;
   _radius2 = 100;
   _radiusnoise = random(10);
   _radiusnoise2 = random(10);
   frpos1 = new PVector(fcenter.x + (_radius1 * sin(rad)), fcenter.y + (_radius1 * cos(rad)));
   frpos2 = new PVector(frpos1.x + (_radius2 * sin(rad2)), frpos1.y + (_radius2 * cos(rad2)));
circlePos = new soundCircle(); }
void update(float amp){ _radiusnoise += 0.005; _radiusnoise2 += 0.004; _radius1 = (noise(_radiusnoise) * 400) + 10; _radius2 = (noise(_radiusnoise2) * 500) + 10; _angnoise += 0.001; _angle += (noise(_angnoise) *2) - 2 + amp*10; if (_angle > 360) { _angle -= 360; } if (_angle < 0) { _angle += 360; } rad = _angle*DEG_TO_RAD; _angnoise2 += 0.001; _angle2 += (noise(_angnoise2) *2) - 1 + amp*15; if (_angle2 > 360) { _angle2 -= 360; } if (_angle2 < 0) { _angle2 += 360; } rad2 = _angle2*DEG_TO_RAD; frpos1 = new PVector(fcenter.x + (_radius1 * sin(rad)), fcenter.y + (_radius1 * cos(rad))); frpos2 = new PVector(frpos1.x + (_radius2 * sin(rad2)), frpos1.y + (_radius2 * cos(rad2))); circlePos.update(frpos2.x,frpos2.y); } }

サークルを描くクラス
Class for drawing circles


/**The sound Circle class
  */
class soundCircle extends PVector {  
  int pointCount = 360;
  int stepNum = 20;
  int arrayNum = (pointCount/stepNum);
  float radius[] = new float[pointCount+1];
  float[] xpoint = new float[pointCount+1];
  float[] ypoint = new float[pointCount+1];  
  float angle;
  int changeNum = 1;
  
  soundCircle(){
    stroke(255);
    strokeWeight(1);
    smooth();
    noFill();
    
    for(int j=0; j<=360; j+=stepNum){
      radius[j] = 5;
    }
  }
  
  void update(float xCenter, float yCenter){
    translate(xCenter, yCenter);
    
    for(int i=0; i<=360 ; i+=stepNum){
      if(radius[i] > 70){changeNum = -1;}else if(radius[i] <= 2){changeNum = 1;}
      radius[i] += (2*noise(5))*changeNum;
    }
    
    for(int j=0; j<=360; j+=stepNum){
      float radJ = radians(j);
      xpoint[j] = (radius[j])*cos(radJ);
      ypoint[j] = (radius[j])*sin(radJ);
    }
    
    beginShape();
    for(int k=0; k<=360+stepNum*2; k+=stepNum){
      if(k<=360){curveVertex(xpoint[k],ypoint[k]);}
      if(k == 360+stepNum){curveVertex(xpoint[stepNum],ypoint[stepNum]);}
      if(k == 360+stepNum*2){curveVertex(xpoint[stepNum*2],ypoint[stepNum*2]);}
    }
    endShape(CLOSE);
  }
}

ーーーーー
この投稿から、シンタックスハイライトでコードの表記を始めました。もし、コードの崩れや等にて読みづらいところありましたらInstagram もしくは、Twitterにてメッセージをお願いします。
Starting with this post, we have begun using syntax highlighting to describe the code. If you find any code that is broken or difficult to read, please send us a message on Instagram or Twitter.
ーーーーー

Recommends

ProcessingによるCGとメディアアート

初版/ 2018.12.18
ページ数/320ページ
出版社/講談社
言語/日本語

ーーーーー
”Books”では、”barbe_generative_Library”として、
barbe_generative_diary の創作において実際に購入し、読んだ本を紹介します。

ーーーーー