OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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_UTIL_H_ | |
6 #define MEDIA_MIDI_MIDI_MESSAGE_UTIL_H_ | |
7 | |
8 #include <deque> | |
9 #include <vector> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "media/base/media_export.h" | |
13 | |
14 namespace media { | |
15 | |
16 // A simple message splitter for possibly unsafe MIDI data stream. This class | |
17 // allows you to | |
18 // - maintain fragmented MIDI message. | |
19 // - skip any invalid data sequence. | |
20 // - reorder MIDI messages so that "System Real Time Message", which can be | |
21 // inserted at any point of the byte stream, is placed at the boundary of | |
22 // complete MIDI messages. | |
23 // - (Optional) reconstruct complete MIDI messages from data stream that is | |
24 // compressed with "running status". | |
25 // | |
26 // Example (pseudo message loop): | |
27 // MIDIMessageQueue queue(true); // true to support "running status" | |
28 // while (true) { | |
29 // if (is_incomming_midi_data_available()) { | |
30 // std::vector<uint8> incomming_data; | |
31 // read_incomming_midi_data(&incomming_data) | |
32 // queue.Add(incomming_data); | |
33 // } | |
34 // while (true) { | |
35 // std::vector<uint8> next_message; | |
36 // queue.Get(&next_message); | |
37 // if (!next_message.empty()) | |
38 // dispatch(next_message); | |
39 // } | |
40 // } | |
41 class MEDIA_EXPORT MIDIMessageQueue { | |
42 public: | |
43 // Initializes the queue. Set true to |allow_running_status| to enable | |
44 // "MIDI running status" reconstruction. | |
45 explicit MIDIMessageQueue(bool allow_running_status); | |
46 ~MIDIMessageQueue(); | |
47 | |
48 // Enqueues |data| to the internal buffer. | |
49 void Add(const std::vector<uint8>& data); | |
50 // Enqueues |data| to the internal buffer. |data| must be valid until | |
51 // this method returns. | |
52 void Add(const uint8* data, size_t length); | |
53 | |
54 // Fills the next complete MIDI message into |message|. If |message| is | |
55 // not empty, the data sequence falls into one of the following types of | |
56 // MIDI message. | |
57 // - Single "Channel Voice Message" (w/o "System Real Time Messages") | |
58 // - Single "Channel Mode Message" (w/o "System Real Time Messages") | |
59 // - Single "System Exclusive Message" (w/o "System Real Time Messages") | |
60 // - Single "System Common Message" (w/o "System Real Time Messages") | |
61 // - Single "System Real Time message" | |
62 // |message| is empty if there is no complete MIDI message any more. | |
63 void Get(std::vector<uint8>* message); | |
64 | |
65 private: | |
66 std::deque<uint8> queue_; | |
67 std::vector<uint8> next_message_; | |
68 const bool allow_running_status_; | |
69 DISALLOW_COPY_AND_ASSIGN(MIDIMessageQueue); | |
70 }; | |
71 | |
72 // Returns true if |data| is valid for MIDIOutput.send API. Note that: | |
73 // - Fragmentation is not allowed except for real time message. | |
74 // - Running status is not allowed. | |
75 MEDIA_EXPORT bool IsValidMIDIMessagesForWebMIDI( | |
Takashi Toyoshima
2013/11/20 10:04:58
I prefer IsValidWebMIDIMessage or IsCompleteMIDIMe
yukawa
2013/11/20 11:06:08
Yeah, agreed. I chose IsValidWebMIDIMessage.
| |
76 const std::vector<uint8>& data); | |
77 | |
78 } // namespace media | |
79 | |
80 #endif // MEDIA_MIDI_MIDI_MESSAGE_UTIL_H_ | |
OLD | NEW |