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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 265 by capela, Wed Sep 29 16:05:24 2004 UTC revision 382 by capela, Mon Feb 14 15:42:38 2005 UTC
# Line 1  Line 1 
1  // qsamplerChannel.cpp  // qsamplerChannel.cpp
2  //  //
3  /****************************************************************************  /****************************************************************************
4     Copyright (C) 2003-2004, rncbc aka Rui Nuno Capela. All rights reserved.     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     This program is free software; you can redistribute it and/or
7     modify it under the terms of the GNU General Public License     modify it under the terms of the GNU General Public License
# Line 22  Line 22 
22  #include "qsamplerChannel.h"  #include "qsamplerChannel.h"
23    
24  #include "qsamplerMainForm.h"  #include "qsamplerMainForm.h"
25    #include "qsamplerChannelForm.h"
26    
27  #include "config.h"  #include "config.h"
28    
29    #include <qfileinfo.h>
30    
31    #ifdef CONFIG_LIBGIG
32    #include "gig.h"
33    #endif
34    
35    #define QSAMPLER_INSTRUMENT_MAX 8
36    
37    
38  //-------------------------------------------------------------------------  //-------------------------------------------------------------------------
39  // qsamplerChannel - Sampler channel structure.  // qsamplerChannel - Sampler channel structure.
# Line 37  qsamplerChannel::qsamplerChannel ( qsamp Line 46  qsamplerChannel::qsamplerChannel ( qsamp
46      m_iChannelID = iChannelID;      m_iChannelID = iChannelID;
47    
48  //  m_sEngineName       = QObject::tr("(No engine)");  //  m_sEngineName       = QObject::tr("(No engine)");
49  //  m_sInstrumentFile   = QObject::tr("(No instrument)");  //  m_sInstrumentName   = QObject::tr("(No instrument)");
50    //  m_sInstrumentFile   = m_sInstrumentName;
51      m_iInstrumentNr     = -1;      m_iInstrumentNr     = -1;
52      m_iInstrumentStatus = -1;      m_iInstrumentStatus = -1;
53      m_sMidiDriver       = "Alsa";   // DEPRECATED.      m_sMidiDriver       = "Alsa";   // DEPRECATED.
# Line 76  lscp_client_t *qsamplerChannel::client ( Line 86  lscp_client_t *qsamplerChannel::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.  // Channel-ID (aka Sammpler-Channel) accessors.
134  int qsamplerChannel::channelID (void)  int qsamplerChannel::channelID (void)
135  {  {
# Line 88  void qsamplerChannel::setChannelID ( int Line 142  void qsamplerChannel::setChannelID ( int
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.  // Engine name accessors.
153  QString& qsamplerChannel::engineName (void)  QString& qsamplerChannel::engineName (void)
154  {  {
# Line 96  QString& qsamplerChannel::engineName (vo Line 157  QString& qsamplerChannel::engineName (vo
157    
158  bool qsamplerChannel::loadEngine ( const QString& sEngineName )  bool qsamplerChannel::loadEngine ( const QString& sEngineName )
159  {  {
160      if (client() == NULL)      if (client() == NULL || m_iChannelID < 0)
161          return false;          return false;
162    
163      if (::lscp_load_engine(client(), sEngineName.latin1(), m_iChannelID) != LSCP_OK) {      if (::lscp_load_engine(client(), sEngineName.latin1(), m_iChannelID) != LSCP_OK) {
# Line 121  int qsamplerChannel::instrumentNr (void) Line 182  int qsamplerChannel::instrumentNr (void)
182      return m_iInstrumentNr;      return m_iInstrumentNr;
183  }  }
184    
185    // Instrument name accessor.
186    QString& qsamplerChannel::instrumentName (void)
187    {
188        return m_sInstrumentName;
189    }
190    
191  // Instrument status accessor.  // Instrument status accessor.
192  int qsamplerChannel::instrumentStatus (void)  int qsamplerChannel::instrumentStatus (void)
193  {  {
# Line 130  int qsamplerChannel::instrumentStatus (v Line 197  int qsamplerChannel::instrumentStatus (v
197  // Instrument file loader.  // Instrument file loader.
198  bool qsamplerChannel::loadInstrument ( const QString& sInstrumentFile, int iInstrumentNr )  bool qsamplerChannel::loadInstrument ( const QString& sInstrumentFile, int iInstrumentNr )
199  {  {
200      if (client() == NULL)      if (client() == NULL || m_iChannelID < 0)
201          return false;          return false;
202    
203      if (::lscp_load_instrument_non_modal(client(), sInstrumentFile.latin1(), iInstrumentNr, m_iChannelID) != LSCP_OK) {      if (::lscp_load_instrument_non_modal(client(), sInstrumentFile.latin1(), iInstrumentNr, m_iChannelID) != LSCP_OK) {
# Line 140  bool qsamplerChannel::loadInstrument ( c Line 207  bool qsamplerChannel::loadInstrument ( c
207    
208      m_sInstrumentFile = sInstrumentFile;      m_sInstrumentFile = sInstrumentFile;
209      m_iInstrumentNr = iInstrumentNr;      m_iInstrumentNr = iInstrumentNr;
210    #ifdef CONFIG_INSTRUMENT_NAME
211        m_sInstrumentName = QString::null;  // We'll get it later on channel_info...
212    #else
213        m_sInstrumentName = getInstrumentName(sInstrumentFile, iInstrumentNr, true);
214    #endif
215      m_iInstrumentStatus = 0;      m_iInstrumentStatus = 0;
216    
217      return true;      return true;
# Line 154  QString& qsamplerChannel::midiDriver (vo Line 226  QString& qsamplerChannel::midiDriver (vo
226    
227  bool qsamplerChannel::setMidiDriver ( const QString& sMidiDriver )  bool qsamplerChannel::setMidiDriver ( const QString& sMidiDriver )
228  {  {
229      if (client() == NULL)      if (client() == NULL || m_iChannelID < 0)
230          return false;          return false;
231    
232      if (::lscp_set_channel_midi_type(client(), m_iChannelID, sMidiDriver.latin1()) != LSCP_OK) {      if (::lscp_set_channel_midi_type(client(), m_iChannelID, sMidiDriver.latin1()) != LSCP_OK) {
# Line 175  int qsamplerChannel::midiDevice (void) Line 247  int qsamplerChannel::midiDevice (void)
247    
248  bool qsamplerChannel::setMidiDevice ( int iMidiDevice )  bool qsamplerChannel::setMidiDevice ( int iMidiDevice )
249  {  {
250      if (client() == NULL)      if (client() == NULL || m_iChannelID < 0)
251          return false;          return false;
252    
253      if (::lscp_set_channel_midi_device(client(), m_iChannelID, iMidiDevice) != LSCP_OK) {      if (::lscp_set_channel_midi_device(client(), m_iChannelID, iMidiDevice) != LSCP_OK) {
# Line 196  int qsamplerChannel::midiPort (void) Line 268  int qsamplerChannel::midiPort (void)
268    
269  bool qsamplerChannel::setMidiPort ( int iMidiPort )  bool qsamplerChannel::setMidiPort ( int iMidiPort )
270  {  {
271      if (client() == NULL)      if (client() == NULL || m_iChannelID < 0)
272          return false;          return false;
273    
274      if (::lscp_set_channel_midi_port(client(), m_iChannelID, iMidiPort) != LSCP_OK) {      if (::lscp_set_channel_midi_port(client(), m_iChannelID, iMidiPort) != LSCP_OK) {
# Line 217  int qsamplerChannel::midiChannel (void) Line 289  int qsamplerChannel::midiChannel (void)
289    
290  bool qsamplerChannel::setMidiChannel ( int iMidiChannel )  bool qsamplerChannel::setMidiChannel ( int iMidiChannel )
291  {  {
292      if (client() == NULL)      if (client() == NULL || m_iChannelID < 0)
293          return false;          return false;
294    
295      if (::lscp_set_channel_midi_channel(client(), m_iChannelID, iMidiChannel) != LSCP_OK) {      if (::lscp_set_channel_midi_channel(client(), m_iChannelID, iMidiChannel) != LSCP_OK) {
# Line 238  int qsamplerChannel::audioDevice (void) Line 310  int qsamplerChannel::audioDevice (void)
310    
311  bool qsamplerChannel::setAudioDevice ( int iAudioDevice )  bool qsamplerChannel::setAudioDevice ( int iAudioDevice )
312  {  {
313      if (client() == NULL)      if (client() == NULL || m_iChannelID < 0)
314          return false;          return false;
315    
316      if (::lscp_set_channel_audio_device(client(), m_iChannelID, iAudioDevice) != LSCP_OK) {      if (::lscp_set_channel_audio_device(client(), m_iChannelID, iAudioDevice) != LSCP_OK) {
# Line 259  QString& qsamplerChannel::audioDriver (v Line 331  QString& qsamplerChannel::audioDriver (v
331    
332  bool qsamplerChannel::setAudioDriver ( const QString& sAudioDriver )  bool qsamplerChannel::setAudioDriver ( const QString& sAudioDriver )
333  {  {
334      if (client() == NULL)      if (client() == NULL || m_iChannelID < 0)
335          return false;          return false;
336    
337      if (::lscp_set_channel_audio_type(client(), m_iChannelID, sAudioDriver.latin1()) != LSCP_OK) {      if (::lscp_set_channel_audio_type(client(), m_iChannelID, sAudioDriver.latin1()) != LSCP_OK) {
# Line 280  float qsamplerChannel::volume (void) Line 352  float qsamplerChannel::volume (void)
352    
353  bool qsamplerChannel::setVolume ( float fVolume )  bool qsamplerChannel::setVolume ( float fVolume )
354  {  {
355      if (client() == NULL)      if (client() == NULL || m_iChannelID < 0)
356          return false;          return false;
357    
358      if (::lscp_set_channel_volume(client(), m_iChannelID, fVolume) != LSCP_OK) {      if (::lscp_set_channel_volume(client(), m_iChannelID, fVolume) != LSCP_OK) {
# Line 293  bool qsamplerChannel::setVolume ( float Line 365  bool qsamplerChannel::setVolume ( float
365  }  }
366    
367    
368    // Istrument name remapper.
369    void qsamplerChannel::updateInstrumentName (void)
370    {
371    #ifndef CONFIG_INSTRUMENT_NAME
372            m_sInstrumentName = getInstrumentName(m_sInstrumentFile,
373                    m_iInstrumentNr, (options() && options()->bInstrumentNames));
374    #endif
375    }
376    
377    
378  // Update whole channel info state.  // Update whole channel info state.
379  void qsamplerChannel::updateChannelInfo (void)  bool qsamplerChannel::updateChannelInfo (void)
380  {  {
381      // Check if we're up and connected.      if (client() == NULL || m_iChannelID < 0)
382      if (client() == NULL)          return false;
         return;  
383    
384      // Read channel information.      // Read channel information.
385      lscp_channel_info_t *pChannelInfo = ::lscp_get_channel_info(client(), m_iChannelID);      lscp_channel_info_t *pChannelInfo = ::lscp_get_channel_info(client(), m_iChannelID);
386      if (pChannelInfo == NULL) {      if (pChannelInfo == NULL) {
387          appendMessagesClient("lscp_get_channel_info");          appendMessagesClient("lscp_get_channel_info");
388          appendMessagesError(QObject::tr("Could not get channel information.\n\nSorry."));          appendMessagesError(QObject::tr("Could not get channel information.\n\nSorry."));
389      } else {          return false;
         // Cache in channel information.  
         m_sEngineName       = pChannelInfo->engine_name;  
         m_sInstrumentFile   = pChannelInfo->instrument_file;  
         m_iInstrumentNr     = pChannelInfo->instrument_nr;  
         m_iInstrumentStatus = pChannelInfo->instrument_status;  
         m_iMidiDevice       = pChannelInfo->midi_device;  
         m_iMidiPort         = pChannelInfo->midi_port;  
         m_iMidiChannel      = pChannelInfo->midi_channel;  
         m_iAudioDevice      = pChannelInfo->audio_device;  
         m_fVolume           = pChannelInfo->volume;  
         // Some sanity checks.  
         if (m_sEngineName == "NONE")  
             m_sEngineName = QString::null;  
         if (m_sInstrumentFile == "NONE")  
             m_sInstrumentFile = QString::null;  
390      }      }
391    
392    #ifdef CONFIG_INSTRUMENT_NAME
393            // We got all actual instrument datum...
394            m_sInstrumentFile = pChannelInfo->instrument_file;
395            m_iInstrumentNr   = pChannelInfo->instrument_nr;
396            m_sInstrumentName = pChannelInfo->instrument_name;
397    #else
398            // First, check if intrument name has changed,
399            // taking care that instrument name lookup might be expensive,
400            // so we better make it only once and when really needed...
401            if ((m_sInstrumentFile != pChannelInfo->instrument_file) ||
402                    (m_iInstrumentNr   != pChannelInfo->instrument_nr)) {
403                    m_sInstrumentFile = pChannelInfo->instrument_file;
404                    m_iInstrumentNr   = pChannelInfo->instrument_nr;
405                    updateInstrumentName();
406            }
407    #endif
408        // Cache in other channel information.
409        m_sEngineName       = pChannelInfo->engine_name;
410        m_iInstrumentStatus = pChannelInfo->instrument_status;
411        m_iMidiDevice       = pChannelInfo->midi_device;
412        m_iMidiPort         = pChannelInfo->midi_port;
413        m_iMidiChannel      = pChannelInfo->midi_channel;
414        m_iAudioDevice      = pChannelInfo->audio_device;
415        m_fVolume           = pChannelInfo->volume;
416        // Some sanity checks.
417        if (m_sEngineName == "NONE")
418            m_sEngineName = QString::null;
419        if (m_sInstrumentFile == "NONE") {
420            m_sInstrumentFile = QString::null;
421            m_sInstrumentName = QString::null;
422            }
423    
424        return true;
425  }  }
426    
427    
428  // Reset channel method.  // Reset channel method.
429  void qsamplerChannel::resetChannel (void)  bool qsamplerChannel::resetChannel (void)
430  {  {
431      // Check if we're up and connected.      if (client() == NULL || m_iChannelID < 0)
432      if (client() == NULL)          return false;
         return;  
433    
434      if (::lscp_reset_channel(client(), m_iChannelID) != LSCP_OK)      if (::lscp_reset_channel(client(), m_iChannelID) != LSCP_OK) {
435          appendMessagesClient("lscp_reset_channel");          appendMessagesClient("lscp_reset_channel");
436      else          return false;
437          appendMessages(QObject::tr("Channel %1 reset.").arg(m_iChannelID));      }
438    
439        appendMessages(QObject::tr("Channel %1 reset.").arg(m_iChannelID));
440        return true;
441    }
442    
443    
444    // Channel setup dialog form.
445    bool qsamplerChannel::channelSetup ( QWidget *pParent )
446    {
447        bool bResult = false;
448    
449        qsamplerChannelForm *pChannelForm = new qsamplerChannelForm(pParent);
450        if (pChannelForm) {
451            pChannelForm->setup(this);
452            bResult = pChannelForm->exec();
453            delete pChannelForm;
454        }
455    
456        return bResult;
457  }  }
458    
459    
460  // Redirected messages output methods.  // Redirected messages output methods.
461  void qsamplerChannel::appendMessages( const QString& s )  void qsamplerChannel::appendMessages( const QString& s )
462  {  {
463      m_pMainForm->appendMessages(s);      if (m_pMainForm) m_pMainForm->appendMessages(s);
464  }  }
465    
466  void qsamplerChannel::appendMessagesColor( const QString& s, const QString& c )  void qsamplerChannel::appendMessagesColor( const QString& s, const QString& c )
467  {  {
468      m_pMainForm->appendMessagesColor(s, c);      if (m_pMainForm) m_pMainForm->appendMessagesColor(s, c);
469  }  }
470    
471  void qsamplerChannel::appendMessagesText( const QString& s )  void qsamplerChannel::appendMessagesText( const QString& s )
472  {  {
473      m_pMainForm->appendMessagesText(s);      if (m_pMainForm) m_pMainForm->appendMessagesText(s);
474  }  }
475    
476  void qsamplerChannel::appendMessagesError( const QString& s )  void qsamplerChannel::appendMessagesError( const QString& s )
477  {  {
478      m_pMainForm->appendMessagesError(s);      if (m_pMainForm) m_pMainForm->appendMessagesError(s);
479  }  }
480    
481  void qsamplerChannel::appendMessagesClient( const QString& s )  void qsamplerChannel::appendMessagesClient( const QString& s )
482  {  {
483      m_pMainForm->appendMessagesClient(s);      if (m_pMainForm) m_pMainForm->appendMessagesClient(s);
484    }
485    
486    
487    // Context menu event handler.
488    void qsamplerChannel::contextMenuEvent( QContextMenuEvent *pEvent )
489    {
490        if (m_pMainForm) m_pMainForm->contextMenuEvent(pEvent);
491    }
492    
493    
494    // Retrieve the instrument list of a instrument file (.gig).
495    QStringList qsamplerChannel::getInstrumentList( const QString& sInstrumentFile,
496            bool bInstrumentNames )
497    {
498        QFileInfo fileinfo(sInstrumentFile);
499        QString sInstrumentName = fileinfo.fileName();
500        QStringList instlist;
501    
502        if (fileinfo.exists()) {
503    #ifdef CONFIG_LIBGIG
504                    if (bInstrumentNames) {
505                    RIFF::File *pRiff = new RIFF::File(sInstrumentFile);
506                    gig::File  *pGig  = new gig::File(pRiff);
507                    gig::Instrument *pInstrument = pGig->GetFirstInstrument();
508                    while (pInstrument) {
509                        instlist.append((pInstrument->pInfo)->Name.c_str());
510                        pInstrument = pGig->GetNextInstrument();
511                    }
512                    delete pGig;
513                    delete pRiff;
514                    }
515                    else
516    #endif
517            for (int iInstrumentNr = 0; iInstrumentNr < QSAMPLER_INSTRUMENT_MAX; iInstrumentNr++)
518                instlist.append(sInstrumentName + " [" + QString::number(iInstrumentNr) + "]");
519        }
520        else instlist.append(sInstrumentName);
521    
522        return instlist;
523    }
524    
525    
526    // Retrieve the spacific instrument name of a instrument file (.gig), given its index.
527    QString qsamplerChannel::getInstrumentName( const QString& sInstrumentFile,
528            int iInstrumentNr, bool bInstrumentNames )
529    {
530        QFileInfo fileinfo(sInstrumentFile);
531        QString sInstrumentName = fileinfo.fileName();
532    
533        if (fileinfo.exists()) {
534    #ifdef CONFIG_LIBGIG
535                    if (bInstrumentNames) {
536                    RIFF::File *pRiff = new RIFF::File(sInstrumentFile);
537                    gig::File  *pGig  = new gig::File(pRiff);
538                    int iIndex = 0;
539                    gig::Instrument *pInstrument = pGig->GetFirstInstrument();
540                    while (pInstrument) {
541                        if (iIndex == iInstrumentNr) {
542                            sInstrumentName = (pInstrument->pInfo)->Name.c_str();
543                            break;
544                        }
545                        iIndex++;
546                        pInstrument = pGig->GetNextInstrument();
547                    }
548                    delete pGig;
549                    delete pRiff;
550                    }
551                    else
552    #endif
553            sInstrumentName += " [" + QString::number(iInstrumentNr) + "]";
554        }
555    
556        return sInstrumentName;
557  }  }
558    
559    

Legend:
Removed from v.265  
changed lines
  Added in v.382

  ViewVC Help
Powered by ViewVC