rockbox/rbutil/rbutilqt/base/encoders.cpp
Dominik Wenger 357d35c20e rbutil: move the tts and encoders and its settings interface to base/
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@20825 a1c6a512-1295-4272-9138-f99709370657
2009-04-29 21:29:35 +00:00

233 lines
7 KiB
C++

/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
*
* Copyright (C) 2007 by Dominik Wenger
* $Id$
*
* All files in this archive are subject to the GNU General Public License.
* See the file COPYING in the source tree root for full license agreement.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include "encoders.h"
#include "utils.h"
/*********************************************************************
* Encoder Base
**********************************************************************/
QMap<QString,QString> EncBase::encoderList;
EncBase::EncBase(QObject *parent): EncTtsSettingInterface(parent)
{
}
// initialize list of encoders
void EncBase::initEncodernamesList()
{
encoderList["rbspeex"] = "Rockbox Speex Encoder";
encoderList["lame"] = "Lame Mp3 Encoder";
}
// get nice name for a specific encoder
QString EncBase::getEncoderName(QString encoder)
{
if(encoderList.isEmpty())
initEncodernamesList();
return encoderList.value(encoder);
}
// get a specific encoder object
EncBase* EncBase::getEncoder(QObject* parent,QString encoder)
{
EncBase* enc;
if(encoder == "lame")
{
enc = new EncExes(encoder,parent);
return enc;
}
else // rbspeex is default
{
enc = new EncRbSpeex(parent);
return enc;
}
}
QStringList EncBase::getEncoderList()
{
if(encoderList.isEmpty())
initEncodernamesList();
return encoderList.keys();
}
/*********************************************************************
* GEneral Exe Encoder
**********************************************************************/
EncExes::EncExes(QString name,QObject *parent) : EncBase(parent)
{
m_name = name;
m_TemplateMap["lame"] = "\"%exe\" %options \"%input\" \"%output\"";
}
void EncExes::generateSettings()
{
QString exepath =settings->subValue(m_name,RbSettings::EncoderPath).toString();
if(exepath == "") exepath = findExecutable(m_name);
insertSetting(eEXEPATH,new EncTtsSetting(this,EncTtsSetting::eSTRING,"Path to Encoder:",exepath,EncTtsSetting::eBROWSEBTN));
insertSetting(eEXEOPTIONS,new EncTtsSetting(this,EncTtsSetting::eSTRING,"Encoder options:",settings->subValue(m_name,RbSettings::EncoderOptions)));
}
void EncExes::saveSettings()
{
settings->setSubValue(m_name,RbSettings::EncoderPath,getSetting(eEXEPATH)->current().toString());
settings->setSubValue(m_name,RbSettings::EncoderOptions,getSetting(eEXEOPTIONS)->current().toString());
settings->sync();
}
bool EncExes::start()
{
m_EncExec = settings->subValue(m_name, RbSettings::EncoderPath).toString();
m_EncOpts = settings->subValue(m_name, RbSettings::EncoderOptions).toString();
m_EncTemplate = m_TemplateMap.value(m_name);
QFileInfo enc(m_EncExec);
if(enc.exists())
{
return true;
}
else
{
return false;
}
}
bool EncExes::encode(QString input,QString output)
{
//qDebug() << "encoding..";
QString execstring = m_EncTemplate;
execstring.replace("%exe",m_EncExec);
execstring.replace("%options",m_EncOpts);
execstring.replace("%input",input);
execstring.replace("%output",output);
qDebug() << execstring;
QProcess::execute(execstring);
return true;
}
bool EncExes::configOk()
{
QString path = settings->subValue(m_name, RbSettings::EncoderPath).toString();
if (QFileInfo(path).exists())
return true;
return false;
}
/*********************************************************************
* RB SPEEX ENCODER
**********************************************************************/
EncRbSpeex::EncRbSpeex(QObject *parent) : EncBase(parent)
{
}
void EncRbSpeex::generateSettings()
{
insertSetting(eVOLUME,new EncTtsSetting(this,EncTtsSetting::eDOUBLE,"Volume:",settings->subValue("rbspeex",RbSettings::EncoderVolume),1.0,10.0));
insertSetting(eQUALITY,new EncTtsSetting(this,EncTtsSetting::eDOUBLE,"Quality:",settings->subValue("rbspeex",RbSettings::EncoderQuality),0,10.0));
insertSetting(eCOMPLEXITY,new EncTtsSetting(this,EncTtsSetting::eINT,"Complexity:",settings->subValue("rbspeex",RbSettings::EncoderComplexity),0,10));
insertSetting(eNARROWBAND,new EncTtsSetting(this,EncTtsSetting::eBOOL,"Use Narrowband:",settings->subValue("rbspeex",RbSettings::EncoderNarrowBand)));
}
void EncRbSpeex::saveSettings()
{
//save settings in user config
settings->setSubValue("rbspeex",RbSettings::EncoderVolume,getSetting(eVOLUME)->current().toDouble());
settings->setSubValue("rbspeex",RbSettings::EncoderQuality,getSetting(eQUALITY)->current().toDouble());
settings->setSubValue("rbspeex",RbSettings::EncoderComplexity,getSetting(eCOMPLEXITY)->current().toInt());
settings->setSubValue("rbspeex",RbSettings::EncoderNarrowBand,getSetting(eNARROWBAND)->current().toBool());
settings->sync();
}
bool EncRbSpeex::start()
{
// try to get config from settings
quality = settings->subValue("rbspeex", RbSettings::EncoderQuality).toDouble();
complexity = settings->subValue("rbspeex", RbSettings::EncoderComplexity).toInt();
volume = settings->subValue("rbspeex", RbSettings::EncoderVolume).toDouble();
narrowband = settings->subValue("rbspeex", RbSettings::EncoderNarrowBand).toBool();
return true;
}
bool EncRbSpeex::encode(QString input,QString output)
{
qDebug() << "encoding " << input << " to "<< output;
char errstr[512];
FILE *fin,*fout;
if ((fin = fopen(input.toLocal8Bit(), "rb")) == NULL) {
qDebug() << "Error: could not open input file\n";
return false;
}
if ((fout = fopen(output.toLocal8Bit(), "wb")) == NULL) {
qDebug() << "Error: could not open output file\n";
return false;
}
int ret = encode_file(fin, fout, quality, complexity, narrowband, volume,
errstr, sizeof(errstr));
fclose(fout);
fclose(fin);
if (!ret) {
/* Attempt to delete unfinished output */
qDebug() << "Error:" << errstr;
QFile(output).remove();
return false;
}
return true;
}
bool EncRbSpeex::configOk()
{
bool result=true;
// check config
if(settings->subValue("rbspeex", RbSettings::EncoderVolume).toDouble() <= 0)
result =false;
if(settings->subValue("rbspeex", RbSettings::EncoderQuality).toDouble() <= 0)
result =false;
if(settings->subValue("rbspeex", RbSettings::EncoderComplexity).toInt() <= 0)
result =false;
return result;
}