![]() |
Mixxx
|
00001 00002 #include <QtDebug> 00003 00004 #include "BPMDetect.h" 00005 #include "trackinfoobject.h" 00006 #include "track/beatgrid.h" 00007 #include "track/beatfactory.h" 00008 #include "analyserbpm.h" 00009 00010 00011 AnalyserBPM::AnalyserBPM(ConfigObject<ConfigValue> *_config) { 00012 m_pConfig = _config; 00013 m_pDetector = NULL; 00014 } 00015 00016 void AnalyserBPM::initialise(TrackPointer tio, int sampleRate, int totalSamples) { 00017 Q_UNUSED(totalSamples); 00018 m_iMinBpm = m_pConfig->getValueString(ConfigKey("[BPM]","BPMRangeStart")).toInt(); 00019 m_iMaxBpm = m_pConfig->getValueString(ConfigKey("[BPM]","BPMRangeEnd")).toInt(); 00020 m_bProcessEntireSong = (bool)m_pConfig->getValueString(ConfigKey("[BPM]","AnalyzeEntireSong")).toInt(); 00021 // var not used, remove? TODO(bkgood) 00022 // int defaultrange = m_pConfig->getValueString(ConfigKey("[BPM]","BPMAboveRangeEnabled")).toInt(); 00023 bool bpmEnabled = (bool)m_pConfig->getValueString(ConfigKey("[BPM]","BPMDetectionEnabled")).toInt(); 00024 00025 // If BPM detection is enabled and the track does not have a BPM already, 00026 // create a detector. 00027 if(bpmEnabled && tio->getBpm() <= 0.0) { 00028 // All SoundSource's return stereo data, no matter the real file's type 00029 m_pDetector = new soundtouch::BPMDetect(2, sampleRate); 00030 //m_pDetector = new BPMDetect(tio->getChannels(), sampleRate); 00031 // defaultrange ? MIN_BPM : m_iMinBpm, 00032 // defaultrange ? MAX_BPM : m_iMaxBpm); 00033 } 00034 } 00035 00036 void AnalyserBPM::process(const CSAMPLE *pIn, const int iLen) { 00037 // Check if BPM detection is enabled 00038 if(m_pDetector == NULL) { 00039 return; 00040 } 00041 //qDebug() << "AnalyserBPM::process() processing " << iLen << " samples"; 00042 00043 m_pDetector->inputSamples(pIn, iLen/2); 00044 } 00045 00046 float AnalyserBPM::correctBPM( float BPM, int min, int max, int aboveRange) { 00047 //qDebug() << "BPM range is" << min << "to" << max; 00048 if ( BPM == 0 ) return BPM; 00049 00050 if (aboveRange == 0) { 00051 if( BPM*2 < max ) BPM *= 2; 00052 while ( BPM > max ) BPM /= 2; 00053 } 00054 while ( BPM < min ) BPM *= 2; 00055 00056 return BPM; 00057 } 00058 00059 void AnalyserBPM::finalise(TrackPointer tio) { 00060 // Check if BPM detection is enabled 00061 if(m_pDetector == NULL) { 00062 return; 00063 } 00064 00065 float bpm = m_pDetector->getBpm(); 00066 if(bpm != 0) { 00067 // Shift it by 2's until it is in the desired range 00068 float newbpm = correctBPM(bpm, m_iMinBpm, m_iMaxBpm, m_pConfig->getValueString(ConfigKey("[BPM]","BPMAboveRangeEnabled")).toInt()); 00069 00070 tio->setBpm(newbpm); 00071 tio->setBpmConfirm(); 00072 00073 // Currently, the BPM is only analyzed if the track has no BPM. This 00074 // means we don't have to worry that the track already has an existing 00075 // BeatGrid. 00076 BeatsPointer pBeats = BeatFactory::makeBeatGrid(tio, newbpm, 0.0f); 00077 tio->setBeats(pBeats); 00078 00079 //if(pBpmReceiver) { 00080 //pBpmReceiver->setComplete(tio, false, bpm); 00081 //} 00082 //qDebug() << "AnalyserBPM BPM detection successful for" << tio->getFilename(); 00083 //qDebug() << "AnalyserBPM BPM is " << newbpm << " (raw: " << bpm << ")"; 00084 } else { 00085 //qDebug() << "AnalyserBPM BPM detection failed, setting to 0."; 00086 } 00087 00088 // Cleanup the BPM detector 00089 delete m_pDetector; 00090 m_pDetector = NULL; 00091 00092 }