Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include "MMSYSTEM.H"
- #define MIDI_IN_DEV 5
- DECLARE_COMPONENT_VERSION(
- "foo_miditransport",
- "0.0.1",
- "Add transport support from external MIDI interfaces such as MIDI controllers, keyboards, ...\n"
- "patrizio.boschi@gmail.com"
- );
- /* Generic function to schedule a function from worker thread to be executed on main thread.
- See Zao post @ http://www.hydrogenaud.io/forums/index.php?s=&showtopic=77922&view=findpost&p=680673 */
- template <typename F>
- void in_main_thread(F f)
- {
- struct in_main : main_thread_callback
- {
- void callback_run() override
- {
- f();
- }
- in_main(F f) : f(f) {}
- F f;
- };
- static_api_ptr_t<main_thread_callback_manager>()->add_callback(new service_impl_t<in_main>(f));
- }
- static unsigned char CC_command, CC_value; // Parameters for executeCC
- /* Handles the CC */
- void executeCC() {
- static_api_ptr_t<playback_control> pc;
- switch (CC_command) {
- case 118:
- if (CC_value == 127) pc->stop();
- break;
- case 119:
- if (CC_value == 127) pc->play_or_pause();
- break;
- case 116:
- if (CC_value == 127) pc->playback_seek_delta(-5);
- break;
- case 117:
- if (CC_value == 127) pc->playback_seek_delta(5);
- break;
- }
- }
- /*
- Check which MIDI command has been received and dispatches the corresponding method to handle it.
- Refer to http://www.midi.org/techspecs/midimessages.php for MIDI protocol description
- */
- void dispatchCommand(DWORD dwParam1) {
- unsigned char status = LOBYTE(dwParam1);
- unsigned char data1 = HIBYTE(dwParam1);
- unsigned char data2 = LOBYTE(HIWORD(dwParam1));
- unsigned char channel = status & 0x0F;
- unsigned char message = (status & 0x70) >> 4;
- std::string messageText;
- switch (message) {
- case 0:
- messageText = "Note Off";
- break;
- case 1:
- messageText = "Note On";
- break;
- case 2:
- messageText = "Aftertouch";
- break;
- case 3:
- messageText = "CC";
- break;
- case 4:
- messageText = "Program Change";
- break;
- case 5:
- messageText = "Channel Aftertouch";
- break;
- case 6:
- messageText = "Pitch Bend";
- break;
- }
- console::formatter() << "MIDI IN - Channel: " << channel + 1 << " - Status: " << message << " (" << messageText.c_str() << ") - Data1: " << data1 << " Data2: " << data2;
- if (message == 3) { // CC command
- CC_command = data1;
- CC_value = data2;
- in_main_thread(executeCC);
- }
- /* TODO
- other commands...
- */
- }
- /* Callback called after every MIDI event */
- void CALLBACK midiCallback(HMIDIIN handle, UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2)
- {
- switch (uMsg)
- {
- case MIM_DATA:
- dispatchCommand(dwParam1);
- break;
- case MIM_OPEN:
- console::formatter() << "MIDI OPEN";
- break;
- case MIM_CLOSE:
- console::formatter() << "MIDI CLOSE";
- break;
- case MIM_ERROR:
- console::formatter() << "MIDI ERROR";
- break;
- case MIM_LONGERROR:
- console::formatter() << "MIDI LONGERROR";
- break;
- case MIM_MOREDATA:
- console::formatter() << "MIDI MOREDATA";
- break;
- }
- }
- class test : public initquit {
- public:
- virtual void on_init()
- {
- unsigned int i, iNumDevs;
- MIDIINCAPS mic;
- MMRESULT res;
- HMIDIIN inHandle;
- /* Get the number of MIDI In devices in this computer */
- iNumDevs = midiInGetNumDevs();
- /* Go through all of those devices, displaying their names */
- for (i = 0; i < iNumDevs; i++)
- {
- /* Get info about the next device */
- if (!midiInGetDevCaps(i, &mic, sizeof(MIDIINCAPS)))
- {
- /* How to print a wstring? */
- std::wstring ws(mic.szPname);
- std::string str(ws.begin(), ws.end());
- console::formatter() << "Device ID #" << i << ": " << str.c_str();
- }
- }
- /* Open MIDI device and register callback */
- res = midiInOpen(&inHandle, MIDI_IN_DEV, (DWORD_PTR)midiCallback, (DWORD_PTR)this, CALLBACK_FUNCTION);
- if (res != MMSYSERR_NOERROR) {
- console::formatter() << "midiInOpen error";
- return;
- }
- /* Start midi input */
- res = midiInStart(inHandle);
- }
- virtual void on_quit()
- {
- }
- };
- initquit_factory_t<test> g_foo;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement