File: HaikuMidi.cpp

package info (click to toggle)
csound 1%3A6.18.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 62,416 kB
  • sloc: ansic: 192,636; cpp: 14,151; javascript: 9,654; objc: 9,181; java: 3,337; python: 3,333; sh: 1,783; yacc: 1,255; xml: 985; perl: 635; lisp: 411; tcl: 341; lex: 217; makefile: 126
file content (304 lines) | stat: -rw-r--r-- 9,133 bytes parent folder | download | duplicates (4)
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
/*
  HaikuMidi.cpp:

  Haiku real-time MIDI handling for Csound5

  Copyright (C) 2012-2019 Peter J. Goodeve

  This file is part of Csound.

  The Csound Library is free software; you can redistribute it
  and/or modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  Csound is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with Csound; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  02111-1307 USA

*/


#include <malloc.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <Locker.h>

#include <MidiRoster.h>
#include <MidiProducer.h>
#include <MidiConsumer.h>

#include "haiku_midi.h"

#define XDPRINTF(x)
#define DPRINTF(x)
//#define DPRINTF(x) printf x


//////////////////////////////////////////////////////////


class MidiInHandler : public BMidiLocalConsumer
{
        friend class MidiIn;
        MidiIn * param;
public:
        MidiInHandler(MidiIn * mp, const char * name=NULL)
         :  BMidiLocalConsumer(name), param(mp), nextevent(0) {
                midi_in_sem = create_sem(0, "midi input");
         }
        ~MidiInHandler() {
                delete_sem(midi_in_sem);
        }


        void NoteOff(uchar channel,
                     uchar note,
                     uchar velocity,
                     bigtime_t time);

        void NoteOn(uchar channel,
                    uchar note,
                    uchar velocity,
                    bigtime_t time);

        void KeyPressure(uchar channel,
                         uchar note,
                         uchar pressure,
                         bigtime_t time);

        void ControlChange(uchar channel,
                           uchar controlNumber,
                           uchar controlValue,
                           bigtime_t time);

        void ProgramChange(uchar channel,
                           uchar programNumber,
                           bigtime_t time);

        void ChannelPressure(uchar channel,
                             uchar pressure,
                             bigtime_t time);

        void PitchBend(uchar channel,
                       uchar lsb,
                       uchar msb,
                       bigtime_t time);

        void SystemExclusive(void* data,
                             size_t dataLength,
                             bigtime_t time);

        void SystemCommon(uchar statusByte,
                          uchar data1,
                          uchar data2,
                          bigtime_t time);

        void SystemRealTime(uchar statusByte, bigtime_t time);


        void midiEventIn(MidiEvent &s, bigtime_t time);
        void midiSysExIn(void *data, size_t size, bigtime_t time);

        uint32 nextevent;
        sem_id midi_in_sem;         // to be waited on by MIDI loop (w. timeout)
};


//////////////////////////////////////////////////////////

void MidiInHandler::NoteOff(uchar channel,
                            uchar note,
                            uchar velocity,
                            bigtime_t time)
{
        MidiEvent s(3, 0x80|channel, note, velocity);
        midiEventIn(s, time);
}


void MidiInHandler::NoteOn(uchar channel,
                           uchar note,
                           uchar velocity,
                           bigtime_t time)
{
        MidiEvent s(3, 0x90|channel, note, velocity);
        midiEventIn(s, time);
}


void MidiInHandler::KeyPressure(uchar channel,
                                uchar note,
                                uchar pressure,
                                bigtime_t time)
{
        MidiEvent s(3, 0xA0|channel, note, pressure);
        midiEventIn(s, time);
}


void MidiInHandler::ControlChange(uchar channel,
                                  uchar controlNumber,
                                  uchar controlValue,
                                  bigtime_t time)
{
        MidiEvent s(3, 0xB0|channel, controlNumber, controlValue);
        midiEventIn(s, time);
}


void MidiInHandler::ProgramChange(uchar channel,
                                  uchar programNumber,
                                  bigtime_t time)
{
        MidiEvent s(2, 0xC0|channel, programNumber);
        midiEventIn(s, time);
}


