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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
|
/*
Rosegarden
A sequencer and musical notation editor.
This program is Copyright 2000-2008
Guillaume Laurent <glaurent@telegraph-road.org>,
Chris Cannam <cannam@all-day-breakfast.com>,
Richard Bown <bownie@bownie.com>
The moral right of the authors to claim authorship of this work
has been asserted.
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 <iostream>
#include <sstream>
#include "Studio.h"
#include "MidiDevice.h"
#include "AudioDevice.h"
#include "Instrument.h"
#include "Segment.h"
#include "Track.h"
#include "Composition.h"
using std::cerr;
using std::endl;
namespace Rosegarden
{
Studio::Studio() :
m_midiThruFilter(0),
m_midiRecordFilter(0),
m_mixerDisplayOptions(0),
m_metronomeDevice(0)
{
// We _always_ have a buss with id zero, for the master out
m_busses.push_back(new Buss(0));
// And we always create one audio record in
m_recordIns.push_back(new RecordIn());
}
Studio::~Studio()
{
DeviceListIterator dIt = m_devices.begin();
for (; dIt != m_devices.end(); ++dIt)
delete(*dIt);
m_devices.clear();
for (size_t i = 0; i < m_busses.size(); ++i) {
delete m_busses[i];
}
for (size_t i = 0; i < m_recordIns.size(); ++i) {
delete m_recordIns[i];
}
}
void
Studio::addDevice(const std::string &name,
DeviceId id,
Device::DeviceType type)
{
switch(type)
{
case Device::Midi:
m_devices.push_back(new MidiDevice(id, name, MidiDevice::Play));
break;
case Device::Audio:
m_devices.push_back(new AudioDevice(id, name));
break;
default:
std::cerr << "Studio::addDevice() - unrecognised device"
<< endl;
break;
}
}
void
Studio::addDevice(Device *device)
{
m_devices.push_back(device);
}
void
Studio::removeDevice(DeviceId id)
{
DeviceListIterator it;
for (it = m_devices.begin(); it != m_devices.end(); it++)
{
if ((*it)->getId() == id) {
delete *it;
m_devices.erase(it);
return;
}
}
}
InstrumentList
Studio::getAllInstruments()
{
InstrumentList list, subList;
DeviceListIterator it;
// Append lists
//
for (it = m_devices.begin(); it != m_devices.end(); it++)
{
// get sub list
subList = (*it)->getAllInstruments();
// concetenate
list.insert(list.end(), subList.begin(), subList.end());
}
return list;
}
InstrumentList
Studio::getPresentationInstruments()
{
InstrumentList list, subList;
std::vector<Device*>::iterator it;
MidiDevice *midiDevice;
// Append lists
//
for (it = m_devices.begin(); it != m_devices.end(); it++)
{
midiDevice = dynamic_cast<MidiDevice*>(*it);
if (midiDevice)
{
// skip read-only devices
if (midiDevice->getDirection() == MidiDevice::Record)
continue;
}
// get sub list
subList = (*it)->getPresentationInstruments();
// concatenate
list.insert(list.end(), subList.begin(), subList.end());
}
return list;
}
Instrument*
Studio::getInstrumentById(InstrumentId id)
{
std::vector<Device*>::iterator it;
InstrumentList list;
InstrumentList::iterator iit;
for (it = m_devices.begin(); it != m_devices.end(); it++)
{
list = (*it)->getAllInstruments();
for (iit = list.begin(); iit != list.end(); iit++)
if ((*iit)->getId() == id)
return (*iit);
}
return 0;
}
// From a user selection (from a "Presentation" list) return
// the matching Instrument
//
Instrument*
Studio::getInstrumentFromList(int index)
{
std::vector<Device*>::iterator it;
InstrumentList list;
InstrumentList::iterator iit;
int count = 0;
for (it = m_devices.begin(); it != m_devices.end(); it++)
{
MidiDevice *midiDevice = dynamic_cast<MidiDevice*>(*it);
if (midiDevice)
{
// skip read-only devices
if (midiDevice->getDirection() == MidiDevice::Record)
continue;
}
list = (*it)->getPresentationInstruments();
for (iit = list.begin(); iit != list.end(); iit++)
{
if (count == index)
return (*iit);
count++;
}
}
return 0;
}
Instrument *
Studio::getInstrumentFor(Segment *segment)
{
if (!segment) return 0;
if (!segment->getComposition()) return 0;
TrackId tid = segment->getTrack();
Track *track = segment->getComposition()->getTrackById(tid);
if (!track) return 0;
return getInstrumentFor(track);
}
Instrument *
Studio::getInstrumentFor(Track *track)
{
if (!track) return 0;
InstrumentId iid = track->getInstrument();
return getInstrumentById(iid);
}
BussList
Studio::getBusses()
{
return m_busses;
}
Buss *
Studio::getBussById(BussId id)
{
for (BussList::iterator i = m_busses.begin(); i != m_busses.end(); ++i) {
if ((*i)->getId() == id) return *i;
}
return 0;
}
void
Studio::addBuss(Buss *buss)
{
m_busses.push_back(buss);
}
PluginContainer *
Studio::getContainerById(InstrumentId id)
{
PluginContainer *pc = getInstrumentById(id);
if (pc) return pc;
else return getBussById(id);
}
RecordIn *
Studio::getRecordIn(int number)
{
if (number >= 0 && number < int(m_recordIns.size())) return m_recordIns[number];
else return 0;
}
// Clear down the devices - the devices will clear down their
// own Instruments.
//
void
Studio::clear()
{
InstrumentList list;
std::vector<Device*>::iterator it;
for (it = m_devices.begin(); it != m_devices.end(); it++)
delete *it;
m_devices.erase(m_devices.begin(), m_devices.end());
}
std::string
Studio::toXmlString()
{
return toXmlString(std::vector<DeviceId>());
}
std::string
Studio::toXmlString(const std::vector<DeviceId> &devices)
{
std::stringstream studio;
studio << "<studio thrufilter=\"" << m_midiThruFilter
<< "\" recordfilter=\"" << m_midiRecordFilter
<< "\" audioinputpairs=\"" << m_recordIns.size()
<< "\" mixerdisplayoptions=\"" << m_mixerDisplayOptions
<< "\" metronomedevice=\"" << m_metronomeDevice
<< "\">" << endl << endl;
studio << endl;
InstrumentList list;
// Get XML version of devices
//
if (devices.empty()) { // export all devices and busses
for (DeviceListIterator it = m_devices.begin();
it != m_devices.end(); it++) {
studio << (*it)->toXmlString() << endl << endl;
}
for (BussList::iterator it = m_busses.begin();
it != m_busses.end(); ++it) {
studio << (*it)->toXmlString() << endl << endl;
}
} else {
for (std::vector<DeviceId>::const_iterator di(devices.begin());
di != devices.end(); ++di) {
Device *d = getDevice(*di);
if (!d) {
std::cerr << "WARNING: Unknown device id " << (*di)
<< " in Studio::toXmlString" << std::endl;
} else {
studio << d->toXmlString() << endl << endl;
}
}
}
studio << endl << endl;
#if (__GNUC__ < 3)
studio << "</studio>" << endl << std::ends;
#else
studio << "</studio>" << endl;
#endif
return studio.str();
}
// Run through the Devices checking for MidiDevices and
// returning the first Metronome we come across
//
const MidiMetronome*
Studio::getMetronomeFromDevice(DeviceId id)
{
std::vector<Device*>::iterator it;
MidiDevice *midiDevice;
for (it = m_devices.begin(); it != m_devices.end(); it++)
{
midiDevice = dynamic_cast<MidiDevice*>(*it);
if (midiDevice &&
midiDevice->getId() == id &&
midiDevice->getMetronome())
{
return midiDevice->getMetronome();
}
}
return 0;
}
// Scan all MIDI devices for available channels and map
// them to a current program
Instrument*
Studio::assignMidiProgramToInstrument(MidiByte program,
int msb, int lsb,
bool percussion)
{
MidiDevice *midiDevice;
std::vector<Device*>::iterator it;
Rosegarden::InstrumentList::iterator iit;
Rosegarden::InstrumentList instList;
// Instruments that we may return
//
Rosegarden::Instrument *newInstrument = 0;
Rosegarden::Instrument *firstInstrument = 0;
bool needBank = (msb >= 0 || lsb >= 0);
if (needBank) {
if (msb < 0) msb = 0;
if (lsb < 0) lsb = 0;
}
// Pass one - search through all MIDI instruments looking for
// a match that we can re-use. i.e. if we have a matching
// Program Change then we can use this Instrument.
//
for (it = m_devices.begin(); it != m_devices.end(); it++)
{
midiDevice = dynamic_cast<MidiDevice*>(*it);
if (midiDevice && midiDevice->getDirection() == MidiDevice::Play)
{
instList = (*it)->getPresentationInstruments();
for (iit = instList.begin(); iit != instList.end(); iit++)
{
if (firstInstrument == 0)
firstInstrument = *iit;
// If we find an Instrument sending the right program already.
//
if ((*iit)->sendsProgramChange() &&
(*iit)->getProgramChange() == program &&
(!needBank || ((*iit)->sendsBankSelect() &&
(*iit)->getMSB() == msb &&
(*iit)->getLSB() == lsb &&
(*iit)->isPercussion() == percussion)))
{
return (*iit);
}
else
{
// Ignore the program change and use the percussion
// flag.
//
if ((*iit)->isPercussion() && percussion)
{
return (*iit);
}
// Otherwise store the first Instrument for
// possible use later.
//
if (newInstrument == 0 &&
(*iit)->sendsProgramChange() == false &&
(*iit)->sendsBankSelect() == false &&
(*iit)->isPercussion() == percussion)
newInstrument = *iit;
}
}
}
}
// Okay, if we've got this far and we have a new Instrument to use
// then use it.
//
if (newInstrument != 0)
{
newInstrument->setSendProgramChange(true);
newInstrument->setProgramChange(program);
if (needBank) {
newInstrument->setSendBankSelect(true);
newInstrument->setPercussion(percussion);
newInstrument->setMSB(msb);
newInstrument->setLSB(lsb);
}
}
else // Otherwise we just reuse the first Instrument we found
newInstrument = firstInstrument;
return newInstrument;
}
// Just make all of these Instruments available for automatic
// assignment in the assignMidiProgramToInstrument() method
// by invalidating the ProgramChange flag.
//
// This method sounds much more dramatic than it actually is -
// it could probably do with a rename.
//
//
void
Studio::unassignAllInstruments()
{
MidiDevice *midiDevice;
AudioDevice *audioDevice;
std::vector<Device*>::iterator it;
Rosegarden::InstrumentList::iterator iit;
Rosegarden::InstrumentList instList;
int channel = 0;
for (it = m_devices.begin(); it != m_devices.end(); it++)
{
midiDevice = dynamic_cast<MidiDevice*>(*it);
if (midiDevice)
{
instList = (*it)->getPresentationInstruments();
for (iit = instList.begin(); iit != instList.end(); iit++)
{
// Only for true MIDI Instruments - not System ones
//
if ((*iit)->getId() >= MidiInstrumentBase)
{
(*iit)->setSendBankSelect(false);
(*iit)->setSendProgramChange(false);
(*iit)->setMidiChannel(channel);
channel = ( channel + 1 ) % 16;
(*iit)->setSendPan(false);
(*iit)->setSendVolume(false);
(*iit)->setPan(MidiMidValue);
(*iit)->setVolume(100);
}
}
}
else
{
audioDevice = dynamic_cast<AudioDevice*>(*it);
if (audioDevice)
{
instList = (*it)->getPresentationInstruments();
for (iit = instList.begin(); iit != instList.end(); iit++)
(*iit)->emptyPlugins();
}
}
}
}
void
Studio::clearMidiBanksAndPrograms()
{
MidiDevice *midiDevice;
std::vector<Device*>::iterator it;
for (it = m_devices.begin(); it != m_devices.end(); it++)
{
midiDevice = dynamic_cast<MidiDevice*>(*it);
if (midiDevice)
{
midiDevice->clearProgramList();
midiDevice->clearBankList();
}
}
}
void
Studio::clearBusses()
{
for (size_t i = 0; i < m_busses.size(); ++i) {
delete m_busses[i];
}
m_busses.clear();
m_busses.push_back(new Buss(0));
}
void
Studio::clearRecordIns()
{
for (size_t i = 0; i < m_recordIns.size(); ++i) {
delete m_recordIns[i];
}
m_recordIns.clear();
m_recordIns.push_back(new RecordIn());
}
Device*
Studio::getDevice(DeviceId id)
{
//cerr << "Studio[" << this << "]::getDevice(" << id << ")... ";
std::vector<Device*>::iterator it;
for (it = m_devices.begin(); it != m_devices.end(); it++) {
// if (it != m_devices.begin()) cerr << ", ";
// cerr << (*it)->getId();
if ((*it)->getId() == id) {
//cerr << ". Found" << endl;
return (*it);
}
}
//cerr << ". Not found" << endl;
return 0;
}
std::string
Studio::getSegmentName(InstrumentId id)
{
MidiDevice *midiDevice;
std::vector<Device*>::iterator it;
Rosegarden::InstrumentList::iterator iit;
Rosegarden::InstrumentList instList;
for (it = m_devices.begin(); it != m_devices.end(); it++)
{
midiDevice = dynamic_cast<MidiDevice*>(*it);
if (midiDevice)
{
instList = (*it)->getAllInstruments();
for (iit = instList.begin(); iit != instList.end(); iit++)
{
if ((*iit)->getId() == id)
{
if ((*iit)->sendsProgramChange())
{
return (*iit)->getProgramName();
}
else
{
return midiDevice->getName() + " " + (*iit)->getName();
}
}
}
}
}
return std::string("");
}
InstrumentId
Studio::getAudioPreviewInstrument()
{
AudioDevice *audioDevice;
std::vector<Device*>::iterator it;
for (it = m_devices.begin(); it != m_devices.end(); it++)
{
audioDevice = dynamic_cast<AudioDevice*>(*it);
// Just the first one will do - we can make this more
// subtle if we need to later.
//
if (audioDevice)
return audioDevice->getPreviewInstrument();
}
// system instrument - won't accept audio
return 0;
}
bool
Studio::haveMidiDevices() const
{
Rosegarden::DeviceListConstIterator it = m_devices.begin();
for (; it != m_devices.end(); it++)
{
if ((*it)->getType() == Device::Midi) return true;
}
return false;
}
}
|