/[svn]/qsampler/trunk/src/qsamplerChannel.cpp
ViewVC logotype

Contents of /qsampler/trunk/src/qsamplerChannel.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 961 - (show annotations) (download)
Sun Dec 3 18:26:13 2006 UTC (17 years, 4 months ago) by capela
File size: 25966 byte(s)
- Adding preliminary MIDI instrument mapping support; now
  with an instrument list widget and editing capabilities.

1 // qsamplerChannel.cpp
2 //
3 /****************************************************************************
4 Copyright (C) 2004-2006, rncbc aka Rui Nuno Capela. All rights reserved.
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 *****************************************************************************/
21
22 #include "qsamplerAbout.h"
23 #include "qsamplerChannel.h"
24
25 #include "qsamplerMainForm.h"
26 #include "qsamplerChannelForm.h"
27
28 #include <qfileinfo.h>
29 #include <qcombobox.h>
30
31 #ifdef CONFIG_LIBGIG
32 #include "gig.h"
33 #endif
34
35 #define QSAMPLER_INSTRUMENT_MAX 100
36
37
38 //-------------------------------------------------------------------------
39 // qsamplerChannel - Sampler channel structure.
40 //
41
42 // Constructor.
43 qsamplerChannel::qsamplerChannel ( int iChannelID )
44 {
45 m_iChannelID = iChannelID;
46
47 // m_sEngineName = noEngineName();
48 // m_sInstrumentName = noInstrumentName();
49 // m_sInstrumentFile = m_sInstrumentName;
50 m_iInstrumentNr = -1;
51 m_iInstrumentStatus = -1;
52 m_sMidiDriver = "ALSA";
53 m_iMidiDevice = -1;
54 m_iMidiPort = -1;
55 m_iMidiChannel = -1;
56 m_sAudioDriver = "ALSA";
57 m_iAudioDevice = -1;
58 m_fVolume = 0.0;
59 m_bMute = false;
60 m_bSolo = false;
61 }
62
63 // Default destructor.
64 qsamplerChannel::~qsamplerChannel (void)
65 {
66 }
67
68
69 // Create a new sampler channel, if not already.
70 bool qsamplerChannel::addChannel (void)
71 {
72 qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
73 if (pMainForm == NULL)
74 return false;
75 if (pMainForm->client() == NULL)
76 return false;
77
78 // Are we a new channel?
79 if (m_iChannelID < 0) {
80 m_iChannelID = ::lscp_add_channel(pMainForm->client());
81 if (m_iChannelID < 0) {
82 appendMessagesClient("lscp_add_channel");
83 appendMessagesError(
84 QObject::tr("Could not add channel.\n\nSorry."));
85 } // Otherwise it's created...
86 else appendMessages(QObject::tr("added."));
87 }
88
89 // Return whether we're a valid channel...
90 return (m_iChannelID >= 0);
91 }
92
93
94 // Remove sampler channel.
95 bool qsamplerChannel::removeChannel (void)
96 {
97 qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
98 if (pMainForm == NULL)
99 return false;
100 if (pMainForm->client() == NULL)
101 return false;
102
103 // Are we an existing channel?
104 if (m_iChannelID >= 0) {
105 if (::lscp_remove_channel(pMainForm->client(), m_iChannelID) != LSCP_OK) {
106 appendMessagesClient("lscp_remove_channel");
107 appendMessagesError(QObject::tr("Could not remove channel.\n\nSorry."));
108 } else {
109 // Otherwise it's removed.
110 appendMessages(QObject::tr("removed."));
111 m_iChannelID = -1;
112 }
113 }
114
115 // Return whether we've removed the channel...
116 return (m_iChannelID < 0);
117 }
118
119
120 // Channel-ID (aka Sammpler-Channel) accessors.
121 int qsamplerChannel::channelID (void) const
122 {
123 return m_iChannelID;
124 }
125
126 void qsamplerChannel::setChannelID ( int iChannelID )
127 {
128 m_iChannelID = iChannelID;
129 }
130
131
132 // Readable channel name.
133 QString qsamplerChannel::channelName (void) const
134 {
135 return (m_iChannelID < 0 ? QObject::tr("New Channel") : QObject::tr("Channel %1").arg(m_iChannelID));
136 }
137
138
139 // Engine name accessors.
140 const QString& qsamplerChannel::engineName (void) const
141 {
142 return m_sEngineName;
143 }
144
145 bool qsamplerChannel::loadEngine ( const QString& sEngineName )
146 {
147 qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
148 if (pMainForm == NULL)
149 return false;
150 if (pMainForm->client() == NULL || m_iChannelID < 0)
151 return false;
152 if (m_iInstrumentStatus == 100 && m_sEngineName == sEngineName)
153 return true;
154
155 if (::lscp_load_engine(pMainForm->client(), sEngineName.latin1(), m_iChannelID) != LSCP_OK) {
156 appendMessagesClient("lscp_load_engine");
157 return false;
158 }
159
160 appendMessages(QObject::tr("Engine: %1.").arg(sEngineName));
161
162 m_sEngineName = sEngineName;
163 return true;
164 }
165
166
167 // Instrument filename accessor.
168 const QString& qsamplerChannel::instrumentFile (void) const
169 {
170 return m_sInstrumentFile;
171 }
172
173 // Instrument index accessor.
174 int qsamplerChannel::instrumentNr (void) const
175 {
176 return m_iInstrumentNr;
177 }
178
179 // Instrument name accessor.
180 const QString& qsamplerChannel::instrumentName (void) const
181 {
182 return m_sInstrumentName;
183 }
184
185 // Instrument status accessor.
186 int qsamplerChannel::instrumentStatus (void) const
187 {
188 return m_iInstrumentStatus;
189 }
190
191 // Instrument file loader.
192 bool qsamplerChannel::loadInstrument ( const QString& sInstrumentFile, int iInstrumentNr )
193 {
194 qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
195 if (pMainForm == NULL)
196 return false;
197 if (pMainForm->client() == NULL || m_iChannelID < 0)
198 return false;
199 if (!isInstrumentFile(sInstrumentFile))
200 return false;
201 if (m_iInstrumentStatus == 100 && m_sInstrumentFile == sInstrumentFile && m_iInstrumentNr == iInstrumentNr)
202 return true;
203
204 if (::lscp_load_instrument_non_modal(pMainForm->client(), sInstrumentFile.latin1(), iInstrumentNr, m_iChannelID) != LSCP_OK) {
205 appendMessagesClient("lscp_load_instrument");
206 return false;
207 }
208
209 appendMessages(QObject::tr("Instrument: \"%1\" (%2).")
210 .arg(sInstrumentFile).arg(iInstrumentNr));
211
212 return setInstrument(sInstrumentFile, iInstrumentNr);
213 }
214
215
216 // Special instrument file/name/number settler.
217 bool qsamplerChannel::setInstrument ( const QString& sInstrumentFile, int iInstrumentNr )
218 {
219 m_sInstrumentFile = sInstrumentFile;
220 m_iInstrumentNr = iInstrumentNr;
221 #ifdef CONFIG_INSTRUMENT_NAME
222 m_sInstrumentName = QString::null; // We'll get it, maybe later, on channel_info...
223 #else
224 m_sInstrumentName = getInstrumentName(sInstrumentFile, iInstrumentNr, true);
225 #endif
226 m_iInstrumentStatus = 0;
227
228 return true;
229 }
230
231
232 // MIDI driver type accessors (DEPRECATED).
233 const QString& qsamplerChannel::midiDriver (void) const
234 {
235 return m_sMidiDriver;
236 }
237
238 bool qsamplerChannel::setMidiDriver ( const QString& sMidiDriver )
239 {
240 qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
241 if (pMainForm == NULL)
242 return false;
243 if (pMainForm->client() == NULL || m_iChannelID < 0)
244 return false;
245 if (m_iInstrumentStatus == 100 && m_sMidiDriver == sMidiDriver)
246 return true;
247
248 if (::lscp_set_channel_midi_type(pMainForm->client(), m_iChannelID, sMidiDriver.latin1()) != LSCP_OK) {
249 appendMessagesClient("lscp_set_channel_midi_type");
250 return false;
251 }
252
253 appendMessages(QObject::tr("MIDI driver: %1.").arg(sMidiDriver));
254
255 m_sMidiDriver = sMidiDriver;
256 return true;
257 }
258
259
260 // MIDI device accessors.
261 int qsamplerChannel::midiDevice (void) const
262 {
263 return m_iMidiDevice;
264 }
265
266 bool qsamplerChannel::setMidiDevice ( int iMidiDevice )
267 {
268 qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
269 if (pMainForm == NULL)
270 return false;
271 if (pMainForm->client() == NULL || m_iChannelID < 0)
272 return false;
273 if (m_iInstrumentStatus == 100 && m_iMidiDevice == iMidiDevice)
274 return true;
275
276 if (::lscp_set_channel_midi_device(pMainForm->client(), m_iChannelID, iMidiDevice) != LSCP_OK) {
277 appendMessagesClient("lscp_set_channel_midi_device");
278 return false;
279 }
280
281 appendMessages(QObject::tr("MIDI device: %1.").arg(iMidiDevice));
282
283 m_iMidiDevice = iMidiDevice;
284 return true;
285 }
286
287
288 // MIDI port number accessor.
289 int qsamplerChannel::midiPort (void) const
290 {
291 return m_iMidiPort;
292 }
293
294 bool qsamplerChannel::setMidiPort ( int iMidiPort )
295 {
296 qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
297 if (pMainForm == NULL)
298 return false;
299 if (pMainForm->client() == NULL || m_iChannelID < 0)
300 return false;
301 if (m_iInstrumentStatus == 100 && m_iMidiPort == iMidiPort)
302 return true;
303
304 if (::lscp_set_channel_midi_port(pMainForm->client(), m_iChannelID, iMidiPort) != LSCP_OK) {
305 appendMessagesClient("lscp_set_channel_midi_port");
306 return false;
307 }
308
309 appendMessages(QObject::tr("MIDI port: %1.").arg(iMidiPort));
310
311 m_iMidiPort = iMidiPort;
312 return true;
313 }
314
315
316 // MIDI channel accessor.
317 int qsamplerChannel::midiChannel (void) const
318 {
319 return m_iMidiChannel;
320 }
321
322 bool qsamplerChannel::setMidiChannel ( int iMidiChannel )
323 {
324 qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
325 if (pMainForm == NULL)
326 return false;
327 if (pMainForm->client() == NULL || m_iChannelID < 0)
328 return false;
329 if (m_iInstrumentStatus == 100 && m_iMidiChannel == iMidiChannel)
330 return true;
331
332 if (::lscp_set_channel_midi_channel(pMainForm->client(), m_iChannelID, iMidiChannel) != LSCP_OK) {
333 appendMessagesClient("lscp_set_channel_midi_channel");
334 return false;
335 }
336
337 appendMessages(QObject::tr("MIDI channel: %1.").arg(iMidiChannel));
338
339 m_iMidiChannel = iMidiChannel;
340 return true;
341 }
342
343
344 // Audio device accessor.
345 int qsamplerChannel::audioDevice (void) const
346 {
347 return m_iAudioDevice;
348 }
349
350 bool qsamplerChannel::setAudioDevice ( int iAudioDevice )
351 {
352 qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
353 if (pMainForm == NULL)
354 return false;
355 if (pMainForm->client() == NULL || m_iChannelID < 0)
356 return false;
357 if (m_iInstrumentStatus == 100 && m_iAudioDevice == iAudioDevice)
358 return true;
359
360 if (::lscp_set_channel_audio_device(pMainForm->client(), m_iChannelID, iAudioDevice) != LSCP_OK) {
361 appendMessagesClient("lscp_set_channel_audio_device");
362 return false;
363 }
364
365 appendMessages(QObject::tr("Audio device: %1.").arg(iAudioDevice));
366
367 m_iAudioDevice = iAudioDevice;
368 return true;
369 }
370
371
372 // Audio driver type accessors (DEPRECATED).
373 const QString& qsamplerChannel::audioDriver (void) const
374 {
375 return m_sAudioDriver;
376 }
377
378 bool qsamplerChannel::setAudioDriver ( const QString& sAudioDriver )
379 {
380 qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
381 if (pMainForm == NULL)
382 return false;
383 if (pMainForm->client() == NULL || m_iChannelID < 0)
384 return false;
385 if (m_iInstrumentStatus == 100 && m_sAudioDriver == sAudioDriver)
386 return true;
387
388 if (::lscp_set_channel_audio_type(pMainForm->client(), m_iChannelID, sAudioDriver.latin1()) != LSCP_OK) {
389 appendMessagesClient("lscp_set_channel_audio_type");
390 return false;
391 }
392
393 appendMessages(QObject::tr("Audio driver: %1.").arg(sAudioDriver));
394
395 m_sAudioDriver = sAudioDriver;
396 return true;
397 }
398
399
400 // Channel volume accessors.
401 float qsamplerChannel::volume (void) const
402 {
403 return m_fVolume;
404 }
405
406 bool qsamplerChannel::setVolume ( float fVolume )
407 {
408 qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
409 if (pMainForm == NULL)
410 return false;
411 if (pMainForm->client() == NULL || m_iChannelID < 0)
412 return false;
413 if (m_iInstrumentStatus == 100 && m_fVolume == fVolume)
414 return true;
415
416 if (::lscp_set_channel_volume(pMainForm->client(), m_iChannelID, fVolume) != LSCP_OK) {
417 appendMessagesClient("lscp_set_channel_volume");
418 return false;
419 }
420
421 appendMessages(QObject::tr("Volume: %1.").arg(fVolume));
422
423 m_fVolume = fVolume;
424 return true;
425 }
426
427
428 // Sampler channel mute state.
429 bool qsamplerChannel::channelMute (void) const
430 {
431 return m_bMute;
432 }
433
434 bool qsamplerChannel::setChannelMute ( bool bMute )
435 {
436 qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
437 if (pMainForm == NULL)
438 return false;
439 if (pMainForm->client() == NULL || m_iChannelID < 0)
440 return false;
441 if (m_iInstrumentStatus == 100 && ((m_bMute && bMute) || (!m_bMute && !bMute)))
442 return true;
443
444 #ifdef CONFIG_MUTE_SOLO
445 if (::lscp_set_channel_mute(pMainForm->client(), m_iChannelID, bMute) != LSCP_OK) {
446 appendMessagesClient("lscp_set_channel_mute");
447 return false;
448 }
449 appendMessages(QObject::tr("Mute: %1.").arg((int) bMute));
450 m_bMute = bMute;
451 return true;
452 #else
453 return false;
454 #endif
455 }
456
457
458 // Sampler channel solo state.
459 bool qsamplerChannel::channelSolo (void) const
460 {
461 return m_bSolo;
462 }
463
464 bool qsamplerChannel::setChannelSolo ( bool bSolo )
465 {
466 qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
467 if (pMainForm == NULL)
468 return false;
469 if (pMainForm->client() == NULL || m_iChannelID < 0)
470 return false;
471 if (m_iInstrumentStatus == 100 && ((m_bSolo && bSolo) || (!m_bSolo && !bSolo)))
472 return true;
473
474 #ifdef CONFIG_MUTE_SOLO
475 if (::lscp_set_channel_solo(pMainForm->client(), m_iChannelID, bSolo) != LSCP_OK) {
476 appendMessagesClient("lscp_set_channel_solo");
477 return false;
478 }
479 appendMessages(QObject::tr("Solo: %1.").arg((int) bSolo));
480 m_bSolo = bSolo;
481 return true;
482 #else
483 return false;
484 #endif
485 }
486
487
488 // Audio routing accessors.
489 int qsamplerChannel::audioChannel ( int iAudioOut ) const
490 {
491 return m_audioRouting[iAudioOut];
492 }
493
494 bool qsamplerChannel::setAudioChannel ( int iAudioOut, int iAudioIn )
495 {
496 qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
497 if (pMainForm == NULL)
498 return false;
499 if (pMainForm->client() == NULL || m_iChannelID < 0)
500 return false;
501 if (m_iInstrumentStatus == 100 &&
502 m_audioRouting[iAudioOut] == iAudioIn)
503 return true;
504
505 if (::lscp_set_channel_audio_channel(pMainForm->client(),
506 m_iChannelID, iAudioOut, iAudioIn) != LSCP_OK) {
507 appendMessagesClient("lscp_set_channel_audio_channel");
508 return false;
509 }
510
511 appendMessages(QObject::tr("Audio Channel: %1 -> %2.")
512 .arg(iAudioOut).arg(iAudioIn));
513
514 m_audioRouting[iAudioOut] = iAudioIn;
515 return true;
516 }
517
518 // The audio routing map itself.
519 const qsamplerChannelRoutingMap& qsamplerChannel::audioRouting (void) const
520 {
521 return m_audioRouting;
522 }
523
524
525 // Istrument name remapper.
526 void qsamplerChannel::updateInstrumentName (void)
527 {
528 #ifndef CONFIG_INSTRUMENT_NAME
529 m_sInstrumentName = getInstrumentName(m_sInstrumentFile,
530 m_iInstrumentNr, (options() && options()->bInstrumentNames));
531 #endif
532 }
533
534
535 // Update whole channel info state.
536 bool qsamplerChannel::updateChannelInfo (void)
537 {
538 qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
539 if (pMainForm == NULL)
540 return false;
541 if (pMainForm->client() == NULL || m_iChannelID < 0)
542 return false;
543
544 // Read channel information.
545 lscp_channel_info_t *pChannelInfo = ::lscp_get_channel_info(pMainForm->client(), m_iChannelID);
546 if (pChannelInfo == NULL) {
547 appendMessagesClient("lscp_get_channel_info");
548 appendMessagesError(QObject::tr("Could not get channel information.\n\nSorry."));
549 return false;
550 }
551
552 #ifdef CONFIG_INSTRUMENT_NAME
553 // We got all actual instrument datum...
554 m_sInstrumentFile = pChannelInfo->instrument_file;
555 m_iInstrumentNr = pChannelInfo->instrument_nr;
556 m_sInstrumentName = pChannelInfo->instrument_name;
557 #else
558 // First, check if intrument name has changed,
559 // taking care that instrument name lookup might be expensive,
560 // so we better make it only once and when really needed...
561 if ((m_sInstrumentFile != pChannelInfo->instrument_file) ||
562 (m_iInstrumentNr != pChannelInfo->instrument_nr)) {
563 m_sInstrumentFile = pChannelInfo->instrument_file;
564 m_iInstrumentNr = pChannelInfo->instrument_nr;
565 updateInstrumentName();
566 }
567 #endif
568 // Cache in other channel information.
569 m_sEngineName = pChannelInfo->engine_name;
570 m_iInstrumentStatus = pChannelInfo->instrument_status;
571 m_iMidiDevice = pChannelInfo->midi_device;
572 m_iMidiPort = pChannelInfo->midi_port;
573 m_iMidiChannel = pChannelInfo->midi_channel;
574 m_iAudioDevice = pChannelInfo->audio_device;
575 m_fVolume = pChannelInfo->volume;
576 #ifdef CONFIG_MUTE_SOLO
577 m_bMute = pChannelInfo->mute;
578 m_bSolo = pChannelInfo->solo;
579 #endif
580 // Some sanity checks.
581 if (m_sEngineName == "NONE" || m_sEngineName.isEmpty())
582 m_sEngineName = QString::null;
583 if (m_sInstrumentFile == "NONE" || m_sInstrumentFile.isEmpty()) {
584 m_sInstrumentFile = QString::null;
585 m_sInstrumentName = QString::null;
586 }
587
588 // Time for device info grabbing...
589 lscp_device_info_t *pDeviceInfo;
590 const QString sNone = QObject::tr("(none)");
591 // Audio device driver type.
592 pDeviceInfo = ::lscp_get_audio_device_info(pMainForm->client(), m_iAudioDevice);
593 if (pDeviceInfo == NULL) {
594 appendMessagesClient("lscp_get_audio_device_info");
595 m_sAudioDriver = sNone;
596 } else {
597 m_sAudioDriver = pDeviceInfo->driver;
598 }
599 // MIDI device driver type.
600 pDeviceInfo = ::lscp_get_midi_device_info(pMainForm->client(), m_iMidiDevice);
601 if (pDeviceInfo == NULL) {
602 appendMessagesClient("lscp_get_midi_device_info");
603 m_sMidiDriver = sNone;
604 } else {
605 m_sMidiDriver = pDeviceInfo->driver;
606 }
607
608 // Set the audio routing map.
609 m_audioRouting.clear();
610 char **ppszRouting = pChannelInfo->audio_routing;
611 for (int i = 0; ppszRouting && ppszRouting[i]; i++) {
612 m_audioRouting[i] = ::atoi(ppszRouting[i]);
613 }
614
615 return true;
616 }
617
618
619 // Reset channel method.
620 bool qsamplerChannel::channelReset (void)
621 {
622 qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
623 if (pMainForm == NULL)
624 return false;
625 if (pMainForm->client() == NULL || m_iChannelID < 0)
626 return false;
627
628 if (::lscp_reset_channel(pMainForm->client(), m_iChannelID) != LSCP_OK) {
629 appendMessagesClient("lscp_reset_channel");
630 return false;
631 }
632
633 appendMessages(QObject::tr("reset."));
634
635 return true;
636 }
637
638
639 // Channel setup dialog form.
640 bool qsamplerChannel::channelSetup ( QWidget *pParent )
641 {
642 qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
643 if (pMainForm == NULL)
644 return false;
645
646 bool bResult = false;
647
648 appendMessages(QObject::tr("setup..."));
649
650 qsamplerChannelForm *pChannelForm = new qsamplerChannelForm(pParent);
651 if (pChannelForm) {
652 pChannelForm->setup(this);
653 bResult = pChannelForm->exec();
654 delete pChannelForm;
655 }
656
657 return bResult;
658 }
659
660
661 // Redirected messages output methods.
662 void qsamplerChannel::appendMessages( const QString& s ) const
663 {
664 qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
665 if (pMainForm)
666 pMainForm->appendMessages(channelName() + ' ' + s);
667 }
668
669 void qsamplerChannel::appendMessagesColor( const QString& s,
670 const QString& c ) const
671 {
672 qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
673 if (pMainForm)
674 pMainForm->appendMessagesColor(channelName() + ' ' + s, c);
675 }
676
677 void qsamplerChannel::appendMessagesText( const QString& s ) const
678 {
679 qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
680 if (pMainForm)
681 pMainForm->appendMessagesText(channelName() + ' ' + s);
682 }
683
684 void qsamplerChannel::appendMessagesError( const QString& s ) const
685 {
686 qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
687 if (pMainForm)
688 pMainForm->appendMessagesError(channelName() + "\n\n" + s);
689 }
690
691 void qsamplerChannel::appendMessagesClient( const QString& s ) const
692 {
693 qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
694 if (pMainForm)
695 pMainForm->appendMessagesClient(channelName() + ' ' + s);
696 }
697
698
699 // Context menu event handler.
700 void qsamplerChannel::contextMenuEvent( QContextMenuEvent *pEvent )
701 {
702 qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
703 if (pMainForm)
704 pMainForm->contextMenuEvent(pEvent);
705 }
706
707
708 // FIXME: Check whether a given file is an instrument file.
709 bool qsamplerChannel::isInstrumentFile ( const QString& sInstrumentFile )
710 {
711 bool bResult = false;
712
713 QFile file(sInstrumentFile);
714 if (file.open(IO_ReadOnly)) {
715 char achHeader[16];
716 if (file.readBlock(achHeader, 16)) {
717 bResult = (::memcmp(&achHeader[0], "RIFF", 4) == 0
718 && ::memcmp(&achHeader[8], "DLS LIST", 8) == 0);
719 }
720 file.close();
721 }
722
723 return bResult;
724 }
725
726
727 // Retrieve the instrument list of a instrument file (.gig).
728 QStringList qsamplerChannel::getInstrumentList( const QString& sInstrumentFile,
729 bool bInstrumentNames )
730 {
731 QString sInstrumentName = QFileInfo(sInstrumentFile).fileName();
732 QStringList instlist;
733
734 if (isInstrumentFile(sInstrumentFile)) {
735 #ifdef CONFIG_LIBGIG
736 if (bInstrumentNames) {
737 RIFF::File *pRiff = new RIFF::File(sInstrumentFile.latin1());
738 gig::File *pGig = new gig::File(pRiff);
739 gig::Instrument *pInstrument = pGig->GetFirstInstrument();
740 while (pInstrument) {
741 instlist.append((pInstrument->pInfo)->Name.c_str());
742 pInstrument = pGig->GetNextInstrument();
743 }
744 delete pGig;
745 delete pRiff;
746 }
747 else
748 #endif
749 for (int iInstrumentNr = 0; iInstrumentNr < QSAMPLER_INSTRUMENT_MAX; iInstrumentNr++)
750 instlist.append(sInstrumentName + " [" + QString::number(iInstrumentNr) + "]");
751 }
752 else instlist.append(noInstrumentName());
753
754 return instlist;
755 }
756
757
758 // Retrieve the spacific instrument name of a instrument file (.gig), given its index.
759 QString qsamplerChannel::getInstrumentName( const QString& sInstrumentFile,
760 int iInstrumentNr, bool bInstrumentNames )
761 {
762 QString sInstrumentName;
763
764 if (isInstrumentFile(sInstrumentFile)) {
765 sInstrumentName = QFileInfo(sInstrumentFile).fileName();
766 #ifdef CONFIG_LIBGIG
767 if (bInstrumentNames) {
768 RIFF::File *pRiff = new RIFF::File(sInstrumentFile.latin1());
769 gig::File *pGig = new gig::File(pRiff);
770 int iIndex = 0;
771 gig::Instrument *pInstrument = pGig->GetFirstInstrument();
772 while (pInstrument) {
773 if (iIndex == iInstrumentNr) {
774 sInstrumentName = (pInstrument->pInfo)->Name.c_str();
775 break;
776 }
777 iIndex++;
778 pInstrument = pGig->GetNextInstrument();
779 }
780 delete pGig;
781 delete pRiff;
782 }
783 else
784 #endif
785 sInstrumentName += " [" + QString::number(iInstrumentNr) + "]";
786 }
787 else sInstrumentName = noInstrumentName();
788
789 return sInstrumentName;
790 }
791
792
793 // Common invalid name-helpers.
794 QString qsamplerChannel::noEngineName (void)
795 {
796 return QObject::tr("(No engine)");
797 }
798
799 QString qsamplerChannel::noInstrumentName (void)
800 {
801 return QObject::tr("(No instrument)");
802 }
803
804 QString qsamplerChannel::loadingInstrument (void) {
805 return QObject::tr("(Loading instrument...)");
806 }
807
808
809
810 //-------------------------------------------------------------------------
811 // qsamplerChannelRoutingTable - Channel routing table.
812 //
813
814 // Constructor.
815 qsamplerChannelRoutingTable::qsamplerChannelRoutingTable (
816 QWidget *pParent, const char *pszName )
817 : QTable(pParent, pszName)
818 {
819 // Set fixed number of columns.
820 QTable::setNumCols(2);
821 QTable::setShowGrid(false);
822 QTable::setSorting(false);
823 QTable::setFocusStyle(QTable::FollowStyle);
824 QTable::setSelectionMode(QTable::NoSelection);
825 // No vertical header.
826 QTable::verticalHeader()->hide();
827 QTable::setLeftMargin(0);
828 // Initialize the fixed table column headings.
829 QHeader *pHeader = QTable::horizontalHeader();
830 pHeader->setLabel(0, tr("Sampler Channel"));
831 pHeader->setLabel(1, tr("Device Channel"));
832 // Set read-onlyness of each column
833 QTable::setColumnReadOnly(0, true);
834 // QTable::setColumnReadOnly(1, false); -- of course not.
835 QTable::setColumnStretchable(1, true);
836 }
837
838 // Default destructor.
839 qsamplerChannelRoutingTable::~qsamplerChannelRoutingTable (void)
840 {
841 }
842
843
844 // Routing map table renderer.
845 void qsamplerChannelRoutingTable::refresh ( qsamplerDevice *pDevice,
846 const qsamplerChannelRoutingMap& routing )
847 {
848 if (pDevice == NULL)
849 return;
850
851 // Always (re)start it empty.
852 QTable::setUpdatesEnabled(false);
853 QTable::setNumRows(0);
854
855 // The common device port item list.
856 QStringList opts;
857 qsamplerDevicePortList& ports = pDevice->ports();
858 qsamplerDevicePort *pPort;
859 for (pPort = ports.first(); pPort; pPort = ports.next()) {
860 opts.append(pDevice->deviceTypeName()
861 + ' ' + pDevice->driverName()
862 + ' ' + pPort->portName());
863 }
864
865 // Those items shall have a proper pixmap...
866 QPixmap pmChannel = QPixmap::fromMimeSource("qsamplerChannel.png");
867 QPixmap pmDevice;
868 switch (pDevice->deviceType()) {
869 case qsamplerDevice::Audio:
870 pmDevice = QPixmap::fromMimeSource("audio2.png");
871 break;
872 case qsamplerDevice::Midi:
873 pmDevice = QPixmap::fromMimeSource("midi2.png");
874 break;
875 case qsamplerDevice::None:
876 break;
877 }
878
879 // Fill the routing table...
880 QTable::insertRows(0, routing.count());
881 int iRow = 0;
882 qsamplerChannelRoutingMap::ConstIterator iter;
883 for (iter = routing.begin(); iter != routing.end(); ++iter) {
884 QTable::setPixmap(iRow, 0, pmChannel);
885 QTable::setText(iRow, 0, pDevice->deviceTypeName()
886 + ' ' + QString::number(iter.key()));
887 qsamplerChannelRoutingComboBox *pComboItem =
888 new qsamplerChannelRoutingComboBox(this, opts, pmDevice);
889 pComboItem->setCurrentItem(iter.data());
890 QTable::setItem(iRow, 1, pComboItem);
891 ++iRow;
892 }
893
894 // Adjust optimal column widths.
895 QTable::adjustColumn(0);
896 QTable::adjustColumn(1);
897
898 QTable::setUpdatesEnabled(true);
899 QTable::updateContents();
900 }
901
902
903 // Commit any pending editing.
904 void qsamplerChannelRoutingTable::flush (void)
905 {
906 if (QTable::isEditing())
907 QTable::endEdit(QTable::currEditRow(), QTable::currEditCol(), true, true);
908 }
909
910
911 //-------------------------------------------------------------------------
912 // qsamplerChannelRoutingComboBox - Custom combo box for routing table.
913 //
914
915 // Constructor.
916 qsamplerChannelRoutingComboBox::qsamplerChannelRoutingComboBox (
917 QTable *pTable, const QStringList& list, const QPixmap& pixmap )
918 : QTableItem(pTable, QTableItem::WhenCurrent, QString::null, pixmap),
919 m_list(list)
920 {
921 m_iCurrentItem = 0;
922 }
923
924 // Public accessors.
925 void qsamplerChannelRoutingComboBox::setCurrentItem ( int iCurrentItem )
926 {
927 m_iCurrentItem = iCurrentItem;
928
929 QTableItem::setText(m_list[iCurrentItem]);
930 }
931
932 int qsamplerChannelRoutingComboBox::currentItem (void) const
933 {
934 return m_iCurrentItem;
935 }
936
937 // Virtual implemetations.
938 QWidget *qsamplerChannelRoutingComboBox::createEditor (void) const
939 {
940 QComboBox *pComboBox = new QComboBox(QTableItem::table()->viewport());
941 QObject::connect(pComboBox, SIGNAL(activated(int)),
942 QTableItem::table(), SLOT(doValueChanged()));
943 for (QStringList::ConstIterator iter = m_list.begin();
944 iter != m_list.end(); iter++) {
945 pComboBox->insertItem(QTableItem::pixmap(), *iter);
946 }
947 pComboBox->setCurrentItem(m_iCurrentItem);
948 return pComboBox;
949 }
950
951 void qsamplerChannelRoutingComboBox::setContentFromEditor ( QWidget *pWidget )
952 {
953 if (pWidget->inherits("QComboBox")) {
954 QComboBox *pComboBox = (QComboBox *) pWidget;
955 m_iCurrentItem = pComboBox->currentItem();
956 QTableItem::setText(pComboBox->currentText());
957 }
958 else QTableItem::setContentFromEditor(pWidget);
959 }
960
961
962 // end of qsamplerChannel.cpp

  ViewVC Help
Powered by ViewVC