yukawa@chromium.org | 09ef89a | 2013-11-29 06:27:41 +0000 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef MEDIA_MIDI_MIDI_MESSAGE_QUEUE_H_ |
| 6 | #define MEDIA_MIDI_MIDI_MESSAGE_QUEUE_H_ |
| 7 | |
avi | 793390d | 2015-12-22 22:22:36 -0800 | [diff] [blame] | 8 | #include <stddef.h> |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 9 | #include <stdint.h> |
| 10 | |
yukawa@chromium.org | 09ef89a | 2013-11-29 06:27:41 +0000 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
Prashant Malani | f55fb1d | 2019-08-08 17:00:08 +0000 | [diff] [blame] | 13 | #include "base/containers/circular_deque.h" |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 14 | #include "base/macros.h" |
brettw | 49ff017 | 2015-05-05 12:43:04 -0700 | [diff] [blame] | 15 | #include "media/midi/midi_export.h" |
yukawa@chromium.org | 09ef89a | 2013-11-29 06:27:41 +0000 | [diff] [blame] | 16 | |
toyoshim | e147c5e | 2015-05-07 21:58:31 -0700 | [diff] [blame] | 17 | namespace midi { |
yukawa@chromium.org | 09ef89a | 2013-11-29 06:27:41 +0000 | [diff] [blame] | 18 | |
| 19 | // A simple message splitter for possibly unsafe/corrupted MIDI data stream. |
| 20 | // This class allows you to: |
| 21 | // - maintain fragmented MIDI message. |
| 22 | // - skip any invalid data sequence. |
| 23 | // - reorder MIDI messages so that "System Real Time Message", which can be |
| 24 | // inserted at any point of the byte stream, is placed at the boundary of |
| 25 | // complete MIDI messages. |
| 26 | // - (Optional) reconstruct complete MIDI messages from data stream where |
| 27 | // MIDI status byte is abbreviated (a.k.a. "running status"). |
| 28 | // |
| 29 | // Example (pseudo message loop): |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 30 | // MidiMessageQueue queue(true); // true to support "running status" |
yukawa@chromium.org | 09ef89a | 2013-11-29 06:27:41 +0000 | [diff] [blame] | 31 | // while (true) { |
| 32 | // if (is_incoming_midi_data_available()) { |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 33 | // std::vector<uint8_t> incoming_data; |
yukawa@chromium.org | 09ef89a | 2013-11-29 06:27:41 +0000 | [diff] [blame] | 34 | // read_incoming_midi_data(&incoming_data) |
| 35 | // queue.Add(incoming_data); |
| 36 | // } |
| 37 | // while (true) { |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 38 | // std::vector<uint8_t> next_message; |
yukawa@chromium.org | 09ef89a | 2013-11-29 06:27:41 +0000 | [diff] [blame] | 39 | // queue.Get(&next_message); |
| 40 | // if (!next_message.empty()) |
| 41 | // dispatch(next_message); |
| 42 | // } |
| 43 | // } |
brettw | 49ff017 | 2015-05-05 12:43:04 -0700 | [diff] [blame] | 44 | class MIDI_EXPORT MidiMessageQueue { |
yukawa@chromium.org | 09ef89a | 2013-11-29 06:27:41 +0000 | [diff] [blame] | 45 | public: |
| 46 | // Initializes the queue. Set true to |allow_running_status| to enable |
| 47 | // "MIDI running status" reconstruction. |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 48 | explicit MidiMessageQueue(bool allow_running_status); |
Peter Boström | 5363403 | 2021-09-22 20:24:34 +0000 | [diff] [blame] | 49 | |
| 50 | MidiMessageQueue(const MidiMessageQueue&) = delete; |
| 51 | MidiMessageQueue& operator=(const MidiMessageQueue&) = delete; |
| 52 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 53 | ~MidiMessageQueue(); |
yukawa@chromium.org | 09ef89a | 2013-11-29 06:27:41 +0000 | [diff] [blame] | 54 | |
| 55 | // Enqueues |data| to the internal buffer. |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 56 | void Add(const std::vector<uint8_t>& data); |
| 57 | void Add(const uint8_t* data, size_t length); |
yukawa@chromium.org | 09ef89a | 2013-11-29 06:27:41 +0000 | [diff] [blame] | 58 | |
| 59 | // Fills the next complete MIDI message into |message|. If |message| is |
| 60 | // not empty, the data sequence falls into one of the following types of |
| 61 | // MIDI message. |
| 62 | // - Single "Channel Voice Message" (w/o "System Real Time Messages") |
| 63 | // - Single "Channel Mode Message" (w/o "System Real Time Messages") |
| 64 | // - Single "System Exclusive Message" (w/o "System Real Time Messages") |
| 65 | // - Single "System Common Message" (w/o "System Real Time Messages") |
| 66 | // - Single "System Real Time message" |
| 67 | // |message| is empty if there is no complete MIDI message any more. |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 68 | void Get(std::vector<uint8_t>* message); |
yukawa@chromium.org | 09ef89a | 2013-11-29 06:27:41 +0000 | [diff] [blame] | 69 | |
| 70 | private: |
Prashant Malani | f55fb1d | 2019-08-08 17:00:08 +0000 | [diff] [blame] | 71 | base::circular_deque<uint8_t> queue_; |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 72 | std::vector<uint8_t> next_message_; |
yukawa@chromium.org | 09ef89a | 2013-11-29 06:27:41 +0000 | [diff] [blame] | 73 | const bool allow_running_status_; |
yukawa@chromium.org | 09ef89a | 2013-11-29 06:27:41 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
toyoshim | e147c5e | 2015-05-07 21:58:31 -0700 | [diff] [blame] | 76 | } // namespace midi |
yukawa@chromium.org | 09ef89a | 2013-11-29 06:27:41 +0000 | [diff] [blame] | 77 | |
| 78 | #endif // MEDIA_MIDI_MIDI_MESSAGE_QUEUE_H_ |