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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 341 - (show annotations) (download)
Tue Jan 18 11:29:01 2005 UTC (19 years, 3 months ago) by capela
File size: 13769 byte(s)
* Actual instrument names are now optionally retrieved
  from the instrument file, even though libgig is available,
  avoiding excessively annoying load times while on the
  channel dialog, when huge instrument files are selected.

1 // qsamplerChannel.cpp
2 //
3 /****************************************************************************
4 Copyright (C) 2003-2005, 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
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 *****************************************************************************/
21
22 #include "qsamplerChannel.h"
23
24 #include "qsamplerMainForm.h"
25 #include "qsamplerChannelForm.h"
26
27 #include "config.h"
28
29 #include <qfileinfo.h>
30
31 #ifdef CONFIG_LIBGIG
32 #include "gig.h"
33 #else
34 #define QSAMPLER_INSTRUMENT_MAX 8
35 #endif
36
37
38 //-------------------------------------------------------------------------
39 // qsamplerChannel - Sampler channel structure.
40 //
41 bool qsamplerChannel::g_bInstrumentNames = false;
42
43 // Constructor.
44 qsamplerChannel::qsamplerChannel ( qsamplerMainForm *pMainForm, int iChannelID )
45 {
46 m_pMainForm = pMainForm;
47 m_iChannelID = iChannelID;
48
49 // m_sEngineName = QObject::tr("(No engine)");
50 // m_sInstrumentFile = QObject::tr("(No instrument)");
51 m_iInstrumentNr = -1;
52 m_iInstrumentStatus = -1;
53 m_sMidiDriver = "Alsa"; // DEPRECATED.
54 m_iMidiDevice = -1;
55 m_iMidiPort = -1;
56 m_iMidiChannel = -1;
57 m_sAudioDriver = "Alsa"; // DEPRECATED.
58 m_iAudioDevice = -1;
59 m_fVolume = 0.0;
60
61 }
62
63 // Default destructor.
64 qsamplerChannel::~qsamplerChannel (void)
65 {
66 }
67
68
69 // The global options settings delegated property.
70 qsamplerOptions *qsamplerChannel::options (void)
71 {
72 if (m_pMainForm == NULL)
73 return NULL;
74
75 return m_pMainForm->options();
76 }
77
78
79 // The client descriptor delegated property.
80 lscp_client_t *qsamplerChannel::client (void)
81 {
82 if (m_pMainForm == NULL)
83 return NULL;
84
85 return m_pMainForm->client();
86 }
87
88
89 // Create a new sampler channel, if not already.
90 bool qsamplerChannel::addChannel (void)
91 {
92 if (client() == NULL)
93 return false;
94
95 // Are we a new channel?
96 if (m_iChannelID < 0) {
97 m_iChannelID = ::lscp_add_channel(client());
98 if (m_iChannelID < 0) {
99 appendMessagesClient("lscp_add_channel");
100 appendMessagesError(QObject::tr("Could not create the new channel.\n\nSorry."));
101 } // Otherwise it's created...
102 else appendMessages(QObject::tr("Channel %1 created.").arg(m_iChannelID));
103 }
104
105 // Return whether we're a valid channel...
106 return (m_iChannelID >= 0);
107 }
108
109
110 // Remove sampler channel.
111 bool qsamplerChannel::removeChannel (void)
112 {
113 if (client() == NULL)
114 return false;
115
116 // Are we an existing channel?
117 if (m_iChannelID >= 0) {
118 if (::lscp_remove_channel(client(), m_iChannelID) != LSCP_OK) {
119 appendMessagesClient("lscp_remove_channel");
120 appendMessagesError(QObject::tr("Could not remove channel.\n\nSorry."));
121 } else {
122 // Otherwise it's removed.
123 appendMessages(QObject::tr("Channel %1 removed.").arg(m_iChannelID));
124 m_iChannelID = -1;
125 }
126 }
127
128 // Return whether we've removed the channel...
129 return (m_iChannelID < 0);
130 }
131
132
133 // Channel-ID (aka Sammpler-Channel) accessors.
134 int qsamplerChannel::channelID (void)
135 {
136 return m_iChannelID;
137 }
138
139 void qsamplerChannel::setChannelID ( int iChannelID )
140 {
141 m_iChannelID = iChannelID;
142 }
143
144
145 // Readable channel name.
146 QString qsamplerChannel::channelName (void)
147 {
148 return (m_iChannelID < 0 ? QObject::tr("New Channel") : QObject::tr("Channel %1").arg(m_iChannelID));
149 }
150
151
152 // Engine name accessors.
153 QString& qsamplerChannel::engineName (void)
154 {
155 return m_sEngineName;
156 }
157
158 bool qsamplerChannel::loadEngine ( const QString& sEngineName )
159 {
160 if (client() == NULL || m_iChannelID < 0)
161 return false;
162
163 if (::lscp_load_engine(client(), sEngineName.latin1(), m_iChannelID) != LSCP_OK) {
164 appendMessagesClient("lscp_load_engine");
165 return false;
166 }
167
168 m_sEngineName = sEngineName;
169 return true;
170 }
171
172
173 // Instrument filename accessor.
174 QString& qsamplerChannel::instrumentFile (void)
175 {
176 return m_sInstrumentFile;
177 }
178
179 // Instrument index accessor.
180 int qsamplerChannel::instrumentNr (void)
181 {
182 return m_iInstrumentNr;
183 }
184
185 // Instrument status accessor.
186 int qsamplerChannel::instrumentStatus (void)
187 {
188 return m_iInstrumentStatus;
189 }
190
191 // Instrument file loader.
192 bool qsamplerChannel::loadInstrument ( const QString& sInstrumentFile, int iInstrumentNr )
193 {
194 if (client() == NULL || m_iChannelID < 0)
195 return false;
196
197 if (::lscp_load_instrument_non_modal(client(), sInstrumentFile.latin1(), iInstrumentNr, m_iChannelID) != LSCP_OK) {
198 appendMessagesClient("lscp_load_instrument");
199 return false;
200 }
201
202 m_sInstrumentFile = sInstrumentFile;
203 m_iInstrumentNr = iInstrumentNr;
204 m_iInstrumentStatus = 0;
205
206 return true;
207 }
208
209
210 // MIDI driver type accessors (DEPRECATED).
211 QString& qsamplerChannel::midiDriver (void)
212 {
213 return m_sMidiDriver;
214 }
215
216 bool qsamplerChannel::setMidiDriver ( const QString& sMidiDriver )
217 {
218 if (client() == NULL || m_iChannelID < 0)
219 return false;
220
221 if (::lscp_set_channel_midi_type(client(), m_iChannelID, sMidiDriver.latin1()) != LSCP_OK) {
222 appendMessagesClient("lscp_set_channel_midi_type");
223 return false;
224 }
225
226 m_sMidiDriver = sMidiDriver;
227 return true;
228 }
229
230
231 // MIDI device accessors.
232 int qsamplerChannel::midiDevice (void)
233 {
234 return m_iMidiDevice;
235 }
236
237 bool qsamplerChannel::setMidiDevice ( int iMidiDevice )
238 {
239 if (client() == NULL || m_iChannelID < 0)
240 return false;
241
242 if (::lscp_set_channel_midi_device(client(), m_iChannelID, iMidiDevice) != LSCP_OK) {
243 appendMessagesClient("lscp_set_channel_midi_device");
244 return false;
245 }
246
247 m_iMidiDevice = iMidiDevice;
248 return true;
249 }
250
251
252 // MIDI port number accessor.
253 int qsamplerChannel::midiPort (void)
254 {
255 return m_iMidiPort;
256 }
257
258 bool qsamplerChannel::setMidiPort ( int iMidiPort )
259 {
260 if (client() == NULL || m_iChannelID < 0)
261 return false;
262
263 if (::lscp_set_channel_midi_port(client(), m_iChannelID, iMidiPort) != LSCP_OK) {
264 appendMessagesClient("lscp_set_channel_midi_port");
265 return false;
266 }
267
268 m_iMidiPort = iMidiPort;
269 return true;
270 }
271
272
273 // MIDI channel accessor.
274 int qsamplerChannel::midiChannel (void)
275 {
276 return m_iMidiChannel;
277 }
278
279 bool qsamplerChannel::setMidiChannel ( int iMidiChannel )
280 {
281 if (client() == NULL || m_iChannelID < 0)
282 return false;
283
284 if (::lscp_set_channel_midi_channel(client(), m_iChannelID, iMidiChannel) != LSCP_OK) {
285 appendMessagesClient("lscp_set_channel_midi_channel");
286 return false;
287 }
288
289 m_iMidiChannel = iMidiChannel;
290 return true;
291 }
292
293
294 // Audio device accessor.
295 int qsamplerChannel::audioDevice (void)
296 {
297 return m_iAudioDevice;
298 }
299
300 bool qsamplerChannel::setAudioDevice ( int iAudioDevice )
301 {
302 if (client() == NULL || m_iChannelID < 0)
303 return false;
304
305 if (::lscp_set_channel_audio_device(client(), m_iChannelID, iAudioDevice) != LSCP_OK) {
306 appendMessagesClient("lscp_set_channel_audio_device");
307 return false;
308 }
309
310 m_iAudioDevice = iAudioDevice;
311 return true;
312 }
313
314
315 // Audio driver type accessors (DEPRECATED).
316 QString& qsamplerChannel::audioDriver (void)
317 {
318 return m_sAudioDriver;
319 }
320
321 bool qsamplerChannel::setAudioDriver ( const QString& sAudioDriver )
322 {
323 if (client() == NULL || m_iChannelID < 0)
324 return false;
325
326 if (::lscp_set_channel_audio_type(client(), m_iChannelID, sAudioDriver.latin1()) != LSCP_OK) {
327 appendMessagesClient("lscp_set_channel_audio_type");
328 return false;
329 }
330
331 m_sAudioDriver = sAudioDriver;
332 return true;
333 }
334
335
336 // Channel volume accessors.
337 float qsamplerChannel::volume (void)
338 {
339 return m_fVolume;
340 }
341
342 bool qsamplerChannel::setVolume ( float fVolume )
343 {
344 if (client() == NULL || m_iChannelID < 0)
345 return false;
346
347 if (::lscp_set_channel_volume(client(), m_iChannelID, fVolume) != LSCP_OK) {
348 appendMessagesClient("lscp_set_channel_volume");
349 return false;
350 }
351
352 m_fVolume = fVolume;
353 return true;
354 }
355
356
357 // Update whole channel info state.
358 bool qsamplerChannel::updateChannelInfo (void)
359 {
360 if (client() == NULL || m_iChannelID < 0)
361 return false;
362
363 // Read channel information.
364 lscp_channel_info_t *pChannelInfo = ::lscp_get_channel_info(client(), m_iChannelID);
365 if (pChannelInfo == NULL) {
366 appendMessagesClient("lscp_get_channel_info");
367 appendMessagesError(QObject::tr("Could not get channel information.\n\nSorry."));
368 return false;
369 }
370
371 // Cache in channel information.
372 m_sEngineName = pChannelInfo->engine_name;
373 m_sInstrumentFile = pChannelInfo->instrument_file;
374 m_iInstrumentNr = pChannelInfo->instrument_nr;
375 m_iInstrumentStatus = pChannelInfo->instrument_status;
376 m_iMidiDevice = pChannelInfo->midi_device;
377 m_iMidiPort = pChannelInfo->midi_port;
378 m_iMidiChannel = pChannelInfo->midi_channel;
379 m_iAudioDevice = pChannelInfo->audio_device;
380 m_fVolume = pChannelInfo->volume;
381 // Some sanity checks.
382 if (m_sEngineName == "NONE")
383 m_sEngineName = QString::null;
384 if (m_sInstrumentFile == "NONE")
385 m_sInstrumentFile = QString::null;
386
387 return true;
388 }
389
390
391 // Reset channel method.
392 bool qsamplerChannel::resetChannel (void)
393 {
394 if (client() == NULL || m_iChannelID < 0)
395 return false;
396
397 if (::lscp_reset_channel(client(), m_iChannelID) != LSCP_OK) {
398 appendMessagesClient("lscp_reset_channel");
399 return false;
400 }
401
402 appendMessages(QObject::tr("Channel %1 reset.").arg(m_iChannelID));
403 return true;
404 }
405
406
407 // Channel setup dialog form.
408 bool qsamplerChannel::channelSetup ( QWidget *pParent )
409 {
410 bool bResult = false;
411
412 qsamplerChannelForm *pChannelForm = new qsamplerChannelForm(pParent);
413 if (pChannelForm) {
414 pChannelForm->setup(this);
415 bResult = pChannelForm->exec();
416 delete pChannelForm;
417 }
418
419 return bResult;
420 }
421
422
423 // Redirected messages output methods.
424 void qsamplerChannel::appendMessages( const QString& s )
425 {
426 if (m_pMainForm) m_pMainForm->appendMessages(s);
427 }
428
429 void qsamplerChannel::appendMessagesColor( const QString& s, const QString& c )
430 {
431 if (m_pMainForm) m_pMainForm->appendMessagesColor(s, c);
432 }
433
434 void qsamplerChannel::appendMessagesText( const QString& s )
435 {
436 if (m_pMainForm) m_pMainForm->appendMessagesText(s);
437 }
438
439 void qsamplerChannel::appendMessagesError( const QString& s )
440 {
441 if (m_pMainForm) m_pMainForm->appendMessagesError(s);
442 }
443
444 void qsamplerChannel::appendMessagesClient( const QString& s )
445 {
446 if (m_pMainForm) m_pMainForm->appendMessagesClient(s);
447 }
448
449
450 // Context menu event handler.
451 void qsamplerChannel::contextMenuEvent( QContextMenuEvent *pEvent )
452 {
453 if (m_pMainForm) m_pMainForm->contextMenuEvent(pEvent);
454 }
455
456
457 // Retrieve the instrument list of a instrument file (.gig).
458 QStringList qsamplerChannel::getInstrumentList( const QString& sInstrumentFile )
459 {
460 QFileInfo fileinfo(sInstrumentFile);
461 QString sInstrumentName = fileinfo.fileName();
462 QStringList instlist;
463
464 if (fileinfo.exists()) {
465 #ifdef CONFIG_LIBGIG
466 if (g_bInstrumentNames) {
467 RIFF::File *pRiff = new RIFF::File(sInstrumentFile);
468 gig::File *pGig = new gig::File(pRiff);
469 gig::Instrument *pInstrument = pGig->GetFirstInstrument();
470 while (pInstrument) {
471 instlist.append((pInstrument->pInfo)->Name.c_str());
472 pInstrument = pGig->GetNextInstrument();
473 }
474 delete pGig;
475 delete pRiff;
476 }
477 else
478 #else
479 for (int iInstrumentNr = 0; iInstrumentNr < QSAMPLER_INSTRUMENT_MAX; iInstrumentNr++)
480 instlist.append(sInstrumentName + " [" + QString::number(iInstrumentNr) + "]");
481 #endif
482 }
483 else instlist.append(sInstrumentName);
484
485 return instlist;
486 }
487
488
489 // Retrieve the spacific instrument name of a instrument file (.gig), given its index.
490 QString qsamplerChannel::getInstrumentName( const QString& sInstrumentFile, int iInstrumentNr )
491 {
492 QFileInfo fileinfo(sInstrumentFile);
493 QString sInstrumentName = fileinfo.fileName();
494
495 if (fileinfo.exists()) {
496 #ifdef CONFIG_LIBGIG
497 if (g_bInstrumentNames) {
498 RIFF::File *pRiff = new RIFF::File(sInstrumentFile);
499 gig::File *pGig = new gig::File(pRiff);
500 int iIndex = 0;
501 gig::Instrument *pInstrument = pGig->GetFirstInstrument();
502 while (pInstrument) {
503 if (iIndex == iInstrumentNr) {
504 sInstrumentName = (pInstrument->pInfo)->Name.c_str();
505 break;
506 }
507 iIndex++;
508 pInstrument = pGig->GetNextInstrument();
509 }
510 delete pGig;
511 delete pRiff;
512 }
513 else
514 #else
515 sInstrumentName += " [" + QString::number(iInstrumentNr) + "]";
516 #endif
517 }
518
519 return sInstrumentName;
520 }
521
522
523 // Instrument name(s) retrieval mode--global option.
524 bool qsamplerChannel::instrumentNames (void)
525 {
526 return g_bInstrumentNames;
527 }
528
529 void qsamplerChannel::setInstrumentNames ( bool bInstrumentNames )
530 {
531 g_bInstrumentNames = bInstrumentNames;
532 }
533
534
535 // end of qsamplerChannel.cpp

  ViewVC Help
Powered by ViewVC