Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/thestk/rtmidi
Browse files Browse the repository at this point in the history
  • Loading branch information
garyscavone committed Sep 1, 2017
2 parents 41f80e6 + 493e236 commit 88e53b9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
50 changes: 31 additions & 19 deletions RtMidi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,28 +344,41 @@ double MidiInApi :: getMessage( std::vector<unsigned char> *message )
return 0.0;
}

if ( inputData_.queue.size == 0 ) return 0.0;

double timeStamp;
if (!inputData_.queue.pop(message, &timeStamp))
return 0.0;

return timeStamp;
}

bool MidiInApi::MidiQueue::push(const MidiInApi::MidiMessage& msg)
unsigned int MidiInApi::MidiQueue::size(unsigned int *__back,
unsigned int *__front)
{
// As long as we haven't reached our queue size limit, push the message.
unsigned int _back = back;
unsigned int _front = front;
unsigned int size;

// Access back/front members exactly once and make stack copies for
// size calculation
unsigned int _back = back, _front = front, _size;
if (_back >= _front)
size = _back - _front;
_size = _back - _front;
else
size = ringSize - _front + _back;
_size = ringSize - _front + _back;

// Return copies of back/front so no new and unsynchronized accesses
// to member variables are needed.
if (__back) *__back = _back;
if (__front) *__front = _front;
return _size;
}

// As long as we haven't reached our queue size limit, push the message.
bool MidiInApi::MidiQueue::push(const MidiInApi::MidiMessage& msg)
{
// Local stack copies of front/back
unsigned int _back, _front, _size;

// Get back/front indexes exactly once and calculate current size
_size = size(&_back, &_front);

if ( size < ringSize-1 )
if ( _size < ringSize-1 )
{
ring[_back] = msg;
back = (back+1)%ringSize;
Expand All @@ -377,21 +390,20 @@ bool MidiInApi::MidiQueue::push(const MidiInApi::MidiMessage& msg)

bool MidiInApi::MidiQueue::pop(std::vector<unsigned char> *msg, double* timeStamp)
{
unsigned int _back = back;
unsigned int _front = front;
unsigned int size;
// Local stack copies of front/back
unsigned int _back, _front, _size;

if (_back >= _front)
size = _back - _front;
else
size = ringSize - _front + _back;
// Get back/front indexes exactly once and calculate current size
_size = size(&_back, &_front);

if (size == 0)
if (_size == 0)
return false;

// Copy queued message to the vector pointer argument and then "pop" it.
msg->assign( ring[_front].bytes.begin(), ring[_front].bytes.end() );
*timeStamp = ring[_front].timeStamp;

// Update front
front = (front+1)%ringSize;
return true;
}
Expand Down
5 changes: 3 additions & 2 deletions RtMidi.h
Original file line number Diff line number Diff line change
Expand Up @@ -516,15 +516,16 @@ class MidiInApi : public MidiApi
struct MidiQueue {
unsigned int front;
unsigned int back;
unsigned int size;
unsigned int ringSize;
MidiMessage *ring;

// Default constructor.
MidiQueue()
:front(0), back(0), size(0), ringSize(0), ring(0) {}
:front(0), back(0), ringSize(0), ring(0) {}
bool push(const MidiMessage&);
bool pop(std::vector<unsigned char>*, double*);
unsigned int size(unsigned int *back=0,
unsigned int *front=0);
};

// The RtMidiInData structure is used to pass private class data to
Expand Down

0 comments on commit 88e53b9

Please sign in to comment.