JackWinMMEInputPort.cpp 10.3 KB
Newer Older
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
/*
Copyright (C) 2011 Devin Anderson

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

This program 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include <cassert>
#include <memory>
#include <stdexcept>

#include "JackError.h"
#include "JackMidiUtil.h"
#include "JackWinMMEInputPort.h"
Stephane Letz's avatar
Stephane Letz committed
27
#include "JackMidiWriteQueue.h"
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

using Jack::JackWinMMEInputPort;

///////////////////////////////////////////////////////////////////////////////
// Static callbacks
///////////////////////////////////////////////////////////////////////////////

void CALLBACK
JackWinMMEInputPort::HandleMidiInputEvent(HMIDIIN handle, UINT message,
                                          DWORD port, DWORD param1,
                                          DWORD param2)
{
    ((JackWinMMEInputPort *) port)->ProcessWinMME(message, param1, param2);
}

///////////////////////////////////////////////////////////////////////////////
// Class
///////////////////////////////////////////////////////////////////////////////

JackWinMMEInputPort::JackWinMMEInputPort(const char *alias_name,
                                         const char *client_name,
                                         const char *driver_name, UINT index,
                                         size_t max_bytes, size_t max_messages)
{
    thread_queue = new JackMidiAsyncQueue(max_bytes, max_messages);
sletz's avatar
sletz committed
53
    std::auto_ptr<JackMidiAsyncQueue> thread_queue_ptr(thread_queue);
54
    write_queue = new JackMidiBufferWriteQueue();
sletz's avatar
sletz committed
55
    std::auto_ptr<JackMidiBufferWriteQueue> write_queue_ptr(write_queue);
56
57
    sysex_buffer = new jack_midi_data_t[max_bytes];
    char error_message[MAXERRORLENGTH];
58
59
    MMRESULT result = midiInOpen(&handle, index, (DWORD)HandleMidiInputEvent,
                                 (DWORD)this,
60
                                 CALLBACK_FUNCTION | MIDI_IO_STATUS);
61
    if (result != MMSYSERR_NOERROR) {
62
        GetInErrorString(result, error_message);
63
64
65
66
67
68
        goto delete_sysex_buffer;
    }
    sysex_header.dwBufferLength = max_bytes;
    sysex_header.dwBytesRecorded = 0;
    sysex_header.dwFlags = 0;
    sysex_header.dwUser = 0;
Stephane Letz's avatar
Stephane Letz committed
69
    sysex_header.lpData = (LPSTR)(((LPBYTE) &sysex_header) + sizeof(MIDIHDR));
70
71
72
    sysex_header.lpNext = 0;
    result = midiInPrepareHeader(handle, &sysex_header, sizeof(MIDIHDR));
    if (result != MMSYSERR_NOERROR) {
73
        GetInErrorString(result, error_message);
74
75
76
77
        goto close_handle;
    }
    result = midiInAddBuffer(handle, &sysex_header, sizeof(MIDIHDR));
    if (result != MMSYSERR_NOERROR) {
78
        GetInErrorString(result, error_message);
79
80
        goto unprepare_header;
    }
Stephane Letz's avatar
Stephane Letz committed
81

82
83
84
85
    MIDIINCAPS capabilities;
    char *name_tmp;
    result = midiInGetDevCaps(index, &capabilities, sizeof(capabilities));
    if (result != MMSYSERR_NOERROR) {
86
        WriteInError("JackWinMMEInputPort [constructor]", "midiInGetDevCaps",
87
                   result);
Stephane Letz's avatar
Stephane Letz committed
88
        name_tmp = (char*) driver_name;
89
90
91
    } else {
        name_tmp = capabilities.szPname;
    }
Stephane Letz's avatar
Stephane Letz committed
92

93
94
95
    snprintf(alias, sizeof(alias) - 1, "%s:%s:in%d", alias_name, name_tmp,
             index + 1);
    snprintf(name, sizeof(name) - 1, "%s:capture_%d", client_name, index + 1);
96
97
    jack_event = 0;
    started = false;
sletz's avatar
sletz committed
98
99
    write_queue_ptr.release();
    thread_queue_ptr.release();
100
101
102
    return;

 unprepare_header:
Stephane Letz's avatar
Stephane Letz committed
103
    result = midiInUnprepareHeader(handle, &sysex_header, sizeof(MIDIHDR));
104
    if (result != MMSYSERR_NOERROR) {
105
        WriteInError("JackWinMMEInputPort [constructor]",
106
107
108
109
110
                   "midiInUnprepareHeader", result);
    }
 close_handle:
    result = midiInClose(handle);
    if (result != MMSYSERR_NOERROR) {
111
        WriteInError("JackWinMMEInputPort [constructor]", "midiInClose", result);
112
113
    }
 delete_sysex_buffer:
sletz's avatar
sletz committed
114
    delete[] sysex_buffer;
115
116
117
118
119
120
121
    throw std::runtime_error(error_message);
}

JackWinMMEInputPort::~JackWinMMEInputPort()
{
    MMRESULT result = midiInReset(handle);
    if (result != MMSYSERR_NOERROR) {
122
        WriteInError("JackWinMMEInputPort [destructor]", "midiInReset", result);
123
    }
Stephane Letz's avatar
Stephane Letz committed
124
    result = midiInUnprepareHeader(handle, &sysex_header, sizeof(MIDIHDR));
125
    if (result != MMSYSERR_NOERROR) {
126
        WriteInError("JackWinMMEInputPort [destructor]", "midiInUnprepareHeader",
127
128
129
130
                   result);
    }
    result = midiInClose(handle);
    if (result != MMSYSERR_NOERROR) {
131
        WriteInError("JackWinMMEInputPort [destructor]", "midiInClose", result);
132
133
134
135
136
137
138
139
140
141
142
143
144
145
    }
    delete[] sysex_buffer;
    delete thread_queue;
    delete write_queue;
}

void
JackWinMMEInputPort::EnqueueMessage(jack_nframes_t time, size_t length,
                                    jack_midi_data_t *data)
{
    switch (thread_queue->EnqueueEvent(time, length, data)) {
    case JackMidiWriteQueue::BUFFER_FULL:
        jack_error("JackWinMMEInputPort::EnqueueMessage - The thread queue "
                   "cannot currently accept a %d-byte event.  Dropping event.",
Stephane Letz's avatar
Stephane Letz committed
146
                   length);
147
148
149
150
        break;
    case JackMidiWriteQueue::BUFFER_TOO_SMALL:
        jack_error("JackWinMMEInputPort::EnqueueMessage - The thread queue "
                   "buffer is too small to enqueue a %d-byte event.  Dropping "
Stephane Letz's avatar
Stephane Letz committed
151
                   "event.", length);
152
153
154
155
156
157
158
        break;
    default:
        ;
    }
}

void
159
160
JackWinMMEInputPort::ProcessJack(JackMidiBuffer *port_buffer,
                                 jack_nframes_t frames)
161
162
163
164
165
{
    write_queue->ResetMidiBuffer(port_buffer, frames);
    if (! jack_event) {
        jack_event = thread_queue->DequeueEvent();
    }
sletz's avatar
sletz committed
166
    for (; jack_event; jack_event = thread_queue->DequeueEvent()) {
Stephane Letz's avatar
Stephane Letz committed
167
168
        switch (write_queue->EnqueueEvent(jack_event)) {
        case JackMidiWriteQueue::BUFFER_TOO_SMALL:
169
170
            jack_error("JackWinMMEMidiInputPort::Process - The buffer write "
                       "queue couldn't enqueue a %d-byte event. Dropping "
Stephane Letz's avatar
Stephane Letz committed
171
                       "event.", jack_event->size);
172
            // Fallthrough on purpose
sletz's avatar
sletz committed
173
        case JackMidiWriteQueue::OK:
174
175
176
177
178
179
180
181
182
            continue;
        }
        break;
    }
}

void
JackWinMMEInputPort::ProcessWinMME(UINT message, DWORD param1, DWORD param2)
{
183
    set_threaded_log_function();
sletz's avatar
sletz committed
184
    jack_nframes_t current_frame = GetCurrentFrame();
sletz's avatar
sletz committed
185

186
187
188
189
190
191
192
193
194
195
196
197
    switch (message) {
    case MIM_CLOSE:
        jack_info("JackWinMMEInputPort::ProcessWinMME - MIDI device closed.");
        break;
    case MIM_MOREDATA:
        jack_info("JackWinMMEInputPort::ProcessWinMME - The MIDI input device "
                  "driver thinks that JACK is not processing messages fast "
                  "enough.");
        // Fallthrough on purpose.
    case MIM_DATA:
        jack_midi_data_t message_buffer[3];
        jack_midi_data_t status = param1 & 0xff;
sletz's avatar
sletz committed
198
        int length = GetMessageLength(status);
sletz's avatar
sletz committed
199

200
201
        switch (length) {
        case 3:
sletz's avatar
sletz committed
202
             message_buffer[2] = (param1 >> 16)  & 0xff;
203
204
            // Fallthrough on purpose.
        case 2:
sletz's avatar
sletz committed
205
            message_buffer[1] = (param1 >> 8) & 0xff;
206
207
208
209
210
211
212
213
214
215
216
217
218
219
            // Fallthrough on purpose.
        case 1:
            message_buffer[0] = status;
            break;
        case 0:
            jack_error("JackWinMMEInputPort::ProcessWinMME - **BUG** MIDI "
                       "input driver sent an MIM_DATA message with a sysex "
                       "status byte.");
            return;
        case -1:
            jack_error("JackWinMMEInputPort::ProcessWinMME - **BUG** MIDI "
                       "input driver sent an MIM_DATA message with an invalid "
                       "status byte.");
            return;
sletz's avatar
sletz committed
220
        }
221
222
223
        EnqueueMessage(current_frame, (size_t) length, message_buffer);
        break;
    case MIM_LONGDATA:
Stephane Letz's avatar
Stephane Letz committed
224
        LPMIDIHDR header = (LPMIDIHDR) param1;
225
        jack_midi_data_t *data = (jack_midi_data_t *) header->lpData;
Stephane Letz's avatar
Stephane Letz committed
226
227
        size_t length1 = header->dwBytesRecorded;
        if ((data[0] != 0xf0) || (data[length1 - 1] != 0xf7)) {
228
229
230
            jack_error("JackWinMMEInputPort::ProcessWinMME - Discarding "
                       "%d-byte sysex chunk.", length);
        } else {
Stephane Letz's avatar
Stephane Letz committed
231
            EnqueueMessage(current_frame, length1, data);
232
233
234
235
236
237
238
        }
        // Is this realtime-safe?  This function isn't run in the JACK thread,
        // but we still want it to perform as quickly as possible.  Even if
        // this isn't realtime safe, it may not be avoidable.
        MMRESULT result = midiInAddBuffer(handle, &sysex_header,
                                          sizeof(MIDIHDR));
        if (result != MMSYSERR_NOERROR) {
239
            WriteInError("JackWinMMEInputPort::ProcessWinMME", "midiInAddBuffer",
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
                       result);
        }
        break;
    case MIM_LONGERROR:
        jack_error("JackWinMMEInputPort::ProcessWinMME - Invalid or "
                   "incomplete sysex message received.");
        break;
    case MIM_OPEN:
        jack_info("JackWinMMEInputPort::ProcessWinMME - MIDI device opened.");
    }
}

bool
JackWinMMEInputPort::Start()
{
    if (! started) {
        MMRESULT result = midiInStart(handle);
        started = result == MMSYSERR_NOERROR;
        if (! started) {
259
            WriteInError("JackWinMMEInputPort::Start", "midiInStart", result);
260
261
262
263
264
265
266
267
268
269
270
271
        }
    }
    return started;
}

bool
JackWinMMEInputPort::Stop()
{
    if (started) {
        MMRESULT result = midiInStop(handle);
        started = result != MMSYSERR_NOERROR;
        if (started) {
272
            WriteInError("JackWinMMEInputPort::Stop", "midiInStop", result);
273
274
275
276
277
278
        }
    }
    return ! started;
}

void
279
280
281
282
283
284
285
286
287
288
JackWinMMEInputPort::GetInErrorString(MMRESULT error, LPTSTR text)
{
    MMRESULT result = midiInGetErrorText(error, text, MAXERRORLENGTH);
    if (result != MMSYSERR_NOERROR) {
        snprintf(text, MAXERRORLENGTH, "Unknown error code '%d'", error);
    }
}

void
JackWinMMEInputPort::WriteInError(const char *jack_func, const char *mm_func,
289
290
                                MMRESULT result)
{
Stephane Letz's avatar
Stephane Letz committed
291
    char error_message[MAXERRORLENGTH];
292
    GetInErrorString(result, error_message);
293
294
    jack_error("%s - %s: %s", jack_func, mm_func, error_message);
}
295

sletz's avatar
sletz committed
296