Skip to content

Instantly share code, notes, and snippets.

@RyanScottLewis
Created April 11, 2011 03:25
Show Gist options
  • Select an option

  • Save RyanScottLewis/913020 to your computer and use it in GitHub Desktop.

Select an option

Save RyanScottLewis/913020 to your computer and use it in GitHub Desktop.
MIDI WinMM Extension
#include "windows.h"
#include "mmsystem.h"
#include "ruby.h"
static VALUE rb_mMIDI_devices(VALUE self)
{
VALUE rb_hash_inputDeviceList, rb_hash_outputDeviceList, rb_hash_resultDeviceList;
unsigned long midiInNumDevs, midiOutNumDevs, i;
MIDIOUTCAPS moc;
MIDIINCAPS mic;
/* init the result hashes */
rb_hash_inputDeviceList = rb_hash_new();
rb_hash_outputDeviceList = rb_hash_new();
rb_hash_resultDeviceList = rb_hash_new();
/* Get the number of MIDI In devices in this computer */
midiInNumDevs = midiInGetNumDevs();
midiOutNumDevs = midiOutGetNumDevs();
/* Go through all of those devices, adding them to thei respected hashes */
for (i = 0; i < midiInNumDevs; i++) {
if ( !midiInGetDevCaps( i, &mic, sizeof(MIDIINCAPS) ) ) {
rb_hash_aset(rb_hash_inputDeviceList, INT2NUM(i), rb_str_new2(mic.szPname));
}
}
for (i = 0; i < midiOutNumDevs; i++) {
if ( !midiOutGetDevCaps( i, &moc, sizeof(MIDIOUTCAPS) ) ) {
rb_hash_aset(rb_hash_outputDeviceList, INT2NUM(i), rb_str_new2(moc.szPname));
}
}
/* Add the device hashes to the result hash */
rb_hash_aset(rb_hash_resultDeviceList, rb_str_new2("input"), rb_hash_inputDeviceList);
rb_hash_aset(rb_hash_resultDeviceList, rb_str_new2("output"), rb_hash_outputDeviceList);
return rb_hash_resultDeviceList;
}
static VALUE rb_mMIDI_set_input_device(VALUE self, VALUE value)
{
return rb_iv_set(self, "@input_device", INT2NUM(value));
}
static VALUE rb_mMIDI_get_input_device(VALUE self)
{
return rb_iv_get(self, "@input_device");
}
static VALUE rb_mMIDI_set_output_device(VALUE self, VALUE value)
{
return rb_iv_set(self, "@output_device", INT2NUM(value));
}
static VALUE rb_mMIDI_get_output_device(VALUE self)
{
return rb_iv_get(self, "@output_device");
}
void Init_midi() {
VALUE rb_mMIDI = rb_define_module("MIDI");
rb_iv_set(rb_mMIDI, "@input_device", INT2NUM(0));
rb_iv_set(rb_mMIDI, "@output_device", INT2NUM(0));
rb_define_module_function(rb_mMIDI, "devices", rb_mMIDI_devices, 0);
rb_define_module_function(rb_mMIDI, "set_input_device", rb_mMIDI_set_input_device, 1);
rb_define_module_function(rb_mMIDI, "get_input_device", rb_mMIDI_get_input_device, 0);
rb_define_module_function(rb_mMIDI, "set_output_device", rb_mMIDI_set_output_device, 1);
rb_define_module_function(rb_mMIDI, "get_output_device", rb_mMIDI_set_output_device, 0);
// rb_define_module_function(rb_mMIDI, "input_proc", rb_mMIDI, 0);
// rb_define_module_function(rb_mMIDI, "start_input", rb_mMIDI, 0);
// rb_define_module_function(rb_mMIDI, "stop_input", rb_mMIDI, 0);
}
// #include "windows.h"
// #include "mmsystem.h"
// #include "ruby.h"
// /* Input -------------------------------------------------------------------- */
// static VALUE rb_cInput_devices(VALUE self)
// {
// VALUE rb_hashDeviceList;
// MIDIINCAPS mic;
// unsigned long iNumDevs, i;
// rb_hashDeviceList = rb_hash_new();
// /* 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)))
// {
// rb_hash_aset(rb_hashDeviceList, INT2NUM(i), rb_str_new2(mic.szPname));
// }
// }
// return rb_hashDeviceList;
// }
// static VALUE rb_cInput_proc(VALUE self)
// {
// rb_need_block();
// VALUE proc;
// proc = rb_block_proc();
// rb_iv_set(self, "@proc", proc);
// return proc;
// }
// void CALLBACK MidiInProc(HMIDIIN hMidiIn, UINT wMsg, DWORD_PTR proc, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
// {
// if (proc)
// {
// rb_funcall(proc, rb_intern("call"), wMsg, proc, dwParam1, dwParam2);
// }
// }
// static VALUE rb_cInput_start(VALUE self, VALUE id)
// {
// VALUE proc;
// HMIDIIN handle;
// proc = rb_iv_get(self, "@proc");
// midiInOpen(&handle, FIX2INT(id), (DWORD)MidiInProc, (DWORD)proc, CALLBACK_FUNCTION);
// rb_iv_set(self, "@device_handle", INT2FIX(handle));
// midiInStart(handle);
// return proc;
// }
// static VALUE rb_cInput_stop(VALUE self)
// {
// HMIDIIN handle;
// handle = FIX2INT(rb_iv_get(self, "@device_handle"));
// midiInStop(handle);
// midiInReset(handle);
// midiInClose(handle);
// return Qnil;
// }
// /* Output ------------------------------------------------------------------- */
// static VALUE rb_cOutput_devices(VALUE self)
// {
// VALUE rb_hashDeviceList;
// MIDIOUTCAPS moc;
// unsigned long iNumDevs, i;
// rb_hashDeviceList = rb_hash_new();
// /* Get the number of MIDI In devices in this computer */
// iNumDevs = midiOutGetNumDevs();
// /* Go through all of those devices, displaying their names */
// for (i = 0; i < iNumDevs; i++)
// {
// /* Get info about the next device */
// if (!midiOutGetDevCaps(i, &moc, sizeof(MIDIOUTCAPS)))
// {
// rb_hash_aset(rb_hashDeviceList, INT2NUM(i), rb_str_new2(moc.szPname));
// }
// }
// return rb_hashDeviceList;
// }
// void Init_midi() {
// VALUE rb_mMIDI = rb_define_module("MIDI");
// VALUE rb_cInput = rb_define_class_under(rb_mMIDI, "Input", rb_cObject);
// rb_define_method(rb_cInput, "devices", rb_cInput_devices, 0);
// rb_define_method(rb_cInput, "proc", rb_cInput_proc, 0);
// rb_define_method(rb_cInput, "start", rb_cInput_start, 1);
// rb_define_method(rb_cInput, "stop", rb_cInput_stop, 0);
// VALUE rb_cOutput = rb_define_class_under(rb_mMIDI, "Output", rb_cObject);
// rb_define_method(rb_cOutput, "devices", rb_cOutput_devices, 0);
// }
$LOAD_PATH.unshift(File.dirname(__FILE__))
require "ext/midi"
require "pp"
pp MIDI.devices
pp MIDI.get_input_device
MIDI.set_input_device(1)
pp MIDI.get_input_device
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment