OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 MIDIInput::MIDIInput(ScriptExecutionContext* context, const String& id, const St
ring& manufacturer, const String& name, const String& version) | 42 MIDIInput::MIDIInput(ScriptExecutionContext* context, const String& id, const St
ring& manufacturer, const String& name, const String& version) |
43 : MIDIPort(context, id, manufacturer, name, MIDIPortTypeInput, version) | 43 : MIDIPort(context, id, manufacturer, name, MIDIPortTypeInput, version) |
44 { | 44 { |
45 ScriptWrappable::init(this); | 45 ScriptWrappable::init(this); |
46 } | 46 } |
47 | 47 |
48 void MIDIInput::didReceiveMIDIData(unsigned portIndex, const unsigned char* data
, size_t length, double timeStamp) | 48 void MIDIInput::didReceiveMIDIData(unsigned portIndex, const unsigned char* data
, size_t length, double timeStamp) |
49 { | 49 { |
50 ASSERT(isMainThread()); | 50 ASSERT(isMainThread()); |
51 | 51 |
52 RefPtr<Uint8Array> array = Uint8Array::create(length); | 52 // The received MIDI data may contain one or more messages. |
53 array->setRange(data, length, 0); | 53 // The Web MIDI API requires that a separate event be dispatched for each me
ssage, |
| 54 // so we walk through the data and dispatch one at a time. |
| 55 size_t i = 0; |
| 56 while (i < length) { |
| 57 unsigned char status = data[i]; |
| 58 unsigned char strippedStatus = status & 0xf0; |
54 | 59 |
55 dispatchEvent(MIDIMessageEvent::create(timeStamp, array)); | 60 // FIXME: support System Exclusive. |
| 61 if (strippedStatus >= 0xf0) |
| 62 break; |
| 63 |
| 64 // All non System Exclusive messages have a total size of 3 except for P
rogram Change and Channel Pressure. |
| 65 size_t totalMessageSize = (strippedStatus == 0xc0 || strippedStatus == 0
xd0) ? 2 : 3; |
| 66 |
| 67 if (i + totalMessageSize <= length) { |
| 68 RefPtr<Uint8Array> array = Uint8Array::create(totalMessageSize); |
| 69 array->setRange(data + i, totalMessageSize, 0); |
| 70 |
| 71 dispatchEvent(MIDIMessageEvent::create(timeStamp, array)); |
| 72 } |
| 73 |
| 74 i += totalMessageSize; |
| 75 } |
56 } | 76 } |
57 | 77 |
58 } // namespace WebCore | 78 } // namespace WebCore |
OLD | NEW |