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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 299 - (show annotations) (download)
Wed Nov 17 15:41:58 2004 UTC (19 years, 4 months ago) by capela
File size: 12779 byte(s)
* Instrument index selection now made via combo box widget;
  actual instrument names are now properly retrieved from
  the instrument file, provided if libgig is available.

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

  ViewVC Help
Powered by ViewVC