aboutsummaryrefslogtreecommitdiff
path: root/core/qmpmidioutrtmidi.cpp
blob: 1b897df5c8763845ad40cc5530ee62213b1a8c81 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <cstdio>
#include <cstring>
#include <vector>
#include RT_MIDI_H
#include "qmpmidioutrtmidi.hpp"
qmpMidiOutRtMidi::qmpMidiOutRtMidi(unsigned _portid)
{
	portid=_portid;
	outport=NULL;
}
qmpMidiOutRtMidi::~qmpMidiOutRtMidi()
{
	if(!outport)return;
	if(outport->isPortOpen())outport->closePort();
	delete outport;outport=NULL;
}
void qmpMidiOutRtMidi::deviceInit()
{
	try
	{
		outport=new RtMidiOut();
	}
	catch(RtMidiError &e)
	{
		printf("Cannot create RtMidi Output instance: %s\n",e.what());
		outport=NULL;
	}
}
void qmpMidiOutRtMidi::deviceDeinit()
{
	if(!outport||!outport->isPortOpen())return;
	outport->closePort();
}
void qmpMidiOutRtMidi::basicMessage(uint8_t type,uint8_t p1,uint8_t p2)
{
	if(!outport||!outport->isPortOpen())return;
	std::vector<unsigned char>msg;
	msg.push_back(type);
	msg.push_back(p1);
	if(((type&0xF0)!=0xC0)&&((type&0xF0)!=0xD0))
	msg.push_back(p2);
	outport->sendMessage(&msg);
}
void qmpMidiOutRtMidi::extendedMessage(uint8_t length,const char *data)
{
	if(!outport||!outport->isPortOpen())return;
	std::vector<unsigned char>msg(data,data+length);
	outport->sendMessage(&msg);
}
void qmpMidiOutRtMidi::rpnMessage(uint8_t ch,uint16_t type,uint16_t val)
{
	basicMessage(0xB0|ch,0x64,type&0x7F);
	basicMessage(0xB0|ch,0x65,type>>7);
	basicMessage(0xB0|ch,0x06,val>>7);
	basicMessage(0xB0|ch,0x26,val&0x7F);
}
void qmpMidiOutRtMidi::nrpnMessage(uint8_t ch,uint16_t type,uint16_t val)
{
	basicMessage(0xB0|ch,0x62,type&0x7F);
	basicMessage(0xB0|ch,0x63,type>>7);
	basicMessage(0xB0|ch,0x06,val>>7);
	basicMessage(0xB0|ch,0x26,val&0x7F);
}
void qmpMidiOutRtMidi::panic(uint8_t ch)
{
	//maybe all notes off is more close to panic?
	basicMessage(0xE0|ch,0x0,0x40);
	basicMessage(0xB0|ch,123,0);
}
void qmpMidiOutRtMidi::reset(uint8_t ch)
{
	basicMessage(0xB0|ch,121,0);
	basicMessage(0xB0|ch,123,0);
}
void qmpMidiOutRtMidi::onMapped(uint8_t,int)
{
	if(!outport)deviceInit();
	if(outport->isPortOpen())return;
	try
	{
		outport->openPort(portid);
	}
	catch(RtMidiError &e)
	{
		printf("Device initialization failure: %s\n",e.what());
	}

}
void qmpMidiOutRtMidi::onUnmapped(uint8_t ch,int refcnt)
{
	panic(ch);
	if(!refcnt&&outport)outport->closePort();
}

RtMidiOut* qmpRtMidiManager::dummy=NULL;
void qmpRtMidiManager::createDevices()
{
	try{dummy=new RtMidiOut();}
	catch(RtMidiError &e)
	{
		printf("Failed to initialize the dummy device: %s\n",e.what());
		dummy=NULL;return;
	}
	for(unsigned i=0;i<dummy->getPortCount();++i)
	devices.push_back(std::make_pair(new qmpMidiOutRtMidi(i),dummy->getPortName(i)));
}
void qmpRtMidiManager::deleteDevices()
{
	for(size_t i=0;i<devices.size();++i)
		delete devices[i].first;
	devices.clear();
	delete dummy;
}
std::vector<std::pair<qmpMidiOutRtMidi*,std::string>> qmpRtMidiManager::getDevices()
{
	return devices;
}