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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC