OSDN Git Service

Updated Monkey's Audio binary to v4.11 (2013-01-20), including STDERR flush fix.
[lamexp/LameXP.git] / src / Model_AudioFile.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2013 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "Model_AudioFile.h"
23
24 #include <QTime>
25 #include <QObject>
26 #include <QMutexLocker>
27 #include <QFile>
28 #include <limits.h>
29
30 #define U16Str(X) QString::fromUtf16(reinterpret_cast<const unsigned short*>(L##X))
31
32 const unsigned int AudioFileModel::BITDEPTH_IEEE_FLOAT32 = UINT_MAX-1;
33
34 ////////////////////////////////////////////////////////////
35 // Constructor & Destructor
36 ////////////////////////////////////////////////////////////
37
38 AudioFileModel::AudioFileModel(const QString &path, const QString &name)
39 {
40         resetAll();
41
42         m_filePath = path;
43         m_fileName = name;
44 }
45
46 AudioFileModel::AudioFileModel(const AudioFileModel &model, bool copyMetaInfo)
47 {
48         resetAll();
49
50         setFilePath(model.m_filePath);
51         setFormatContainerType(model.m_formatContainerType);
52         setFormatContainerProfile(model.m_formatContainerProfile);
53         setFormatAudioType(model.m_formatAudioType);
54         setFormatAudioProfile(model.m_formatAudioProfile);
55         setFormatAudioVersion(model.m_formatAudioVersion);
56         setFormatAudioEncodeLib(model.m_formatAudioEncodeLib);
57         setFormatAudioSamplerate(model.m_formatAudioSamplerate);
58         setFormatAudioChannels(model.m_formatAudioChannels);
59         setFormatAudioBitdepth(model.m_formatAudioBitdepth);
60         setFormatAudioBitrate(model.m_formatAudioBitrate);
61         setFormatAudioBitrateMode(model.m_formatAudioBitrateMode);
62         setFileDuration(model.m_fileDuration);
63
64         if(copyMetaInfo)
65         {
66                 setFileName(model.m_fileName);
67                 setFileArtist(model.m_fileArtist);
68                 setFileAlbum(model.m_fileAlbum);
69                 setFileGenre(model.m_fileGenre);
70                 setFileComment(model.m_fileComment);
71                 setFileCover(model.m_fileCover);
72                 setFileYear(model.m_fileYear);
73                 setFilePosition(model.m_filePosition);
74         }
75 }
76
77 AudioFileModel &AudioFileModel::operator=(const AudioFileModel &model)
78 {
79         setFilePath(model.m_filePath);
80         setFileName(model.m_fileName);
81         setFileArtist(model.m_fileArtist);
82         setFileAlbum(model.m_fileAlbum);
83         setFileGenre(model.m_fileGenre);
84         setFileComment(model.m_fileComment);
85         setFileCover(model.m_fileCover);
86         setFileYear(model.m_fileYear);
87         setFilePosition(model.m_filePosition);
88         setFileDuration(model.m_fileDuration);
89
90         setFormatContainerType(model.m_formatContainerType);
91         setFormatContainerProfile(model.m_formatContainerProfile);
92         setFormatAudioType(model.m_formatAudioType);
93         setFormatAudioProfile(model.m_formatAudioProfile);
94         setFormatAudioVersion(model.m_formatAudioVersion);
95         setFormatAudioEncodeLib(model.m_formatAudioEncodeLib);
96         setFormatAudioSamplerate(model.m_formatAudioSamplerate);
97         setFormatAudioChannels(model.m_formatAudioChannels);
98         setFormatAudioBitdepth(model.m_formatAudioBitdepth);
99         setFormatAudioBitrate(model.m_formatAudioBitrate);
100         setFormatAudioBitrateMode(model.m_formatAudioBitrateMode);
101
102         return (*this);
103 }
104
105 AudioFileModel::~AudioFileModel(void)
106 {
107 }
108
109 ////////////////////////////////////////////////////////////
110 // Private Functions
111 ////////////////////////////////////////////////////////////
112
113 void AudioFileModel::resetAll(void)
114 {
115         m_filePath.clear();
116         m_fileName.clear();
117         m_fileArtist.clear();
118         m_fileAlbum.clear();
119         m_fileGenre.clear();
120         m_fileComment.clear();
121         m_fileCover.clear();
122         
123         m_fileYear = 0;
124         m_filePosition = 0;
125         m_fileDuration = 0;
126
127         m_formatContainerType.clear();
128         m_formatContainerProfile.clear();
129         m_formatAudioType.clear();
130         m_formatAudioProfile.clear();
131         m_formatAudioVersion.clear();
132         m_formatAudioEncodeLib.clear();
133         
134         m_formatAudioSamplerate = 0;
135         m_formatAudioChannels = 0;
136         m_formatAudioBitdepth = 0;
137         m_formatAudioBitrate = 0;
138         m_formatAudioBitrateMode = BitrateModeUndefined;
139 }
140
141 ////////////////////////////////////////////////////////////
142 // Public Functions
143 ////////////////////////////////////////////////////////////
144
145 // ---------------------------------
146 // Getter methods
147 // ---------------------------------
148
149 const QString &AudioFileModel::filePath(void) const
150 {
151         return m_filePath;
152 }
153
154 const QString &AudioFileModel::fileName(void) const
155 {
156         return m_fileName;
157 }
158
159 const QString &AudioFileModel::fileArtist(void) const
160 {
161         return m_fileArtist;
162 }
163
164 const QString &AudioFileModel::fileAlbum(void) const
165 {
166         return m_fileAlbum;
167 }
168
169 const QString &AudioFileModel::fileGenre(void) const
170 {
171         return m_fileGenre;
172 }
173
174 const QString &AudioFileModel::fileComment(void) const
175 {
176         return m_fileComment;
177 }
178
179 const QString &AudioFileModel::fileCover(void) const
180 {
181         return m_fileCover.filePath();
182 }
183
184 unsigned int AudioFileModel::fileYear(void) const
185 {
186         return m_fileYear;
187 }
188
189 unsigned int AudioFileModel::filePosition(void) const
190 {
191         return m_filePosition;
192 }
193
194 unsigned int AudioFileModel::fileDuration(void) const
195 {
196         return m_fileDuration;
197 }
198
199 const QString &AudioFileModel::formatContainerType(void) const
200 {
201         return m_formatContainerType;
202 }
203
204 const QString &AudioFileModel::formatContainerProfile(void) const
205 {
206         return m_formatContainerProfile;
207 }
208
209 const QString &AudioFileModel::formatAudioType(void) const
210 {
211         return m_formatAudioType;
212 }
213
214 const QString &AudioFileModel::formatAudioProfile(void) const
215 {
216         return m_formatAudioProfile;
217 }
218
219 const QString &AudioFileModel::formatAudioVersion(void) const
220 {
221         return m_formatAudioVersion;
222 }
223
224 unsigned int AudioFileModel::formatAudioSamplerate(void) const
225 {
226         return m_formatAudioSamplerate;
227 }
228
229 unsigned int AudioFileModel::formatAudioChannels(void) const
230 {
231         return m_formatAudioChannels;
232 }
233
234 unsigned int AudioFileModel::formatAudioBitdepth(void) const
235 {
236         return m_formatAudioBitdepth;
237 }
238
239 unsigned int AudioFileModel::formatAudioBitrate(void) const
240 {
241         return m_formatAudioBitrate;
242 }
243
244 unsigned int  AudioFileModel::formatAudioBitrateMode(void) const
245 {
246         return m_formatAudioBitrateMode;
247 }
248
249 const QString AudioFileModel::fileDurationInfo(void) const
250 {
251         if(m_fileDuration)
252         {
253                 QTime time = QTime().addSecs(m_fileDuration);
254                 return time.toString("hh:mm:ss");
255         }
256         else
257         {
258                 return QString();
259         }
260 }
261
262 const QString AudioFileModel::formatContainerInfo(void) const
263 {
264         if(!m_formatContainerType.isEmpty())
265         {
266                 QString info = m_formatContainerType;
267                 if(!m_formatContainerProfile.isEmpty()) info.append(QString(" (%1: %2)").arg(tr("Profile"), m_formatContainerProfile));
268                 return info;
269         }
270         else
271         {
272                 return QString();
273         }
274 }
275
276 const QString AudioFileModel::formatAudioBaseInfo(void) const
277 {
278         if(m_formatAudioSamplerate || m_formatAudioChannels || m_formatAudioBitdepth)
279         {
280                 QString info;
281                 if(m_formatAudioChannels)
282                 {
283                         if(!info.isEmpty()) info.append(", ");
284                         info.append(QString("%1: %2").arg(tr("Channels"), QString::number(m_formatAudioChannels)));
285                 }
286                 if(m_formatAudioSamplerate)
287                 {
288                         if(!info.isEmpty()) info.append(", ");
289                         info.append(QString("%1: %2 Hz").arg(tr("Samplerate"), QString::number(m_formatAudioSamplerate)));
290                 }
291                 if(m_formatAudioBitdepth)
292                 {
293                         if(!info.isEmpty()) info.append(", ");
294                         if(m_formatAudioBitdepth == BITDEPTH_IEEE_FLOAT32)
295                         {
296                                 info.append(QString("%1: %2 Bit (IEEE Float)").arg(tr("Bitdepth"), QString::number(32)));
297                         }
298                         else
299                         {
300                                 info.append(QString("%1: %2 Bit").arg(tr("Bitdepth"), QString::number(m_formatAudioBitdepth)));
301                         }
302                 }
303                 return info;
304         }
305         else
306         {
307                 return QString();
308         }
309 }
310
311 const QString AudioFileModel::formatAudioCompressInfo(void) const
312 {
313         if(!m_formatAudioType.isEmpty())
314         {
315                 QString info;
316                 if(!m_formatAudioProfile.isEmpty() || !m_formatAudioVersion.isEmpty())
317                 {
318                         info.append(QString("%1: ").arg(tr("Type")));
319                 }
320                 info.append(m_formatAudioType);
321                 if(!m_formatAudioProfile.isEmpty())
322                 {
323                         info.append(QString(", %1: %2").arg(tr("Profile"), m_formatAudioProfile));
324                 }
325                 if(!m_formatAudioVersion.isEmpty())
326                 {
327                         info.append(QString(", %1: %2").arg(tr("Version"), m_formatAudioVersion));
328                 }
329                 if(m_formatAudioBitrate > 0)
330                 {
331                         switch(m_formatAudioBitrateMode)
332                         {
333                         case BitrateModeConstant:
334                                 info.append(U16Str(", %1: %2 kbps (%3)").arg(tr("Bitrate"), QString::number(m_formatAudioBitrate), tr("Constant")));
335                                 break;
336                         case BitrateModeVariable:
337                                 info.append(U16Str(", %1: \u2248%2 kbps (%3)").arg(tr("Bitrate"), QString::number(m_formatAudioBitrate), tr("Variable")));
338                                 break;
339                         default:
340                                 info.append(U16Str(", %1: %2 kbps").arg(tr("Bitrate"), QString::number(m_formatAudioBitrate)));
341                                 break;
342                         }
343                 }
344                 if(!m_formatAudioEncodeLib.isEmpty())
345                 {
346                         info.append(QString(", %1: %2").arg(tr("Encoder"), m_formatAudioEncodeLib));
347                 }
348                 return info;
349         }
350         else
351         {
352                 return QString();
353         }
354 }
355
356 // ---------------------------------
357 // Setter methods
358 // ---------------------------------
359
360 void AudioFileModel::setFilePath(const QString &path)
361 {
362         m_filePath = path;
363 }
364
365 void AudioFileModel::setFileName(const QString &name)
366 {
367         m_fileName = name;
368 }
369
370 void AudioFileModel::setFileArtist(const QString &artist)
371 {
372         m_fileArtist = artist;
373 }
374
375 void AudioFileModel::setFileAlbum(const QString &album)
376 {
377         m_fileAlbum = album;
378 }
379
380 void AudioFileModel::setFileGenre(const QString &genre)
381 {
382         m_fileGenre = genre;
383 }
384
385 void AudioFileModel::setFileComment(const QString &comment)
386 {
387         m_fileComment = comment;
388 }
389
390 void AudioFileModel::setFileCover(const QString &coverFile, bool owner)
391 {
392         m_fileCover.setFilePath(coverFile, owner);
393 }
394
395 void AudioFileModel::setFileCover(const ArtworkModel &model)
396 {
397         m_fileCover = model;
398 }
399
400 void AudioFileModel::setFileYear(unsigned int year)
401 {
402         m_fileYear = year;
403 }
404
405 void AudioFileModel::setFilePosition(unsigned int position)
406 {
407         m_filePosition = position;
408 }
409
410 void AudioFileModel::setFileDuration(unsigned int duration)
411 {
412         m_fileDuration = duration;
413 }
414
415 void AudioFileModel::setFormatContainerType(const QString &type)
416 {
417         m_formatContainerType = type;
418 }
419
420 void AudioFileModel::setFormatContainerProfile(const QString &profile)
421 {
422         m_formatContainerProfile = profile;
423 }
424
425 void AudioFileModel::setFormatAudioType(const QString &type)
426 {
427         m_formatAudioType = type;
428 }
429
430 void AudioFileModel::setFormatAudioProfile(const QString &profile)
431 {
432         m_formatAudioProfile = profile;
433 }
434
435 void AudioFileModel::setFormatAudioVersion(const QString &version)
436 {
437         m_formatAudioVersion = version;
438 }
439
440 void AudioFileModel::setFormatAudioEncodeLib(const QString &encodeLib)
441 {
442         m_formatAudioEncodeLib = encodeLib;
443 }
444
445 void AudioFileModel::setFormatAudioSamplerate(unsigned int samplerate)
446 {
447         m_formatAudioSamplerate = samplerate;
448 }
449
450 void AudioFileModel::setFormatAudioChannels(unsigned int channels)
451 {
452         m_formatAudioChannels = channels;
453 }
454
455 void AudioFileModel::setFormatAudioBitdepth(unsigned int bitdepth)
456 {
457         m_formatAudioBitdepth = bitdepth;
458 }
459
460 void AudioFileModel::setFormatAudioBitrate(unsigned int bitrate)
461 {
462         m_formatAudioBitrate = bitrate;
463 }
464
465 void AudioFileModel::setFormatAudioBitrateMode(unsigned int bitrateMode)
466 {
467         m_formatAudioBitrateMode = bitrateMode;
468 }
469
470 void AudioFileModel::updateMetaInfo(const AudioFileModel &model)
471 {
472         if(!model.fileArtist().isEmpty()) setFileArtist(model.fileArtist());
473         if(!model.fileAlbum().isEmpty()) setFileAlbum(model.fileAlbum());
474         if(!model.fileGenre().isEmpty()) setFileGenre(model.fileGenre());
475         if(!model.fileComment().isEmpty()) setFileComment(model.fileComment());
476         if(model.fileYear()) setFileYear(model.fileYear());
477 }