summaryrefslogtreecommitdiffstats
path: root/rbutil/rbutilqt/base/encoderrbspeex.cpp
blob: 2bee66028ac65a4d9d9c1a3dbf2792774ae8dd88 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 *
 *   Copyright (C) 2007 by Dominik Wenger
 *
 * 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 <QtCore>
#include "encoderrbspeex.h"
#include "rbsettings.h"
#include "rbspeex.h"
#include "Logger.h"

EncoderRbSpeex::EncoderRbSpeex(QObject *parent) : EncoderBase(parent)
{

}

void EncoderRbSpeex::generateSettings()
{
    loadSettings();
    insertSetting(eVOLUME, new EncTtsSetting(this, EncTtsSetting::eDOUBLE,
        tr("Volume:"), volume, 0.0, 2.0));
    insertSetting(eQUALITY, new EncTtsSetting(this, EncTtsSetting::eDOUBLE,
        tr("Quality:"), quality, 0, 10.0));
    insertSetting(eCOMPLEXITY, new EncTtsSetting(this, EncTtsSetting::eINT,
        tr("Complexity:"), complexity, 0, 10));
    insertSetting(eNARROWBAND,new EncTtsSetting(this, EncTtsSetting::eBOOL,
        tr("Use Narrowband:"), narrowband));
}

void EncoderRbSpeex::saveSettings()
{
    //save settings in user config
    RbSettings::setSubValue("rbspeex",RbSettings::EncoderVolume,
                            getSetting(eVOLUME)->current().toDouble());
    RbSettings::setSubValue("rbspeex",RbSettings::EncoderQuality,
                            getSetting(eQUALITY)->current().toDouble());
    RbSettings::setSubValue("rbspeex",RbSettings::EncoderComplexity,
                            getSetting(eCOMPLEXITY)->current().toInt());
    RbSettings::setSubValue("rbspeex",RbSettings::EncoderNarrowBand,
                            getSetting(eNARROWBAND)->current().toBool());

    RbSettings::sync();
}


void EncoderRbSpeex::loadSettings(void)
{
    // try to get config from settings
    quality = RbSettings::subValue("rbspeex", RbSettings::EncoderQuality).toDouble();
    if(quality < 0) {
        quality = 8.0;
    }
    complexity = RbSettings::subValue("rbspeex", RbSettings::EncoderComplexity).toInt();
    volume = RbSettings::subValue("rbspeex", RbSettings::EncoderVolume).toDouble();
    narrowband = RbSettings::subValue("rbspeex", RbSettings::EncoderNarrowBand).toBool();
}


bool EncoderRbSpeex::start()
{

    // make sure configuration parameters are set.
    loadSettings();
    return true;
}

bool EncoderRbSpeex::encode(QString input,QString output)
{
    LOG_INFO() << "Encoding " << input << " to "<< output;
    char errstr[512];

    FILE *fin,*fout;
    if ((fin = fopen(input.toLocal8Bit(), "rb")) == nullptr) {
        LOG_ERROR() << "Error: could not open input file\n";
        return false;
    }
    if ((fout = fopen(output.toLocal8Bit(), "wb")) == nullptr) {
        LOG_ERROR() << "Error: could not open output file\n";
        fclose(fin);
        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 */
        LOG_ERROR() << "Error:" << errstr;
        QFile(output).remove();
        return false;
    }
    return true;
}

bool EncoderRbSpeex::configOk()
{
    // check config. Make sure current settings are loaded.
    loadSettings();
    if(volume <= 0 || quality <= 0 || complexity <= 0)
        return false;
    else
        return true;
}