OSDN Git Service

Updated Simplified Chinese translation, thanks to Hongchuan Zhuang <kidneybean@sohu...
[lamexp/LameXP.git] / src / Encoder_Wave.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2023 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; always including the non-optional
9 // LAMEXP GNU GENERAL PUBLIC LICENSE ADDENDUM. See "License.txt" file!
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 //
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #include "Encoder_Wave.h"
24
25 #include <MUtils/Global.h>
26 #include <MUtils/OSSupport.h>
27
28 #include "Global.h"
29 #include "Model_Settings.h"
30
31 typedef struct _callback_t
32 {
33         WaveEncoder *const pInstance;
34         QAtomicInt  *const abortFlag;
35 }
36 callback_t;
37
38 ///////////////////////////////////////////////////////////////////////////////
39 // Encoder Info
40 ///////////////////////////////////////////////////////////////////////////////
41
42 class WaveEncoderInfo : public AbstractEncoderInfo
43 {
44 public:
45         virtual bool isModeSupported(int mode) const
46         {
47                 switch(mode)
48                 {
49                 case SettingsModel::VBRMode:
50                 case SettingsModel::ABRMode:
51                         return false;
52                         break;
53                 case SettingsModel::CBRMode:
54                         return true;
55                         break;
56                 default:
57                         MUTILS_THROW("Bad RC mode specified!");
58                 }
59         }
60
61         virtual int valueCount(int mode) const
62         {
63                 switch(mode)
64                 {
65                 case SettingsModel::VBRMode:
66                 case SettingsModel::ABRMode:
67                 case SettingsModel::CBRMode:
68                         return 0;
69                         break;
70                 default:
71                         MUTILS_THROW("Bad RC mode specified!");
72                 }
73         }
74
75         virtual int valueAt(int mode, int /*index*/) const
76         {
77                 switch(mode)
78                 {
79                 case SettingsModel::VBRMode:
80                 case SettingsModel::ABRMode:
81                 case SettingsModel::CBRMode:
82                         return -1;
83                         break;
84                 default:
85                         MUTILS_THROW("Bad RC mode specified!");
86                 }
87         }
88
89         virtual int valueType(int mode) const
90         {
91                 switch(mode)
92                 {
93                 case SettingsModel::VBRMode:
94                 case SettingsModel::ABRMode:
95                 case SettingsModel::CBRMode:
96                         return TYPE_UNCOMPRESSED;
97                         break;
98                 default:
99                         MUTILS_THROW("Bad RC mode specified!");
100                 }
101         }
102
103         virtual const char *description(void) const
104         {
105                 static const char* s_description = "Wave Audio (PCM)";
106                 return s_description;
107         }
108
109         virtual const char *extension(void) const
110         {
111                 static const char* s_extension = "wav";
112                 return s_extension;
113         }
114
115         virtual bool isResamplingSupported(void) const
116         {
117                 return false;
118         }
119 }
120 static const g_waveEncoderInfo;
121
122 ///////////////////////////////////////////////////////////////////////////////
123 // Encoder implementation
124 ///////////////////////////////////////////////////////////////////////////////
125
126
127 WaveEncoder::WaveEncoder(void)
128 {
129 }
130
131 WaveEncoder::~WaveEncoder(void)
132 {
133 }
134
135 bool WaveEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo& /*metaInfo*/, const unsigned int /*duration*/, const unsigned int /*channels*/, const QString &outputFile, QAtomicInt &abortFlag)
136 {
137         emit messageLogged(QString("Copy file \"%1\" to \"%2\"\n").arg(sourceFile, outputFile));
138
139         callback_t callbackData = { this, &abortFlag };
140
141         emit statusUpdated(0);
142         const bool success = MUtils::OS::copy_file(sourceFile, outputFile, true, progressCallback, &callbackData);
143         emit statusUpdated(100);
144
145         if (success)
146         {
147                 emit messageLogged(L1S("File copied successfully."));
148         }
149         else
150         {
151                 emit messageLogged(CHECK_FLAG(abortFlag) ? L1S("Operation cancelled by user!")  : L1S("Error: Failed to copy file!"));
152         }
153
154         return success;
155 }
156
157 bool WaveEncoder::progressCallback(const double &progress, void *const userData)
158 {
159         if (const callback_t *const ptr = reinterpret_cast<callback_t*>(userData))
160         {
161                 ptr->pInstance->updateProgress(progress);
162                 return ptr->abortFlag->operator!();
163         }
164         return true;
165 }
166
167 void WaveEncoder::updateProgress(const double &progress)
168 {
169         emit statusUpdated(qRound(progress * 100.0));
170 }
171
172 bool WaveEncoder::isFormatSupported(const QString &containerType, const QString& /*containerProfile*/, const QString &formatType, const QString& /*formatProfile*/, const QString& /*formatVersion*/)
173 {
174         if(containerType.compare(L1S("Wave"), Qt::CaseInsensitive) == 0)
175         {
176                 if(formatType.compare(L1S("PCM"), Qt::CaseInsensitive) == 0)
177                 {
178                         return true;
179                 }
180         }
181         return false;
182 }
183
184 const AbstractEncoderInfo *WaveEncoder::getEncoderInfo(void)
185 {
186         return &g_waveEncoderInfo;
187 }