2015.08.06
- 人×技術
openframeworks サウンド篇 tiger1
TIGER-TANIOKAです。
記事とか書くの初めてなんでちょっと文章がアレですが
がんばります。
openFrameWorksでサウンドビジュアライズに挑戦。
サウンドファイルの再生,FFT解析
##ofApp.h ============================================================
class ofApp : public ofBaseApp{
public:
...
...
...
ofSoundPlayer player;
float* fft; ※FFT解析結果を格納する配列(ポインタ)
nBandsToGet = 1024; ※FFTの周波数成分の解像度を設定 (2の累乗)
##ofApp.cpp ============================================================
void ofApp::setup(){
player.loadSound("verdy.mp3");
player.setLoop(true);
player.play();
}
void ofApp::update(){ ※サウンドプレーヤーを更新して、FFT解析
ofSoundUpdate();
fft = ofSoundGetSpectrum(nBandsToGet);
}
すんなり音なりました、
うれしいです。tiger!
ビジュアル描画
解析結果(FFT)の値をつかって描画してみます!
背景を黒にする。
下の画像の黒い部分の座標を取得。
その部分に円を描いてみる。
おぉ、おしゃれっすね、tiger!
だけどちょっとさみしい感じがしますね。
黒い部分の座標を利用して、
線足してみます+++++++++++
ソリッド感でてきた!tigertigertiger!
ただ、まだなんか足りない感じがします。。。
背景をもう少し派手にしてみるため、
パーティクルうにょうにょさせてみました。
TigerKneeee!!!!!!!!
かっこよくなってきました!
今日はここまでにしておきます。
楽曲は弊社タンバに提供いただきました。
第8回:openFrameworks サウンド・音響合成 ofxMaxim, ofxOsc, ofxSuperCollider | yoppa orgを参考にさせて頂きました。
田所さんのParticles使わせていただきました。
最終的にコードは下記になりました。
##ofApp.h ============================================================
#pragma once
#include "ofMain.h"
#include "Particles.h"
#include "ofxGui.h"
#define IMG_W 853
#define IMG_H 480
class ofApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
void createPoint();
ofSoundPlayer player;
float* fft;
ofxPanel gui;
ofxFloatSlider speed;
ofxFloatSlider friction;
int width = 0;
int count = 0;
int particleNum = 7000;
Particles* particles;
vector<ofvec2f> dotPoints;
ofImage tiger;
unsigned char* tigerPixels;
vector<ofvec2f> tigerPoints;
};
##ofApp.cpp ============================================================
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
ofBackground(0);
ofSetBackgroundAuto(false);
tiger.loadImage("typo3.jpg");
tiger.setImageType(OF_IMAGE_GRAYSCALE);
tigerPixels = tiger.getPixels();
for(int y = 0; y < IMG_H; y++){
for(int x = 0; x < IMG_W; x++){
int col = tigerPixels[y * IMG_W + x];
if(col < 10){
tigerPoints.push_back(ofVec2f(x ,y));
}
}
}
player.loadSound("verdy.mp3");
player.setLoop(true);
player.play();
particles = new Particles(particleNum);
for(int i = 0; i < particleNum; i++){
ofVec3f p;
p.x = tigerPoints[ofRandom(tigerPoints.size())].x;
p.y = tigerPoints[ofRandom(tigerPoints.size())].y;
particles->addParticle(p);
}
gui.setup();
gui.add(speed.setup("speed" , 1.5 , 0.0 , 5.0));
gui.add(friction.setup("friction" , 0.1 , 0.0 , 2.0));
}
void ofApp::createPoint(){
int index = ofRandom(tigerPoints.size());
dotPoints.clear();
for(int v = index; v < index + ofRandom(tigerPoints.size()); v++){
int kk = ofRandom(tigerPoints.size());
dotPoints.push_back(ofVec2f(tigerPoints[kk].x + speed * ofRandom(-1.0 , 1.0) , tigerPoints[kk].y + speed * ofRandom(-1.0 , 1.0)));
}
}
//--------------------------------------------------------------
void ofApp::update(){
ofSoundUpdate();
particles->resetForce();
particles->friction = friction;
count = 0;
for(int i = 0; i < particleNum; i++){
ofVec2f pPoint = ofVec2f(particles->positions[i].x ,particles->positions[i].y);
ofVec2f tgtPoint = ofVec2f(tigerPoints[count].x , tigerPoints[count].y);
int deg = atan2((pPoint.y - tgtPoint.y) , (pPoint.x - tgtPoint.x)) * 180 / PI;
deg += 180;
ofVec3f force;
force.x = cos(deg * PI / 180) * speed / ofRandom(100);
force.y = sin(deg * PI / 180) * speed / ofRandom(100);
particles->addForce(i , force);
count += 10;
if(count > tigerPoints.size()){
count = 0;
}
}
particles->addDampingForce();
particles->updatePos();
particles->resetOffWalls();
}
//--------------------------------------------------------------
void ofApp::draw(){
int ram;
fft = ofSoundGetSpectrum(2);
fft[0] = fft[0] * 1.4;
speed = fft[0] * 70.0;
friction = fft[0] * 3.0;
width = fft[0] * 50.0;
ofEnableBlendMode(OF_BLENDMODE_ALPHA);
ofSetColor(0,0,0,70);
ofRect(0, 0, IMG_W, IMG_H);
ofEnableBlendMode(OF_BLENDMODE_ADD);
if(speed > 0.2){
createPoint();
}
if(speed > 6.8){
ram = ofRandom(0 , 10);
}
ofSetColor(255);
if(ram < 2.9){
for(int i = 0; i < dotPoints.size(); i++){
ofCircle(dotPoints[i].x + speed * ofRandom(-1.0 , 1.0) , dotPoints[i].y + speed * ofRandom(-1.0 , 1.0) , width);
}
}else if(ram < 8.8){
particles->draw();
}else{
for(int l = 0; l < ofRandom(dotPoints.size()); l++){
ofSetLineWidth(ofRandom(0.1 , 10.0));
ofLine(dotPoints[l].x , dotPoints[l].y , dotPoints[max(l - 1 , 0)].x , dotPoints[max(l - 1 , 0)].y);
}
}
// gui.draw();
}