--- qsampler/trunk/src/qsamplerChannel.cpp 2004/11/17 15:41:58 299 +++ qsampler/trunk/src/qsamplerChannel.cpp 2005/02/17 17:27:59 388 @@ -1,7 +1,7 @@ // qsamplerChannel.cpp // /**************************************************************************** - Copyright (C) 2003-2004, rncbc aka Rui Nuno Capela. All rights reserved. + Copyright (C) 2003-2005, rncbc aka Rui Nuno Capela. All rights reserved. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License @@ -22,6 +22,7 @@ #include "qsamplerChannel.h" #include "qsamplerMainForm.h" +#include "qsamplerChannelForm.h" #include "config.h" @@ -29,10 +30,10 @@ #ifdef CONFIG_LIBGIG #include "gig.h" -#else -#define QSAMPLER_INSTRUMENT_MAX 10 #endif +#define QSAMPLER_INSTRUMENT_MAX 8 + //------------------------------------------------------------------------- // qsamplerChannel - Sampler channel structure. @@ -44,8 +45,9 @@ m_pMainForm = pMainForm; m_iChannelID = iChannelID; -// m_sEngineName = QObject::tr("(No engine)"); -// m_sInstrumentFile = QObject::tr("(No instrument)"); +// m_sEngineName = noEngineName(); +// m_sInstrumentName = noInstrumentName(); +// m_sInstrumentFile = m_sInstrumentName; m_iInstrumentNr = -1; m_iInstrumentStatus = -1; m_sMidiDriver = "Alsa"; // DEPRECATED. @@ -180,6 +182,12 @@ return m_iInstrumentNr; } +// Instrument name accessor. +QString& qsamplerChannel::instrumentName (void) +{ + return m_sInstrumentName; +} + // Instrument status accessor. int qsamplerChannel::instrumentStatus (void) { @@ -191,17 +199,31 @@ { if (client() == NULL || m_iChannelID < 0) return false; + if (!isInstrumentFile(sInstrumentFile)) + return false; if (::lscp_load_instrument_non_modal(client(), sInstrumentFile.latin1(), iInstrumentNr, m_iChannelID) != LSCP_OK) { appendMessagesClient("lscp_load_instrument"); return false; } + return setInstrument(sInstrumentFile, iInstrumentNr); +} + + +// Special instrument file/name/number settler. +bool qsamplerChannel::setInstrument ( const QString& sInstrumentFile, int iInstrumentNr ) +{ m_sInstrumentFile = sInstrumentFile; m_iInstrumentNr = iInstrumentNr; +#ifdef CONFIG_INSTRUMENT_NAME + m_sInstrumentName = QString::null; // We'll get it, maybe later, on channel_info... +#else + m_sInstrumentName = getInstrumentName(sInstrumentFile, iInstrumentNr, true); +#endif m_iInstrumentStatus = 0; - return true; + return true; } @@ -352,6 +374,16 @@ } +// Istrument name remapper. +void qsamplerChannel::updateInstrumentName (void) +{ +#ifndef CONFIG_INSTRUMENT_NAME + m_sInstrumentName = getInstrumentName(m_sInstrumentFile, + m_iInstrumentNr, (options() && options()->bInstrumentNames)); +#endif +} + + // Update whole channel info state. bool qsamplerChannel::updateChannelInfo (void) { @@ -366,10 +398,24 @@ return false; } - // Cache in channel information. +#ifdef CONFIG_INSTRUMENT_NAME + // We got all actual instrument datum... + m_sInstrumentFile = pChannelInfo->instrument_file; + m_iInstrumentNr = pChannelInfo->instrument_nr; + m_sInstrumentName = pChannelInfo->instrument_name; +#else + // First, check if intrument name has changed, + // taking care that instrument name lookup might be expensive, + // so we better make it only once and when really needed... + if ((m_sInstrumentFile != pChannelInfo->instrument_file) || + (m_iInstrumentNr != pChannelInfo->instrument_nr)) { + m_sInstrumentFile = pChannelInfo->instrument_file; + m_iInstrumentNr = pChannelInfo->instrument_nr; + updateInstrumentName(); + } +#endif + // Cache in other 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; @@ -379,8 +425,10 @@ // Some sanity checks. if (m_sEngineName == "NONE") m_sEngineName = QString::null; - if (m_sInstrumentFile == "NONE") + if (m_sInstrumentFile == "NONE") { m_sInstrumentFile = QString::null; + m_sInstrumentName = QString::null; + } return true; } @@ -402,92 +450,153 @@ } +// Channel setup dialog form. +bool qsamplerChannel::channelSetup ( QWidget *pParent ) +{ + bool bResult = false; + + qsamplerChannelForm *pChannelForm = new qsamplerChannelForm(pParent); + if (pChannelForm) { + pChannelForm->setup(this); + bResult = pChannelForm->exec(); + delete pChannelForm; + } + + return bResult; +} + + // Redirected messages output methods. void qsamplerChannel::appendMessages( const QString& s ) { - m_pMainForm->appendMessages(s); + if (m_pMainForm) m_pMainForm->appendMessages(s); } void qsamplerChannel::appendMessagesColor( const QString& s, const QString& c ) { - m_pMainForm->appendMessagesColor(s, c); + if (m_pMainForm) m_pMainForm->appendMessagesColor(s, c); } void qsamplerChannel::appendMessagesText( const QString& s ) { - m_pMainForm->appendMessagesText(s); + if (m_pMainForm) m_pMainForm->appendMessagesText(s); } void qsamplerChannel::appendMessagesError( const QString& s ) { - m_pMainForm->appendMessagesError(s); + if (m_pMainForm) m_pMainForm->appendMessagesError(s); } void qsamplerChannel::appendMessagesClient( const QString& s ) { - m_pMainForm->appendMessagesClient(s); + if (m_pMainForm) m_pMainForm->appendMessagesClient(s); +} + + +// Context menu event handler. +void qsamplerChannel::contextMenuEvent( QContextMenuEvent *pEvent ) +{ + if (m_pMainForm) m_pMainForm->contextMenuEvent(pEvent); +} + + +// FIXME: Check whether a given file is an instrument file. +bool qsamplerChannel::isInstrumentFile ( const QString& sInstrumentFile ) +{ + bool bResult = false; + + QFile file(sInstrumentFile); + if (file.open(IO_ReadOnly)) { + char achHeader[4]; + if (file.readBlock(achHeader, 4)) { + bResult = (achHeader[0] == 'R' + && achHeader[1] == 'I' + && achHeader[2] == 'F' + && achHeader[3] == 'F'); + } + file.close(); + } + + return bResult; } // Retrieve the instrument list of a instrument file (.gig). -QStringList qsamplerChannel::getInstrumentList( const QString& sInstrumentFile ) +QStringList qsamplerChannel::getInstrumentList( const QString& sInstrumentFile, + bool bInstrumentNames ) { - QFileInfo fileinfo(sInstrumentFile); - QString sInstrumentName = fileinfo.fileName(); + QString sInstrumentName = QFileInfo(sInstrumentFile).fileName(); QStringList instlist; - if (fileinfo.exists()) { + if (isInstrumentFile(sInstrumentFile)) { #ifdef CONFIG_LIBGIG - RIFF::File *pRiff = new RIFF::File(sInstrumentFile); - gig::File *pGig = new gig::File(pRiff); - gig::Instrument *pInstrument = pGig->GetFirstInstrument(); - while (pInstrument) { - sInstrumentName = (pInstrument->pInfo)->Name; - instlist.append(sInstrumentName); - pInstrument = pGig->GetNextInstrument(); - } - delete pGig; - delete pRiff; -#else + if (bInstrumentNames) { + RIFF::File *pRiff = new RIFF::File(sInstrumentFile); + gig::File *pGig = new gig::File(pRiff); + gig::Instrument *pInstrument = pGig->GetFirstInstrument(); + while (pInstrument) { + instlist.append((pInstrument->pInfo)->Name.c_str()); + pInstrument = pGig->GetNextInstrument(); + } + delete pGig; + delete pRiff; + } + else +#endif for (int iInstrumentNr = 0; iInstrumentNr < QSAMPLER_INSTRUMENT_MAX; iInstrumentNr++) instlist.append(sInstrumentName + " [" + QString::number(iInstrumentNr) + "]"); -#endif } - else instlist.append(sInstrumentName); - + else instlist.append(noInstrumentName()); + return instlist; } // Retrieve the spacific instrument name of a instrument file (.gig), given its index. -QString qsamplerChannel::getInstrumentName( const QString& sInstrumentFile, int iInstrumentNr ) +QString qsamplerChannel::getInstrumentName( const QString& sInstrumentFile, + int iInstrumentNr, bool bInstrumentNames ) { - QFileInfo fileinfo(sInstrumentFile); - QString sInstrumentName = fileinfo.fileName(); + QString sInstrumentName; - if (fileinfo.exists()) { + if (isInstrumentFile(sInstrumentFile)) { + sInstrumentName = QFileInfo(sInstrumentFile).fileName(); #ifdef CONFIG_LIBGIG - RIFF::File *pRiff = new RIFF::File(sInstrumentFile); - gig::File *pGig = new gig::File(pRiff); - int iIndex = 0; - gig::Instrument *pInstrument = pGig->GetFirstInstrument(); - while (pInstrument) { - if (iIndex == iInstrumentNr) { - sInstrumentName = (pInstrument->pInfo)->Name; - break; - } - iIndex++; - pInstrument = pGig->GetNextInstrument(); - } - delete pGig; - delete pRiff; -#else - sInstrumentName += " [" + QString::number(iInstrumentNr) + "]"; + if (bInstrumentNames) { + RIFF::File *pRiff = new RIFF::File(sInstrumentFile); + gig::File *pGig = new gig::File(pRiff); + int iIndex = 0; + gig::Instrument *pInstrument = pGig->GetFirstInstrument(); + while (pInstrument) { + if (iIndex == iInstrumentNr) { + sInstrumentName = (pInstrument->pInfo)->Name.c_str(); + break; + } + iIndex++; + pInstrument = pGig->GetNextInstrument(); + } + delete pGig; + delete pRiff; + } + else #endif + sInstrumentName += " [" + QString::number(iInstrumentNr) + "]"; } + else sInstrumentName = noInstrumentName(); return sInstrumentName; } +// Common invalid name-helpers. +QString qsamplerChannel::noEngineName (void) +{ + return QObject::tr("(No engine)"); +} + +QString qsamplerChannel::noInstrumentName (void) +{ + return QObject::tr("(No instrument)"); +} + + // end of qsamplerChannel.cpp