Reduce code duplications

This commit is contained in:
Hyunin Song
2018-03-15 06:47:42 +09:00
parent d6529ae1e8
commit ad9ea6f362
11 changed files with 68 additions and 218 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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";

View File

@@ -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 )

View File

@@ -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 )
{

View File

@@ -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();
}

View File

@@ -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 )
{

View File

@@ -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) :

View File

@@ -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("");