summaryrefslogtreecommitdiff
blob: dc080bb9847010c3eacb3c7f985bceef403b2493 (plain)
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
From 8d5f2ca81db8fae6b59b6b9b8bda91526507dbd1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Lalinsk=C3=BD?= <lukas@oxygene.sk>
Date: Sat, 10 Feb 2018 06:52:10 +0100
Subject: [PATCH] Change LoadAudioFile to not depend on little-endian CPU

Fixes https://github.com/acoustid/chromaprint/issues/46
---
 tests/test_utils.cpp |  9 +++++++++
 tests/test_utils.h   | 14 +++++++++-----
 2 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/tests/test_utils.cpp b/tests/test_utils.cpp
index fc8bb86..536f46e 100644
--- a/tests/test_utils.cpp
+++ b/tests/test_utils.cpp
@@ -2,6 +2,7 @@
 #include <algorithm>
 #include <limits>
 #include "utils.h"
+#include "test_utils.h"
 
 using namespace chromaprint;
 
@@ -92,3 +93,11 @@ TEST(Utils, CountSetBits64) {
     EXPECT_EQ(64, CountSetBits(0xFFFFFFFFFFFFFFFFU));
     EXPECT_EQ(8, CountSetBits(0x0101010101010101U));
 }
+
+TEST(Utils, LoadAudioFile) {
+	std::vector<short> data = LoadAudioFile("data/test_mono_44100.raw");
+	ASSERT_EQ(data.size(), 176400/2);
+	EXPECT_EQ(data[1000], 0);
+	EXPECT_EQ(data[2000], 107);
+	EXPECT_EQ(data[3000], 128);
+}
diff --git a/tests/test_utils.h b/tests/test_utils.h
index 3631007..d01e9e3 100644
--- a/tests/test_utils.h
+++ b/tests/test_utils.h
@@ -31,11 +31,15 @@ inline std::vector<short> LoadAudioFile(const std::string &file_name)
 {
 	std::string path = TESTS_DIR + file_name;
 	std::ifstream file(path.c_str(), std::ifstream::in | std::ifstream::binary);
-	file.seekg(0, std::ios::end);
-	int length = file.tellg();
-	file.seekg(0, std::ios::beg);
-	std::vector<short> data(length / 2);
-	file.read((char *)&data[0], length);
+	uint8_t buf[4096];
+	std::vector<int16_t> data;
+	while (!file.eof()) {
+		file.read((char *) buf, 4096);
+		size_t nread = file.gcount();
+		for (size_t i = 0; i < nread - 1; i += 2) {
+			data.push_back((int16_t) (((uint16_t) buf[i+1] << 8) | ((uint16_t) buf[i])));
+		}
+	}
 	file.close();
 	return data;
 }