/[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 371 by capela, Fri Feb 11 15:36:06 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 109  bool qsamplerChannel::loadEngine ( const Line 170  bool qsamplerChannel::loadEngine ( const
170  }  }
171    
172    
173    // Instrument name accessor.
174    QString& qsamplerChannel::instrumentName (void)
175    {
176        return m_sInstrumentName;
177    }
178    
179  // Instrument filename accessor.  // Instrument filename accessor.
180  QString& qsamplerChannel::instrumentFile (void)  QString& qsamplerChannel::instrumentFile (void)
181  {  {
# 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 138  bool qsamplerChannel::loadInstrument ( c Line 205  bool qsamplerChannel::loadInstrument ( c
205          return false;          return false;
206      }      }
207    
208        m_sInstrumentName = getInstrumentName(sInstrumentFile, iInstrumentNr, true);
209      m_sInstrumentFile = sInstrumentFile;      m_sInstrumentFile = sInstrumentFile;
210      m_iInstrumentNr = iInstrumentNr;      m_iInstrumentNr = iInstrumentNr;
211      m_iInstrumentStatus = 0;      m_iInstrumentStatus = 0;
# Line 154  QString& qsamplerChannel::midiDriver (vo Line 222  QString& qsamplerChannel::midiDriver (vo
222    
223  bool qsamplerChannel::setMidiDriver ( const QString& sMidiDriver )  bool qsamplerChannel::setMidiDriver ( const QString& sMidiDriver )
224  {  {
225      if (client() == NULL)      if (client() == NULL || m_iChannelID < 0)
226          return false;          return false;
227    
228      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 243  int qsamplerChannel::midiDevice (void)
243    
244  bool qsamplerChannel::setMidiDevice ( int iMidiDevice )  bool qsamplerChannel::setMidiDevice ( int iMidiDevice )
245  {  {
246      if (client() == NULL)      if (client() == NULL || m_iChannelID < 0)
247          return false;          return false;
248    
249      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 264  int qsamplerChannel::midiPort (void)
264    
265  bool qsamplerChannel::setMidiPort ( int iMidiPort )  bool qsamplerChannel::setMidiPort ( int iMidiPort )
266  {  {
267      if (client() == NULL)      if (client() == NULL || m_iChannelID < 0)
268          return false;          return false;
269    
270      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 285  int qsamplerChannel::midiChannel (void)
285    
286  bool qsamplerChannel::setMidiChannel ( int iMidiChannel )  bool qsamplerChannel::setMidiChannel ( int iMidiChannel )
287  {  {
288      if (client() == NULL)      if (client() == NULL || m_iChannelID < 0)
289          return false;          return false;
290    
291      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 306  int qsamplerChannel::audioDevice (void)
306    
307  bool qsamplerChannel::setAudioDevice ( int iAudioDevice )  bool qsamplerChannel::setAudioDevice ( int iAudioDevice )
308  {  {
309      if (client() == NULL)      if (client() == NULL || m_iChannelID < 0)
310          return false;          return false;
311    
312      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 327  QString& qsamplerChannel::audioDriver (v
327    
328  bool qsamplerChannel::setAudioDriver ( const QString& sAudioDriver )  bool qsamplerChannel::setAudioDriver ( const QString& sAudioDriver )
329  {  {
330      if (client() == NULL)      if (client() == NULL || m_iChannelID < 0)
331          return false;          return false;
332    
333      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 348  float qsamplerChannel::volume (void)
348    
349  bool qsamplerChannel::setVolume ( float fVolume )  bool qsamplerChannel::setVolume ( float fVolume )
350  {  {
351      if (client() == NULL)      if (client() == NULL || m_iChannelID < 0)
352          return false;          return false;
353    
354      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 361  bool qsamplerChannel::setVolume ( float
361  }  }
362    
363    
364    // Istrument name remapper.
365    void qsamplerChannel::updateInstrumentName (void)
366    {
367            m_sInstrumentName = getInstrumentName(m_sInstrumentFile,
368                    m_iInstrumentNr, (options() && options()->bInstrumentNames));
369    }
370    
371    
372  // Update whole channel info state.  // Update whole channel info state.
373  void qsamplerChannel::updateChannelInfo (void)  bool qsamplerChannel::updateChannelInfo (void)
374  {  {
375      // Check if we're up and connected.      if (client() == NULL || m_iChannelID < 0)
376      if (client() == NULL)          return false;
         return;  
377    
378      // Read channel information.      // Read channel information.
379      lscp_channel_info_t *pChannelInfo = ::lscp_get_channel_info(client(), m_iChannelID);      lscp_channel_info_t *pChannelInfo = ::lscp_get_channel_info(client(), m_iChannelID);
380      if (pChannelInfo == NULL) {      if (pChannelInfo == NULL) {
381          appendMessagesClient("lscp_get_channel_info");          appendMessagesClient("lscp_get_channel_info");
382          appendMessagesError(QObject::tr("Could not get channel information.\n\nSorry."));          appendMessagesError(QObject::tr("Could not get channel information.\n\nSorry."));
383      } 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;  
384      }      }
385    
386            // First, check if intrument name has changed,
387            // taking care that instrument name lookup might be expensive,
388            // so we better make it only once and when really needed...
389            if ((m_sInstrumentFile != pChannelInfo->instrument_file) ||
390                    (m_iInstrumentNr   != pChannelInfo->instrument_nr)) {
391                    m_sInstrumentFile = pChannelInfo->instrument_file;
392                    m_iInstrumentNr   = pChannelInfo->instrument_nr;
393                    updateInstrumentName();
394            }
395        // Cache in other channel information.
396        m_sEngineName       = pChannelInfo->engine_name;
397        m_iInstrumentStatus = pChannelInfo->instrument_status;
398        m_iMidiDevice       = pChannelInfo->midi_device;
399        m_iMidiPort         = pChannelInfo->midi_port;
400        m_iMidiChannel      = pChannelInfo->midi_channel;
401        m_iAudioDevice      = pChannelInfo->audio_device;
402        m_fVolume           = pChannelInfo->volume;
403        // Some sanity checks.
404        if (m_sEngineName == "NONE")
405            m_sEngineName = QString::null;
406        if (m_sInstrumentFile == "NONE") {
407            m_sInstrumentFile = QString::null;
408            m_sInstrumentName = QString::null;
409            }
410    
411        return true;
412  }  }
413    
414    
415  // Reset channel method.  // Reset channel method.
416  void qsamplerChannel::resetChannel (void)  bool qsamplerChannel::resetChannel (void)
417  {  {
418      // Check if we're up and connected.      if (client() == NULL || m_iChannelID < 0)
419      if (client() == NULL)          return false;
         return;  
420    
421      if (::lscp_reset_channel(client(), m_iChannelID) != LSCP_OK)      if (::lscp_reset_channel(client(), m_iChannelID) != LSCP_OK) {
422          appendMessagesClient("lscp_reset_channel");          appendMessagesClient("lscp_reset_channel");
423      else          return false;
424          appendMessages(QObject::tr("Channel %1 reset.").arg(m_iChannelID));      }
425    
426        appendMessages(QObject::tr("Channel %1 reset.").arg(m_iChannelID));
427        return true;
428    }
429    
430    
431    // Channel setup dialog form.
432    bool qsamplerChannel::channelSetup ( QWidget *pParent )
433    {
434        bool bResult = false;
435    
436        qsamplerChannelForm *pChannelForm = new qsamplerChannelForm(pParent);
437        if (pChannelForm) {
438            pChannelForm->setup(this);
439            bResult = pChannelForm->exec();
440            delete pChannelForm;
441        }
442    
443        return bResult;
444  }  }
445    
446    
447  // Redirected messages output methods.  // Redirected messages output methods.
448  void qsamplerChannel::appendMessages( const QString& s )  void qsamplerChannel::appendMessages( const QString& s )
449  {  {
450      m_pMainForm->appendMessages(s);      if (m_pMainForm) m_pMainForm->appendMessages(s);
451  }  }
452    
453  void qsamplerChannel::appendMessagesColor( const QString& s, const QString& c )  void qsamplerChannel::appendMessagesColor( const QString& s, const QString& c )
454  {  {
455      m_pMainForm->appendMessagesColor(s, c);      if (m_pMainForm) m_pMainForm->appendMessagesColor(s, c);
456  }  }
457    
458  void qsamplerChannel::appendMessagesText( const QString& s )  void qsamplerChannel::appendMessagesText( const QString& s )
459  {  {
460      m_pMainForm->appendMessagesText(s);      if (m_pMainForm) m_pMainForm->appendMessagesText(s);
461  }  }
462    
463  void qsamplerChannel::appendMessagesError( const QString& s )  void qsamplerChannel::appendMessagesError( const QString& s )
464  {  {
465      m_pMainForm->appendMessagesError(s);      if (m_pMainForm) m_pMainForm->appendMessagesError(s);
466  }  }
467    
468  void qsamplerChannel::appendMessagesClient( const QString& s )  void qsamplerChannel::appendMessagesClient( const QString& s )
469  {  {
470      m_pMainForm->appendMessagesClient(s);      if (m_pMainForm) m_pMainForm->appendMessagesClient(s);
471    }
472    
473    
474    // Context menu event handler.
475    void qsamplerChannel::contextMenuEvent( QContextMenuEvent *pEvent )
476    {
477        if (m_pMainForm) m_pMainForm->contextMenuEvent(pEvent);
478    }
479    
480    
481    // Retrieve the instrument list of a instrument file (.gig).
482    QStringList qsamplerChannel::getInstrumentList( const QString& sInstrumentFile,
483            bool bInstrumentNames )
484    {
485        QFileInfo fileinfo(sInstrumentFile);
486        QString sInstrumentName = fileinfo.fileName();
487        QStringList instlist;
488    
489        if (fileinfo.exists()) {
490    #ifdef CONFIG_LIBGIG
491                    if (bInstrumentNames) {
492                    RIFF::File *pRiff = new RIFF::File(sInstrumentFile);
493                    gig::File  *pGig  = new gig::File(pRiff);
494                    gig::Instrument *pInstrument = pGig->GetFirstInstrument();
495                    while (pInstrument) {
496                        instlist.append((pInstrument->pInfo)->Name.c_str());
497                        pInstrument = pGig->GetNextInstrument();
498                    }
499                    delete pGig;
500                    delete pRiff;
501                    }
502                    else
503    #endif
504            for (int iInstrumentNr = 0; iInstrumentNr < QSAMPLER_INSTRUMENT_MAX; iInstrumentNr++)
505                instlist.append(sInstrumentName + " [" + QString::number(iInstrumentNr) + "]");
506        }
507        else instlist.append(sInstrumentName);
508    
509        return instlist;
510    }
511    
512    
513    // Retrieve the spacific instrument name of a instrument file (.gig), given its index.
514    QString qsamplerChannel::getInstrumentName( const QString& sInstrumentFile,
515            int iInstrumentNr, bool bInstrumentNames )
516    {
517        QFileInfo fileinfo(sInstrumentFile);
518        QString sInstrumentName = fileinfo.fileName();
519    
520        if (fileinfo.exists()) {
521    #ifdef CONFIG_LIBGIG
522                    if (bInstrumentNames) {
523                    RIFF::File *pRiff = new RIFF::File(sInstrumentFile);
524                    gig::File  *pGig  = new gig::File(pRiff);
525                    int iIndex = 0;
526                    gig::Instrument *pInstrument = pGig->GetFirstInstrument();
527                    while (pInstrument) {
528                        if (iIndex == iInstrumentNr) {
529                            sInstrumentName = (pInstrument->pInfo)->Name.c_str();
530                            break;
531                        }
532                        iIndex++;
533                        pInstrument = pGig->GetNextInstrument();
534                    }
535                    delete pGig;
536                    delete pRiff;
537                    }
538                    else
539    #endif
540            sInstrumentName += " [" + QString::number(iInstrumentNr) + "]";
541        }
542    
543        return sInstrumentName;
544  }  }
545    
546    

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

  ViewVC Help
Powered by ViewVC