~mixxxdevelopers/mixxx/mixxx-buildserver

« back to all changes in this revision

Viewing changes to mixxx/src/midi/mididevice.cpp

  • Committer: Albert Santoni
  • Date: 2011-03-20 00:27:15 UTC
  • mfrom: (2607.1.162 mixxx-1.9)
  • Revision ID: alberts@mixxx.org-20110320002715-sa2d88zbuc5kkyya
MergedĀ fromĀ 1.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
    if (m_pMidiMapping == NULL) {
47
47
        m_pMidiMapping = new MidiMapping(this);
48
48
    }
49
 
        
 
49
 
50
50
    // Get --midiDebug command line option
51
51
    QStringList commandLineArgs = QApplication::arguments();
52
52
    m_midiDebug = commandLineArgs.contains("--midiDebug", Qt::CaseInsensitive);
53
 
    
 
53
 
54
54
    connect(m_pMidiMapping, SIGNAL(midiLearningStarted()), this, SLOT(enableMidiLearn()));
55
55
    connect(m_pMidiMapping, SIGNAL(midiLearningFinished()), this, SLOT(disableMidiLearn()));
56
56
}
78
78
void MidiDevice::shutdown()
79
79
{
80
80
    QMutexLocker locker(&m_mappingPtrMutex);
81
 
    //Stop us from processing any MIDI messages that are 
 
81
    //Stop us from processing any MIDI messages that are
82
82
    //received while we're trying to shut down the scripting engine.
83
 
    //This prevents a deadlock that can happen because we've locked 
 
83
    //This prevents a deadlock that can happen because we've locked
84
84
    //the MIDI mapping pointer mutex (in the line above). If a
85
85
    //MIDI message is received while this is locked, the script
86
86
    //engine can end up waiting for the MIDI message to be
87
87
    //mapped and we end up with a deadlock.
88
 
    //Similarly, if a MIDI message is sent from the scripting 
 
88
    //Similarly, if a MIDI message is sent from the scripting
89
89
    //engine while we've held this lock, we'll end up with
90
90
    //a similar deadlock. (Note that shutdownScriptEngine()
91
91
    //waits for the scripting engine thread to terminate,
92
 
    //which will never happen if it's stuck waiting for 
 
92
    //which will never happen if it's stuck waiting for
93
93
    //m_mappingPtrMutex to unlock.
94
94
    setReceiveInhibit(true);
95
95
#ifdef __MIDISCRIPT__
126
126
 
127
127
void MidiDevice::sendSysexMsg(QList<int> data, unsigned int length) {
128
128
    m_mutex.lock();
129
 
    
 
129
 
130
130
    unsigned char * sysexMsg;
131
131
    sysexMsg = new unsigned char [length];
132
132
 
173
173
 
174
174
void MidiDevice::receive(MidiStatusByte status, char channel, char control, char value)
175
175
{
 
176
    // some status bytes can have the channel encoded in them. Take out the
 
177
    // channel when necessary. We do this because later bits of this
 
178
    // function (and perhaps its callchain) assume the channel nibble to be
 
179
    // zero in its comparisons -- bkgood
 
180
    switch (status & 0xF0) {
 
181
    case MIDI_STATUS_NOTE_OFF:
 
182
    case MIDI_STATUS_NOTE_ON:
 
183
    case MIDI_STATUS_AFTERTOUCH:
 
184
    case MIDI_STATUS_CC:
 
185
    case MIDI_STATUS_PROGRAM_CH:
 
186
    case MIDI_STATUS_CH_AFTERTOUCH:
 
187
    case MIDI_STATUS_PITCH_BEND:
 
188
        status = (MidiStatusByte) (status & 0xF0);
 
189
    }
176
190
    QMutexLocker locker(&m_mutex); //Lots of returns in this function. Keeps things simple.
177
191
    if (midiDebugging()) qDebug() << QString("MIDI ch %1: opcode: %2, ctrl: %3, val: %4")
178
192
        .arg(QString::number(channel+1, 16).toUpper())
201
215
    QMutexLocker mappingLocker(&m_mappingPtrMutex);
202
216
 
203
217
    // Only check for a mapping if the status byte is one we know how to handle
204
 
    if (status == MIDI_STATUS_NOTE_ON 
 
218
    if (status == MIDI_STATUS_NOTE_ON
205
219
         || status == MIDI_STATUS_NOTE_OFF
206
 
         || status == MIDI_STATUS_PITCH_BEND 
 
220
         || status == MIDI_STATUS_PITCH_BEND
207
221
         || status == MIDI_STATUS_CC) {
208
222
        // If there was no control bound to that MIDI command, return;
209
223
        if (!m_pMidiMapping->isMidiMessageMapped(inputCommand)) {
218
232
 
219
233
#ifdef __MIDISCRIPT__
220
234
    // Custom MixxxScript (QtScript) handler
221
 
    
 
235
 
222
236
    if (mixxxControl.getMidiOption() == MIDI_OPT_SCRIPT) {
223
 
        // qDebug() << "MidiDevice: Calling script function" << configKey.item << "with" 
 
237
        // qDebug() << "MidiDevice: Calling script function" << configKey.item << "with"
224
238
        //          << (int)channel << (int)control <<  (int)value << (int)status;
225
239
 
226
240
        //Unlock the mutex here to prevent a deadlock if a script needs to send a MIDI message
229
243
 
230
244
        // This needs to be a signal because the MIDI Script Engine thread must execute
231
245
        //  script functions, not this MidiDevice one
232
 
        emit(callMidiScriptFunction(configKey.item, channel, control, value, status, 
 
246
        emit(callMidiScriptFunction(configKey.item, channel, control, value, status,
233
247
                                mixxxControl.getControlObjectGroup()));
234
248
        return;
235
249
    }
239
253
 
240
254
    if (p) //Only pass values on to valid ControlObjects.
241
255
    {
242
 
        double newValue = m_pMidiMapping->ComputeValue(mixxxControl.getMidiOption(), p->GetMidiValue(), value);
243
 
 
244
 
        // ControlPushButton ControlObjects only accept NOTE_ON, so if the midi 
 
256
        double newValue;
 
257
       
 
258
        // compute LSB and MSB for pitch bend messages
 
259
        if (status == MIDI_STATUS_PITCH_BEND) {
 
260
            unsigned int ivalue;
 
261
            ivalue = (value << 7) + control;
 
262
 
 
263
            newValue = m_pMidiMapping->ComputeValue(mixxxControl.getMidiOption(), p->GetMidiValue(), ivalue);
 
264
 
 
265
            // normalize our value to 0-127
 
266
            newValue = (newValue / 0x3FFF) * 0x7F;
 
267
        } else {
 
268
            newValue = m_pMidiMapping->ComputeValue(mixxxControl.getMidiOption(), p->GetMidiValue(), value);
 
269
        }
 
270
 
 
271
        // ControlPushButton ControlObjects only accept NOTE_ON, so if the midi
245
272
        // mapping is <button> we override the Midi 'status' appropriately.
246
273
        switch (mixxxControl.getMidiOption()) {
247
274
            case MIDI_OPT_BUTTON:
248
 
            case MIDI_OPT_SWITCH: status = MIDI_STATUS_NOTE_ON; break; // Buttons and Switches are 
249
 
                                                                       // treated the same, except 
250
 
                                                                       // that their values are 
 
275
            case MIDI_OPT_SWITCH: status = MIDI_STATUS_NOTE_ON; break; // Buttons and Switches are
 
276
                                                                       // treated the same, except
 
277
                                                                       // that their values are
251
278
                                                                       // computed differently.
252
279
            default: break;
253
280
        }
297
324
    ConfigKey configKey(mixxxControl.getControlObjectGroup(), mixxxControl.getControlObjectValue());
298
325
 
299
326
    // Custom MixxxScript (QtScript) handler
300
 
    
 
327
 
301
328
    if (mixxxControl.getMidiOption() == MIDI_OPT_SCRIPT) {
302
 
        // qDebug() << "MidiDevice: Calling script function" << configKey.item << "with" 
 
329
        // qDebug() << "MidiDevice: Calling script function" << configKey.item << "with"
303
330
        //          << (int)channel << (int)control <<  (int)value << (int)status;
304
331
 
305
332
        //Unlock the mutex here to prevent a deadlock if a script needs to send a MIDI message
311
338
        }
312
339
        return;
313
340
    }
314
 
    qDebug() << "MidiDevice: No MIDI Script function found for" << message;
 
341
    qWarning() << "MidiDevice: No MIDI Script function found for" << message;
315
342
    return;
316
343
}
317
344
#endif
328
355
    //See comments for m_bReceiveInhibit.
329
356
    QMutexLocker locker(&m_mutex);
330
357
    m_bReceiveInhibit = inhibit;
331
 
}
 
 
b'\\ No newline at end of file'
 
358
}