Jamoma API  0.6.0.a19
MIDIDestination.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup modularMIDI
4  *
5  * @brief create an virtual MIDI destination
6  *
7  * @details MidiDestination receives messages from any external devices @n
8  * It is a wrapper around several plateform-specific library. @n
9  *
10  * @author Theo Delahogue
11  *
12  * @copyright © 2014, GMEA (http://www.gmea.net) @n
13  * This code is licensed under the terms of the "New BSD License" @n
14  * http://creativecommons.org/licenses/BSD/
15  */
16 
17 #include "MIDIDestination.h"
18 
19 #if !defined(TT_PLATFORM_MAC)
20 MIDIDestination::MIDIDestination(MIDIPtr protocol, TTSymbol& application, TTPtr client) :
21 mProtocol(protocol),
22 mClient(client),
23 mApplication(application)
24 {
25  ;
26 }
27 #else
28 MIDIDestination::MIDIDestination(MIDIPtr protocol, TTSymbol& application, MIDIClientRef client) :
29 mProtocol(protocol),
30 mClient(client),
31 mApplication(application)
32 {
33  ;
34 }
35 #endif
36 
37 MIDIDestination::~MIDIDestination()
38 {
39 #if !defined(TT_PLATFORM_MAC)
40  ;// TODO
41 #else
42  MIDIEndpointDispose(mDestination);
43 #endif
44 }
45 
46 TTErr MIDIDestination::setName(TTSymbol& newName)
47 {
48  if (newName != mName) {
49 
50  mName = newName;
51 
52  setRunning(NO);
53 
54  // release the former destination and then instantiate a new one destination
55 #if !defined(TT_PLATFORM_MAC)
56  ;// TODO
57 #else
58  MIDIEndpointDispose(mDestination);
59 
60  // determine the default system character encoding
61  CFStringEncoding defaultEncoding = CFStringGetSystemEncoding();
62 
63  // create an input destination
64  CFStringRef s_name = CFStringCreateWithCString(kCFAllocatorDefault, mName.c_str(), defaultEncoding);
65  OSStatus status = MIDIDestinationCreate(mClient, s_name, &MIDIDestinationPacketReceivedCallback, TTPtr(this), &mDestination);
66  if (status != noErr) {
67  TTLogError("Could not create destination : %d\n", (int)status);
68  return kTTErrGeneric;
69  }
70 #endif
71  }
72 
73  return kTTErrNone;
74 }
75 
76 TTErr MIDIDestination::setRunning(TTBoolean running)
77 {
78  if (running != mRunning) {
79 
80  mRunning = running;
81  }
82 
83  return kTTErrNone;
84 }
85 
86 #if !defined(TT_PLATFORM_MAC)
87 // TODO
88 #else
89 void MIDIDestinationPacketReceivedCallback(const MIDIPacketList *data, void *baton, void *connectionInfo)
90 {
91  MIDIPacket *packet = (MIDIPacket *) &data->packet[0];
92  MIDIDestination *self = (MIDIDestination *)baton;
93  MIDIParserFrom parser;
94 
95  if (!self->mRunning)
96  return;
97 
98  for (TTUInt32 packetIndex = 0; packetIndex < data->numPackets; packetIndex++) {
99 
100  TTUInt8 statusByte;
101  TTUInt8 dataByte1;
102  TTUInt8 dataByte2;
103 
104  switch (packet->length) {
105  case 1:
106  statusByte = packet->data[0];
107  dataByte1 = 0;
108  dataByte2 = 0;
109  break;
110  case 2:
111  statusByte = packet->data[0];
112  dataByte1 = packet->data[1];
113  dataByte2 = 0;
114  break;
115  case 3:
116  statusByte = packet->data[0];
117  dataByte1 = packet->data[1];
118  dataByte2 = packet->data[2];
119  break;
120  default:
121  // skip packets that are too large
122  continue;
123  }
124 
125  // if the parsing is done : pass address and value
126  if (parser.parse(statusByte, dataByte1, dataByte2))
127  self->mProtocol->receivedMessage(self->mApplication, parser.address, parser.value);
128 
129  packet = MIDIPacketNext(packet);
130  }
131 }
132 #endif
bool TTBoolean
Boolean flag, same as Boolean on the Mac.
Definition: TTBase.h:167
void * TTPtr
A generic pointer.
Definition: TTBase.h:248
create an virtual MIDI destination
The TTSymbol class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbol.h:26
void TTFOUNDATION_EXPORT TTLogError(TTImmutableCString message,...)
Platform and host independent method for posting errors.
Definition: TTBase.cpp:572
Something went wrong, but what exactly is not known. Typically used for context-specific problems...
Definition: TTBase.h:344
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
std::uint32_t TTUInt32
32 bit unsigned integer
Definition: TTBase.h:178
No Error.
Definition: TTBase.h:343
unsigned char TTUInt8
8 bit unsigned integer (char)
Definition: TTBase.h:174