Last active
May 20, 2018 14:44
-
-
Save ixsiid/d13b2420d35117560528cbdd907addff to your computer and use it in GitHub Desktop.
RtMidiライブラリを使って、MIDIインターフェースを作成する ref: https://qiita.com/ixsiid/items/dfd093732d05260ed174
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "midi.hpp" | |
int main () { | |
MidiInterface * p = new MidiInterface("foobar"); | |
p->connect("Midi Through", MidiDirection::OUT); | |
while (true) std::cin.getc(); | |
exit(0); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> aconnect -l | |
client 0: 'System' [type=kernel] | |
0 'Timer ' | |
1 'Announce ' | |
client 14: 'Midi Through' [type=kernel] | |
0 'Midi Through Port-0' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ aconnect -l | |
client 0: 'System' [type=kernel] | |
0 'Timer ' | |
1 'Announce ' | |
client 14: 'Midi Through' [type=kernel] | |
0 'Midi Through Port-0' | |
$ ./create_midi_interface.o & | |
$ aconnect -l | |
client 0: 'System' [type=kernel] | |
0 'Timer ' | |
1 'Announce ' | |
client 14: 'Midi Through' [type=kernel] | |
0 'Midi Through Port-0' | |
Connected From: 128:0[real:0] | |
client 128: 'foobar Midi Output Client' [type=user,pid=1528] | |
0 'foobar Midi Output Port' | |
Connecting To: 14:0[real:0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "midi.hpp" | |
MidiInterface::MidiInterface(const char * interfaceName) { | |
name = interfaceName; | |
} | |
RtMidi * MidiInterface::connect(std::string target, MidiDirection direction) { | |
char clientName[256] = ""; | |
sprintf(clientName, "%s Midi %s Client", name, direction == MidiDirection::IN ? "Input" : "Output"); | |
RtMidi * midi = direction == MidiDirection::IN ? | |
(RtMidi *)new RtMidiIn (RtMidi::UNSPECIFIED, clientName) : | |
(RtMidi *)new RtMidiOut(RtMidi::UNSPECIFIED, clientName); | |
unsigned int portCount = midi->getPortCount(); | |
for (unsigned int i=0; i<portCount; i++) { | |
std::string portName = midi->getPortName(i).c_str(); | |
std::string::size_type index = portName.find(target, 0); | |
if (index != std::string::npos) { | |
char portName[256] = ""; | |
sprintf(portName, "%s Midi %s Port", name, direction == MidiDirection::IN ? "Input" : "Output"); | |
midi->openPort(i, portName); | |
return midi; | |
} | |
} | |
return NULL; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "RtMidi.h" | |
enum struct MidiDirection { | |
IN, | |
OUT, | |
}; | |
class MidiInterface { | |
private: | |
const char * name; | |
public: | |
MidiInterface(const char * interfaceName); | |
RtMidi * connect(std::string target, MidiDirection direction); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment