aboutsummaryrefslogtreecommitdiffstats
path: root/src/wav.cpp
blob: b7757f69cf4fe4be48732b40bd8c73987cde07ee (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
#include <functional>
#include <memory>
#include <cerrno>
#include <algorithm>
#include <string>

#include <sys/stat.h>
#include <string.h>

#include "wav.h"
#include "pcmengin.h"

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

TWav::TWav(const std::string& filename)
    : File(SndfileHandle(filename)) {
    //disable scaling short -> [-1.0, 1.0]
    File.command(SFC_SET_NORM_DOUBLE /*| SFC_SET_NORM_FLOAT*/, nullptr, SF_FALSE);
}

TWav::TWav(const std::string& filename, int channels, int sampleRate)
    : File(SndfileHandle(filename, SFM_WRITE, fileext_to_libsndfmt(filename) | SF_FORMAT_PCM_16, channels, sampleRate)) {
    //disable scaling short -> [-1.0, 1.0]
    File.command(SFC_SET_NORM_DOUBLE /*| SFC_SET_NORM_FLOAT*/, nullptr, SF_FALSE);
}
TWav::~TWav() {
}

uint64_t TWav::GetTotalSamples() const {
    return File.frames();
}
uint32_t TWav::GetChannelNum() const {
    return File.channels();
}
uint32_t TWav::GetSampleRate() const {
    return File.samplerate();
}