OSDN Git Service

Happy new year 2015 !!!
[lamexp/LameXP.git] / src / Encoder_Wave.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2015 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, but always including the *additional*
9 // restrictions defined in the "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 "Global.h"
26 #include "Model_Settings.h"
27
28 #include <QDir>
29
30 //Windows includes
31 #define NOMINMAX
32 #define WIN32_LEAN_AND_MEAN
33 #include <Windows.h>
34 #include <Shellapi.h>
35
36 #define FIX_SEPARATORS(STR) for(int i = 0; STR[i]; i++) { if(STR[i] == L'/') STR[i] = L'\\'; }
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 static const g_waveEncoderInfo;
110
111 ///////////////////////////////////////////////////////////////////////////////
112 // Encoder implementation
113 ///////////////////////////////////////////////////////////////////////////////
114
115
116 WaveEncoder::WaveEncoder(void)
117 {
118 }
119
120 WaveEncoder::~WaveEncoder(void)
121 {
122 }
123
124 bool WaveEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo &metaInfo, const unsigned int duration, const QString &outputFile, volatile bool *abortFlag)
125 {
126         SHFILEOPSTRUCTW fileOperation;
127         memset(&fileOperation, 0, sizeof(SHFILEOPSTRUCTW));
128         fileOperation.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI | FOF_FILESONLY;
129
130         emit messageLogged(QString("Copy file \"%1\" to \"%2\"").arg(sourceFile, outputFile));
131         fileOperation.wFunc = FO_COPY;
132
133         /*
134         if(lamexp_temp_folder().compare(QFileInfo(sourceFile).canonicalPath(), Qt::CaseInsensitive) == 0)
135         {
136                 //If the source is in the TEMP folder take shortcut and move the file
137                 emit messageLogged(QString("Moving file \"%1\" to \"%2\"").arg(sourceFile, outputFile));
138                 fileOperation.wFunc = FO_MOVE;
139         }
140         else
141         {
142                 //...otherwise we actually copy the file in order to keep the source
143                 emit messageLogged(QString("Copy file \"%1\" to \"%2\"").arg(sourceFile, outputFile));
144                 fileOperation.wFunc = FO_COPY;
145         }
146         */
147         
148         size_t srcLen = wcslen(reinterpret_cast<const wchar_t*>(sourceFile.utf16())) + 3;
149         wchar_t *srcBuffer = new wchar_t[srcLen];
150         memset(srcBuffer, 0, srcLen * sizeof(wchar_t));
151         wcsncpy_s(srcBuffer, srcLen, reinterpret_cast<const wchar_t*>(sourceFile.utf16()), _TRUNCATE);
152         FIX_SEPARATORS (srcBuffer);
153         fileOperation.pFrom = srcBuffer;
154
155         size_t outLen = wcslen(reinterpret_cast<const wchar_t*>(outputFile.utf16())) + 3;
156         wchar_t *outBuffer = new wchar_t[outLen];
157         memset(outBuffer, 0, outLen * sizeof(wchar_t));
158         wcsncpy_s(outBuffer, outLen, reinterpret_cast<const wchar_t*>(outputFile.utf16()), _TRUNCATE);
159         FIX_SEPARATORS (outBuffer);
160         fileOperation.pTo = outBuffer;
161
162         emit statusUpdated(0);
163         int result = SHFileOperation(&fileOperation);
164         emit statusUpdated(100);
165
166         emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", result));
167
168         delete [] srcBuffer;
169         delete [] outBuffer;
170
171         return (result == 0 && fileOperation.fAnyOperationsAborted == false);
172 }
173
174 QString WaveEncoder::extension(void)
175 {
176         return "wav";
177 }
178
179 bool WaveEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
180 {
181         if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
182         {
183                 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
184                 {
185                         return true;
186                 }
187         }
188         return false;
189 }
190
191 const AbstractEncoderInfo *WaveEncoder::getEncoderInfo(void)
192 {
193         return &g_waveEncoderInfo;
194 }