-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathIO.h
executable file
·78 lines (62 loc) · 1.97 KB
/
IO.h
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
#pragma once
#include <algorithm>
#include <cmath>
#include <limits>
#include <numeric>
#include <stdexcept>
#include <string>
#include <vector>
namespace stftpitchshift
{
class IO
{
public:
static void read(const std::string& path, std::vector<float>& data, double& samplerate, size_t& channels);
static void read(const std::string& path, std::vector<double>& data, double& samplerate, size_t& channels);
static void write(const std::string& path, const std::vector<float>& data, const double samplerate, const size_t channels);
static void write(const std::string& path, const std::vector<double>& data, const double samplerate, const size_t channels);
template<class T>
static void norms(std::vector<T>& data, const std::vector<T>& target)
{
const T a = std::inner_product(target.begin(), target.end(), target.begin(), T(0));
const T b = std::inner_product(data.begin(), data.end(), data.begin(), T(0));
if (b == 0)
{
return;
}
const T c = std::sqrt(a / b);
std::transform(data.begin(), data.end(), data.begin(),
[c](const T value) { return value * c; });
}
template<class T>
static void clip(std::vector<T>& data)
{
const T min = T(-0.999);
const T max = T(+0.999);
for (size_t i = 0; i < data.size(); ++i)
{
data[i] = std::min(std::max(data[i], min), max);
}
}
template<class T>
static void split(const size_t size, const T* input, T* const output)
{
const size_t halfsize = size / 2;
for (size_t i = 0; i < halfsize; ++i)
{
output[i] = input[i * 2];
output[i + halfsize] = input[i * 2 + 1];
}
}
template<class T>
static void merge(const size_t size, const T* input, T* const output)
{
const size_t halfsize = size / 2;
for (size_t i = 0; i < halfsize; ++i)
{
output[i * 2] = input[i];
output[i * 2 + 1] = input[i + halfsize];
}
}
};
}