summaryrefslogtreecommitdiffstats
path: root/src/gui/studio/StudioControl.cpp
blob: 04a838c1c232a23ab1de9cf40b08b7e17f7e23f7 (plain)
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */

/*
    Rosegarden
    A MIDI and audio sequencer and musical notation editor.
 
    This program is Copyright 2000-2008
        Guillaume Laurent   <[email protected]>,
        Chris Cannam        <[email protected]>,
        Richard Bown        <[email protected]>
 
    The moral rights of Guillaume Laurent, Chris Cannam, and Richard
    Bown to claim authorship of this work have been asserted.
 
    Other copyrights also apply to some parts of this work.  Please
    see the AUTHORS file and individual file headers for details.
 
    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.  See the file
    COPYING included with this distribution for more information.
*/


#include "StudioControl.h"

#include "sound/Midi.h"
#include "misc/Debug.h"
#include "base/MidiProgram.h"
#include "base/Profiler.h"
#include "base/RealTime.h"
#include "gui/application/RosegardenApplication.h"
#include "sound/MappedCommon.h"
#include "sound/MappedComposition.h"
#include "sound/MappedEvent.h"
#include "sound/MappedInstrument.h"
#include "sound/MappedStudio.h"
#include <tqcstring.h>
#include <tqdatastream.h>
#include <tqstring.h>


