Skip to content

Commit

Permalink
Timeout added to drum element note/key status LEDs turning off.
Browse files Browse the repository at this point in the history
  • Loading branch information
rncbc committed Mar 31, 2017
1 parent 651539e commit de288af
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 89 deletions.
28 changes: 5 additions & 23 deletions src/drumkv1.cpp
Expand Up @@ -628,7 +628,6 @@ class drumkv1_elem : public drumkv1_list<drumkv1_elem>
void reset();

void midiInEnabled(bool on);
bool midiInNote(int note) const;
uint32_t midiInCount();

drumkv1_sample gen1_sample;
Expand Down Expand Up @@ -765,15 +764,12 @@ class drumkv1_midi_in : public drumkv1_sched

drumkv1_midi_in (drumkv1 *pDrumk)
: drumkv1_sched(pDrumk, MidiIn),
m_enabled(false), m_count(0)
{ for (int i = 0; i < MAX_NOTES; ++i) m_notes[i] = false; }
m_enabled(false), m_count(0) {}

void schedule_event()
{ if (m_enabled && ++m_count < 2) schedule(-1); }
void schedule_note_on(int note)
{ m_notes[note] = true; if (m_enabled) schedule(note); }
void schedule_note_off(int note)
{ m_notes[note] = false; if (m_enabled) schedule(note); }
void schedule_note(int key, int vel)
{ if (m_enabled) schedule((vel << 7) | key); }

void process(int) {}

Expand All @@ -787,14 +783,10 @@ class drumkv1_midi_in : public drumkv1_sched
return ret;
}

bool note_on (int note) const
{ return m_notes [note]; }

private:

bool m_enabled;
uint32_t m_count;
bool m_notes[MAX_NOTES];
};


Expand Down Expand Up @@ -856,7 +848,6 @@ class drumkv1_impl
void reset();

void midiInEnabled(bool on);
bool midiInNote(int note) const;
uint32_t midiInCount();

protected:
Expand Down Expand Up @@ -1529,7 +1520,7 @@ void drumkv1_impl::process_midi ( uint8_t *data, uint32_t size )
m_group[pv->group] = pv;
}
}
m_midi_in.schedule_note_on(key);
m_midi_in.schedule_note(key, value);
}
// note off
else if (status == 0x80 || (status == 0x90 && value == 0)) {
Expand All @@ -1544,7 +1535,7 @@ void drumkv1_impl::process_midi ( uint8_t *data, uint32_t size )
}
}
}
m_midi_in.schedule_note_off(key);
m_midi_in.schedule_note(key, 0);
}
// key pressure/poly.aftertouch
else if (status == 0xa0) {
Expand Down Expand Up @@ -1752,11 +1743,6 @@ void drumkv1_impl::midiInEnabled ( bool on )
m_midi_in.enabled(on);
}

bool drumkv1_impl::midiInNote ( int note ) const
{
return m_midi_in.note_on(note);
}

uint32_t drumkv1_impl::midiInCount (void)
{
return m_midi_in.count();
Expand Down Expand Up @@ -2392,10 +2378,6 @@ void drumkv1::midiInEnabled ( bool on )
m_pImpl->midiInEnabled(on);
}

bool drumkv1::midiInNote ( int note ) const
{
return m_pImpl->midiInNote(note);
}

uint32_t drumkv1::midiInCount (void)
{
Expand Down
1 change: 0 additions & 1 deletion src/drumkv1.h
Expand Up @@ -178,7 +178,6 @@ class drumkv1
virtual void updatePreset(bool bDirty) = 0;

void midiInEnabled(bool on);
bool midiInNote(int note) const;
uint32_t midiInCount();

protected:
Expand Down
4 changes: 0 additions & 4 deletions src/drumkv1_ui.cpp
Expand Up @@ -144,10 +144,6 @@ void drumkv1_ui::midiInEnabled ( bool bEnabled )
m_pDrumk->midiInEnabled(bEnabled);
}

