| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/midi/midi_manager_mac.h" | 5 #include "media/midi/midi_manager_mac.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/sys_string_conversions.h" | 12 #include "base/strings/sys_string_conversions.h" |
| 13 | 13 |
| 14 #include <CoreAudio/HostTime.h> | 14 #include <CoreAudio/HostTime.h> |
| 15 | 15 |
| 16 using base::IntToString; | 16 using base::IntToString; |
| 17 using base::SysCFStringRefToUTF8; | 17 using base::SysCFStringRefToUTF8; |
| 18 using std::string; | 18 using std::string; |
| 19 | 19 |
| 20 // NB: System MIDI types are pointer types in 32-bit and integer types in | 20 // NB: System MIDI types are pointer types in 32-bit and integer types in |
| 21 // 64-bit. Therefore, the initialization is the simplest one that satisfies both | 21 // 64-bit. Therefore, the initialization is the simplest one that satisfies both |
| 22 // (if possible). | 22 // (if possible). |
| 23 | 23 |
| 24 namespace media { | 24 namespace media { |
| 25 | 25 |
| 26 namespace { |
| 27 |
| 28 MidiPortInfo GetPortInfoFromEndpoint( |
| 29 MIDIEndpointRef endpoint) { |
| 30 SInt32 id_number = 0; |
| 31 MIDIObjectGetIntegerProperty(endpoint, kMIDIPropertyUniqueID, &id_number); |
| 32 string id = IntToString(id_number); |
| 33 |
| 34 string manufacturer; |
| 35 CFStringRef manufacturer_ref = NULL; |
| 36 OSStatus result = MIDIObjectGetStringProperty( |
| 37 endpoint, kMIDIPropertyManufacturer, &manufacturer_ref); |
| 38 if (result == noErr) { |
| 39 manufacturer = SysCFStringRefToUTF8(manufacturer_ref); |
| 40 } else { |
| 41 // kMIDIPropertyManufacturer is not supported in IAC driver providing |
| 42 // endpoints, and the result will be kMIDIUnknownProperty (-10835). |
| 43 DLOG(WARNING) << "Failed to get kMIDIPropertyManufacturer with status " |
| 44 << result; |
| 45 } |
| 46 |
| 47 string name; |
| 48 CFStringRef name_ref = NULL; |
| 49 result = MIDIObjectGetStringProperty(endpoint, kMIDIPropertyName, &name_ref); |
| 50 if (result == noErr) |
| 51 name = SysCFStringRefToUTF8(name_ref); |
| 52 else |
| 53 DLOG(WARNING) << "Failed to get kMIDIPropertyName with status " << result; |
| 54 |
| 55 string version; |
| 56 SInt32 version_number = 0; |
| 57 result = MIDIObjectGetIntegerProperty( |
| 58 endpoint, kMIDIPropertyDriverVersion, &version_number); |
| 59 if (result == noErr) { |
| 60 version = IntToString(version_number); |
| 61 } else { |
| 62 // kMIDIPropertyDriverVersion is not supported in IAC driver providing |
| 63 // endpoints, and the result will be kMIDIUnknownProperty (-10835). |
| 64 DLOG(WARNING) << "Failed to get kMIDIPropertyDriverVersion with status " |
| 65 << result; |
| 66 } |
| 67 |
| 68 return MidiPortInfo(id, manufacturer, name, version); |
| 69 } |
| 70 |
| 71 double MIDITimeStampToSeconds(MIDITimeStamp timestamp) { |
| 72 UInt64 nanoseconds = AudioConvertHostTimeToNanos(timestamp); |
| 73 return static_cast<double>(nanoseconds) / 1.0e9; |
| 74 } |
| 75 |
| 76 MIDITimeStamp SecondsToMIDITimeStamp(double seconds) { |
| 77 UInt64 nanos = UInt64(seconds * 1.0e9); |
| 78 return AudioConvertNanosToHostTime(nanos); |
| 79 } |
| 80 |
| 81 } // namespace |
| 82 |
| 26 MidiManager* MidiManager::Create() { | 83 MidiManager* MidiManager::Create() { |
| 27 return new MidiManagerMac(); | 84 return new MidiManagerMac(); |
| 28 } | 85 } |
| 29 | 86 |
| 30 MidiManagerMac::MidiManagerMac() | 87 MidiManagerMac::MidiManagerMac() |
| 31 : midi_client_(0), | 88 : midi_client_(0), |
| 32 coremidi_input_(0), | 89 coremidi_input_(0), |
| 33 coremidi_output_(0), | 90 coremidi_output_(0), |
| 34 packet_list_(NULL), | 91 packet_list_(NULL), |
| 35 midi_packet_(NULL), | 92 midi_packet_(NULL), |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 MIDIEndpointRef destination = destinations_[port_index]; | 241 MIDIEndpointRef destination = destinations_[port_index]; |
| 185 | 242 |
| 186 MIDISend(coremidi_output_, destination, packet_list_); | 243 MIDISend(coremidi_output_, destination, packet_list_); |
| 187 | 244 |
| 188 // Re-initialize for next time. | 245 // Re-initialize for next time. |
| 189 midi_packet_ = MIDIPacketListInit(packet_list_); | 246 midi_packet_ = MIDIPacketListInit(packet_list_); |
| 190 | 247 |
| 191 client->AccumulateMidiBytesSent(data.size()); | 248 client->AccumulateMidiBytesSent(data.size()); |
| 192 } | 249 } |
| 193 | 250 |
| 194 // static | |
| 195 MidiPortInfo MidiManagerMac::GetPortInfoFromEndpoint( | |
| 196 MIDIEndpointRef endpoint) { | |
| 197 SInt32 id_number = 0; | |
| 198 MIDIObjectGetIntegerProperty(endpoint, kMIDIPropertyUniqueID, &id_number); | |
| 199 string id = IntToString(id_number); | |
| 200 | |
| 201 string manufacturer; | |
| 202 CFStringRef manufacturer_ref = NULL; | |
| 203 OSStatus result = MIDIObjectGetStringProperty( | |
| 204 endpoint, kMIDIPropertyManufacturer, &manufacturer_ref); | |
| 205 if (result == noErr) { | |
| 206 manufacturer = SysCFStringRefToUTF8(manufacturer_ref); | |
| 207 } else { | |
| 208 // kMIDIPropertyManufacturer is not supported in IAC driver providing | |
| 209 // endpoints, and the result will be kMIDIUnknownProperty (-10835). | |
| 210 DLOG(WARNING) << "Failed to get kMIDIPropertyManufacturer with status " | |
| 211 << result; | |
| 212 } | |
| 213 | |
| 214 string name; | |
| 215 CFStringRef name_ref = NULL; | |
| 216 result = MIDIObjectGetStringProperty(endpoint, kMIDIPropertyName, &name_ref); | |
| 217 if (result == noErr) | |
| 218 name = SysCFStringRefToUTF8(name_ref); | |
| 219 else | |
| 220 DLOG(WARNING) << "Failed to get kMIDIPropertyName with status " << result; | |
| 221 | |
| 222 string version; | |
| 223 SInt32 version_number = 0; | |
| 224 result = MIDIObjectGetIntegerProperty( | |
| 225 endpoint, kMIDIPropertyDriverVersion, &version_number); | |
| 226 if (result == noErr) { | |
| 227 version = IntToString(version_number); | |
| 228 } else { | |
| 229 // kMIDIPropertyDriverVersion is not supported in IAC driver providing | |
| 230 // endpoints, and the result will be kMIDIUnknownProperty (-10835). | |
| 231 DLOG(WARNING) << "Failed to get kMIDIPropertyDriverVersion with status " | |
| 232 << result; | |
| 233 } | |
| 234 | |
| 235 return MidiPortInfo(id, manufacturer, name, version); | |
| 236 } | |
| 237 | |
| 238 // static | |
| 239 double MidiManagerMac::MIDITimeStampToSeconds(MIDITimeStamp timestamp) { | |
| 240 UInt64 nanoseconds = AudioConvertHostTimeToNanos(timestamp); | |
| 241 return static_cast<double>(nanoseconds) / 1.0e9; | |
| 242 } | |
| 243 | |
| 244 // static | |
| 245 MIDITimeStamp MidiManagerMac::SecondsToMIDITimeStamp(double seconds) { | |
| 246 UInt64 nanos = UInt64(seconds * 1.0e9); | |
| 247 return AudioConvertNanosToHostTime(nanos); | |
| 248 } | |
| 249 | |
| 250 } // namespace media | 251 } // namespace media |
| OLD | NEW |