Skip to content

Instantly share code, notes, and snippets.

@RyanScottLewis
RyanScottLewis / gist:711462
Created November 23, 2010 08:34
Array#to_h, Struct#to_h, and Struct#to_json
require 'json'
class Array
def to_h
inject({}) { |memo, kv| key, value = *kv; memo[key] = value; memo }
end
end
class Struct
def to_h
@RyanScottLewis
RyanScottLewis / rulebook_rewrite.rb
Created December 21, 2010 20:02
Rewrite of my RuleBook gem
module RuleBook
def RuleBook.add_rules(object, instance_or_class, rules)
instance_or_class = instance_or_class.to_s.downcase.strip.to_sym
raise(ArgumentError, "'instance_or_class' must equal :instance or :class") unless [:instance, :class].include?(instance_or_class)
raise(ArgumentError, "'rules' must be a Hash") unless rules.is_a?(Hash)
unless object.instance_variable_get(:@_rulebook_initiated) # Class instance variable. Not class variable. Not instance variable. Is confusing.
object.instance_variable_set(:@_rulebook_initiated, true)
object.instance_variable_set(:@_rulebook, {:instance=>{}, :class=>{}}) # @_rulebook is actually two rulebooks, instance and class
end
my_proj = Project.new do
drum_kit do
kick "kick.wav"
closed_hi_hat "closedhihat.wav"
snare "snare.wav"
end
patterns do
simple do
kick
kick & snare
require 'ffi'
# http://msdn.microsoft.com/en-us/library/aa383751(VS.85).aspx
# DWORD appears to be an :int (32 bits that is, so :int should work well)
# HWND appears to be a :pointer see this thread for why you should not actually read from the value it points to. You may as well just use a :long or :ulong.
# LPDWORD is a pointer (the P standing for pointer)
# LPARAM appears to be a long (hence the L)
# WPARAM appears to be a long (i.e. 64 bits on 64bit OS).
# BOOL is an :int
@RyanScottLewis
RyanScottLewis / gist:911786
Created April 9, 2011 21:23
BCR2000/BCF2000 SysEx Messages
require 'midi-winmm'
class Numeric
def to_hex
"%02x" % self
end
end
class String
def to_hex
@RyanScottLewis
RyanScottLewis / midi.c
Created April 11, 2011 03:25
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;
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'midi-winmm'
# tested with midi-winmm, but I assume will work with alsa-rawmidi
class Numeric
def to_hex_str
"%02x" % self
end
end
@RyanScottLewis
RyanScottLewis / nodelist.rb
Created April 18, 2011 04:38
trying to make an Array without.. well.. an Array
class Node
attr_accessor :index, :next, :value
def initialize(index, value)
@index, @value, @next = index, value, nil
end
end
class NodeList
include Enumerable
module MIDI
# Source: http://www.midi.org/techspecs/manid.php
Manufacturers = {
"Ta Horng Musical Instrument" => [0x00, 0x00, 0x74],
"e-Tek Labs (Forte Tech)" => [0x00, 0x00, 0x75],
"Electro-Voice" => [0x00, 0x00, 0x76],
"Midisoft Corporation" => [0x00, 0x00, 0x77],
"QSound Labs" => [0x00, 0x00, 0x78],
"Westrex" => [0x00, 0x00, 0x79],
"Nvidia" => [0x00, 0x00, 0x7A],
user system total real
load YAML
doc["manufacturers"]["Roland"] 65
0.093000 0.156000 0.249000 ( 0.241016)
load Consts
MIDI::CC_FOOT_CONTROLLER: 4
0.000000 0.000000 0.000000 ( 0.003001)
load Manufacturers
Roland Corporation: [65]
0.000000 0.000000 0.000000 ( 0.004000)