Jamoma API  0.6.0.a19
MIDIOutput.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup modularMIDI
4  *
5  * @brief bind to an external device destination
6  *
7  * @details MidiOutput sends messages to one external device. @n
8  * It is a wrapper around the PortMidi 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 "MIDIOutput.h"
18 
19 MIDIOutput::MIDIOutput(MIDIPtr protocol, TTSymbol& application) :
20 mProtocol(protocol),
21 mStream(NULL),
22 mRunning(NO),
23 mApplication(application)
24 {
25  ;
26 }
27 
28 MIDIOutput::~MIDIOutput()
29 {
30  mRunning = NO;
31 
32  if (mStream) {
33  Pm_Close(mStream);
34  mStream = NULL;
35  }
36 }
37 
38 TTErr MIDIOutput::setName(TTSymbol& newName)
39 {
40  const PmDeviceInfo* deviceInfo;
41  int i, deviceCount;
42  PmError err = pmNoError;
43 
44  if (newName != mName) {
45 
46  // check there is an output with this name
47  deviceCount = Pm_CountDevices();
48  for (i = 0; i < deviceCount; i++) {
49 
50  deviceInfo = Pm_GetDeviceInfo(i);
51 
52  if (deviceInfo->output && newName == TTSymbol(deviceInfo->name))
53  break;
54  }
55 
56  if (i == deviceCount)
57  return kTTErrGeneric;
58 
59  mName = newName;
60 
61  setRunning(NO);
62 
63  if (mStream) {
64  Pm_Close(mStream);
65  mStream = NULL;
66  }
67 
68  err = Pm_OpenOutput(&mStream, i, NULL, kMidiBufferSize, NULL, NULL, 0);
69  if (err) {
70 
71  TTLogError("MIDIOutput::setName : can't open the %s device\n", mName.c_str());
72  return kTTErrGeneric;
73  }
74  }
75 
76  return kTTErrNone;
77 }
78 
79 TTErr MIDIOutput::setRunning(TTBoolean running)
80 {
81  if (running != mRunning) {
82 
83  mRunning = running;
84  }
85 
86  return kTTErrNone;
87 }
88 
89 TTErr MIDIOutput::sendMessage(TTAddress& address, const TTValue& value)
90 {
91  if (mRunning) {
92 
93  MIDIParserTo parser;
94  TTBoolean done;
95 
96  // until the parsing ends
97  do {
98  done = parser.parse(address, value);
99  int message = Pm_Message(parser.statusByte, parser.dataByte1, parser.dataByte2);
100  Pm_WriteShort(mStream, 0, message);
101  }
102  while (!done);
103 
104  return kTTErrNone;
105  }
106 
107  return kTTErrGeneric;
108 }
bool TTBoolean
Boolean flag, same as Boolean on the Mac.
Definition: TTBase.h:167
The TTAddress class is used to represent a string and efficiently pass and compare that string...
Definition: TTAddress.h:29
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
No Error.
Definition: TTBase.h:343
bind to an external device destination
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34