Repository navigation
Expand file tree
/
Copy pathMp3Export.cpp
More file actions
303 lines (274 loc) · 11.5 KB
/
Copy pathMp3Export.cpp
File metadata and controls
303 lines (274 loc) · 11.5 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include "mp3lame/lame.h"
#include "modplug/modplug.h"
#include "Mp3Export.hpp"
#include "FileUtils.hpp"
#include <QFile>
#include <QDebug>
#include <QByteArray>
#include "MessageBox.hpp"
//#define VERBOSE_LOGGING
#undef WRITE_ID3V1_TAG
using namespace bb::system;
Mp3Export::Mp3Export(QObject * parent)
: QObject(parent) {
}
Mp3Export::~Mp3Export() {
#ifdef VERBOSE_LOGGING
qDebug() << "Mp3Export::~Mp3Export()";
#endif
}
bool Mp3Export::convert(PlaybackConfig &config,
QString const& inputFileName,
QString const& outputFileName) {
bool result = false;
#ifdef VERBOSE_LOGGING
qDebug() << "Starting converting" << inputFileName << "to" << outputFileName;
#endif
QString comment = QString("Created by ModPlayer from %1").arg(FileUtils::fileNameOnly(inputFileName));
#ifdef VERBOSE_LOGGING
qDebug() << "Comment:" << comment;
#endif
QFile inputFile(inputFileName);
if(!FileUtils::fileExists(inputFileName)) {
qDebug() << "Input file" << inputFileName << "does not exist";
return result;
}
if(!inputFile.open(QFile::ReadOnly)) {
qDebug() << "Failed to open input file" << inputFileName;
return result;
}
QFile outputFile(outputFileName);
if(FileUtils::fileExists(outputFileName)) {
qDebug() << "Deleting output file" << outputFileName;
if(!outputFile.remove()) {
qDebug() << "Failed to delete output file" << outputFileName;
return result;
}
}
if(!outputFile.open(QFile::WriteOnly)) {
qDebug() << "Failed to open output file" << outputFileName;
return result;
}
QByteArray data = inputFile.readAll();
if(data.size() == 0) {
qDebug() << "Failed to read file" << inputFileName;
return result;
}
#ifdef VERBOSE_LOGGING
qDebug() << "Read" << data.size() << "bytes from file" << inputFileName;
#endif
ModPlugFile* module = ::ModPlug_Load(data.data(), data.size());
if(module == NULL) {
qDebug() << "Failed to load module" << inputFileName;
return result;
}
#ifdef VERBOSE_LOGGING
qDebug() << "Module" << inputFileName << " loaded successfully";
#endif
const int numOrders = ::ModPlug_NumOrders(module);
int currentOrder = 0;
MessageBox message(tr("Creating MP3 file"),
tr("Creating %1").arg(FileUtils::fileNameOnly(outputFileName)),
false,
0);
int readBytes;
int writeBytes;
const size_t BUFFER_SIZE = 1024*1024;
QByteArray mod_buffer(BUFFER_SIZE, 0);
QByteArray mp3_buffer(BUFFER_SIZE, 0);
do {
lame_t lame = ::lame_init();
if(lame == NULL) {
qDebug() << "Failed to initialize lame encoder";
break;
}
#ifdef VERBOSE_LOGGING
qDebug() << "Lame encoder initialized successfully";
#endif
const int frequency = config.frequency();
const bool isStereo = config.stereo();
const int sampleBitSize = config.sampleSize();
const int numBytesPerSample = sampleBitSize >> 3;
const int numChannels = (isStereo ? 2 : 1);
#ifdef VERBOSE_LOGGING
qDebug().nospace()
<< "Frequency: "
<< frequency
<< ", stereo: "
<< isStereo
<< ", channels: "
<< numChannels
<< ", bytes per sample: "
<< numBytesPerSample;
qDebug().space();
#endif
if(-1 == ::lame_set_in_samplerate(lame, frequency)) {
qDebug() << "Failed to set input sample rate:" << frequency;
break;
}
if(-1 == ::lame_set_out_samplerate(lame, frequency)) {
qDebug() << "Failed to set output sample rate:" << frequency;
break;
}
if(-1 == ::lame_set_mode(lame, isStereo ? STEREO : MONO)) {
qDebug() << "Failed to set mode:" << (isStereo ? STEREO : MONO);
break;
}
if(-1 == ::lame_set_num_channels(lame, numChannels)) {
qDebug() << "Failed to set number of channels:" << numChannels;
break;
}
if(-1 == ::lame_set_quality(lame, 5)) {
qDebug() << "Failed to set quality:" << 5;
break;
}
if(-1 == ::lame_set_VBR(lame, vbr_default)) {
qDebug() << "Failed to set variable bit rate";
break;
}
#ifdef WRITE_ID3V1_TAG
if(title.length() > 0) {
id3tag_set_title(lame, title.toAscii().constData());
}
if(comment.length() > 0) {
id3tag_set_comment(lame, comment.toAscii().constData());
}
::lame_set_write_id3tag_automatic(lame, 0);
#endif
if(0 != ::lame_init_params(lame)) {
qDebug() << "Failed to initialize lame encoder parameters";
break;
}
#ifdef VERBOSE_LOGGING
qDebug().nospace()
<< "Starting encoding, input buffer="
<< mod_buffer.size()
<< ", output buffer="
<< mp3_buffer.size();
qDebug().space();
#endif
do {
readBytes = ::ModPlug_Read(module,
mod_buffer.data(),
mod_buffer.size());
#ifdef VERBOSE_LOGGING
qDebug() << "Read" << readBytes << "bytes";
#endif
if (readBytes == 0) {
#ifdef VERBOSE_LOGGING
qDebug() << "Flushing lame encoder";
#endif
writeBytes = ::lame_encode_flush(lame,
reinterpret_cast<unsigned char*>(mp3_buffer.data()),
mp3_buffer.size());
} else {
const int numSamplesPerChannel = readBytes / (numBytesPerSample * numChannels);
if(isStereo)
{
switch(numBytesPerSample){
case 4:
writeBytes = ::lame_encode_buffer_interleaved_int(lame,
reinterpret_cast<int*>(mod_buffer.data()),
numSamplesPerChannel,
reinterpret_cast<unsigned char*>(mp3_buffer.data()),
mp3_buffer.size());
break;
case 2:
writeBytes = ::lame_encode_buffer_interleaved(lame,
reinterpret_cast<short int*>(mod_buffer.data()),
numSamplesPerChannel,
reinterpret_cast<unsigned char*>(mp3_buffer.data()),
mp3_buffer.size());
break;
case 1:
writeBytes = ::lame_encode_buffer_interleaved_char(lame,
reinterpret_cast<char*>(mod_buffer.data()),
numSamplesPerChannel,
reinterpret_cast<unsigned char*>(mp3_buffer.data()),
mp3_buffer.size());
break;
default:
break;
}
}
else
{
// MONO
switch(numBytesPerSample){
case 4:
writeBytes = ::lame_encode_buffer_int(lame,
reinterpret_cast<int*>(mod_buffer.data()),
reinterpret_cast<int*>(mod_buffer.data()),
numSamplesPerChannel,
reinterpret_cast<unsigned char*>(mp3_buffer.data()),
mp3_buffer.size());
break;
case 2:
writeBytes = ::lame_encode_buffer(lame,
reinterpret_cast<short int*>(mod_buffer.data()),
reinterpret_cast<short int*>(mod_buffer.data()),
numSamplesPerChannel,
reinterpret_cast<unsigned char*>(mp3_buffer.data()),
mp3_buffer.size());
break;
case 1:
writeBytes = ::lame_encode_buffer_char(lame,
reinterpret_cast<char*>(mod_buffer.data()),
reinterpret_cast<char*>(mod_buffer.data()),
numSamplesPerChannel,
reinterpret_cast<unsigned char*>(mp3_buffer.data()),
mp3_buffer.size());
break;
default:
break;
}
}
}
const int bytesWritten = outputFile.write(mp3_buffer.data(),
writeBytes);
if(writeBytes != bytesWritten) {
qDebug().nospace();
qDebug() << "Failed to write" << writeBytes << "bytes, written:" << bytesWritten;
qDebug().space();
break;
}
if(!outputFile.flush()) {
qDebug() << "Failed to flush";
break;
}
#ifdef VERBOSE_LOGGING
qDebug() << "Wrote" << bytesWritten << "bytes";
#endif
int order = ::ModPlug_GetCurrentOrder(module);
if(order != currentOrder) {
currentOrder = order;
int progress = currentOrder * 100 / numOrders;
message.setProgress(progress);
}
} while(readBytes > 0);
#ifdef WRITE_ID3V1_TAG
writeBytes = ::lame_get_id3v1_tag(lame,
reinterpret_cast<unsigned char*>(mp3_buffer.data()),
mp3_buffer.size());
if(writeBytes > 0) {
const int bytesWritten = outputFile.write(mp3_buffer.data(),
writeBytes);
if(writeBytes != bytesWritten) {
qDebug() << "Failed to write idv3 tag" << writeBytes << "bytes, written:" << bytesWritten;
break;
}
if(!outputFile.flush()) {
qDebug() << "Failed to flush idv3 tag";
break;
}
}
#endif
if(0 != ::lame_close(lame)) {
qDebug() << "Failed to close lame encoder";
break;
}
result = true;
} while(0);
::ModPlug_Unload(module);
return result;
}