ofx Relatime Audio Power Spectrum



/*
  This example demonstrates how to use the GRT FFT algorithm in openFrameworks
 */

#pragma once

#include "ofMain.h"
#include "ofxGrt.h"

//State that we want to use the GRT namespace
using namespace GRT;

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 audioIn(float * input, int bufferSize, int nChannels);
   
    //Create some variables for the demo
    FFT fft;
    ofxGrtTimeseriesPlot magnitudePlot;
};

//cpp -----------------------------------------------------

/*

 */

#include "ofApp.h"

#define AUDIO_BUFFER_SIZE 512
#define AUDIO_SAMPLE_RATE 44100
#define FFT_WINDOW_SIZE 2048
#define FFT_HOP_SIZE AUDIO_BUFFER_SIZE

//--------------------------------------------------------------
void ofApp::setup(){
   
    ofSetFrameRate(60);
   
    fft.init(FFT_WINDOW_SIZE,FFT_HOP_SIZE,1,FFT::RECTANGULAR_WINDOW,true,false);

    magnitudePlot.setup( FFT_WINDOW_SIZE/2, 1 );

    ofSoundStreamSetup(2, 1, this, AUDIO_SAMPLE_RATE, AUDIO_BUFFER_SIZE, 4);
}

//--------------------------------------------------------------
void ofApp::update(){
   
    //Grab the current mouse x and y position
    VectorDouble sample(2);

    vector< FastFourierTransform > &results =  fft.getFFTResultsPtr();
    magnitudePlot.setData( results[0].getMagnitudeData() );
}

//--------------------------------------------------------------
void ofApp::draw(){
   
    ofBackground(0, 0, 0);
   
    float margin = 10;
    magnitudePlot.draw( margin, margin, ofGetWidth() - margin*2, 250 );
   
}

void ofApp::audioIn(float * input, int bufferSize, int nChannels){

    GRT::VectorDouble data(1);
    for (int i=0; i<bufferSize; i++) {
        data[0] = input[i];
        fft.update( data );
    }
}

댓글

  1. /*
    This example demonstrates how to use the GRT FFT algorithm in openFrameworks
    */

    #pragma once

    #include "ofMain.h"
    #include "ofxGrt.h" # 오디오 스펙트럼 용.

    //State that we want to use the GRT namespace
    using namespace GRT; # 네임 스페이스

    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 audioIn(float * input, int bufferSize, int nChannels);

    //Create some variables for the demo
    FFT fft; # FFT 변수
    ofxGrtTimeseriesPlot magnitudePlot; # 시계열 그래프
    };

    답글삭제
  2. /*

    */

    #include "ofApp.h"

    // 상수 선언
    #define AUDIO_BUFFER_SIZE 512 // 오디오 버퍼 사이즈
    #define AUDIO_SAMPLE_RATE 44100 // 샘플 레이트
    #define FFT_WINDOW_SIZE 2048 // 푸리에변환 1차원 윈도우 사이즈
    #define FFT_HOP_SIZE AUDIO_BUFFER_SIZE // 윈도우 stride, 오디오 버퍼 사이즈

    //--------------------------------------------------------------
    void ofApp::setup(){

    ofSetFrameRate(60); // 화면 갱신율 60 fr/sec
    //푸리에변환 초기화
    fft.init(FFT_WINDOW_SIZE,FFT_HOP_SIZE,1,FFT::RECTANGULAR_WINDOW,true,false);
    //직각 윈도우

    //루트민스퀘어 이미지 플랏 초기화, 높이, 폭
    magnitudePlot.setup( FFT_WINDOW_SIZE/2, 1 );
    //마이크 입력 사운드스트림 설정.
    ofSoundStreamSetup(2, 1, this, AUDIO_SAMPLE_RATE, AUDIO_BUFFER_SIZE, 4);
    }

    //--------------------------------------------------------------
    void ofApp::update(){

    //Grab the current mouse x and y position
    // 더블 백터로 마우스 좌표 캡쳐
    VectorDouble sample(2);
    // 오디오 푸리에변환
    vector< FastFourierTransform > &results = fft.getFFTResultsPtr();
    // 진폭 플랏을 위한 데이타 할당. 속도 데이타를 위해 실수데이타만 사용?
    magnitudePlot.setData( results[0].getMagnitudeData() );
    }

    //--------------------------------------------------------------
    void ofApp::draw(){
    // 검정 배경
    ofBackground(100, 100, 100);

    float margin = 10; // 그래프 드로잉 위치
    // 10,10, w=마진고려, h=250
    magnitudePlot.draw( margin, margin, ofGetWidth() - margin*2, 250 );
    }
    // 오디오 입력 메써드 인줄 알았더니, 이벤트 핸들러? -----------------------------
    void ofApp::audioIn(float * input, int bufferSize, int nChannels){

    GRT::VectorDouble data(1); // 배정도 벡터
    for (int i=0; i<bufferSize; i++) {
    // 오디오 버퍼 사이즈 만큼 반복
    data[0] = input[i]; // 오디오 취득
    fft.update( data ); // 푸리에 데이타 갱신.
    }
    }

    //--------------------------------------------------------------
    void ofApp::keyPressed(int key){

    }

    //--------------------------------------------------------------
    void ofApp::keyReleased(int key){

    }

    //--------------------------------------------------------------
    void ofApp::mouseMoved(int x, int y ){

    }

    //--------------------------------------------------------------
    void ofApp::mouseDragged(int x, int y, int button){

    }

    //--------------------------------------------------------------
    void ofApp::mousePressed(int x, int y, int button){

    }

    //--------------------------------------------------------------
    void ofApp::mouseReleased(int x, int y, int button){

    }

    //--------------------------------------------------------------
    void ofApp::windowResized(int w, int h){

    }

    //--------------------------------------------------------------
    void ofApp::gotMessage(ofMessage msg){

    }

    //--------------------------------------------------------------
    void ofApp::dragEvent(ofDragInfo dragInfo){

    }

    답글삭제

댓글 쓰기

이 블로그의 인기 게시물

파이썬으로 Homomorphic Filtering 하기

파이썬으로 2D FFT/iFFT 하기: numpy 버전