1 /*
2 * midi/win32/MidiReceiver_win32.cpp
3 *
4 * Copyright 2008 Peter Barth
5 *
6 * This file is part of Milkytracker.
7 *
8 * Milkytracker is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * Milkytracker 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
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with Milkytracker. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23 #include "MidiReceiver_win32.h"
24 #include <iostream>
25 #include "ShortMsg.h"
26 #include "Tracker.h"
27 #include "PPMutex.h"
28 #include "MidiTools.h"
29
30 using midi::CMIDIInDevice;
31 using midi::CMIDIReceiver;
32
MidiReceiver(Tracker & theTracker,PPMutex & mutex)33 MidiReceiver::MidiReceiver(Tracker& theTracker, PPMutex& mutex) :
34 tracker(theTracker),
35 criticalSection(mutex),
36 recordVelocity(false),
37 velocityAmplify(100)
38 {
39 }
40
41 // Function called to receive short messages
ReceiveMsg(DWORD Msg,DWORD TimeStamp)42 void MidiReceiver::ReceiveMsg(DWORD Msg, DWORD TimeStamp)
43 {
44 midi::CShortMsg ShortMsg(Msg, TimeStamp);
45
46 int command = static_cast<int>(ShortMsg.GetCommand());
47
48 if (command >= 128 && command <= 143)
49 {
50 int note = static_cast<int>(ShortMsg.GetData1());
51 // One octave less
52 note -= 12;
53 if (note < 1) note = 1;
54
55 criticalSection.lock();
56 tracker.sendNoteUp(note);
57 criticalSection.unlock();
58 }
59 else if (command >= 144 && command <= 159)
60 {
61 int note = static_cast<int>(ShortMsg.GetData1());
62 // One octave less
63 note -= 12;
64 if (note < 1) note = 1;
65
66 int velocity = static_cast<int>(ShortMsg.GetData2());
67
68 if (velocity == 0)
69 {
70 criticalSection.lock();
71 tracker.sendNoteUp(note);
72 criticalSection.unlock();
73 }
74 else
75 {
76 criticalSection.lock();
77 tracker.sendNoteDown(note, recordVelocity ? vol126to255(velocity-1, velocityAmplify) : -1);
78 criticalSection.unlock();
79 }
80 }
81
82 /*std::cout << "Command: " << static_cast<int>(ShortMsg.GetCommand());
83 std::cout << "\nChannel: " << static_cast<int>(ShortMsg.GetChannel());
84 std::cout << "\nDataByte1: " << static_cast<int>(ShortMsg.GetData1());
85 std::cout << "\nDataByte2: " << static_cast<int>(ShortMsg.GetData2());
86 std::cout << "\nTimeStamp: " << static_cast<int>(ShortMsg.GetTimeStamp());
87 std::cout << "\n\n";*/
88 }
89
90 // Receives long messages
ReceiveMsg(LPSTR Msg,DWORD BytesRecorded,DWORD TimeStamp)91 void MidiReceiver::ReceiveMsg(LPSTR Msg, DWORD BytesRecorded, DWORD TimeStamp)
92 {
93 }
94
95 // Called when an invalid short message is received
OnError(DWORD Msg,DWORD TimeStamp)96 void MidiReceiver::OnError(DWORD Msg, DWORD TimeStamp)
97 {
98 }
99
100 // Called when an invalid long message is received
OnError(LPSTR Msg,DWORD BytesRecorded,DWORD TimeStamp)101 void MidiReceiver::OnError(LPSTR Msg, DWORD BytesRecorded, DWORD TimeStamp)
102 {
103 }
104