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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2387 - (show annotations) (download)
Sat Dec 29 00:21:11 2012 UTC (5 years, 7 months ago) by capela
File size: 27540 byte(s)
* Preparations for Qt5 migration. (TESTING)
1 // qsamplerChannel.cpp
2 //
3 /****************************************************************************
4 Copyright (C) 2004-2012, rncbc aka Rui Nuno Capela. All rights reserved.
5 Copyright (C) 2007, 2008 Christian Schoenebeck
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 *****************************************************************************/
22
23 #include "qsamplerAbout.h"
24 #include "qsamplerChannel.h"
25 #include "qsamplerUtilities.h"
26
27 #include "qsamplerMainForm.h"
28 #include "qsamplerChannelForm.h"
29
30 #include <QFileInfo>
31 #include <QComboBox>
32
33 #ifdef CONFIG_LIBGIG
34 #include "gig.h"
35 #endif
36
37 namespace QSampler {
38
39 #define QSAMPLER_INSTRUMENT_MAX 100
40
41 #define UNICODE_RIGHT_ARROW QChar(char(0x92), char(0x21))
42
43
44 //-------------------------------------------------------------------------
45 // QSampler::Channel - Sampler channel structure.
46 //
47
48 // Constructor.
49 Channel::Channel ( int iChannelID )
50 {
51 m_iChannelID = iChannelID;
52
53 // m_sEngineName = noEngineName();
54 // m_sInstrumentName = noInstrumentName();
55 // m_sInstrumentFile = m_sInstrumentName;
56 m_iInstrumentNr = -1;
57 m_iInstrumentStatus = -1;
58 m_sMidiDriver = "ALSA";
59 m_iMidiDevice = -1;
60 m_iMidiPort = -1;
61 m_iMidiChannel = -1;
62 m_iMidiMap = -1;
63 m_sAudioDriver = "ALSA";
64 m_iAudioDevice = -1;
65 m_fVolume = 0.0f;
66 m_bMute = false;
67 m_bSolo = false;
68 }
69
70 // Default destructor.
71 Channel::~Channel (void)
72 {
73 }
74
75
76 // Create a new sampler channel, if not already.
77 bool Channel::addChannel (void)
78 {
79 MainForm* pMainForm = MainForm::getInstance();
80 if (pMainForm == NULL)
81 return false;
82 if (pMainForm->client() == NULL)
83 return false;
84
85 // Are we a new channel?
86 if (m_iChannelID < 0) {
87 m_iChannelID = ::lscp_add_channel(pMainForm->client());
88 if (m_iChannelID < 0) {
89 appendMessagesClient("lscp_add_channel");
90 appendMessagesError(
91 QObject::tr("Could not add channel.\n\nSorry."));
92 } // Otherwise it's created...
93 else appendMessages(QObject::tr("added."));
94 }
95
96 // Return whether we're a valid channel...
97 return (m_iChannelID >= 0);
98 }
99
100
101 // Remove sampler channel.
102 bool Channel::removeChannel (void)
103 {
104 MainForm *pMainForm = MainForm::getInstance();
105 if (pMainForm == NULL)
106 return false;
107 if (pMainForm->client() == NULL)
108 return false;
109
110 // Are we an existing channel?
111 if (m_iChannelID >= 0) {
112 if (::lscp_remove_channel(pMainForm->client(), m_iChannelID) != LSCP_OK) {
113 appendMessagesClient("lscp_remove_channel");
114 appendMessagesError(QObject::tr("Could not remove channel.\n\nSorry."));
115 } else {
116 // Otherwise it's removed.
117 appendMessages(QObject::tr("removed."));
118 m_iChannelID = -1;
119 }
120 }
121
122 // Return whether we've removed the channel...
123 return (m_iChannelID < 0);
124 }
125
126
127 // Channel-ID (aka Sammpler-Channel) accessors.
128 int Channel::channelID (void) const
129 {
130 return m_iChannelID;
131 }
132
133 void Channel::setChannelID ( int iChannelID )
134 {
135 m_iChannelID = iChannelID;
136 }
137
138
139 // Readable channel name.
140 QString Channel::channelName (void) const
141 {
142 return (m_iChannelID < 0 ? QObject::tr("New Channel") : QObject::tr("Channel %1").arg(m_iChannelID));
143 }
144
145
146 // Engine name accessors.
147 const QString& Channel::engineName (void) const
148 {
149 return m_sEngineName;
150 }
151
152 bool Channel::loadEngine ( const QString& sEngineName )
153 {
154 MainForm *pMainForm = MainForm::getInstance();
155 if (pMainForm == NULL)
156 return false;
157 if (pMainForm->client() == NULL || m_iChannelID < 0)
158 return false;
159 if (m_iInstrumentStatus == 100 && m_sEngineName == sEngineName)
160 return true;
161
162 if (::lscp_load_engine(pMainForm->client(),
163 sEngineName.toUtf8().constData(), m_iChannelID) != LSCP_OK) {
164 appendMessagesClient("lscp_load_engine");
165 return false;
166 }
167
168 appendMessages(QObject::tr("Engine: %1.").arg(sEngineName));
169
170 m_sEngineName = sEngineName;
171 return true;
172 }
173
174
175 // Instrument filename accessor.
176 const QString& Channel::instrumentFile (void) const
177 {
178 return m_sInstrumentFile;
179 }
180
181 // Instrument index accessor.
182 int Channel::instrumentNr (void) const
183 {
184 return m_iInstrumentNr;
185 }
186
187 // Instrument name accessor.
188 const QString& Channel::instrumentName (void) const
189 {
190 return m_sInstrumentName;
191 }
192
193 // Instrument status accessor.
194 int Channel::instrumentStatus (void) const
195 {
196 return m_iInstrumentStatus;
197 }
198
199 // Instrument file loader.
200 bool Channel::loadInstrument ( const QString& sInstrumentFile, int iInstrumentNr )
201 {
202 MainForm *pMainForm = MainForm::getInstance();
203 if (pMainForm == NULL)
204 return false;
205 if (pMainForm->client() == NULL || m_iChannelID < 0)
206 return false;
207 if (!QFileInfo(sInstrumentFile).exists())
208 return false;
209 if (m_iInstrumentStatus == 100
210 && m_sInstrumentFile == sInstrumentFile
211 && m_iInstrumentNr == iInstrumentNr)
212 return true;
213
214 if (::lscp_load_instrument_non_modal(
215 pMainForm->client(),
216 qsamplerUtilities::lscpEscapePath(
217 sInstrumentFile).toUtf8().constData(),
218 iInstrumentNr, m_iChannelID
219 ) != LSCP_OK) {
220 appendMessagesClient("lscp_load_instrument");
221 return false;
222 }
223
224 appendMessages(QObject::tr("Instrument: \"%1\" (%2).")
225 .arg(sInstrumentFile).arg(iInstrumentNr));
226
227 return setInstrument(sInstrumentFile, iInstrumentNr);
228 }
229
230
231 // Special instrument file/name/number settler.
232 bool Channel::setInstrument ( const QString& sInstrumentFile, int iInstrumentNr )
233 {
234 m_sInstrumentFile = sInstrumentFile;
235 m_iInstrumentNr = iInstrumentNr;
236 #ifdef CONFIG_INSTRUMENT_NAME
237 m_sInstrumentName = QString::null; // We'll get it, maybe later, on channel_info...
238 #else
239 m_sInstrumentName = getInstrumentName(sInstrumentFile, iInstrumentNr, true);
240 #endif
241 m_iInstrumentStatus = 0;
242
243 return true;
244 }
245
246
247 // MIDI driver type accessors (DEPRECATED).
248 const QString& Channel::midiDriver (void) const
249 {
250 return m_sMidiDriver;
251 }
252
253 bool Channel::setMidiDriver ( const QString& sMidiDriver )
254 {
255 MainForm *pMainForm = MainForm::getInstance();
256 if (pMainForm == NULL)
257 return false;
258 if (pMainForm->client() == NULL || m_iChannelID < 0)
259 return false;
260 if (m_iInstrumentStatus == 100 && m_sMidiDriver == sMidiDriver)
261 return true;
262
263 if (::lscp_set_channel_midi_type(pMainForm->client(),
264 m_iChannelID, sMidiDriver.toUtf8().constData()) != LSCP_OK) {
265 appendMessagesClient("lscp_set_channel_midi_type");
266 return false;
267 }
268
269 appendMessages(QObject::tr("MIDI driver: %1.").arg(sMidiDriver));
270
271 m_sMidiDriver = sMidiDriver;
272 return true;
273 }
274
275
276 // MIDI device accessors.
277 int Channel::midiDevice (void) const
278 {
279 return m_iMidiDevice;
280 }
281
282 bool Channel::setMidiDevice ( int iMidiDevice )
283 {
284 MainForm *pMainForm = MainForm::getInstance();
285 if (pMainForm == NULL)
286 return false;
287 if (pMainForm->client() == NULL || m_iChannelID < 0)
288 return false;
289 if (m_iInstrumentStatus == 100 && m_iMidiDevice == iMidiDevice)
290 return true;
291
292 if (::lscp_set_channel_midi_device(pMainForm->client(), m_iChannelID, iMidiDevice) != LSCP_OK) {
293 appendMessagesClient("lscp_set_channel_midi_device");
294 return false;
295 }
296
297 appendMessages(QObject::tr("MIDI device: %1.").arg(iMidiDevice));
298
299 m_iMidiDevice = iMidiDevice;
300 return true;
301 }
302
303
304 // MIDI port number accessor.
305 int Channel::midiPort (void) const
306 {
307 return m_iMidiPort;
308 }
309
310 bool Channel::setMidiPort ( int iMidiPort )
311 {
312 MainForm *pMainForm = MainForm::getInstance();
313 if (pMainForm == NULL)
314 return false;
315 if (pMainForm->client() == NULL || m_iChannelID < 0)
316 return false;
317 if (m_iInstrumentStatus == 100 && m_iMidiPort == iMidiPort)
318 return true;
319
320 if (::lscp_set_channel_midi_port(pMainForm->client(), m_iChannelID, iMidiPort) != LSCP_OK) {
321 appendMessagesClient("lscp_set_channel_midi_port");
322 return false;
323 }
324
325 appendMessages(QObject::tr("MIDI port: %1.").arg(iMidiPort));
326
327 m_iMidiPort = iMidiPort;
328 return true;
329 }
330
331
332 // MIDI channel accessor.
333 int Channel::midiChannel (void) const
334 {
335 return m_iMidiChannel;
336 }
337
338 bool Channel::setMidiChannel ( int iMidiChannel )
339 {
340 MainForm *pMainForm = MainForm::getInstance();
341 if (pMainForm == NULL)
342 return false;
343 if (pMainForm->client() == NULL || m_iChannelID < 0)
344 return false;
345 if (m_iInstrumentStatus == 100 && m_iMidiChannel == iMidiChannel)
346 return true;
347
348 if (::lscp_set_channel_midi_channel(pMainForm->client(), m_iChannelID, iMidiChannel) != LSCP_OK) {
349 appendMessagesClient("lscp_set_channel_midi_channel");
350 return false;
351 }
352
353 appendMessages(QObject::tr("MIDI channel: %1.").arg(iMidiChannel));
354
355 m_iMidiChannel = iMidiChannel;
356 return true;
357 }
358
359
360 // MIDI instrument map accessor.
361 int Channel::midiMap (void) const
362 {
363 return m_iMidiMap;
364 }
365
366 bool Channel::setMidiMap ( int iMidiMap )
367 {
368 MainForm *pMainForm = MainForm::getInstance();
369 if (pMainForm == NULL)
370 return false;
371 if (pMainForm->client() == NULL || m_iChannelID < 0)
372 return false;
373 if (m_iInstrumentStatus == 100 && m_iMidiMap == iMidiMap)
374 return true;
375 #ifdef CONFIG_MIDI_INSTRUMENT
376 if (::lscp_set_channel_midi_map(pMainForm->client(), m_iChannelID, iMidiMap) != LSCP_OK) {
377 appendMessagesClient("lscp_set_channel_midi_map");
378 return false;
379 }
380 #endif
381 appendMessages(QObject::tr("MIDI map: %1.").arg(iMidiMap));
382
383 m_iMidiMap = iMidiMap;
384 return true;
385 }
386
387
388 // Audio device accessor.
389 int Channel::audioDevice (void) const
390 {
391 return m_iAudioDevice;
392 }
393
394 bool Channel::setAudioDevice ( int iAudioDevice )
395 {
396 MainForm *pMainForm = MainForm::getInstance();
397 if (pMainForm == NULL)
398 return false;
399 if (pMainForm->client() == NULL || m_iChannelID < 0)
400 return false;
401 if (m_iInstrumentStatus == 100 && m_iAudioDevice == iAudioDevice)
402 return true;
403
404 if (::lscp_set_channel_audio_device(pMainForm->client(), m_iChannelID, iAudioDevice) != LSCP_OK) {
405 appendMessagesClient("lscp_set_channel_audio_device");
406 return false;
407 }
408
409 appendMessages(QObject::tr("Audio device: %1.").arg(iAudioDevice));
410
411 m_iAudioDevice = iAudioDevice;
412 return true;
413 }
414
415
416 // Audio driver type accessors (DEPRECATED).
417 const QString& Channel::audioDriver (void) const
418 {
419 return m_sAudioDriver;
420 }
421
422 bool Channel::setAudioDriver ( const QString& sAudioDriver )
423 {
424 MainForm *pMainForm = MainForm::getInstance();
425 if (pMainForm == NULL)
426 return false;
427 if (pMainForm->client() == NULL || m_iChannelID < 0)
428 return false;
429 if (m_iInstrumentStatus == 100 && m_sAudioDriver == sAudioDriver)
430 return true;
431
432 if (::lscp_set_channel_audio_type(pMainForm->client(),
433 m_iChannelID, sAudioDriver.toUtf8().constData()) != LSCP_OK) {
434 appendMessagesClient("lscp_set_channel_audio_type");
435 return false;
436 }
437
438 appendMessages(QObject::tr("Audio driver: %1.").arg(sAudioDriver));
439
440 m_sAudioDriver = sAudioDriver;
441 return true;
442 }
443
444
445 // Channel volume accessors.
446 float Channel::volume (void) const
447 {
448 return m_fVolume;
449 }
450
451 bool Channel::setVolume ( float fVolume )
452 {
453 MainForm *pMainForm = MainForm::getInstance();
454 if (pMainForm == NULL)
455 return false;
456 if (pMainForm->client() == NULL || m_iChannelID < 0)
457 return false;
458 if (m_iInstrumentStatus == 100 && m_fVolume == fVolume)
459 return true;
460
461 if (::lscp_set_channel_volume(pMainForm->client(), m_iChannelID, fVolume) != LSCP_OK) {
462 appendMessagesClient("lscp_set_channel_volume");
463 return false;
464 }
465
466 appendMessages(QObject::tr("Volume: %1.").arg(fVolume));
467
468 m_fVolume = fVolume;
469 return true;
470 }
471
472
473 // Sampler channel mute state.
474 bool Channel::channelMute (void) const
475 {
476 return m_bMute;
477 }
478
479 bool Channel::setChannelMute ( bool bMute )
480 {
481 MainForm *pMainForm = MainForm::getInstance();
482 if (pMainForm == NULL)
483 return false;
484 if (pMainForm->client() == NULL || m_iChannelID < 0)
485 return false;
486 if (m_iInstrumentStatus == 100 && ((m_bMute && bMute) || (!m_bMute && !bMute)))
487 return true;
488
489 #ifdef CONFIG_MUTE_SOLO
490 if (::lscp_set_channel_mute(pMainForm->client(), m_iChannelID, bMute) != LSCP_OK) {
491 appendMessagesClient("lscp_set_channel_mute");
492 return false;
493 }
494 appendMessages(QObject::tr("Mute: %1.").arg((int) bMute));
495 m_bMute = bMute;
496 return true;
497 #else
498 return false;
499 #endif
500 }
501
502
503 // Sampler channel solo state.
504 bool Channel::channelSolo (void) const
505 {
506 return m_bSolo;
507 }
508
509 bool Channel::setChannelSolo ( bool bSolo )
510 {
511 MainForm *pMainForm = MainForm::getInstance();
512 if (pMainForm == NULL)
513 return false;
514 if (pMainForm->client() == NULL || m_iChannelID < 0)
515 return false;
516 if (m_iInstrumentStatus == 100 && ((m_bSolo && bSolo) || (!m_bSolo && !bSolo)))
517 return true;
518
519 #ifdef CONFIG_MUTE_SOLO
520 if (::lscp_set_channel_solo(pMainForm->client(), m_iChannelID, bSolo) != LSCP_OK) {
521 appendMessagesClient("lscp_set_channel_solo");
522 return false;
523 }
524 appendMessages(QObject::tr("Solo: %1.").arg((int) bSolo));
525 m_bSolo = bSolo;
526 return true;
527 #else
528 return false;
529 #endif
530 }
531
532
533 // Audio routing accessors.
534 int Channel::audioChannel ( int iAudioOut ) const
535 {
536 return m_audioRouting[iAudioOut];
537 }
538
539 bool Channel::setAudioChannel ( int iAudioOut, int iAudioIn )
540 {
541 MainForm *pMainForm = MainForm::getInstance();
542 if (pMainForm == NULL)
543 return false;
544 if (pMainForm->client() == NULL || m_iChannelID < 0)
545 return false;
546 if (m_iInstrumentStatus == 100 &&
547 m_audioRouting[iAudioOut] == iAudioIn)
548 return true;
549
550 if (::lscp_set_channel_audio_channel(pMainForm->client(),
551 m_iChannelID, iAudioOut, iAudioIn) != LSCP_OK) {
552 appendMessagesClient("lscp_set_channel_audio_channel");
553 return false;
554 }
555
556 appendMessages(QObject::tr("Audio Channel: %1 -> %2.")
557 .arg(iAudioOut).arg(iAudioIn));
558
559 m_audioRouting[iAudioOut] = iAudioIn;
560 return true;
561 }
562
563 // The audio routing map itself.
564 const ChannelRoutingMap& Channel::audioRouting (void) const
565 {
566 return m_audioRouting;
567 }
568
569
570 // Istrument name remapper.
571 void Channel::updateInstrumentName (void)
572 {
573 #ifndef CONFIG_INSTRUMENT_NAME
574 m_sInstrumentName = getInstrumentName(m_sInstrumentFile,
575 m_iInstrumentNr, (options() && options()->bInstrumentNames));
576 #endif
577 }
578
579
580 // Update whole channel info state.
581 bool Channel::updateChannelInfo (void)
582 {
583 MainForm *pMainForm = MainForm::getInstance();
584 if (pMainForm == NULL)
585 return false;
586 if (pMainForm->client() == NULL || m_iChannelID < 0)
587 return false;
588
589 // Read channel information.
590 lscp_channel_info_t *pChannelInfo = ::lscp_get_channel_info(pMainForm->client(), m_iChannelID);
591 if (pChannelInfo == NULL) {
592 appendMessagesClient("lscp_get_channel_info");
593 appendMessagesError(QObject::tr("Could not get channel information.\n\nSorry."));
594 return false;
595 }
596
597 #ifdef CONFIG_INSTRUMENT_NAME
598 // We got all actual instrument datum...
599 m_sInstrumentFile =
600 qsamplerUtilities::lscpEscapedPathToPosix(pChannelInfo->instrument_file);
601 m_iInstrumentNr = pChannelInfo->instrument_nr;
602 m_sInstrumentName =
603 qsamplerUtilities::lscpEscapedTextToRaw(pChannelInfo->instrument_name);
604 #else
605 // First, check if intrument name has changed,
606 // taking care that instrument name lookup might be expensive,
607 // so we better make it only once and when really needed...
608 if ((m_sInstrumentFile != pChannelInfo->instrument_file) ||
609 (m_iInstrumentNr != pChannelInfo->instrument_nr)) {
610 m_sInstrumentFile = pChannelInfo->instrument_file;
611 m_iInstrumentNr = pChannelInfo->instrument_nr;
612 updateInstrumentName();
613 }
614 #endif
615 // Cache in other channel information.
616 m_sEngineName = pChannelInfo->engine_name;
617 m_iInstrumentStatus = pChannelInfo->instrument_status;
618 m_iMidiDevice = pChannelInfo->midi_device;
619 m_iMidiPort = pChannelInfo->midi_port;
620 m_iMidiChannel = pChannelInfo->midi_channel;
621 #ifdef CONFIG_MIDI_INSTRUMENT
622 m_iMidiMap = pChannelInfo->midi_map;
623 #endif
624 m_iAudioDevice = pChannelInfo->audio_device;
625 m_fVolume = pChannelInfo->volume;
626 #ifdef CONFIG_MUTE_SOLO
627 m_bMute = pChannelInfo->mute;
628 m_bSolo = pChannelInfo->solo;
629 #endif
630 // Some sanity checks.
631 if (m_sEngineName == "NONE" || m_sEngineName.isEmpty())
632 m_sEngineName = QString::null;
633 if (m_sInstrumentFile == "NONE" || m_sInstrumentFile.isEmpty()) {
634 m_sInstrumentFile = QString::null;
635 m_sInstrumentName = QString::null;
636 }
637
638 // Time for device info grabbing...
639 lscp_device_info_t *pDeviceInfo;
640 const QString sNone = QObject::tr("(none)");
641 // Audio device driver type.
642 pDeviceInfo = ::lscp_get_audio_device_info(pMainForm->client(), m_iAudioDevice);
643 if (pDeviceInfo == NULL) {
644 appendMessagesClient("lscp_get_audio_device_info");
645 m_sAudioDriver = sNone;
646 } else {
647 m_sAudioDriver = pDeviceInfo->driver;
648 }
649 // MIDI device driver type.
650 pDeviceInfo = ::lscp_get_midi_device_info(pMainForm->client(), m_iMidiDevice);
651 if (pDeviceInfo == NULL) {
652 appendMessagesClient("lscp_get_midi_device_info");
653 m_sMidiDriver = sNone;
654 } else {
655 m_sMidiDriver = pDeviceInfo->driver;
656 }
657
658 // Set the audio routing map.
659 m_audioRouting.clear();
660 #ifdef CONFIG_AUDIO_ROUTING
661 int *piAudioRouting = pChannelInfo->audio_routing;
662 for (int i = 0; piAudioRouting && piAudioRouting[i] >= 0; i++)
663 m_audioRouting[i] = piAudioRouting[i];
664 #else
665 char **ppszAudioRouting = pChannelInfo->audio_routing;
666 for (int i = 0; ppszAudioRouting && ppszAudioRouting[i]; i++)
667 m_audioRouting[i] = ::atoi(ppszAudioRouting[i]);
668 #endif
669
670 return true;
671 }
672
673
674 // Reset channel method.
675 bool Channel::channelReset (void)
676 {
677 MainForm *pMainForm = MainForm::getInstance();
678 if (pMainForm == NULL)
679 return false;
680 if (pMainForm->client() == NULL || m_iChannelID < 0)
681 return false;
682
683 if (::lscp_reset_channel(pMainForm->client(), m_iChannelID) != LSCP_OK) {
684 appendMessagesClient("lscp_reset_channel");
685 return false;
686 }
687
688 appendMessages(QObject::tr("reset."));
689
690 return true;
691 }
692
693
694 // Spawn instrument editor method.
695 bool Channel::editChannel (void)
696 {
697 #ifdef CONFIG_EDIT_INSTRUMENT
698
699 MainForm *pMainForm = MainForm::getInstance();
700 if (pMainForm == NULL)
701 return false;
702 if (pMainForm->client() == NULL || m_iChannelID < 0)
703 return false;
704
705 if (::lscp_edit_channel_instrument(pMainForm->client(), m_iChannelID)
706 != LSCP_OK) {
707 appendMessagesClient("lscp_edit_channel_instrument");
708 appendMessagesError(QObject::tr(
709 "Could not launch an appropriate instrument editor "
710 "for the given instrument!\n\n"
711 "Make sure you have an appropriate "
712 "instrument editor like 'gigedit' installed "
713 "and that it placed its mandatory DLL file "
714 "into the sampler's plugin directory.")
715 );
716 return false;
717 }
718
719 appendMessages(QObject::tr("edit instrument."));
720
721 return true;
722
723 #else
724
725 appendMessagesError(QObject::tr(
726 "Sorry, QSampler was compiled for a version of liblscp "
727 "which lacks this feature.\n\n"
728 "You may want to update liblscp and recompile QSampler afterwards.")
729 );
730
731 return false;
732
733 #endif
734 }
735
736
737 // Channel setup dialog form.
738 bool Channel::channelSetup ( QWidget *pParent )
739 {
740 MainForm *pMainForm = MainForm::getInstance();
741 if (pMainForm == NULL)
742 return false;
743
744 bool bResult = false;
745
746 appendMessages(QObject::tr("setup..."));
747
748 ChannelForm *pChannelForm = new ChannelForm(pParent);
749 if (pChannelForm) {
750 pChannelForm->setup(this);
751 bResult = pChannelForm->exec();
752 delete pChannelForm;
753 }
754
755 return bResult;
756 }
757
758
759 // Redirected messages output methods.
760 void Channel::appendMessages( const QString& s ) const
761 {
762 MainForm *pMainForm = MainForm::getInstance();
763 if (pMainForm)
764 pMainForm->appendMessages(channelName() + ' ' + s);
765 }
766
767 void Channel::appendMessagesColor( const QString& s,
768 const QString& c ) const
769 {
770 MainForm *pMainForm = MainForm::getInstance();
771 if (pMainForm)
772 pMainForm->appendMessagesColor(channelName() + ' ' + s, c);
773 }
774
775 void Channel::appendMessagesText( const QString& s ) const
776 {
777 MainForm *pMainForm = MainForm::getInstance();
778 if (pMainForm)
779 pMainForm->appendMessagesText(channelName() + ' ' + s);
780 }
781
782 void Channel::appendMessagesError( const QString& s ) const
783 {
784 MainForm *pMainForm = MainForm::getInstance();
785 if (pMainForm)
786 pMainForm->appendMessagesError(channelName() + "\n\n" + s);
787 }
788
789 void Channel::appendMessagesClient( const QString& s ) const
790 {
791 MainForm *pMainForm = MainForm::getInstance();
792 if (pMainForm)
793 pMainForm->appendMessagesClient(channelName() + ' ' + s);
794 }
795
796
797 // Context menu event handler.
798 void Channel::contextMenuEvent( QContextMenuEvent *pEvent )
799 {
800 MainForm *pMainForm = MainForm::getInstance();
801 if (pMainForm)
802 pMainForm->contextMenuEvent(pEvent);
803 }
804
805
806 // FIXME: Check whether a given file is an instrument file (DLS only).
807 bool Channel::isDlsInstrumentFile ( const QString& sInstrumentFile )
808 {
809 bool bResult = false;
810
811 QFile file(sInstrumentFile);
812 if (file.open(QIODevice::ReadOnly)) {
813 char achHeader[16];
814 if (file.read(achHeader, 16) > 0) {
815 bResult = (::memcmp(&achHeader[0], "RIFF", 4) == 0
816 && ::memcmp(&achHeader[8], "DLS LIST", 8) == 0);
817 }
818 file.close();
819 }
820
821 return bResult;
822 }
823
824
825 // Retrieve the instrument list of a instrument file (.gig).
826 QStringList Channel::getInstrumentList( const QString& sInstrumentFile,
827 bool bInstrumentNames )
828 {
829 QStringList instlist;
830
831 if (isDlsInstrumentFile(sInstrumentFile)) {
832 #ifdef CONFIG_LIBGIG
833 if (bInstrumentNames) {
834 RIFF::File *pRiff
835 = new RIFF::File(sInstrumentFile.toUtf8().constData());
836 gig::File *pGig = new gig::File(pRiff);
837 #if HAVE_LIBGIG_SETAUTOLOAD
838 // prevent sleepy response time on large .gig files
839 pGig->SetAutoLoad(false);
840 #endif
841 gig::Instrument *pInstrument = pGig->GetFirstInstrument();
842 while (pInstrument) {
843 instlist.append((pInstrument->pInfo)->Name.c_str());
844 pInstrument = pGig->GetNextInstrument();
845 }
846 delete pGig;
847 delete pRiff;
848 }
849 #endif
850 }
851
852 if (instlist.isEmpty()) {
853 QFileInfo fi(sInstrumentFile);
854 if (fi.exists()) {
855 QString sInstrumentName = fi.fileName();
856 int iInstrumentNr = 0;
857 while (iInstrumentNr < QSAMPLER_INSTRUMENT_MAX) {
858 instlist.append(sInstrumentName
859 + " [" + QString::number(++iInstrumentNr) + "]");
860 }
861 }
862 else instlist.append(noInstrumentName());
863 }
864
865 return instlist;
866 }
867
868
869 // Retrieve the spacific instrument name of a instrument file (.gig), given its index.
870 QString Channel::getInstrumentName( const QString& sInstrumentFile,
871 int iInstrumentNr, bool bInstrumentNames )
872 {
873 QString sInstrumentName;
874
875 if (isDlsInstrumentFile(sInstrumentFile)) {
876 #ifdef CONFIG_LIBGIG
877 if (bInstrumentNames) {
878 RIFF::File *pRiff
879 = new RIFF::File(sInstrumentFile.toUtf8().constData());
880 gig::File *pGig = new gig::File(pRiff);
881 #if HAVE_LIBGIG_SETAUTOLOAD
882 // prevent sleepy response time on large .gig files
883 pGig->SetAutoLoad(false);
884 #endif
885 int iIndex = 0;
886 gig::Instrument *pInstrument = pGig->GetFirstInstrument();
887 while (pInstrument) {
888 if (iIndex == iInstrumentNr) {
889 sInstrumentName = (pInstrument->pInfo)->Name.c_str();
890 break;
891 }
892 iIndex++;
893 pInstrument = pGig->GetNextInstrument();
894 }
895 delete pGig;
896 delete pRiff;
897 }
898 #endif
899 }
900
901 if (sInstrumentName.isEmpty()) {
902 QFileInfo fi(sInstrumentFile);
903 if (fi.exists()) {
904 sInstrumentName = fi.fileName();
905 sInstrumentName += " [" + QString::number(iInstrumentNr) + "]";
906 }
907 else sInstrumentName = noInstrumentName();
908 }
909
910 return sInstrumentName;
911 }
912
913
914 // Common invalid name-helpers.
915 QString Channel::noEngineName (void)
916 {
917 return QObject::tr("(No engine)");
918 }
919
920 QString Channel::noInstrumentName (void)
921 {
922 return QObject::tr("(No instrument)");
923 }
924
925 QString Channel::loadingInstrument (void) {
926 return QObject::tr("(Loading instrument...)");
927 }
928
929
930 //-------------------------------------------------------------------------
931 // QSampler::ChannelRoutingModel - data model for audio routing
932 // (used for QTableView)
933
934 ChannelRoutingModel::ChannelRoutingModel ( QObject *pParent )
935 : QAbstractTableModel(pParent), m_pDevice(NULL)
936 {
937 }
938
939
940 int ChannelRoutingModel::rowCount ( const QModelIndex& /*parent*/) const
941 {
942 return (m_pDevice) ? m_routing.size() : 0;
943 }
944
945
946 int ChannelRoutingModel::columnCount ( const QModelIndex& /*parent*/) const
947 {
948 return 1;
949 }
950
951
952 Qt::ItemFlags ChannelRoutingModel::flags ( const QModelIndex& /*index*/) const
953 {
954 return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled;
955 }
956
957
958 bool ChannelRoutingModel::setData ( const QModelIndex& index,
959 const QVariant& value, int /*role*/)
960 {
961 if (!index.isValid())
962 return false;
963
964 m_routing[index.row()] = value.toInt();
965
966 emit dataChanged(index, index);
967 return true;
968 }
969
970
971 QVariant ChannelRoutingModel::data ( const QModelIndex &index, int role ) const
972 {
973 if (!index.isValid())
974 return QVariant();
975 if (role != Qt::DisplayRole)
976 return QVariant();
977 if (index.column() != 0)
978 return QVariant();
979
980 ChannelRoutingItem item;
981
982 // The common device port item list.
983 DevicePortList& ports = m_pDevice->ports();
984 QListIterator<DevicePort *> iter(ports);
985 while (iter.hasNext()) {
986 DevicePort *pPort = iter.next();
987 item.options.append(
988 m_pDevice->deviceTypeName()
989 + ' ' + m_pDevice->driverName()
990 + ' ' + pPort->portName()
991 );
992 }
993
994 item.selection = m_routing[index.row()];
995
996 return QVariant::fromValue(item);
997 }
998
999
1000 QVariant ChannelRoutingModel::headerData ( int section,
1001 Qt::Orientation orientation, int role) const
1002 {
1003 if (role != Qt::DisplayRole)
1004 return QVariant();
1005
1006 switch (orientation) {
1007 case Qt::Horizontal:
1008 return UNICODE_RIGHT_ARROW + QObject::tr(" Device Channel");
1009 case Qt::Vertical:
1010 return QObject::tr("Audio Channel ") +
1011 QString::number(section) + " " + UNICODE_RIGHT_ARROW;
1012 default:
1013 return QVariant();
1014 }
1015 }
1016
1017
1018 void ChannelRoutingModel::refresh ( Device *pDevice,
1019 const ChannelRoutingMap& routing )
1020 {
1021 m_pDevice = pDevice;
1022 m_routing = routing;
1023 // inform the outer world (QTableView) that our data changed
1024 #if QT_VERSION < 0x050000
1025 QAbstractTableModel::reset();
1026 #else
1027 QAbstractTableModel::beginResetModel();
1028 QAbstractTableModel::endResetModel();
1029 #endif
1030 }
1031
1032
1033 //-------------------------------------------------------------------------
1034 // QSampler::ChannelRoutingDelegate - table cell renderer for audio routing
1035 //
1036
1037 ChannelRoutingDelegate::ChannelRoutingDelegate ( QObject *pParent )
1038 : QItemDelegate(pParent)
1039 {
1040 }
1041
1042
1043 QWidget* ChannelRoutingDelegate::createEditor ( QWidget *pParent,
1044 const QStyleOptionViewItem & option, const QModelIndex& index ) const
1045 {
1046 if (!index.isValid())
1047 return NULL;
1048
1049 if (index.column() != 0)
1050 return NULL;
1051
1052 ChannelRoutingItem item = index.model()->data(index, Qt::DisplayRole).value<ChannelRoutingItem>();
1053
1054 QComboBox* pComboBox = new QComboBox(pParent);
1055 pComboBox->addItems(item.options);
1056 pComboBox->setCurrentIndex(item.selection);
1057 pComboBox->setEnabled(true);
1058 pComboBox->setGeometry(option.rect);
1059 return pComboBox;
1060 }
1061
1062
1063 void ChannelRoutingDelegate::setEditorData ( QWidget *pEditor,
1064 const QModelIndex &index) const
1065 {
1066 ChannelRoutingItem item = index.model()->data(index,
1067 Qt::DisplayRole).value<ChannelRoutingItem> ();
1068 QComboBox* pComboBox = static_cast<QComboBox*> (pEditor);
1069 pComboBox->setCurrentIndex(item.selection);
1070 }
1071
1072
1073 void ChannelRoutingDelegate::setModelData ( QWidget* pEditor,
1074 QAbstractItemModel *pModel, const QModelIndex& index ) const
1075 {
1076 QComboBox *pComboBox = static_cast<QComboBox*> (pEditor);
1077 pModel->setData(index, pComboBox->currentIndex());
1078 }
1079
1080
1081 void ChannelRoutingDelegate::updateEditorGeometry ( QWidget *pEditor,
1082 const QStyleOptionViewItem& option, const QModelIndex &/* index */) const
1083 {
1084 pEditor->setGeometry(option.rect);
1085 }
1086
1087 } // namespace QSampler
1088
1089
1090 // end of qsamplerChannel.cpp

  ViewVC Help
Powered by ViewVC