aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/Input/cue/decoder_cue.cpp
blob: 8ee78610523f4b1e22a75d397e19789736bab885 (plain) (blame)
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
/***************************************************************************
 *   Copyright (C) 2008-2009 by Ilya Kotov                                 *
 *   forkotov02@hotmail.ru                                                 *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <qmmp/buffer.h>
#include <qmmp/output.h>
#include <qmmp/recycler.h>
#include <qmmp/fileinfo.h>
#include <qmmp/decoderfactory.h>
#include <qmmp/soundcore.h>

#include <QObject>
#include <QFile>

#include "cueparser.h"
#include "decoder_cue.h"


DecoderCUE::DecoderCUE(const QString &url, QIODevice *input)
        : Decoder(input)
{
    m_path = url;
    m_decoder = 0;
}

DecoderCUE::~DecoderCUE()
{
    if(m_decoder)
        delete m_decoder;
    m_decoder = 0;
}

bool DecoderCUE::initialize()
{
    QString p = QUrl(m_path).path();
    p.replace(QString(QUrl::toPercentEncoding("#")), "#");
    p.replace(QString(QUrl::toPercentEncoding("%")), "%");
    CUEParser parser(p);
    if (parser.count() == 0)
    {
        qWarning("DecoderCUE: invalid cue file");
        return FALSE;
    }
    int track = m_path.section("#", -1).toInt();
    m_path = parser.filePath(track);
    if (!QFile::exists(m_path))
    {
        qWarning("DecoderCUE: file \"%s\" doesn't exist", qPrintable(m_path));
        return FALSE;
    }
    DecoderFactory *df = Decoder::findByPath(m_path);
    if (!df)
    {
        qWarning("DecoderCUE: unsupported file format");
        return FALSE;
    }
    m_length = parser.length(track);
    m_offset = parser.offset(track);

    m_decoder = df->create(new QFile(m_path), m_path);
    if(!m_decoder->initialize())
    {
        qWarning("DecoderCUE: invalid audio file");
        return FALSE;
    }
    m_decoder->seek(m_offset);

    configure(m_decoder->audioParameters().sampleRate(),
              m_decoder->audioParameters().channels(),
              m_decoder->audioParameters().bits());
    offset_in_bytes = audioParameters().sampleRate() *
                      audioParameters().channels() *
                      audioParameters().bits() * m_length/8000;
    m_totalBytes = 0;
    return TRUE;
}

qint64 DecoderCUE::totalTime()
{
    return m_decoder ? m_length : 0;
}

void DecoderCUE::seek(qint64 pos)
{
     m_decoder->seek(m_offset + pos);
     m_totalBytes = audioParameters().sampleRate() *
                    audioParameters().channels() *
                    audioParameters().bits() * pos/8000;
}

qint64 DecoderCUE::read(char *data, qint64 size)
{
    qint64 len = m_decoder->read(data, size);
    m_totalBytes += len;
    if(len > offset_in_bytes - m_totalBytes)
    {
        len = offset_in_bytes - m_totalBytes;
        int sample_size = audioParameters().bits() * audioParameters().channels()/8;
        len = (len / sample_size) * sample_size;
    }
    if(len < 0)
        len = 0;
    return len;
}

int DecoderCUE::bitrate()
{
    return m_decoder->bitrate();
}