From ad9ea6f3622981db39eaceedfee9baa3a3f30862 Mon Sep 17 00:00:00 2001 From: Hyunin Song Date: Thu, 15 Mar 2018 06:47:42 +0900 Subject: [PATCH] Reduce code duplications --- include/Groove.h | 20 +++++++++--- include/GrooveExperiments.h | 13 +------- include/HalfSwing.h | 13 +------- include/HydrogenSwing.h | 13 +------- include/MidiSwing.h | 4 +-- src/core/Groove.cpp | 34 ++++++++++++++++--- src/core/GrooveExperiments.cpp | 60 +++------------------------------- src/core/GrooveFactory.cpp | 8 +++++ src/core/HalfSwing.cpp | 52 ++--------------------------- src/core/HydrogenSwing.cpp | 56 ++----------------------------- src/core/MidiSwing.cpp | 13 +------- 11 files changed, 68 insertions(+), 218 deletions(-) diff --git a/include/Groove.h b/include/Groove.h index 8bc4e1877..aa9cb0275 100644 --- a/include/Groove.h +++ b/include/Groove.h @@ -35,11 +35,12 @@ #include "Pattern.h" #include "SerializingObject.h" -class Groove : public SerializingObject +class Groove : public QObject, public SerializingObject { + Q_OBJECT public: - Groove(); + Groove(QObject* parent = nullptr); /* * Groove should return true if the note should be played in the curr_time tick, @@ -56,9 +57,10 @@ public: */ virtual int isInTick( MidiTime * _cur_start, fpp_t _frames, f_cnt_t _offset, Note * _n, Pattern * _p ); + int amount() const {return m_amount;} - virtual void saveSettings( QDomDocument & _doc, QDomElement & _element ); - virtual void loadSettings( const QDomElement & _this ); + virtual void saveSettings( QDomDocument & doc, QDomElement & element ); + virtual void loadSettings(const QDomElement & element ); virtual QWidget * instantiateView( QWidget * _parent ); @@ -66,6 +68,16 @@ public: { return "none"; } + +signals: + void amountChanged(int newAmount); + +public slots: + void setAmount(int amount); + +protected: + int m_amount; + float m_swingFactor; // = (m_amount / 127) }; #endif // GROOVE_H diff --git a/include/GrooveExperiments.h b/include/GrooveExperiments.h index 3ceaaa935..aea9a3347 100644 --- a/include/GrooveExperiments.h +++ b/include/GrooveExperiments.h @@ -13,7 +13,7 @@ /** * A groove thats new */ -class GrooveExperiments : public QObject, public Groove +class GrooveExperiments : public Groove { Q_OBJECT public: @@ -22,12 +22,9 @@ public: virtual ~GrooveExperiments(); void init(); - int amount(); int isInTick(MidiTime * _cur_start, const fpp_t _frames, const f_cnt_t _offset, Note * _n, Pattern * _p ); - void loadSettings( const QDomElement & _this ); - void saveSettings( QDomDocument & _doc, QDomElement & _element ); inline virtual QString nodeName() const { return "experiment"; @@ -37,20 +34,12 @@ public: QWidget * instantiateView( QWidget * _parent ); -signals: - void shiftAmountChanged(int _newAmount); - - public slots: // valid values are from 0 - 127 - void setAmount(int _amount); void update(); private: int m_frames_per_tick; - int m_shiftAmount; - float m_shiftFactor;// = (m_shiftAmount / 127) - } ; class GrooveExperimentsView : public QWidget diff --git a/include/HalfSwing.h b/include/HalfSwing.h index d91a4b343..cf8ab80c2 100644 --- a/include/HalfSwing.h +++ b/include/HalfSwing.h @@ -13,7 +13,7 @@ /** * A groove thatjust latter half of the HydrogenSwing algo. */ -class HalfSwing : public QObject, public Groove +class HalfSwing : public Groove { Q_OBJECT public: @@ -22,12 +22,9 @@ public: virtual ~HalfSwing(); void init(); - int amount(); int isInTick(MidiTime * _cur_start, const fpp_t _frames, const f_cnt_t _offset, Note * _n, Pattern * _p ); - void loadSettings( const QDomElement & _this ); - void saveSettings( QDomDocument & _doc, QDomElement & _element ); inline virtual QString nodeName() const { return "half"; @@ -35,20 +32,12 @@ public: QWidget * instantiateView( QWidget * _parent ); -signals: - void swingAmountChanged(int _newAmount); - - public slots: // valid values are from 0 - 127 - void setAmount(int _amount); void update(); private: int m_frames_per_tick; - int m_swingAmount; - float m_swingFactor;// = (m_swingAmount / 127) - } ; class HalfSwingView : public QWidget diff --git a/include/HydrogenSwing.h b/include/HydrogenSwing.h index ec329cf4d..1a6406e7d 100644 --- a/include/HydrogenSwing.h +++ b/include/HydrogenSwing.h @@ -13,7 +13,7 @@ /** * A groove that mimics Hydrogen drum machine's swing feature */ -class HydrogenSwing : public QObject, public Groove +class HydrogenSwing : public Groove { Q_OBJECT public: @@ -22,12 +22,9 @@ public: virtual ~HydrogenSwing(); void init(); - int amount(); int isInTick(MidiTime * _cur_start, const fpp_t _frames, const f_cnt_t _offset, Note * _n, Pattern * _p ); - void loadSettings( const QDomElement & _this ); - void saveSettings( QDomDocument & _doc, QDomElement & _element ); inline virtual QString nodeName() const { return "hydrogen"; @@ -37,20 +34,12 @@ public: QWidget * instantiateView( QWidget * _parent ); -signals: - void swingAmountChanged(int _newAmount); - - public slots: // valid values are from 0 - 127 - void setAmount(int _amount); void update(); private: int m_frames_per_tick; - int m_swingAmount; - float m_swingFactor;// = (m_swingAmount / 127) - } ; class HydrogenSwingView : public QWidget diff --git a/include/MidiSwing.h b/include/MidiSwing.h index 396dcd8f0..741641fca 100644 --- a/include/MidiSwing.h +++ b/include/MidiSwing.h @@ -13,7 +13,7 @@ * A swing groove that adjusts by whole ticks. * Someone might like it, also might be able to save the output to a midi file later. */ -class MidiSwing : public QObject, public Groove +class MidiSwing : public Groove { Q_OBJECT public: @@ -25,8 +25,6 @@ public: int isInTick(MidiTime * cur_start, const fpp_t _frames, const f_cnt_t _offset, Note * n, Pattern * p ); int isInTick(MidiTime * _cur_start, Note * _n, Pattern * _p ); - void loadSettings( const QDomElement & _this ); - void saveSettings( QDomDocument & _doc, QDomElement & _element ); inline virtual QString nodeName() const { return "midi"; diff --git a/src/core/Groove.cpp b/src/core/Groove.cpp index 49bfc6437..b5b32ffeb 100644 --- a/src/core/Groove.cpp +++ b/src/core/Groove.cpp @@ -9,7 +9,8 @@ #include "Pattern.h" #include "Song.h" -Groove::Groove() +Groove::Groove(QObject *parent) : + QObject(parent) { } @@ -23,15 +24,40 @@ int Groove::isInTick(MidiTime * _cur_start, fpp_t _frames, f_cnt_t _offset, return _n->pos().getTicks() == _cur_start->getTicks() ? 0 : -1; } - -void Groove::saveSettings( QDomDocument & _doc, QDomElement & _element ) +void Groove::setAmount(int amount) { + if (amount < 0) { + amount = 0; + } + if (amount > 127) { + amount = 127; + } + + m_amount = amount; + m_swingFactor = ((float)m_amount) / 127.0f; + emit amountChanged(m_amount); + } -void Groove::loadSettings( const QDomElement & _this ) +void Groove::saveSettings(QDomDocument & doc, QDomElement & element ) { + Q_UNUSED(doc); + element.setAttribute("amount", m_amount); +} +void Groove::loadSettings( const QDomElement & element ) +{ + bool ok; + int amount = element.attribute("amount").toInt(&ok); + if (ok) + { + setAmount(amount); + } + else + { + setAmount(0); + } } QWidget * Groove::instantiateView( QWidget * _parent ) diff --git a/src/core/GrooveExperiments.cpp b/src/core/GrooveExperiments.cpp index 9b1d19181..5235a6509 100644 --- a/src/core/GrooveExperiments.cpp +++ b/src/core/GrooveExperiments.cpp @@ -37,12 +37,11 @@ #include "stdio.h" -GrooveExperiments::GrooveExperiments(QObject * _parent) : - QObject( _parent ), - Groove() +GrooveExperiments::GrooveExperiments(QObject * parent) : + Groove(parent) { - m_shiftAmount = 0; - m_shiftFactor = 0.0; + m_amount = 0; + m_swingFactor = 0.0; init(); update(); } @@ -63,41 +62,11 @@ void GrooveExperiments::init() } -int GrooveExperiments::amount() -{ - return m_shiftAmount; -} - void GrooveExperiments::update() { m_frames_per_tick = Engine::framesPerTick(); } -void GrooveExperiments::setAmount(int _amount) -{ - - if (_amount > 0 && _amount <= 127) - { - m_shiftAmount = _amount; - m_shiftFactor = (((int)m_shiftAmount) / 127); - emit shiftAmountChanged(m_shiftAmount); - } - else if (_amount == 0) - { - m_shiftAmount = 0; - m_shiftFactor = 0.0; - emit shiftAmountChanged(m_shiftAmount); - } - else - { - m_shiftAmount = 127; - m_shiftFactor = 1.0; - emit shiftAmountChanged(m_shiftAmount); - } - -} - - int GrooveExperiments::isInTick(MidiTime * _cur_start, const fpp_t _frames, const f_cnt_t _offset, Note * _n, Pattern * _p ) { // TODO why is this wrong on boot how do we set it once not every loop @@ -129,7 +98,7 @@ int GrooveExperiments::isInTick(MidiTime * _cur_start, const fpp_t _frames, cons if ( pos_in_eigth >= 0 ) { - float ticks_to_shift = ((pos_in_eigth - 12) * -m_shiftFactor); + float ticks_to_shift = ((pos_in_eigth - 12) * -m_swingFactor); f_cnt_t frames_to_shift = (int)(ticks_to_shift * m_frames_per_tick); @@ -153,25 +122,6 @@ int GrooveExperiments::isInTick(MidiTime * _cur_start, const fpp_t _frames, cons // else no groove adjustments return _n->pos().getTicks() == _cur_start->getTicks() ? 0 : -1; } -// FIXME: Broken. -void GrooveExperiments::saveSettings( QDomDocument & _doc, QDomElement & _element ) -{ - _element.setAttribute("shiftAmount", m_shiftAmount); -} -// FIXME: Broken. -void GrooveExperiments::loadSettings( const QDomElement & _this ) -{ - bool ok; - int amount = _this.attribute("shiftAmount").toInt(&ok); - if (ok) - { - setAmount(amount); - } - else - { - setAmount(0); - } -} QWidget * GrooveExperiments::instantiateView( QWidget * _parent ) { diff --git a/src/core/GrooveFactory.cpp b/src/core/GrooveFactory.cpp index b8ec3e937..10705f18d 100644 --- a/src/core/GrooveFactory.cpp +++ b/src/core/GrooveFactory.cpp @@ -3,7 +3,9 @@ #include "GrooveFactory.h" #include "Groove.h" +#include "GrooveExperiments.h" #include "MidiSwing.h" +#include "HalfSwing.h" #include "HydrogenSwing.h" GrooveFactory::GrooveFactory() @@ -26,6 +28,12 @@ Groove * GrooveFactory::create(QString _grooveType) { if (_grooveType == "midi") { return new MidiSwing(); } + if (_grooveType == "half") { + return new HalfSwing(); + } + if (_grooveType == "experiment") { + return new GrooveExperiments(); + } return new Groove(); } diff --git a/src/core/HalfSwing.cpp b/src/core/HalfSwing.cpp index ffa03b2c4..9c98a439d 100644 --- a/src/core/HalfSwing.cpp +++ b/src/core/HalfSwing.cpp @@ -40,10 +40,9 @@ HalfSwing::HalfSwing(QObject * _parent) : - QObject( _parent ), - Groove() + Groove(_parent) { - m_swingAmount = 0; + m_amount = 0; m_swingFactor = 0.0; init(); update(); @@ -65,40 +64,12 @@ void HalfSwing::init() } -int HalfSwing::amount() -{ - return m_swingAmount; -} void HalfSwing::update() { m_frames_per_tick = Engine::framesPerTick(); } -void HalfSwing::setAmount(int _amount) -{ - - if (_amount > 0 && _amount <= 127) - { - m_swingAmount = _amount; - m_swingFactor = (((int)m_swingAmount) / 127); - emit swingAmountChanged(m_swingAmount); - } - else if (_amount == 0) - { - m_swingAmount = 0; - m_swingFactor = 0.0; - emit swingAmountChanged(m_swingAmount); - } - else - { - m_swingAmount = 127; - m_swingFactor = 1.0; - emit swingAmountChanged(m_swingAmount); - } - -} - int HalfSwing::isInTick(MidiTime * _cur_start, const fpp_t _frames, const f_cnt_t _offset, Note * _n, Pattern * _p ) @@ -159,25 +130,6 @@ int HalfSwing::isInTick(MidiTime * _cur_start, const fpp_t _frames, const f_cnt_ // else no groove adjustments return _n->pos().getTicks() == _cur_start->getTicks() ? 0 : -1; } -// FIXME: Broken. -void HalfSwing::saveSettings( QDomDocument & _doc, QDomElement & _element ) -{ - _element.setAttribute("swingAmount", m_swingAmount); -} -// FIXME: Broken. -void HalfSwing::loadSettings( const QDomElement & _this ) -{ - bool ok; - int amount = _this.attribute("swingAmount").toInt(&ok); - if (ok) - { - setAmount(amount); - } - else - { - setAmount(0); - } -} QWidget * HalfSwing::instantiateView( QWidget * _parent ) { diff --git a/src/core/HydrogenSwing.cpp b/src/core/HydrogenSwing.cpp index 30bc840c0..76fcd4719 100644 --- a/src/core/HydrogenSwing.cpp +++ b/src/core/HydrogenSwing.cpp @@ -40,10 +40,9 @@ HydrogenSwing::HydrogenSwing(QObject * _parent) : - QObject( _parent ), - Groove() + Groove(_parent) { - m_swingAmount = 0; + m_amount = 0; m_swingFactor = 0.0; init(); update(); @@ -65,41 +64,11 @@ void HydrogenSwing::init() } -int HydrogenSwing::amount() -{ - return m_swingAmount; -} - void HydrogenSwing::update() { m_frames_per_tick = Engine::framesPerTick(); } -void HydrogenSwing::setAmount(int _amount) -{ - - if (_amount > 0 && _amount <= 127) - { - m_swingAmount = _amount; - m_swingFactor = (((int)m_swingAmount) / 127); - emit swingAmountChanged(m_swingAmount); - } - else if (_amount == 0) - { - m_swingAmount = 0; - m_swingFactor = 0.0; - emit swingAmountChanged(m_swingAmount); - } - else - { - m_swingAmount = 127; - m_swingFactor = 1.0; - emit swingAmountChanged(m_swingAmount); - } - -} - - int HydrogenSwing::isInTick(MidiTime * _cur_start, const fpp_t _frames, const f_cnt_t _offset, Note * _n, Pattern * _p ) { @@ -166,32 +135,11 @@ int HydrogenSwing::isInTick(MidiTime * _cur_start, const fpp_t _frames, const f_ return _n->pos().getTicks() == _cur_start->getTicks() ? 0 : -1; } -void HydrogenSwing::saveSettings( QDomDocument & _doc, QDomElement & _element ) -{ - _element.setAttribute("swingAmount", m_swingAmount); -} - -void HydrogenSwing::loadSettings( const QDomElement & _this ) -{ - bool ok; - int amount = _this.attribute("swingAmount").toInt(&ok); - if (ok) - { - setAmount(amount); - } - else - { - setAmount(0); - } -} - QWidget * HydrogenSwing::instantiateView( QWidget * _parent ) { return new HydrogenSwingView(this, _parent); } - - // VIEW // HydrogenSwingView::HydrogenSwingView(HydrogenSwing * _hy_swing, QWidget * _parent) : diff --git a/src/core/MidiSwing.cpp b/src/core/MidiSwing.cpp index 747fd31ec..877eb3fa7 100644 --- a/src/core/MidiSwing.cpp +++ b/src/core/MidiSwing.cpp @@ -28,8 +28,7 @@ #include "MidiSwing.h" MidiSwing::MidiSwing(QObject * _parent) : - QObject( _parent ), - Groove() + Groove(_parent) { } @@ -73,16 +72,6 @@ int MidiSwing::isInTick(MidiTime * _cur_start, Note * _n, Pattern * _p ) } -void MidiSwing::saveSettings( QDomDocument & _doc, QDomElement & _element ) -{ - -} - -void MidiSwing::loadSettings( const QDomElement & _this ) -{ - -} - QWidget * MidiSwing::instantiateView( QWidget * _parent ) { return new QLabel("");