aboutsummaryrefslogtreecommitdiffstats
path: root/src/pcm_io_sndfile.cpp
blob: 52737c02f69e8b99e11f802b7285ed685eacd0ab (plain) (blame)
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
/*
 * 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 <sndfile.hh>
#include <algorithm>

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;
}

class TPCMIOSndFile : public IPCMProviderImpl {
public:
    TPCMIOSndFile(const std::string& filename)
        : File(SndfileHandle(filename))
    {
        File.command(SFC_SET_NORM_DOUBLE /*| SFC_SET_NORM_FLOAT*/, nullptr, SF_TRUE);
    }
    TPCMIOSndFile(const std::string& filename, int channels, int sampleRate)
        : File(SndfileHandle(filename, SFM_WRITE, fileext_to_libsndfmt(filename) | SF_FORMAT_PCM_16, channels, sampleRate))
    {
        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<TFloat>& buf, size_t sz) override {
        return File.readf(buf[0], sz);
    }
    size_t Write(const TPCMBuffer<TFloat>& 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);
}