summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/portmidi/pm_haiku/pmhaiku.cpp
blob: 6d21d8c5a679c7ff02418f621120d939b7cbfe7f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/* pmhaiku.cpp -- PortMidi os-dependent code */

#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <MidiConsumer.h>
#include <MidiEndpoint.h>
#include <MidiProducer.h>
#include <MidiRoster.h>
#include <MidiSynth.h>
#include "portmidi.h"
#include "pmutil.h"
#include "pminternal.h"

// as long as Java on Haiku uses ~/.java/... to store user preferences, we can directly reuse the Linux version
// (it shouldn't, see https://github.com/haikuports/haikuports/issues/7119)
extern "C" {
#include "../pm_linux/finddefault.h"
}

namespace {
    struct PmInputConsumer : BMidiLocalConsumer {
        PmInputConsumer(PmInternal *midi) :
            BMidiLocalConsumer("PortMidi input consumer"),
            midi(midi)
        {
        }


        void Data(uchar *data, size_t length, bool atomic, bigtime_t time)
        {
            if (!atomic)
                return; // should these be also supported?

            if (data[0] == B_SYS_EX_START) {
                pm_read_bytes(midi, data, length, time / 1000);
            } else {
                PmEvent event;
                switch (length) {
                case 1:
                    event.message = Pm_Message(data[0], 0, 0);
                    break;
                case 2:
                    event.message = Pm_Message(data[0], data[1], 0);
                    break;
                case 3:
                    event.message = Pm_Message(data[0], data[1], data[2]);
                    break;
                default:
                    printf("Unexpected message length for short message, got %" B_PRIuSIZE "\n", length);
                    break;
                }
                event.timestamp = time / 1000;
                pm_read_short(midi, &event);
            }
        }

    private:
        PmInternal *midi;
    };

    struct PmOutputInfo {
        BMidiLocalProducer *producer;
        std::vector<unsigned char> sysexBuffer;
    };

    struct PmSynthOutputInfo {
        BMidiSynth midiSynth;
        std::vector<unsigned char> sysexBuffer;
    };


    PmTimestamp synchronize(PmInternal *midi)
    {
        return 0;
    }


    PmError in_open(PmInternal *midi, void *driverInfo)
    {
        int32 endpointID = (int32)(intptr_t)pm_descriptors[midi->device_id].descriptor;
        BMidiProducer *producer = BMidiRoster::FindProducer(endpointID);
        if (!producer)
            return pmInvalidDeviceId;
        PmInputConsumer *consumer = new PmInputConsumer(midi);
        status_t status = producer->Connect(consumer);
        if (status != B_OK) {
            consumer->Release();
            producer->Release();
            strcpy(pm_hosterror_text, strerror(status));
            pm_hosterror = TRUE;
            return pmHostError;
        }
        midi->api_info = consumer;
        producer->Release();
        return pmNoError;
    }


    PmError in_abort(PmInternal *midi)
    {
        return pmNoError;
    }


    PmError in_close(PmInternal *midi)
    {
        int32 endpointID = (int32)(intptr_t)pm_descriptors[midi->device_id].descriptor;
        BMidiProducer *producer = BMidiRoster::FindProducer(endpointID);
        if (!producer)
            return pmInvalidDeviceId;
        PmInputConsumer *consumer = (PmInputConsumer*)midi->api_info;
        status_t status = producer->Disconnect(consumer);
        if (status != B_OK) {
            consumer->Release();
            producer->Release();
            strcpy(pm_hosterror_text, strerror(status));
            pm_hosterror = TRUE;
            return pmHostError;
        }
        consumer->Release();
        midi->api_info = NULL;
        producer->Release();
        return pmNoError;
    }


