-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathtest_xaudio.cpp
More file actions
72 lines (59 loc) · 2.37 KB
/
test_xaudio.cpp
File metadata and controls
72 lines (59 loc) · 2.37 KB
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
/***************************************************************************
* Copyright (c) Wolf Vollprecht, Sylvain Corlay and Johan Mabille *
* Copyright (c) QuantStack *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#include "gtest/gtest.h"
#include "xtensor-io/xaudio.hpp"
#include "xtensor/xio.hpp"
namespace xt
{
TEST(xaudio, load)
{
auto res = xt::load_audio("files/xtensor.wav");
EXPECT_EQ(std::get<0>(res), 44100); // bitrate
auto& arr = std::get<1>(res);
EXPECT_EQ(arr.shape()[0], 70656ul); // channels
EXPECT_EQ(arr.shape()[1], 2ul); // channels
// test begin / end values
EXPECT_EQ(arr(0, 0), -157);
EXPECT_EQ(arr(0, 1), -57);
EXPECT_EQ(arr(70655, 0), 233);
EXPECT_EQ(arr(70655, 1), 140);
}
TEST(xaudio, roundtrip)
{
auto load_one = xt::load_audio("files/xtensor.wav");
auto& arr_one = std::get<1>(load_one);
xt::dump_audio("files/out.wav", arr_one, std::get<0>(load_one));
auto load_two = xt::load_audio("files/out.wav");
EXPECT_EQ(std::get<0>(load_one), std::get<0>(load_two));
EXPECT_EQ(std::get<1>(load_one), std::get<1>(load_two));
}
TEST(xaudio, dump_sine)
{
int freq = 2000;
int sampling_freq = 44100;
double duration = 1.0;
auto t = xt::arange(0.0, duration, 1.0 / sampling_freq);
auto y = xt::sin(2.0 * numeric_constants<double>::PI * freq * t);
xt::dump_audio("files/sine.wav", y, sampling_freq);
auto load_back = xt::load_audio("files/sine.wav");
xt::xarray<short> expected = xt::cast<short>(xt::round(y * 32767.0));
expected.reshape({44100, 1});
EXPECT_TRUE(all(equal(std::get<1>(load_back), expected)));
EXPECT_EQ(std::get<0>(load_back), sampling_freq);
}
TEST(xaudio, errors)
{
EXPECT_THROW(load_audio("files/big.jpg"), std::runtime_error);
EXPECT_THROW(load_audio("hahahahahaha.jpg"), std::runtime_error);
auto load_one = xt::load_audio("files/xtensor.wav");
auto& arr_one = std::get<1>(load_one);
EXPECT_THROW(xt::dump_audio("/nonexistingdir/out.wav", arr_one, std::get<0>(load_one)),
std::runtime_error);
}
}