bool drumkv1_ui::midiInNote ( int note ) const
{
return m_pDrumk->midiInNote(note);
}

uint32_t drumkv1_ui::midiInCount (void)
{
Expand Down
1 change: 0 additions & 1 deletion src/drumkv1_ui.h
Expand Up @@ -66,7 +66,6 @@ class drumkv1_ui
void updatePreset(bool bDirty);

void midiInEnabled(bool bEnabled);
bool midiInNote(int note) const;
uint32_t midiInCount();

private:
Expand Down
2 changes: 1 addition & 1 deletion src/drumkv1widget.cpp
Expand Up @@ -1370,7 +1370,7 @@ void drumkv1widget::updateSchedNotify ( int stype, int sid )
switch (drumkv1_sched::Type(stype)) {
case drumkv1_sched::MidiIn:
if (sid >= 0)
m_ui.Elements->midiInNote(sid);
m_ui.Elements->midiInLedNote(sid & 0x7f, (sid >> 7) & 0x7f);
else
if (pDrumkUi->midiInCount() > 0) {
m_ui.StatusBar->midiInLed(true);
Expand Down
93 changes: 37 additions & 56 deletions src/drumkv1widget_elements.cpp
Expand Up @@ -27,13 +27,12 @@

#include "drumkv1_sample.h"

#include <QAbstractItemModel>
#include <QHeaderView>
#include <QFileInfo>
#include <QMimeData>
#include <QDrag>
#include <QUrl>
#include <QIcon>
#include <QTimer>

#include <QDragEnterEvent>
#include <QDragMoveEvent>
Expand All @@ -43,53 +42,6 @@
//----------------------------------------------------------------------------
// drumkv1widget_elements_model -- List model.

class drumkv1widget_elements_model : public QAbstractItemModel
{
public:

// Constructor.
drumkv1widget_elements_model(drumkv1_ui *pDrumkUi, QObject *pParent = NULL);

// Concretizers (virtual).
int rowCount(const QModelIndex& parent = QModelIndex()) const;
int columnCount(const QModelIndex& parent = QModelIndex()) const;

QVariant headerData(int section, Qt::Orientation orient, int role) const;
QVariant data(const QModelIndex& index, int role) const;

QModelIndex index(int row, int column,
const QModelIndex& parent = QModelIndex()) const;

QModelIndex parent(const QModelIndex&) const;

void reset();

// Accessor specific.
drumkv1_ui *instance() const;

void midiInNote(int note);

protected:

// Other specifics
drumkv1_element *elementFromIndex(const QModelIndex& index) const;

QString itemDisplay(const QModelIndex& index) const;
QString itemToolTip(const QModelIndex& index) const;

int columnAlignment(int column) const;

private:

// Model variables.
QIcon m_icons;
QStringList m_headers;

drumkv1_ui *m_pDrumkUi;

};


// Constructor.
drumkv1widget_elements_model::drumkv1widget_elements_model (
drumkv1_ui *pDrumkUi, QObject *pParent )
Expand All @@ -104,14 +56,17 @@ drumkv1widget_elements_model::drumkv1widget_elements_model (
<< tr("Element")
<< tr("Sample");

for (int i = 0; i < MAX_NOTES; ++i)
m_notes_on[i] = 0;

reset();
}


int drumkv1widget_elements_model::rowCount (
const QModelIndex& /*parent*/ ) const
{
return 128;
return MAX_NOTES;
}


Expand Down Expand Up @@ -146,8 +101,7 @@ QVariant drumkv1widget_elements_model::data (
case Qt::DecorationRole:
if (index.column() == 0) {
return m_icons.pixmap(12, 12, QIcon::Normal,
m_pDrumkUi->midiInNote(index.row())
? QIcon::On : QIcon::Off);
m_notes_on[index.row()] > 0 ? QIcon::On : QIcon::Off);
}
break;
case Qt::DisplayRole:
Expand Down Expand Up @@ -189,9 +143,36 @@ drumkv1_ui *drumkv1widget_elements_model::instance (void) const
}


void drumkv1widget_elements_model::midiInNote ( int note )
void drumkv1widget_elements_model::midiInLedNote ( int key, int vel )
{
if (vel > 0) {
m_notes_on[key] = vel;
midiInLedUpdate(key);
}
else
if (m_notes_on[key] > 0) {
m_notes_off.append(key);
QTimer::singleShot(200, this, SLOT(midiInLedTimeout()));
}
}


void drumkv1widget_elements_model::midiInLedTimeout (void)
{
QListIterator<int> iter(m_notes_off);
while (iter.hasNext()) {
const int key = iter.next();
midiInLedUpdate(key);
m_notes_on[key] = 0;
}

m_notes_off.clear();
}


void drumkv1widget_elements_model::midiInLedUpdate ( int key )
{
const QModelIndex& index = drumkv1widget_elements_model::index(note, 0);
const QModelIndex& index = drumkv1widget_elements_model::index(key, 0);
#if QT_VERSION >= 0x050100
emit dataChanged(index, index, QVector<int>() << Qt::DecorationRole);
#else
Expand Down Expand Up @@ -469,10 +450,10 @@ QSize drumkv1widget_elements::sizeHint (void) const


// MIDI input status update
void drumkv1widget_elements::midiInNote ( int note )
void drumkv1widget_elements::midiInLedNote ( int key, int vel )
{
if (m_pModel)
m_pModel->midiInNote(note);
m_pModel->midiInLedNote(key, vel);
}


Expand Down
68 changes: 65 additions & 3 deletions src/drumkv1widget_elements.h
Expand Up @@ -23,20 +23,82 @@
#define __drumkv1widget_elements_h

#include <QTreeView>
#include <QAbstractItemModel>
#include <QIcon>


// Forwards.
class drumkv1widget_elements_model;

class drumkv1_ui;

class drumkv1_element;
class drumkv1_sample;

class QDragEnterEvent;
class QDragMoveEvent;
class QDropEvent;


//----------------------------------------------------------------------------
// drumkv1widget_elements_model -- List model.

class drumkv1widget_elements_model : public QAbstractItemModel
{
Q_OBJECT

public:

// Constructor.
drumkv1widget_elements_model(drumkv1_ui *pDrumkUi, QObject *pParent = NULL);

// Concretizers (virtual).
int rowCount(const QModelIndex& parent = QModelIndex()) const;
int columnCount(const QModelIndex& parent = QModelIndex()) const;

QVariant headerData(int section, Qt::Orientation orient, int role) const;
QVariant data(const QModelIndex& index, int role) const;

QModelIndex index(int row, int column,
const QModelIndex& parent = QModelIndex()) const;

QModelIndex parent(const QModelIndex&) const;

void reset();

// Accessor specific.
drumkv1_ui *instance() const;

void midiInLedNote(int key, int vel);

protected slots:

void midiInLedTimeout();

protected:

// Other specifics
drumkv1_element *elementFromIndex(const QModelIndex& index) const;

QString itemDisplay(const QModelIndex& index) const;
QString itemToolTip(const QModelIndex& index) const;

int columnAlignment(int column) const;

void midiInLedUpdate(int key);

private:

// Model variables.
QIcon m_icons;
QStringList m_headers;

drumkv1_ui *m_pDrumkUi;

static const int MAX_NOTES = 128;
int m_notes_on[MAX_NOTES];
QList<int> m_notes_off;
};


//----------------------------------------------------------------------------
// drumkv1widget_elements -- Custom (tree) list view.

Expand Down Expand Up @@ -64,7 +126,7 @@ class drumkv1widget_elements : public QTreeView
void refresh();

// MIDI input status update
void midiInNote(int note);
void midiInLedNote(int key, int vel);

signals:

Expand Down

0 comments on commit de288af

Please sign in to comment.