namespace Rosegarden
{

MappedObjectId
StudioControl::createStudioObject(MappedObject::MappedObjectType type)
{
Profiler profiler("StudioControl::createStudioObject", true);

int value = -1;
TQByteArray data;
TQCString replyType;
TQByteArray replyData;
TQDataStream streamOut(data, IO_WriteOnly);

streamOut << (int)type;

if (!rgapp->sequencerCall("createMappedObject(int)",
                          replyType, replyData, data))
{
    SETQMAN_DEBUG << "createStudioObject - "
    << "failed to contact Rosegarden sequencer"
    << endl;
} else
{
    TQDataStream streamIn(replyData, IO_ReadOnly);
    streamIn >> value;
}

return value;
}

bool
StudioControl::destroyStudioObject(MappedObjectId id)
{
    Profiler profiler("StudioControl::destroyStudioObject", true);

    int value = 0;
    TQByteArray data;
    TQCString replyType;
    TQByteArray replyData;
    TQDataStream streamOut(data, IO_WriteOnly);

    streamOut << int(id);

    if (!rgapp->sequencerCall("destroyMappedObject(int)",
                              replyType, replyData, data)) {
        SETQMAN_DEBUG << "destroyStudioObject - "
        << "failed to contact Rosegarden sequencer"
        << endl;
    } else {
        TQDataStream streamIn(replyData, IO_ReadOnly);
        streamIn >> value;
    }

    if (value == 1)
        return true;
    else
        return false;
}

MappedObjectPropertyList
StudioControl::getStudioObjectProperty(MappedObjectId id,
                                       const MappedObjectProperty &property)
{
    Profiler profiler("StudioControl::getStudioObjectProperty", true);

    MappedObjectPropertyList list;

    TQByteArray data;
    TQCString replyType;
    TQByteArray replyData;
    TQDataStream streamOut(data, IO_WriteOnly);

    streamOut << (int)id;
    streamOut << TQString(property);

    if (!rgapp->sequencerCall("getPropertyList(int, TQString)",
                              replyType, replyData, data)) {
        SETQMAN_DEBUG << "getStudioObjectProperty - "
        << "failed to contact Rosegarden sequencer"
        << endl;
    } else {
        TQDataStream streamIn(replyData, IO_ReadOnly);
        streamIn >> list;
    }

    return list;
}

bool
StudioControl::setStudioObjectProperty(MappedObjectId id,
                                       const MappedObjectProperty &property,
                                       MappedObjectValue value)
{
    Profiler profiler("StudioControl::setStudioObjectProperty(float)", true);

    TQByteArray data;
    TQDataStream streamOut(data, IO_WriteOnly);

    streamOut << (int)id;
    streamOut << (TQString)property;
    streamOut << (float)value;

    rgapp->sequencerSend("setMappedProperty(int, TQString, float)", data);

    return true;
}

bool
StudioControl::setStudioObjectProperties(const MappedObjectIdList &ids,
        const MappedObjectPropertyList &properties,
        const MappedObjectValueList &values)
{
    Profiler profiler("StudioControl::setStudioObjectProperties(floats)", true);

    TQByteArray data;
    TQDataStream streamOut(data, IO_WriteOnly);

    streamOut << ids;
    streamOut << properties;
    streamOut << values;

    rgapp->sequencerSend("setMappedProperties(MappedObjectIdList, MappedObjectPropertyList, MappedObjectValueList)", data);

    return true;
}

bool
StudioControl::setStudioObjectProperty(MappedObjectId id,
                                       const MappedObjectProperty &property,
                                       const TQString &value)
{
    Profiler profiler("StudioControl::setStudioObjectProperty(string)", true);

    TQByteArray data;
    TQDataStream streamOut(data, IO_WriteOnly);

    streamOut << (int)id;
    streamOut << (TQString)property;
    streamOut << (TQString)value;

    rgapp->sequencerSend("setMappedProperty(int, TQString, TQString)", data);

    return true;
}

bool
StudioControl::setStudioObjectPropertyList(MappedObjectId id,
        const MappedObjectProperty &property,
        const MappedObjectPropertyList &values)
{
    Profiler profiler("StudioControl::setStudioObjectPropertyList", true);

    TQByteArray data;
    TQDataStream streamOut(data, IO_WriteOnly);

    streamOut << (int)id;
    streamOut << (TQString)property;
    streamOut << values;

    RG_DEBUG << "StudioControl::setStudioObjectPropertyList: " << values.size() << " values for property " << property << endl;

    rgapp->sequencerSend("setMappedPropertyList(int, TQString, MappedObjectPropertyList)",
                         data);

    return true;
}

MappedObjectId
StudioControl::getStudioObjectByType(MappedObject::MappedObjectType type)
{
    Profiler profiler("StudioControl::getStudioObjectByType", true);

    int value = -1;
    TQByteArray data;
    TQCString replyType;
    TQByteArray replyData;
    TQDataStream streamOut(data, IO_WriteOnly);

    streamOut << (int)type;

    if (!rgapp->sequencerCall("getMappedObjectId(int)",
                              replyType, replyData, data)) {
        SETQMAN_DEBUG << "getStudioObjectByType - "
        << "failed to contact Rosegarden sequencer"
        << endl;
    } else {
        TQDataStream streamIn(replyData, IO_ReadOnly);
        streamIn >> value;
    }

    return value;
}

void
StudioControl::setStudioPluginPort(MappedObjectId pluginId,
                                   unsigned long portId,
                                   MappedObjectValue value)
{
    Profiler profiler("StudioControl::setStudioPluginPort", true);

    TQByteArray data;
    TQDataStream streamOut(data, IO_WriteOnly);

    // Use new MappedEvent interface
    //
    streamOut << (int)pluginId;
    streamOut << (unsigned long)portId;
    streamOut << (float)value;

    rgapp->sequencerSend("setMappedPort(int, unsigned long int, float)", data);
}

MappedObjectValue
StudioControl::getStudioPluginPort(MappedObjectId pluginId,
                                   unsigned long portId)
{
    Profiler profiler("StudioControl::getStudioPluginPort", true);

    float value = 0.0;
    TQByteArray data;
    TQCString replyType;
    TQByteArray replyData;
    TQDataStream streamOut(data, IO_WriteOnly);

    streamOut << (int)pluginId;
    streamOut << (unsigned long)portId;

    if (!rgapp->sequencerCall("getMappedPort(int, unsigned long int)",
                              replyType, replyData, data)) {
        SETQMAN_DEBUG << "getStudioPluginPort - "
        << "failed to contact Rosegarden sequencer"
        << endl;
    } else {
        TQDataStream streamIn(replyData, IO_ReadOnly);
        streamIn >> value;
    }

    return value;
}

MappedObjectPropertyList
StudioControl::getPluginInformation()
{
    MappedObjectPropertyList list;

    TQByteArray data;
    TQCString replyType;
    TQByteArray replyData;

    if (!rgapp->sequencerCall("getPluginInformation()",
                              replyType, replyData, data)) {
        SETQMAN_DEBUG << "getPluginInformation - "
        << "failed to contact Rosegarden sequencer"
        << endl;
    } else {
        TQDataStream streamIn(replyData, IO_ReadOnly);
        streamIn >> list;
    }

    return list;
}

TQString
StudioControl::getPluginProgram(MappedObjectId id, int bank, int program)
{
    TQByteArray data;
    TQCString replyType;
    TQByteArray replyData;
    TQDataStream streamOut(data, IO_WriteOnly);

    streamOut << (int)id;
    streamOut << (int)bank;
    streamOut << (int)program;

    TQString programName;

    if (!rgapp->sequencerCall("getPluginProgram(int, int, int)",
                              replyType, replyData, data)) {
        SETQMAN_DEBUG << "getPluginProgram - "
        << "failed to contact Rosegarden sequencer"
        << endl;
    } else {
        TQDataStream streamIn(replyData, IO_ReadOnly);
        streamIn >> programName;
    }

    return programName;
}

unsigned long
StudioControl::getPluginProgram(MappedObjectId id, TQString name)
{
    TQByteArray data;
    TQCString replyType;
    TQByteArray replyData;
    TQDataStream streamOut(data, IO_WriteOnly);

    streamOut << (int)id;
    streamOut << name;

    unsigned long rv;

    if (!rgapp->sequencerCall("getPluginProgram(int, TQString)",
                              replyType, replyData, data)) {
        SETQMAN_DEBUG << "getPluginProgram - "
        << "failed to contact Rosegarden sequencer"
        << endl;
    } else {
        TQDataStream streamIn(replyData, IO_ReadOnly);
        streamIn >> rv;
    }

    return rv;
}

void
StudioControl::connectStudioObjects(MappedObjectId id1,
                                    MappedObjectId id2)
{
    Profiler profiler("StudioControl::connectStudioObjects", true);

    TQByteArray data;
    TQDataStream streamOut(data, IO_WriteOnly);

    streamOut << (int)id1;
    streamOut << (int)id2;

    if (!rgapp->sequencerSend("connectMappedObjects(int, int)", data)) {
        SETQMAN_DEBUG << "connectStudioObjects - "
        << "failed to contact Rosegarden sequencer"
        << endl;
    }

    return ;
}

void
StudioControl::disconnectStudioObjects(MappedObjectId id1,
                                       MappedObjectId id2)
{
    Profiler profiler("StudioControl::disconnectStudioObjects", true);

    TQByteArray data;
    TQDataStream streamOut(data, IO_WriteOnly);

    streamOut << (int)id1;
    streamOut << (int)id2;

    if (!rgapp->sequencerSend("disconnectMappedObjects(int, int)", data)) {
        SETQMAN_DEBUG << "disconnectStudioObjects - "
        << "failed to contact Rosegarden sequencer"
        << endl;
    }

    return ;
}

void
StudioControl::disconnectStudioObject(MappedObjectId id)
{
    Profiler profiler("StudioControl::disconnectStudioObject", true);

    TQByteArray data;
    TQDataStream streamOut(data, IO_WriteOnly);

    streamOut << (int)id;

    if (!rgapp->sequencerSend("disconnectMappedObject(int)", data)) {
        SETQMAN_DEBUG << "disconnectStudioObject - "
        << "failed to contact Rosegarden sequencer"
        << endl;
    }

    return ;
}

void
StudioControl::sendMappedEvent(const MappedEvent &mE)
{
    Profiler profiler("StudioControl::sendMappedEvent", true);

    static MappedEvent mEs;

    mEs = mE; // just in case the passed mapped event has dubious
    // origins and taking its address isn't safe

    mEs.setPersistent(true); // to avoid that MappedComposition dtor try to free it

    MappedComposition mC;
    mC.insert(&mEs);
    StudioControl::sendMappedComposition(mC);
}

void
StudioControl::sendMappedComposition(const MappedComposition &mC)
{
    Profiler profiler("StudioControl::sendMappedComposition", true);

    if (mC.size() == 0)
        return ;

    TQCString replyType;
    TQByteArray replyData;

    MappedComposition::const_iterator it = mC.begin();

    for (; it != mC.end(); it++) {
        TQByteArray data;
        TQDataStream streamOut(data, IO_WriteOnly);

        streamOut << (*it);
        rgapp->sequencerSend("processMappedEvent(MappedEvent)", data);
    }
}

void
StudioControl::sendMappedInstrument(const MappedInstrument &mI)
{
    Profiler profiler("StudioControl::sendMappedInstrument", true);

    TQByteArray data;
    TQDataStream streamOut(data, IO_WriteOnly);

    streamOut << (int)mI.getType();
    streamOut << (unsigned char)mI.getChannel();
    streamOut << (unsigned int)mI.getId();

    rgapp->sequencerSend("setMappedInstrument(int, unsigned char, unsigned int)", data);
}

void
StudioControl::sendQuarterNoteLength(const RealTime &length)
{
    Profiler profiler("StudioControl::sendQuarterNoteLength", true);

    TQByteArray data;
    TQDataStream streamOut(data, IO_WriteOnly);

    streamOut << (long)length.sec;
    streamOut << (long)length.nsec;

    rgapp->sequencerSend("setQuarterNoteLength(long int, long int)", data);
}

void
StudioControl::sendRPN(InstrumentId instrumentId,
                       MidiByte paramMSB,
                       MidiByte paramLSB,
                       MidiByte /* controller */,
                       MidiByte value)
{
    Profiler profiler("StudioControl::sendRPN", true);

    MappedComposition mC;
    MappedEvent *mE =
        new MappedEvent(instrumentId,
                        MappedEvent::MidiController,
                        MIDI_CONTROLLER_RPN_2,
                        paramMSB);
    mC.insert(mE);

    mE = new MappedEvent(instrumentId,
                         MappedEvent::MidiController,
                         MIDI_CONTROLLER_RPN_1,
                         paramLSB);
    mC.insert(mE);

    mE = new MappedEvent(instrumentId,
                         MappedEvent::MidiController,
                         6,  // data value changed
                         value);
    mC.insert(mE);


    // Null the controller using - this is "best practice"
    //
    mE = new MappedEvent(instrumentId,
                         MappedEvent::MidiController,
                         MIDI_CONTROLLER_RPN_2,
                         MidiMaxValue); // null
    mC.insert(mE);

    mE = new MappedEvent(instrumentId,
                         MappedEvent::MidiController,
                         MIDI_CONTROLLER_RPN_1,
                         MidiMaxValue); // null
    mC.insert(mE);


    StudioControl::sendMappedComposition(mC);
}

void
StudioControl::sendNRPN(InstrumentId instrumentId,
                        MidiByte paramMSB,
                        MidiByte paramLSB,
                        MidiByte /* controller */,
                        MidiByte value)
{
    Profiler profiler("StudioControl::sendNRPN", true);

    MappedComposition mC;
    MappedEvent *mE =
        new MappedEvent(instrumentId,
                        MappedEvent::MidiController,
                        MIDI_CONTROLLER_NRPN_2,
                        paramMSB);
    mC.insert(mE);

    mE = new MappedEvent(instrumentId,
                         MappedEvent::MidiController,
                         MIDI_CONTROLLER_NRPN_1,
                         paramLSB);
    mC.insert(mE);

    mE = new MappedEvent(instrumentId,
                         MappedEvent::MidiController,
                         6,  // data value changed
                         value);
    mC.insert(mE);


    // Null the controller using - this is "best practice"
    //
    mE = new MappedEvent(instrumentId,
                         MappedEvent::MidiController,
                         MIDI_CONTROLLER_RPN_2,
                         MidiMaxValue); // null
    mC.insert(mE);

    mE = new MappedEvent(instrumentId,
                         MappedEvent::MidiController,
                         MIDI_CONTROLLER_RPN_1,
                         MidiMaxValue); // null
    mC.insert(mE);
}

}