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
|
/*
* This file is part of AtracDEnc.
*
* AtracDEnc is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* AtracDEnc 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with AtracDEnc; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "wav.h"
#include "utf8_file.h"
#include <sndfile.hh>
#include <algorithm>
#include <stdexcept>
static int fileext_to_libsndfmt(const std::string& filename) {
int fmt = SF_FORMAT_WAV; //default fmt
if (filename == "-")
return SF_FORMAT_AU;
size_t pos = filename.find_last_of(".");
if (pos == std::string::npos || pos == filename.size() - 1) //no dot or filename.
return fmt;
std::string ext = filename.substr(pos+1);
std::transform(ext.begin(), ext.end(), ext.begin(), ::toupper);
if (ext == "AU") {
fmt = SF_FORMAT_AU;
} else if (ext == "AIFF") {
fmt = SF_FORMAT_AIFF;
} else if (ext == "PCM" || ext == "RAW") {
fmt = SF_FORMAT_RAW;
}
return fmt;
}
static SndfileHandle OpenSndFileRead(const std::string& filename) {
#ifdef _WIN32
const std::wstring wpath = NAtracDEnc::Utf8ToWidePath(filename);
return SndfileHandle(wpath.c_str());
#else
return SndfileHandle(filename);
#endif
}
static SndfileHandle OpenSndFileWrite(const std::string& filename, int channels, int sampleRate) {
const int format = fileext_to_libsndfmt(filename) | SF_FORMAT_PCM_16;
#ifdef _WIN32
const std::wstring wpath = NAtracDEnc::Utf8ToWidePath(filename);
return SndfileHandle(wpath.c_str(), SFM_WRITE, format, channels, sampleRate);
#else
return SndfileHandle(filename, SFM_WRITE, format, channels, sampleRate);
#endif
}
static void CheckSndFileOpen(SndfileHandle& file, const std::string& path, const char* mode) {
if (file.rawHandle()) {
return;
}
std::string msg = "unable to open ";
msg += mode;
msg += " file '";
msg += path;
msg += "'";
if (file) {
const char* err = file.strError();
if (err && err[0]) {
msg += ": ";
msg += err;
}
}
throw std::runtime_error(msg);
}
class TPCMIOSndFile : public IPCMProviderImpl {
public:
TPCMIOSndFile(const std::string& filename)
: File(OpenSndFileRead(filename))
{
CheckSndFileOpen(File, filename, "input");
File.command(SFC_SET_NORM_DOUBLE /*| SFC_SET_NORM_FLOAT*/, nullptr, SF_TRUE);
}
TPCMIOSndFile(const std::string& filename, int channels, int sampleRate)
: File(OpenSndFileWrite(filename, channels, sampleRate))
{
CheckSndFileOpen(File, filename, "output");
File.command(SFC_SET_NORM_DOUBLE /*| SFC_SET_NORM_FLOAT*/, nullptr, SF_TRUE);
}
public:
size_t GetChannelsNum() const override {
return File.channels();
}
size_t GetSampleRate() const override {
return File.samplerate();
}
size_t GetTotalSamples() const override {
return File.frames();
}
size_t Read(TPCMBuffer& buf, size_t sz) override {
return File.readf(buf[0], sz);
}
size_t Write(const TPCMBuffer& buf, size_t sz) override {
return File.writef(buf[0], sz);
}
private:
mutable SndfileHandle File;
};
IPCMProviderImpl* CreatePCMIOReadImpl(const std::string& path) {
return new TPCMIOSndFile(path);
}
IPCMProviderImpl* CreatePCMIOWriteImpl(const std::string& path, int channels, int sampleRate) {
return new TPCMIOSndFile(path, channels, sampleRate);
}
|