This source file includes following definitions.
- IsDataByte
- IsFirstStatusByte
- IsSystemRealTimeMessage
- Add
- Add
- Get
#include "media/midi/midi_message_queue.h"
#include <algorithm>
#include "base/logging.h"
#include "media/midi/midi_message_util.h"
namespace media {
namespace {
const uint8 kSysEx = 0xf0;
const uint8 kEndOfSysEx = 0xf7;
bool IsDataByte(uint8 data) {
return (data & 0x80) == 0;
}
bool IsFirstStatusByte(uint8 data) {
return !IsDataByte(data) && data != kEndOfSysEx;
}
bool IsSystemRealTimeMessage(uint8 data) {
return 0xf8 <= data && data <= 0xff;
}
}
MidiMessageQueue::MidiMessageQueue(bool allow_running_status)
: allow_running_status_(allow_running_status) {}
MidiMessageQueue::~MidiMessageQueue() {}
void MidiMessageQueue::Add(const std::vector<uint8>& data) {
queue_.insert(queue_.end(), data.begin(), data.end());
}
void MidiMessageQueue::Add(const uint8* data, size_t length) {
queue_.insert(queue_.end(), data, data + length);
}
void MidiMessageQueue::Get(std::vector<uint8>* message) {
message->clear();
while (true) {
if (queue_.empty())
return;
const uint8 next = queue_.front();
queue_.pop_front();
if (IsSystemRealTimeMessage(next)) {
message->push_back(next);
return;
}
if (!next_message_.empty() &&
((next_message_[0] == kSysEx && IsFirstStatusByte(next)) ||
(next_message_[0] != kSysEx && !IsDataByte(next)))) {
next_message_.clear();
}
if (next_message_.empty()) {
if (IsFirstStatusByte(next)) {
next_message_.push_back(next);
} else {
}
continue;
}
const uint8 status_byte = next_message_[0];
next_message_.push_back(next);
if (status_byte == kSysEx) {
if (next == kEndOfSysEx) {
std::swap(*message, next_message_);
next_message_.clear();
return;
}
continue;
}
DCHECK(IsDataByte(next));
DCHECK_NE(kSysEx, status_byte);
const size_t target_len = GetMidiMessageLength(status_byte);
if (next_message_.size() < target_len)
continue;
if (next_message_.size() == target_len) {
std::swap(*message, next_message_);
next_message_.clear();
if (allow_running_status_) {
next_message_.push_back(status_byte);
}
return;
}
NOTREACHED();
}
}
}