void MidiInHandler::ChannelPressure(uchar channel,
                                    uchar pressure,
                                    bigtime_t time)
{
        MidiEvent s(2, 0xD0|channel, pressure);
        midiEventIn(s, time);
}


void MidiInHandler::PitchBend(uchar channel,
                              uchar lsb,
                              uchar msb,
                              bigtime_t time)
{
        MidiEvent s(3, 0xE0|channel, lsb, msb);
        midiEventIn(s, time);
}


void MidiInHandler::SystemExclusive(void* data,
                                    size_t dataLength,
                                    bigtime_t time)
{
        midiSysExIn(data, dataLength, time);
}


void MidiInHandler::SystemCommon(uchar statusByte,
                                 uchar data1,
                                 uchar data2,
                                 bigtime_t time)
{
// Ignored for the moment (size varies...)
}


void MidiInHandler::SystemRealTime(uchar statusByte, bigtime_t time)
{
        MidiEvent s(3, statusByte);
        midiEventIn(s, time);
}


/////////////////////////////////////////

// Called by MidiInHandler input functions...:
void MidiInHandler::midiEventIn(MidiEvent &s, bigtime_t time)
{
        nextevent = s;
        acquire_sem_etc(midi_in_sem, 1, B_RELATIVE_TIMEOUT, 1000000);
}


void MidiInHandler::midiSysExIn(void *data, size_t size, bigtime_t time)
{
        // completely ignored (for now anyway...)
}


//////////////////////////////////////////////////////////

static BMidiProducer *FindProducer(const char *name)
{
        if (!name || !*name) return NULL;       // just being safe...
        BMidiProducer *object;
        int32 id = 0;
        DPRINTF(("looking for endpoint %s\n", name);)
        if (*name == '?') {
                fprintf(stderr,
                        "\n===========================\nAvailable MIDI Sources:\n");
                while(object = BMidiRoster::NextProducer(&id)){
                        fprintf(stderr, "    %s\n", object->Name());
                        object->Release();
                }
                fprintf(stderr, "===========================\n\n");
        }
        else while(object = BMidiRoster::NextProducer(&id)){
                DPRINTF(("checking endpoint %p %s\n", object, object->Name());)
                if(!strcmp(object->Name(),name)) {
                        return object;
                }
                object->Release();
        }
        return NULL;
}


MidiIn::MidiIn(const char *name) :
nodename(name), extSource(NULL), handler(NULL)
{
        if (!name || !*name) return;    // empty name -- don't publish
        extSource = FindProducer(name);
        if (*name == '?') return;       // was just a query
        if (extSource) {
                DPRINTF(("Connecting to external Producer %s\n", name);)
                handler = new MidiInHandler(this);      // anonymous endpoint
                if (extSource->Connect(handler) != B_OK) {
                        extSource = NULL;
                        handler->Release();
                        handler = NULL;
                        fprintf(stderr, "Failed to connect to %s!!\n", name);
                }
                else
                        fprintf(stderr, "Connected to MIDI source %s\n", name);
                extSource->Release();   // our extra hold...
        } else {
                DPRINTF(("Registering as Consumer %s\n", name);)
                handler = new MidiInHandler(this, name);
                BMidiRoster::Register(handler); // needs error check!
                DPRINTF(("Published OK -- thread %lx\n", find_thread(NULL));)
                fprintf(stderr, "Providing MIDI Input %s\n", name);
        }
}


MidiIn::~MidiIn()
{
        if (handler) {
                DPRINTF(("Unregistering  %s\n", nodename? nodename : "<NULL>");)
                handler->Release();
                DPRINTF(("handler %p released\n", handler);)
                if (!handler->param->extSource) BMidiRoster::Unregister(handler);
                handler = NULL;
                DPRINTF(("Unregistered... handler now %p\n", handler);)
        }
        DPRINTF(("Unpublished OK -- thread %lx\n", find_thread(NULL));)
}


uint32 MidiIn::GetEvent() {
        uint32 ev = handler->nextevent;
        if (!handler) return 0;
        if (ev != 0) {
                handler->nextevent = 0;
                release_sem(handler->midi_in_sem);
        }
        return ev;
}