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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 303 - (hide annotations) (download)
Fri Nov 19 10:18:59 2004 UTC (19 years, 4 months ago) by capela
File size: 13409 byte(s)
* Sampler channel is now completely detached from UI strip class.

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

  ViewVC Help
Powered by ViewVC