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
|
/***************************************************************************
* Copyright (C) 2007-2018 by Ilya Kotov *
* forkotov02@ya.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., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include <QMessageBox>
#include <QRegExp>
#include <QFileInfo>
#ifdef Q_OS_WIN
#include <windows.h>
#define ENABLE_SNDFILE_WINDOWS_PROTOTYPES 1
#endif
#include <sndfile.h>
#include "decoder_sndfile.h"
#include "decodersndfilefactory.h"
#define WAVE_FORMAT_PCM 0x0001
#define WAVE_FORMAT_ADPCM 0x0002
#define WAVE_FORMAT_IEEE_FLOAT 0x0003
#define WAVE_FORMAT_ALAW 0x0006
#define WAVE_FORMAT_MULAW 0x0007
// DecoderSndFileFactory
bool DecoderSndFileFactory::canDecode(QIODevice *input) const
{
char buf[36] = {0};
if(input->peek(buf, sizeof(buf)) != sizeof(buf))
return false;
if(!memcmp(buf + 8, "WAVE", 4) && (!memcmp(buf, "RIFF", 4) || !memcmp(buf, "RIFX", 4)))
{
quint16 subformat = 0;
if(!memcmp(buf + 12, "fmt ", 4))
{
subformat = (quint16(buf[21]) << 8) + buf[20];
}
else if(!input->isSequential())
{
input->seek(12);
//skip "JUNK" and "bext" chunks
while(!input->atEnd())
{
if(input->peek(buf, sizeof(buf)) != sizeof(buf))
return false;
if(!memcmp(buf, "fmt ", 4))
{
subformat = (quint16(buf[9]) << 8) + buf[8];
break;
}
else if(!memcmp(buf, "JUNK", 4) || !memcmp(buf, "bext", 4))
{
size_t size = buf[4] + (buf[5] << 8) + (buf[6] << 16) + (buf[7] << 24);
if(!input->seek(input->pos() + size + 8))
break;
}
else
{
break;
}
}
input->seek(0);
}
switch (subformat)
{
case WAVE_FORMAT_PCM:
case WAVE_FORMAT_ADPCM:
case WAVE_FORMAT_IEEE_FLOAT:
case WAVE_FORMAT_ALAW:
case WAVE_FORMAT_MULAW:
return true;
default:
return false;
}
}
else if(!memcmp(buf, "FORM", 4))
{
if(!memcmp(buf + 8, "AIFF", 4))
return true;
if(!memcmp(buf + 8, "8SVX", 4))
return true;
}
else if(!memcmp(buf, ".snd", 4) || !memcmp(buf, "dns.", 4))
return true;
else if(!memcmp(buf, "fap ", 4) || !memcmp(buf, " paf", 4))
return true;
else if(!memcmp(buf, "NIST", 4))
return true;
else if(!memcmp(buf, "Crea", 4) && !memcmp(buf + 4, "tive", 4))
return true;
else if(!memcmp(buf, "riff", 4))
return true;
return false;
}
DecoderProperties DecoderSndFileFactory::properties() const
{
DecoderProperties properties;
properties.name = tr("Sndfile Plugin");
properties.filters << "*.wav" << "*.au" << "*.snd" << "*.aif" << "*.aiff" << "*.8svx";
properties.filters << "*.sph" << "*.sf" << "*.voc" << "*.w64";
properties.description = tr("PCM Files");
//properties.contentType = "";
properties.shortName = "sndfile";
properties.hasAbout = true;
properties.hasSettings = false;
properties.noInput = false;
return properties;
}
Decoder *DecoderSndFileFactory::create(const QString &, QIODevice *input)
{
return new DecoderSndFile(input);
}
QList<TrackInfo *> DecoderSndFileFactory::createPlayList(const QString &path, TrackInfo::Parts parts, QStringList *)
{
TrackInfo *info = new TrackInfo(path);
if(parts == TrackInfo::NoParts)
return QList<TrackInfo*>() << info;
SF_INFO snd_info;
SNDFILE *sndfile = 0;
memset(&snd_info, 0, sizeof(snd_info));
snd_info.format = 0;
#ifdef Q_OS_WIN
sndfile = sf_wchar_open(reinterpret_cast<LPCWSTR>(path.utf16()), SFM_READ, &snd_info);
#else
sndfile = sf_open(path.toLocal8Bit().constData(), SFM_READ, &snd_info);
#endif
if(!sndfile)
{
delete info;
return QList<TrackInfo *>();
}
if(parts & TrackInfo::MetaData)
{
const char *title = sf_get_string(sndfile, SF_STR_TITLE);
info->setValue(Qmmp::TITLE, title ? QString::fromUtf8(title) : QString());
const char *date = sf_get_string(sndfile, SF_STR_DATE);
info->setValue(Qmmp::YEAR, date ? QString::fromUtf8(date) : QString());
const char *album = sf_get_string(sndfile, SF_STR_ALBUM);
info->setValue(Qmmp::ALBUM, album ? QString::fromUtf8(album) : QString());
const char *track = sf_get_string(sndfile, SF_STR_TRACKNUMBER);
info->setValue(Qmmp::TRACK, track ? QString::fromUtf8(track) : QString());
const char *artist = sf_get_string(sndfile, SF_STR_ARTIST);
info->setValue(Qmmp::ARTIST, artist ? QString::fromUtf8(artist) : QString());
const char *comment = sf_get_string(sndfile, SF_STR_COMMENT);
info->setValue(Qmmp::COMMENT, comment ? QString::fromUtf8(comment) : QString());
const char *genre = sf_get_string(sndfile, SF_STR_GENRE);
info->setValue(Qmmp::COMMENT, genre ? QString::fromUtf8(genre) : QString());
}
if(parts & TrackInfo::Properties)
{
info->setValue(Qmmp::BITRATE, QFileInfo(path).size() * 8000.0 / info->duration() + 0.5);
info->setValue(Qmmp::SAMPLERATE, snd_info.samplerate);
info->setValue(Qmmp::CHANNELS, snd_info.channels);
switch(snd_info.format & SF_FORMAT_SUBMASK)
{
case SF_FORMAT_PCM_S8:
case SF_FORMAT_PCM_U8:
info->setValue(Qmmp::BITS_PER_SAMPLE, 8);
break;
case SF_FORMAT_PCM_16:
info->setValue(Qmmp::BITS_PER_SAMPLE, 16);
break;
case SF_FORMAT_PCM_24:
info->setValue(Qmmp::BITS_PER_SAMPLE, 24);
break;
case SF_FORMAT_PCM_32:
case SF_FORMAT_FLOAT:
info->setValue(Qmmp::BITS_PER_SAMPLE, 32);
break;
case SF_FORMAT_DOUBLE:
info->setValue(Qmmp::BITS_PER_SAMPLE, 64);
break;
}
SF_FORMAT_INFO format_info;
memset(&format_info, 0, sizeof(format_info));
format_info.format = (snd_info.format & SF_FORMAT_TYPEMASK);
sf_command(0, SFC_GET_FORMAT_INFO, &format_info, sizeof(format_info));
info->setValue(Qmmp::FORMAT_NAME, QString::fromLatin1(format_info.name));
info->setDuration(int(snd_info.frames * 1000 / snd_info.samplerate));
}
sf_close(sndfile);
return QList<TrackInfo *>() << info;
}
MetaDataModel* DecoderSndFileFactory::createMetaDataModel(const QString&, bool)
{
return 0;
}
void DecoderSndFileFactory::showSettings(QWidget *)
{}
void DecoderSndFileFactory::showAbout(QWidget *parent)
{
char version [128] = { 0 };
sf_command (NULL, SFC_GET_LIB_VERSION, version, sizeof (version)) ;
QMessageBox::about (parent, tr("About Sndfile Audio Plugin"),
tr("Qmmp Sndfile Audio Plugin")+"\n"+
tr("Compiled against")+" "+QString(version)+"\n" +
tr("Written by: Ilya Kotov <forkotov02@ya.ru>"));
}
QString DecoderSndFileFactory::translation() const
{
return QLatin1String(":/sndfile_plugin_");
}
|