1 /*
2
3 ShortMsg.cpp
4
5 Implementation for the CShortMsg class
6
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA
22
23 Contact: Leslie Sanford (jabberdabber@hotmail.com)
24
25 Last modified: 08/19/2003
26
27 */
28
29
30 //---------------------------------------------------------------------
31 // Dependencies
32 //---------------------------------------------------------------------
33
34
35 #include "midi.h"
36 #include "ShortMsg.h"
37 #include "MIDIInDevice.h"
38
39
40 // Using declaration
41 using midi::CShortMsg;
42
43
44 //---------------------------------------------------------------------
45 // CShortMsg implementation
46 //---------------------------------------------------------------------
47
48
49 // Constructor
CShortMsg(DWORD TimeStamp)50 CShortMsg::CShortMsg(DWORD TimeStamp) :
51 m_Msg(0),
52 m_MsgNoStatus(0)
53 {
54 SetTimeStamp(TimeStamp);
55 }
56
57
58 // Constructor
CShortMsg(DWORD Msg,DWORD TimeStamp)59 CShortMsg::CShortMsg(DWORD Msg, DWORD TimeStamp) :
60 m_Msg(Msg)
61 {
62 unsigned char DataByte1 = GetData1();
63 unsigned char DataByte2 = GetData2();
64
65 m_MsgNoStatus = PackShortMsg(DataByte1, DataByte2);
66
67 SetTimeStamp(TimeStamp);
68 }
69
70
71 // Constructor
CShortMsg(unsigned char Status,unsigned char Data1,unsigned char Data2,DWORD TimeStamp)72 CShortMsg::CShortMsg(unsigned char Status, unsigned char Data1,
73 unsigned char Data2, DWORD TimeStamp)
74 {
75 SetMsg(Status, Data1, Data2);
76 m_MsgNoStatus = PackShortMsg(Data1, Data2);
77
78 SetTimeStamp(TimeStamp);
79 }
80
81
82 // Constructor
CShortMsg(unsigned char Command,unsigned char Channel,unsigned char Data1,unsigned char Data2,DWORD TimeStamp)83 CShortMsg::CShortMsg(unsigned char Command, unsigned char Channel,
84 unsigned char Data1, unsigned char Data2,
85 DWORD TimeStamp)
86 {
87 SetMsg(Command, Channel, Data1, Data2);
88 m_MsgNoStatus = PackShortMsg(Data1, Data2);
89
90 SetTimeStamp(TimeStamp);
91 }
92
93
94 // Gets message
GetMsg() const95 const char *CShortMsg::GetMsg() const
96 {
97 return reinterpret_cast<const char *>(&m_Msg);
98 }
99
100
101 // Gets status byte
GetStatus() const102 unsigned char CShortMsg::GetStatus() const
103 {
104 unsigned char Status;
105 unsigned char Dummy;
106
107 UnpackShortMsg(m_Msg, Status, Dummy, Dummy);
108
109 return Status;
110 }
111
112
113 // Gets MIDI channel
GetChannel() const114 unsigned char CShortMsg::GetChannel() const
115 {
116 unsigned char Channel;
117 unsigned char Dummy;
118
119 UnpackShortMsg(m_Msg, Dummy, Channel, Dummy, Dummy);
120
121 return Channel;
122 }
123
124
125 // Gets command code
GetCommand() const126 unsigned char CShortMsg::GetCommand() const
127 {
128 unsigned char Command;
129 unsigned char Dummy;
130
131 UnpackShortMsg(m_Msg, Command, Dummy, Dummy, Dummy);
132
133 return Command;
134 }
135
136
137 // Gets data byte 1
GetData1() const138 unsigned char CShortMsg::GetData1() const
139 {
140 unsigned char Data1;
141 unsigned char Dummy;
142
143 UnpackShortMsg(m_Msg, Dummy, Dummy, Data1, Dummy);
144
145 return Data1;
146 }
147
148
149 // Gets data byte 2
GetData2() const150 unsigned char CShortMsg::GetData2() const
151 {
152 unsigned char Data2;
153 unsigned char Dummy;
154
155 UnpackShortMsg(m_Msg, Dummy, Dummy, Dummy, Data2);
156
157 return Data2;
158 }
159
160
161 // Sets message
SetMsg(unsigned char Status,unsigned char Data1,unsigned char Data2)162 void CShortMsg::SetMsg(unsigned char Status, unsigned char Data1,
163 unsigned char Data2)
164 {
165 m_Msg = PackShortMsg(Status, Data1, Data2);
166 m_MsgNoStatus = PackShortMsg(Data1, Data2);
167 }
168
169
170 // Sets message
SetMsg(unsigned char Command,unsigned char Channel,unsigned char Data1,unsigned char Data2)171 void CShortMsg::SetMsg(unsigned char Command, unsigned char Channel,
172 unsigned char Data1, unsigned char Data2)
173 {
174 m_Msg = PackShortMsg(Command, Channel, Data1, Data2);
175 m_MsgNoStatus = PackShortMsg(Data1, Data2);
176 }
177
178
179 // Packs data into short message without status byte
PackShortMsg(unsigned char DataByte1,unsigned char DataByte2)180 DWORD CShortMsg::PackShortMsg(unsigned char DataByte1,
181 unsigned char DataByte2)
182 {
183 DWORD Msg = DataByte1;
184 Msg |= DataByte2 << midi::SHORT_MSG_SHIFT;
185
186 return Msg;
187 }
188
189
190 // Packs data into short message with status byte
PackShortMsg(unsigned char Status,unsigned char DataByte1,unsigned char DataByte2)191 DWORD CShortMsg::PackShortMsg(unsigned char Status,
192 unsigned char DataByte1,
193 unsigned char DataByte2)
194 {
195 DWORD Msg = Status;
196 Msg |= DataByte1 << midi::SHORT_MSG_SHIFT;
197 Msg |= DataByte2 << midi::SHORT_MSG_SHIFT * 2;
198
199 return Msg;
200 }
201
202
203 // Packs data into short channel message
PackShortMsg(unsigned char Command,unsigned char Channel,unsigned char DataByte1,unsigned char DataByte2)204 DWORD CShortMsg::PackShortMsg(unsigned char Command,
205 unsigned char Channel,
206 unsigned char DataByte1,
207 unsigned char DataByte2)
208 {
209 DWORD Msg = Command | Channel;
210 Msg |= DataByte1 << midi::SHORT_MSG_SHIFT;
211 Msg |= DataByte2 << midi::SHORT_MSG_SHIFT * 2;
212
213 return Msg;
214 }
215
216
217 // Unpacks short message
UnpackShortMsg(DWORD Msg,unsigned char & Status,unsigned char & DataByte1,unsigned char & DataByte2)218 void CShortMsg::UnpackShortMsg(DWORD Msg, unsigned char &Status,
219 unsigned char &DataByte1,
220 unsigned char &DataByte2)
221 {
222 Status = static_cast<unsigned char>(Msg);
223 DataByte1 = static_cast<unsigned char>
224 (Msg >> midi::SHORT_MSG_SHIFT);
225 DataByte2 = static_cast<unsigned char>
226 (Msg >> midi::SHORT_MSG_SHIFT * 2);
227 }
228
229
230 // Unpacks short channel message
UnpackShortMsg(DWORD Msg,unsigned char & Command,unsigned char & Channel,unsigned char & DataByte1,unsigned char & DataByte2)231 void CShortMsg::UnpackShortMsg(DWORD Msg, unsigned char &Command,
232 unsigned char &Channel,
233 unsigned char &DataByte1,
234 unsigned char &DataByte2)
235 {
236 Command = static_cast<unsigned char>(Msg & ~midi::SHORT_MSG_MASK);
237 Channel = static_cast<unsigned char>(Msg & midi::SHORT_MSG_MASK);
238 DataByte1 = static_cast<unsigned char>
239 (Msg >> midi::SHORT_MSG_SHIFT);
240 DataByte2 = static_cast<unsigned char>
241 (Msg >> midi::SHORT_MSG_SHIFT * 2);
242 }
243