Module: CoreMIDI::API

Defined in:
lib/coremidi.rb,
ext/rbcoremidi.c

Defined Under Namespace

Classes: InputPort, MIDIClient, MidiPacket

Class Method Summary collapse

Class Method Details

.check_for_new_dataObject

Checks for new data and copies it over if there is some.



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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'ext/rbcoremidi.c', line 89

static VALUE t_check_for_new_data(VALUE self)
{
    if( pthread_mutex_trylock(&mutex) != 0 )
    {
        // no data for us yet
        return Qfalse;
    }
    
    // Switch out the arrays. Possibly evil
    CFArrayRef data = midi_data;
    midi_data = CFArrayCreateMutable(kCFAllocatorDefault, 0, NULL);
    
    pthread_mutex_unlock(&mutex);
    
    // We'll use a Ruby Struct to store the data
    VALUE cMidiPacket = rb_const_get(mCoreMIDIAPI, rb_intern("MidiPacket"));
    
    VALUE rb_midi_data = rb_ary_new();
    
    CFIndex idx = 0;
    CFIndex array_size = CFArrayGetCount(data);
    const RbMIDIPacket* current_packet = NULL;
    
    for( ; idx < array_size; ++idx ) 
    {
        current_packet = (const RbMIDIPacket*) CFArrayGetValueAtIndex(data, idx);
        
        VALUE byte_array = rb_ary_new2(current_packet->length);
        
        int i;
        for (i = 0; i < current_packet->length; ++i) 
        {
            rb_ary_push(byte_array, INT2FIX(current_packet->data[i]));
        }
        
        // relies on sizeof(MIDITimeStamp) == sizeof(unsigned long long)
        assert(sizeof(MIDITimeStamp) == sizeof(unsigned long long));
        
        VALUE midi_packet_args[2];
        midi_packet_args[0] = ULL2NUM(current_packet->timeStamp);
        midi_packet_args[1] = byte_array;
        
        rb_ary_push(rb_midi_data, rb_class_new_instance((sizeof(midi_packet_args)/sizeof(midi_packet_args[0])), midi_packet_args, cMidiPacket));
        
        // While we're at it..
        // Free the memory! Save the whales!
        free(current_packet->data);
    }
    
    // Free the memory! Save the whales! Part 2!
    CFRelease(data);
    
    return rb_midi_data;
}

.connect_source_to_port(source_idx, input_port) ⇒ Object

input_port is an InputPort class



225
226
227
228
229
230
231
232
233
234
235
# File 'ext/rbcoremidi.c', line 225

static VALUE t_connect_source_to_port(VALUE self, VALUE source_idx, VALUE input_port)
{
    RbInputPort* port;
    Data_Get_Struct(input_port, RbInputPort, port);
    
    MIDIEndpointRef source = MIDIGetSource(FIX2INT(source_idx));
    
    MIDIPortConnectSource(port->input_port, source, NULL);
    
    return Qtrue;
}

.create_client(client_name) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'ext/rbcoremidi.c', line 144

static VALUE t_create_client(VALUE self, VALUE client_name)
{    
    VALUE midiclient_instance = rb_class_new_instance(0, 0, cMIDIClient);
    if( midiclient_instance == Qnil )
    {
        free_objects();
        rb_fatal("Couldn't create an instance of MIDIClient!");
    }
    
    MIDIClientRef midi_client;
    
    CFStringRef client_str = CFStringCreateWithCString(kCFAllocatorDefault, RSTRING(client_name)->ptr, kCFStringEncodingASCII);
    MIDIClientCreate(client_str, NULL, NULL, &midi_client);
    CFRelease(client_str);
    
    RbMIDIClient* client_struct;
    Data_Get_Struct(midiclient_instance, RbMIDIClient, client_struct);
    
    client_struct->client = midi_client;
    
    return midiclient_instance;
}

.create_input_port(client_instance, port_name) ⇒ Object

Create a new Input Port and saves the Ruby Callback proc.



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'ext/rbcoremidi.c', line 168

static VALUE t_create_input_port(VALUE self, VALUE client_instance, VALUE port_name)
{
    MIDIPortRef in_port;
    
    RbMIDIClient* client;
    Data_Get_Struct(client_instance, RbMIDIClient, client);
    
    CFStringRef port_str = CFStringCreateWithCString(kCFAllocatorDefault, RSTRING(port_name)->ptr, kCFStringEncodingASCII);
    MIDIInputPortCreate(client->client, port_str, RbMIDIReadProc, NULL, &in_port);
    CFRelease(port_str);
    
    VALUE inputport_instance = rb_class_new_instance(0, 0, cInputPort);
    if( inputport_instance == Qnil )
    {
        free_objects();
        rb_fatal("Couldn't create an instance of InputPort!");
    }
    
    RbInputPort* port_struct;
    Data_Get_Struct(inputport_instance, RbInputPort, port_struct);
    
    port_struct->input_port = in_port;
    
    return inputport_instance;
}

.disconnect_source_from_port(source_idx, input_port) ⇒ Object

input_port is an InputPort class



239
240
241
242
243
244
245
246
247
248
249
# File 'ext/rbcoremidi.c', line 239

static VALUE t_disconnect_source_from_port(VALUE self, VALUE source_idx, VALUE input_port)
{
    RbInputPort* port;
    Data_Get_Struct(input_port, RbInputPort, port);
    
    MIDIEndpointRef source = MIDIGetSource(FIX2INT(source_idx));
    
    MIDIPortDisconnectSource(port->input_port, source);
    
    return Qtrue;
}

.get_num_sourcesObject



218
219
220
221
# File 'ext/rbcoremidi.c', line 218

static VALUE t_get_num_sources(VALUE self)
{
    return INT2FIX(MIDIGetNumberOfSources());
}

.get_sourcesObject

Return an array of all available sources, filled with the names of the sources



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'ext/rbcoremidi.c', line 195

static VALUE t_get_sources(VALUE self)
{    
    int number_of_sources = MIDIGetNumberOfSources();
    
    VALUE source_ary = rb_ary_new2(number_of_sources);
    
    int idx;
    for(idx = 0; idx < number_of_sources; ++idx)
    {
        MIDIEndpointRef src = MIDIGetSource(idx);
        CFStringRef pname;
        char name[64];
        
        MIDIObjectGetStringProperty(src, kMIDIPropertyName, &pname);
        CFStringGetCString(pname, name, sizeof(name), 0);
        CFRelease(pname);
        
        rb_ary_push(source_ary, rb_str_new2(name));
    }
    
    return source_ary;
}