Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix: excessive CPU use in the MIDI SenderThread #24

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified run_environment/metronome/metronome-01.wav
Binary file not shown.
18 changes: 12 additions & 6 deletions src/midi/Metronome.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
#include <QFile>
#include <QFileInfo>

#include <QMediaPlayer>
#include <QAudioDeviceInfo>
#include <QSoundEffect>
#include <QtGlobal>

Metronome *Metronome::_instance = new Metronome();
bool Metronome::_enable = false;
Expand All @@ -15,9 +17,13 @@ Metronome::Metronome(QObject *parent) : QObject(parent) {
_file = 0;
num = 4;
denom = 2;
_player = new QMediaPlayer(this, QMediaPlayer::LowLatency);
_player->setVolume(100);
_player->setMedia(QUrl::fromLocalFile(QFileInfo("metronome/metronome-01.wav").absoluteFilePath()));
#if QT_VERSION >= 0x050D00
_player = new QSoundEffect(QAudioDeviceInfo::defaultOutputDevice(), this);
#else
_player = new QSoundEffect(this);
#endif
_player->setVolume(1.0);
_player->setSource(QUrl("qrc:/run_environment/metronome/metronome-01.wav"));
}

void Metronome::setFile(MidiFile *file){
Expand Down Expand Up @@ -87,9 +93,9 @@ void Metronome::setEnabled(bool b){
}

void Metronome::setLoudness(int value){
_instance->_player->setVolume(value);
_instance->_player->setVolume(value / 100.0);
}

int Metronome::loudness(){
return _instance->_player->volume();
return (int)(_instance->_player->volume() * 100);
}
4 changes: 2 additions & 2 deletions src/midi/Metronome.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <QObject>

#include <QMediaPlayer>
#include <QSoundEffect>

class MidiFile;

Expand Down Expand Up @@ -35,7 +35,7 @@ public slots:
void click();
static bool _enable;

QMediaPlayer* _player;
QSoundEffect* _player;
};

#endif
13 changes: 12 additions & 1 deletion src/midi/SenderThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@ SenderThread::SenderThread()
{
_eventQueue = new QQueue<MidiEvent*>;
_noteQueue = new QQueue<MidiEvent*>;
_mutex = new QMutex();
_waitCondition = new QWaitCondition();
}

void SenderThread::run()
{
_mutex->lock();

while (true) {
_waitCondition->wait(_mutex, 500);

// First, send the misc events, such as control change and program change events.
while (!_eventQueue->isEmpty()) {
// send command
Expand All @@ -43,16 +48,22 @@ void SenderThread::run()
MidiOutput::sendEnqueuedCommand(_noteQueue->head()->save());
_noteQueue->pop_front();
}
msleep(1);
}

_mutex->unlock();
}

void SenderThread::enqueue(MidiEvent* event)
{
_mutex->lock();

// If it is a NoteOnEvent or an OffEvent, we put it in _noteQueue.
if (dynamic_cast<NoteOnEvent*>(event) || dynamic_cast<OffEvent*>(event))
_noteQueue->push_back(event);
// Otherwise, it goes into _eventQueue.
else
_eventQueue->push_back(event);

_waitCondition->wakeOne();
_mutex->unlock();
}
4 changes: 4 additions & 0 deletions src/midi/SenderThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
#ifndef SENDERTHREAD_H_
#define SENDERTHREAD_H_

#include <QMutex>
#include <QQueue>
#include <QThread>
#include <QWaitCondition>

#include "MidiOutput.h"

Expand All @@ -34,6 +36,8 @@ class SenderThread : public QThread {
private:
QQueue<MidiEvent*>* _eventQueue;
QQueue<MidiEvent*>* _noteQueue;
QMutex* _mutex;
QWaitCondition* _waitCondition;
};

#endif