    PmError out_open(PmInternal *midi, void *driverInfo)
    {
        int32 endpointID = (int32)(intptr_t)pm_descriptors[midi->device_id].descriptor;
        BMidiConsumer *consumer = BMidiRoster::FindConsumer(endpointID);
        if (!consumer)
            return pmInvalidDeviceId;
        BMidiLocalProducer *producer = new BMidiLocalProducer("PortMidi output producer");
        status_t status = producer->Connect(consumer);
        if (status != B_OK) {
            consumer->Release();
            producer->Release();
            strcpy(pm_hosterror_text, strerror(status));
            pm_hosterror = TRUE;
            return pmHostError;
        }
        PmOutputInfo *info = new PmOutputInfo;
        info->producer = producer;
        midi->api_info = info;
        consumer->Release();
        return pmNoError;
    }


    PmError out_abort(PmInternal *midi)
    {
        return pmNoError;
    }


    PmError out_close(PmInternal *midi)
    {
        int32 endpointID = (int32)(intptr_t)pm_descriptors[midi->device_id].descriptor;
        BMidiConsumer *consumer = BMidiRoster::FindConsumer(endpointID);
        if (!consumer)
            return pmInvalidDeviceId;
        PmOutputInfo *info = (PmOutputInfo*)midi->api_info;
        status_t status = info->producer->Disconnect(consumer);
        if (status != B_OK) {
            consumer->Release();
            midi->api_info = NULL;
            info->producer->Release();
            delete info;
            strcpy(pm_hosterror_text, strerror(status));
            pm_hosterror = TRUE;
            return pmHostError;
        }
        consumer->Release();
        midi->api_info = NULL;
        info->producer->Release();
        delete info;
        return pmNoError;
    }


    // copied from the macOS implementation
    size_t midi_length(uchar status)
    {
        int high, low;
        static size_t high_lengths[] = {
            1, 1, 1, 1, 1, 1, 1, 1,         /* 0x00 through 0x70 */
            3, 3, 3, 3, 2, 2, 3, 1          /* 0x80 through 0xf0 */
        };
        static size_t low_lengths[] = {
            1, 2, 3, 2, 1, 1, 1, 1,         /* 0xf0 through 0xf8 */
            1, 1, 1, 1, 1, 1, 1, 1          /* 0xf9 through 0xff */
        };

        high = status >> 4;
        low = status & 15;

        return (high != 0xF) ? high_lengths[high] : low_lengths[low];
    }


    PmError write_short(PmInternal *midi, PmEvent *buffer)
    {
        PmOutputInfo *info = (PmOutputInfo*)midi->api_info;
        uchar data[3];
        data[0] = Pm_MessageStatus(buffer->message);
        data[1] = Pm_MessageData1(buffer->message);
        data[2] = Pm_MessageData2(buffer->message);
        size_t length = midi_length(data[0]);

        info->producer->SprayData(data, length, true, buffer->timestamp * 1000);

        // TODO: handle latency != 0
        return pmNoError;
    }


    PmError begin_sysex(PmInternal *midi, PmTimestamp timestamp)
    {
        PmOutputInfo *info = (PmOutputInfo*)midi->api_info;
        info->sysexBuffer.clear();
        return pmNoError;
    }


    PmError end_sysex(PmInternal *midi, PmTimestamp timestamp)
    {
        PmOutputInfo *info = (PmOutputInfo*)midi->api_info;
        info->producer->SpraySystemExclusive(&info->sysexBuffer[0], info->sysexBuffer.size(), timestamp * 1000);
        info->sysexBuffer.clear();
        return pmNoError;
    }


    PmError write_byte(PmInternal *midi, unsigned char byte, PmTimestamp timestamp)
    {
        PmOutputInfo *info = (PmOutputInfo*)midi->api_info;
        info->sysexBuffer.push_back(byte);
        return pmNoError;
    }


    PmError write_realtime(PmInternal *midi, PmEvent *buffer)
    {
        PmOutputInfo *info = (PmOutputInfo*)midi->api_info;
        info->producer->SpraySystemRealTime(Pm_MessageStatus(buffer->message), buffer->timestamp * 1000);
        return pmNoError;
    }


    PmError synth_open(PmInternal *midi, void *driverInfo)
    {
        PmSynthOutputInfo *info = new PmSynthOutputInfo;
        info->midiSynth.EnableInput(true, true);
        midi->api_info = info;
        return pmNoError;
    }


    PmError synth_abort(PmInternal *midi)
    {
        return pmNoError;
    }


