[ Video ]
[ About ]
パーリンノイズにX座標とフレーム番号を引数に与えて四角の色を変化。色の変化をきっかけに音が鳴るようにしました。
Change the color of the square by Perlin noise. I made the sound to sound as the trigger of color change.
[ Source ]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#pragma once #include "ofMain.h" 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) {}; vector<ofSoundPlayer> sounds; vector<ofColor> sound_color; }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openframeworks"); ofBackground(0); ofSetLineWidth(3); string sound_path_list[] = { "sound/pianoC2.mp3", "sound/pianoB.mp3", "sound/pianoA.mp3", "sound/pianoG.mp3", "sound/pianoF.mp3", "sound/pianoE.mp3", "sound/pianoD.mp3","sound/pianoC.mp3" }; for (int i = 0; i < 8; i++) { ofSoundPlayer sound; sound.load(sound_path_list[i]); sound.setVolume(0.2); sound.setMultiPlay(true); this->sounds.push_back(sound); ofColor color; color.setHsb(i * 35, 255, 230); this->sound_color.push_back(color); } } //-------------------------------------------------------------- void ofApp::update() { ofSoundUpdate(); } //-------------------------------------------------------------- void ofApp::draw() { int span = 144; for (int x = 0; x < ofGetWidth(); x += span) { int noise_value = ofMap(ofNoise(x * 0.0005 + ofGetFrameNum() * 0.008), 0, 1, 0, this->sounds.size()); int next_noise_value = ofMap(ofNoise(x * 0.0005 + (ofGetFrameNum() + 1) * 0.008), 0, 1, 0, this->sounds.size()); ofFill(); ofSetColor(this->sound_color[next_noise_value]); ofDrawRectangle(x, 0, x + span, ofGetHeight()); ofNoFill(); ofSetColor(239); ofDrawRectangle(x, 0, x + span, ofGetHeight()); if (noise_value != next_noise_value) { this->sounds[next_noise_value].play(); } } } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |