├── .gitignore ├── LICENSE ├── README.md ├── ofxaddons_thumbnail.png └── src ├── ofxBpm.cpp └── ofxBpm.h /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | 6 | # Compiled Dynamic libraries 7 | *.so 8 | *.dylib 9 | 10 | # Compiled Static libraries 11 | *.lai 12 | *.la 13 | *.a 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 mirrorboy 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ofxBpm 2 | ====== 3 | 4 | 5 | BPM Addon for Musical Apps. 6 | 7 | Listener function will be called by addon on set timings. 8 | You can set BPM (Beat per Minutes) and BPB (Beat Per Bar) to this addon. 9 | For example, if you want to listen 4Beat on BPM = 120, then set BPM to 120, BPB to 4. 10 | 11 | This addon uses ofThread. You could not call draw function on listener. 12 | 13 | 14 | ====== 15 | 16 | 17 | Tested on 18 | Max OSX 10.8.4 19 | openFrameworks 0.80 20 | -------------------------------------------------------------------------------- /ofxaddons_thumbnail.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mirrorboy714/ofxBpm/0a958858b86810b49469f5af6c00b55c64c2bdac/ofxaddons_thumbnail.png -------------------------------------------------------------------------------- /src/ofxBpm.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // ofxBpm.cpp 3 | // ofxBpm 4 | // 5 | // Created by mirrorboy on 2013/08/14. 6 | // 7 | // 8 | 9 | 10 | #include "ofxBpm.h" 11 | #include "ofMain.h" 12 | 13 | ofxBpm::ofxBpm(float bpm,int beatPerBar):_beatPerBar(beatPerBar){ 14 | 15 | _isPlaying = false; 16 | _isTick = false; 17 | 18 | setBpm(bpm); 19 | }; 20 | 21 | 22 | void ofxBpm::start(){ 23 | 24 | stop(); 25 | 26 | _isPlaying = true; 27 | reset(); 28 | 29 | startThread(true, false); // blocking, verbose 30 | 31 | } 32 | 33 | void ofxBpm::stop(){ 34 | 35 | //stopThread(); 36 | waitForThread(true); 37 | 38 | _isPlaying = false; 39 | } 40 | 41 | void ofxBpm::reset(){ 42 | 43 | 44 | if( lock() ){ 45 | 46 | _remainderOfTick = 0; 47 | _countOfTick = -1; 48 | _preTime = ofGetElapsedTimeMicros(); 49 | _totalTime = 0; 50 | 51 | unlock(); 52 | } 53 | } 54 | 55 | 56 | void ofxBpm::threadedFunction(){ 57 | 58 | //この 59 | while( isThreadRunning() != 0 ){ 60 | 61 | if( lock() ){ 62 | 63 | if(_isPlaying == true){ 64 | 65 | long nowTime = ofGetElapsedTimeMicros(); 66 | long deltaTime = nowTime - _preTime; 67 | _preTime = nowTime; 68 | 69 | if(_totalTime + _remainderOfTick >= _durationOfTick){ 70 | 71 | 72 | if((_countOfTick % (OFX_BPM_TICK / _beatPerBar)) == 0){ 73 | 74 | ofNotifyEvent(beatEvent); 75 | } 76 | 77 | _remainderOfTick = (_totalTime + _remainderOfTick) % _durationOfTick; 78 | 79 | _totalTime = 0.; 80 | 81 | _isTick = true; 82 | 83 | 84 | 85 | _countOfTick++; 86 | _countOfTick %= OFX_BPM_TICK; 87 | 88 | 89 | }else{ 90 | 91 | _isTick = false; 92 | } 93 | 94 | _totalTime += deltaTime; 95 | 96 | unlock(); 97 | 98 | yield(); 99 | } 100 | } 101 | } 102 | 103 | } 104 | 105 | void ofxBpm::setBeatPerBar(int beatPerBar){ 106 | 107 | if(lock()){ 108 | 109 | _beatPerBar = beatPerBar; 110 | unlock(); 111 | } 112 | } 113 | 114 | void ofxBpm::setBpm(float bpm){ 115 | 116 | if( lock() ){ 117 | 118 | if(bpm < OFX_BPM_MIN){ 119 | 120 | _bpm = OFX_BPM_MIN; 121 | 122 | }else if(bpm >= OFX_BPM_MAX){ 123 | 124 | _bpm = OFX_BPM_MAX; 125 | 126 | }else{ 127 | 128 | _bpm = bpm; 129 | 130 | } 131 | 132 | _durationOfTick = 60. * 1000. * 1000. / (bpm * (OFX_BPM_TICK >> 2)); 133 | 134 | unlock(); 135 | } 136 | } 137 | 138 | float ofxBpm::getBpm() const{ 139 | 140 | return _bpm; 141 | } 142 | 143 | bool ofxBpm::isPlaying() const{ 144 | 145 | return _isPlaying; 146 | } 147 | -------------------------------------------------------------------------------- /src/ofxBpm.h: -------------------------------------------------------------------------------- 1 | // 2 | // ofxBpm.h 3 | // ofxBpm 4 | // 5 | // Created by mirrorboy on 2013/08/14. 6 | // 7 | // 8 | 9 | /* 10 | You must add listener on testApp and listen beat timing. 11 | 12 | example on testApp.cpp 13 | 14 | 15 | ofAddListener(bpm.beatEvent, this, &testApp::play); 16 | 17 | void testApp::play(void){ 18 | 19 | //sound.play(); 20 | //but, you could not draw somethings. 21 | } 22 | 23 | */ 24 | 25 | #pragma once 26 | #include "ofMain.h" 27 | #include "ofThread.h" 28 | 29 | class ofxBpm : private ofThread{ 30 | 31 | public: 32 | static const float OFX_BPM_MAX; 33 | static const float OFX_BPM_DEFAULT; 34 | static const float OFX_BPM_MIN; 35 | static const int OFX_BPM_TICK; 36 | 37 | explicit ofxBpm(float bpm = OFX_BPM_DEFAULT,int beatPerBar = 4); 38 | 39 | void start(); 40 | void stop(); 41 | void reset(); 42 | 43 | void setBpm(float bpm); 44 | float getBpm() const; 45 | 46 | void setBeatPerBar(int beatPerBar); 47 | 48 | bool isPlaying() const; 49 | 50 | ofEvent beatEvent; 51 | 52 | private: 53 | void threadedFunction(); 54 | 55 | 56 | 57 | float _bpm; 58 | bool _isTick; 59 | bool _isPlaying; 60 | int _countOfTick; 61 | long _totalTime; 62 | long _durationOfTick; 63 | long _remainderOfTick; 64 | 65 | float _preTime; 66 | int _beatPerBar; 67 | 68 | inline int getCountOfTick() const; 69 | }; 70 | 71 | //init 72 | const float ofxBpm::OFX_BPM_MAX = 300. ; 73 | const float ofxBpm::OFX_BPM_DEFAULT = 120.; 74 | const float ofxBpm::OFX_BPM_MIN = 1.; 75 | const int ofxBpm::OFX_BPM_TICK = 960; 76 | --------------------------------------------------------------------------------