    PmError synth_close(PmInternal *midi)
    {
        PmSynthOutputInfo *info = (PmSynthOutputInfo*)midi->api_info;
        delete info;
        midi->api_info = NULL;
        return pmNoError;
    }


    PmError write_short_synth(PmInternal *midi, PmEvent *buffer)
    {
        PmSynthOutputInfo *info = (PmSynthOutputInfo*)midi->api_info;
        uchar data[3];
        data[0] = Pm_MessageStatus(buffer->message);
        data[1] = Pm_MessageData1(buffer->message);
        data[2] = Pm_MessageData2(buffer->message);

        switch(data[0] & 0xf0) {
        case B_NOTE_OFF:
            info->midiSynth.NoteOff((data[0] & 0x0f) + 1, data[1], data[2], buffer->timestamp);
            break;
        case B_NOTE_ON:
            info->midiSynth.NoteOn((data[0] & 0x0f) + 1, data[1], data[2], buffer->timestamp);
            break;
        case B_KEY_PRESSURE:
            info->midiSynth.KeyPressure((data[0] & 0x0f + 1), data[1], data[2], buffer->timestamp);
            break;
        case B_CONTROL_CHANGE:
            info->midiSynth.ControlChange((data[0] & 0x0f) + 1, data[1], data[2], buffer->timestamp);
            break;
        case B_PROGRAM_CHANGE:
            info->midiSynth.ProgramChange((data[0] & 0x0f) + 1, data[1], buffer->timestamp);
            break;
        case B_CHANNEL_PRESSURE:
            info->midiSynth.ChannelPressure((data[0] & 0x0f) + 1, data[1], buffer->timestamp);
            break;
        case B_PITCH_BEND:
            info->midiSynth.PitchBend((data[0] & 0x0f) + 1, data[1], data[2], buffer->timestamp);
            break;
        }

        // TODO: handle latency != 0
        return pmNoError;
    }


    PmError begin_sysex_synth(PmInternal *midi, PmTimestamp timestamp)
    {
        PmSynthOutputInfo *info = (PmSynthOutputInfo*)midi->api_info;
        info->sysexBuffer.clear();
        return pmNoError;
    }


    PmError end_sysex_synth(PmInternal *midi, PmTimestamp timestamp)
    {
        PmSynthOutputInfo *info = (PmSynthOutputInfo*)midi->api_info;
        info->midiSynth.SystemExclusive(&info->sysexBuffer[0], info->sysexBuffer.size(), timestamp);
        info->sysexBuffer.clear();
        return pmNoError;
    }


    PmError write_byte_synth(PmInternal *midi, unsigned char byte, PmTimestamp timestamp)
    {
        PmSynthOutputInfo *info = (PmSynthOutputInfo*)midi->api_info;
        info->sysexBuffer.push_back(byte);
        return pmNoError;
    }


    PmError write_realtime_synth(PmInternal *midi, PmEvent *buffer)
    {
        PmSynthOutputInfo *info = (PmSynthOutputInfo*)midi->api_info;
        info->midiSynth.SystemRealTime(Pm_MessageStatus(buffer->message), buffer->timestamp);
        return pmNoError;
    }


    PmError write_flush(PmInternal *midi, PmTimestamp timestamp)
    {
        return pmNoError;
    }


    unsigned int check_host_error(PmInternal *midi)
    {
        return 0;
    }


    pm_fns_node pm_in_dictionary = {
        none_write_short,
        none_sysex,
        none_sysex,
        none_write_byte,
        none_write_short,
        none_write_flush,
        synchronize,
        in_open,
        in_abort,
        in_close,
        success_poll,
        check_host_error
    };

    pm_fns_node pm_out_dictionary = {
        write_short,
        begin_sysex,
        end_sysex,
        write_byte,
        write_realtime,
        write_flush,
        synchronize,
        out_open,
        out_abort,
        out_close,
        none_poll,
        check_host_error
    };


    pm_fns_node pm_synth_dictionary = {
        write_short_synth,
        begin_sysex_synth,
        end_sysex_synth,
        write_byte_synth,
        write_realtime_synth,
        write_flush,
        synchronize,
        synth_open,
        synth_abort,
        synth_close,
        none_poll,
        check_host_error
    };


