From: Paul Brossier Date: Sat, 24 Oct 2015 19:08:03 +0000 (+0200) Subject: src/ofxAubioPitch.{cpp,h}: add simple pitch class X-Git-Url: https://git.aubio.org/?a=commitdiff_plain;h=b1f2e2afc6d4c67110e701c241ed1eadcb46ecdc;p=ofxAubio.git src/ofxAubioPitch.{cpp,h}: add simple pitch class --- diff --git a/src/ofxAubioPitch.cpp b/src/ofxAubioPitch.cpp new file mode 100644 index 0000000..3a25426 --- /dev/null +++ b/src/ofxAubioPitch.cpp @@ -0,0 +1,84 @@ +/* + Copyright (C) 2015 Paul Brossier + + This file is part of ofxAubio. + + ofxAubio is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + aubio is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with aubio. If not, see . + +*/ + +#include "ofxAubioPitch.h" +#include "ofLog.h" + +ofxAubioPitch::ofxAubioPitch() +{ +} + +void ofxAubioPitch::setup() +{ + setup("default", 2048, 256, 44100); +} + +void ofxAubioPitch::setup(string method, int buf_s, int hop_s, int samplerate) +{ + hop_size = (uint_t)hop_s; + buf_size = (uint_t)buf_s; + pitch = new_aubio_pitch((char_t*)method.c_str(), + buf_size, hop_size, samplerate); + aubio_pitch_set_unit(pitch, (char_t*)"midi"); + aubio_input = new_fvec(hop_size); + aubio_output = new_fvec(1); + curpos = 0; + if (pitch) { + ofLogNotice() << "created ofxAubioPitch(" << method + << ", " << buf_size + << ", " << hop_size + << ", " << samplerate + << ")"; + } +} + +ofxAubioPitch::~ofxAubioPitch() +{ + //if (pitch) del_aubio_pitch(Pitch); +} + +void ofxAubioPitch::audioIn(float * input, int bufferSize, int nChannels) +{ + uint_t i, j; + for (i = 0; i < bufferSize; i++) { + // downmix into aubio_input + aubio_input->data[curpos] = 0.; + for (j = 0; j < nChannels; j++) { + aubio_input->data[curpos] += input[ (curpos + i) * nChannels + j]; + } + aubio_input->data[curpos] /= nChannels; + // run aubio block when appropriate + if (curpos == hop_size - 1) + { + blockAudioIn(); + curpos = -1; + } + curpos += 1; + } +} + +void ofxAubioPitch::blockAudioIn() +{ + aubio_pitch_do(pitch, aubio_input, aubio_output); + if (aubio_output->data[0]) { + //ofLogNotice() << "found pitch: " << aubio_output->data[0]; + latestPitch = aubio_output->data[0]; + } +} diff --git a/src/ofxAubioPitch.h b/src/ofxAubioPitch.h new file mode 100644 index 0000000..328808b --- /dev/null +++ b/src/ofxAubioPitch.h @@ -0,0 +1,52 @@ +/* + Copyright (C) 2015 Paul Brossier + + This file is part of ofxAubio. + + ofxAubio is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + aubio is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with aubio. If not, see . + +*/ + +#pragma once + +#include +#include + +using namespace std; + +class ofxAubioPitch { + + public: + + ofxAubioPitch(); + ~ofxAubioPitch(); + + void setup(); + void setup(string method, int buf_s, int hop_s, int samplerate); + + void audioIn(float *input, int bufferSize, int nChannels); + + float latestPitch; + + private: + void blockAudioIn(); + // aubio stuff + uint_t buf_size; + uint_t hop_size; + fvec_t * aubio_input; + fvec_t * aubio_output; + aubio_pitch_t * pitch; + sint_t curpos; + +};