summaryrefslogtreecommitdiffstats
path: root/rbutil/rbutilqt/encoders.cpp
blob: dcd0e13c6d4a4d261f8f78915ff50567e907db8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/***************************************************************************
 *             __________               __   ___.
 *   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"

#ifndef CONSOLE
#include "encodersgui.h"
#include "browsedirtree.h"
#else
#include "encodersguicli.h"
#endif


QMap<QString,QString> EncBase::encoderList;
QMap<QString,EncBase*> EncBase::encoderCache;


// 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(QString encoder)
{
    // check cache
    if(encoderCache.contains(encoder))
        return encoderCache.value(encoder);

    EncBase* enc;
	if(encoder == "lame")
    {
        enc = new EncExes(encoder);
        encoderCache[encoder] = enc;
        return enc;
    }
    else  // rbspeex is default
    {
        enc = new EncRbSpeex();
        encoderCache[encoder] = enc;
        return enc;
    }
}


QStringList EncBase::getEncoderList()
{
    if(encoderList.isEmpty())
        initEncodernamesList();
    return encoderList.keys();
}


/*********************************************************************
* Encoder Base
**********************************************************************/
EncBase::EncBase(QObject *parent): QObject(parent)
{

}

/*********************************************************************
*  GEneral Exe Encoder
**********************************************************************/
EncExes::EncExes(QString name,QObject *parent) : EncBase(parent)
{
    m_name = name;
    
    m_TemplateMap["lame"] = "\"%exe\" %options \"%input\" \"%output\"";
}

bool EncExes::start()
{
    m_EncExec = settings->encoderPath(m_name);
    m_EncOpts = settings->encoderOptions(m_name);
    
    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;
}



void EncExes::showCfg()
{
#ifndef CONSOLE
    EncExesGui gui;
#else
	EncExesGuiCli gui;
#endif
    gui.setCfg(settings);
    gui.showCfg(m_name);
}

bool EncExes::configOk()
{
    QString path = settings->encoderPath(m_name);
    
    if (QFileInfo(path).exists())
        return true;
  
    return false;
}



/*********************************************************************
*  RB SPEEX ENCODER
**********************************************************************/
EncRbSpeex::EncRbSpeex(QObject *parent) : EncBase(parent)
{
   
    defaultQuality = 8.f;
    defaultVolume = 1.f;
    defaultComplexity = 10;
    defaultBand = false;
}


bool EncRbSpeex::start()
{
   
    // try to get config from settings
    quality = settings->encoderQuality("rbspeex");
    complexity = settings->encoderComplexity("rbspeex");
    volume = settings->encoderVolume("rbspeex");
    narrowband = settings->encoderNarrowband("rbspeex");
   

    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;
}


void EncRbSpeex::showCfg()
{
#ifndef CONSOLE
    EncRbSpeexGui gui;
#else
	EncRbSpeexGuiCli gui;
#endif
    gui.setCfg(settings);
    gui.showCfg(defaultQuality,defaultVolume,defaultComplexity,defaultBand);
}

bool EncRbSpeex::configOk()
{
    bool result=true;
    // check config
    
    if(settings->encoderVolume("rbspeex") <= 0)
        result =false;
    
    if(settings->encoderQuality("rbspeex") <= 0)
        result =false;
        
    if(settings->encoderComplexity("rbspeex") <= 0)
        result =false;
       
    return result;
}