    PmError create_virtual(int is_input, const char *name, void *driverInfo)
    {
        BMidiEndpoint *endpoint = is_input ? (BMidiEndpoint*)new BMidiLocalProducer(name) : new BMidiLocalConsumer(name);
        if (!endpoint->IsValid()) {
            endpoint->Release();
            strcpy(pm_hosterror_text, "Endpoint could not be created");
            pm_hosterror = TRUE;
            return pmHostError;
        }
        status_t status = endpoint->Register();
        if (status != B_OK) {
            endpoint->Release();
            strcpy(pm_hosterror_text, strerror(status));
            pm_hosterror = TRUE;
            return pmHostError;
        }
        return pm_add_device(const_cast<char*>("Haiku MIDI kit"), name, is_input, TRUE, (void*)(intptr_t)endpoint->ID(), is_input ? &pm_in_dictionary : &pm_out_dictionary);
    }

    PmError delete_virtual(PmDeviceID id)
    {
        int32 endpointID = (int32)(intptr_t)pm_descriptors[id].descriptor;
        BMidiEndpoint *endpoint = BMidiRoster::FindEndpoint(endpointID);
        //TODO: handle connected producers and consumers
        status_t status = endpoint->Unregister();
        // release twice to actually free the endpoint (FindEndpoint increases the ref-count)
        endpoint->Release();
        endpoint->Release();
        if (status != B_OK) {
            strcpy(pm_hosterror_text, strerror(status));
            pm_hosterror = TRUE;
            return pmHostError;
        }
        return pmNoError;
    }

    PmDeviceID pm_default_input_device_id = -1;
    PmDeviceID pm_default_output_device_id = -1;
}

extern "C" {
    void pm_init()
    {
        pm_add_interf(const_cast<char*>("Haiku MIDI kit"), create_virtual, delete_virtual);

        pm_add_device(const_cast<char*>("Haiku MIDI kit"), "Soft Synth", FALSE, FALSE, NULL, &pm_synth_dictionary);

        int32 id = 0;
        BMidiEndpoint *endpoint;

        while ((endpoint = BMidiRoster::NextEndpoint(&id)) != NULL) {
            bool isInput = endpoint->IsProducer();
            pm_add_device(const_cast<char*>("Haiku MIDI kit"), endpoint->Name(), isInput, FALSE, (void*)(intptr_t)id, isInput ? &pm_in_dictionary : &pm_out_dictionary);
            endpoint->Release();
        }

        // the following (default device handling) is copied from the Linux implementation

        // this is set when we return to Pm_Initialize, but we need it
        // now in order to (successfully) call Pm_CountDevices()
        pm_initialized = TRUE;
        pm_default_input_device_id = find_default_device(
            const_cast<char*>("/PortMidi/PM_RECOMMENDED_INPUT_DEVICE"), TRUE,
            pm_default_input_device_id);
        pm_default_output_device_id = find_default_device(
            const_cast<char*>("/PortMidi/PM_RECOMMENDED_OUTPUT_DEVICE"), FALSE,
            pm_default_output_device_id);
    }


    void pm_term()
    {
        int i;
        for (i = 0; i < pm_descriptor_len; i++) {
            PmInternal *midi = pm_descriptors[i].pm_internal;
            if (midi && midi->api_info) {
                // device is still open, close it
                (*midi->dictionary->close)(midi);
            }
            if (pm_descriptors[i].pub.is_virtual && !pm_descriptors[i].deleted) {
                delete_virtual(i);
            }
        }
    }


    PmDeviceID Pm_GetDefaultInputDeviceID()
    {
        Pm_Initialize();
        return pm_default_input_device_id;
    }


    PmDeviceID Pm_GetDefaultOutputDeviceID()
    {
        Pm_Initialize();
        return pm_default_output_device_id;
    }


    void *pm_alloc(size_t s)
    {
        return malloc(s); 
    }


    void pm_free(void *ptr)
    {
         free(ptr); 
    }
}