summaryrefslogtreecommitdiffstats
path: root/media/midi/midi_message_queue.cc
blob: d35acc27bf93bf6241c72a0de0ac50ac2516fe5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/midi/midi_message_queue.h"

#include <algorithm>

#include "base/logging.h"
#include "media/midi/midi_message_util.h"

namespace media {
namespace midi {
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;
}

bool IsSystemMessage(uint8 data) {
  return 0xf0 <= data;
}

}  // namespace

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

    // "System Real Time Messages" is a special kind of MIDI messages, which can
    // appear at arbitrary byte position of MIDI stream. Here we reorder
    // "System Real Time Messages" prior to |next_message_| so that each message
    // can be clearly separated as a complete MIDI message.
    if (IsSystemRealTimeMessage(next)) {
      message->push_back(next);
      return;
    }

    // Here |next_message_[0]| may contain the previous status byte when
    // |allow_running_status_| is true. Following condition fixes up
    // |next_message_| if running status condition is not fulfilled.
    if (!next_message_.empty() &&
        ((next_message_[0] == kSysEx && IsFirstStatusByte(next)) ||
         (next_message_[0] != kSysEx && !IsDataByte(next)))) {
      // An invalid data sequence is found or running status condition is not
      // fulfilled.
      next_message_.clear();
    }

    if (next_message_.empty()) {
      if (IsFirstStatusByte(next)) {
        next_message_.push_back(next);
      } else {
        // MIDI protocol doesn't provide any error correction mechanism in
        // physical layers, and incoming messages can be corrupted, and should
        // be corrected here.
      }
      continue;
    }

    // Here we can assume |next_message_| starts with a valid status byte.
    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_ && !IsSystemMessage(status_byte)) {
        // Speculatively keep the status byte in case of running status. If this
        // assumption is not true, |next_message_| will be cleared anyway.
        // Note that system common messages should reset the running status.
        next_message_.push_back(status_byte);
      }
      return;
    }

    NOTREACHED();
  }
}

}  // namespace midi
}  // namespace media