mirror of
https://github.com/alexkay/spek.git
synced 2025-04-18 01:22:19 +03:00
tests: Squash audio tests
This commit is contained in:
parent
6a0d525fe4
commit
ae8cd33b8e
@ -7,8 +7,7 @@ endif
|
||||
check_PROGRAMS = $(TESTS)
|
||||
|
||||
test_SOURCES = \
|
||||
test-audio-info.cc \
|
||||
test-audio-read.cc \
|
||||
test-audio.cc \
|
||||
test-utils.cc \
|
||||
test.cc \
|
||||
test.h
|
||||
|
@ -1,84 +0,0 @@
|
||||
/* test-audio-info.cc
|
||||
*
|
||||
* Copyright (C) 2013 Alexander Kojevnikov <alexander@kojevnikov.com>
|
||||
*
|
||||
* Spek is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Spek 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Spek. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <map>
|
||||
|
||||
#include "spek-audio.h"
|
||||
|
||||
#include "test.h"
|
||||
|
||||
struct FileInfo
|
||||
{
|
||||
AudioError error;
|
||||
std::string codec_name;
|
||||
int bit_rate;
|
||||
int sample_rate;
|
||||
int bits_per_sample;
|
||||
int channels;
|
||||
double duration;
|
||||
};
|
||||
|
||||
static void test_file(const std::string& name, const FileInfo& info)
|
||||
{
|
||||
Audio audio;
|
||||
auto file = audio.open(SAMPLES_DIR "/" + name);
|
||||
test("error", info.error, file->get_error());
|
||||
test(file->get_codec_name(), true, !file->get_codec_name().compare(
|
||||
0, info.codec_name.length(), info.codec_name
|
||||
));
|
||||
test("bit rate", info.bit_rate, file->get_bit_rate());
|
||||
test("sample rate", info.sample_rate, file->get_sample_rate());
|
||||
test("bps", info.bits_per_sample, file->get_bits_per_sample());
|
||||
test("channels", info.channels, file->get_channels());
|
||||
test("duration", info.duration, file->get_duration());
|
||||
}
|
||||
|
||||
void test_audio_info()
|
||||
{
|
||||
const double MP3_T = 5.0 * 1152 / 44100; // 5 frames * duration per mp3 frame
|
||||
const double AAC_T = (10240 + 628) / 2.0 / 44100;
|
||||
const double DCA_T = 8.0 * 21180 / 1411216; // file size / bit rate
|
||||
const double AC3_T = 8.0 * 2490 / 190764; // file size / bit rate
|
||||
|
||||
std::map<std::string, FileInfo> files = {
|
||||
{"no.file", {AudioError::CANNOT_OPEN_FILE, "", 0, 0, 0, 0, 0.0}},
|
||||
{"1ch-96000Hz-24bps.flac", {AudioError::OK, "FLAC", 0, 96000, 24, 1, 0.1}},
|
||||
{"2ch-48000Hz-16bps.flac", {AudioError::OK, "FLAC", 0, 48000, 16, 2, 0.1}},
|
||||
{"1ch-96000Hz-24bps.ape", {AudioError::OK, "Monkey", 0, 96000, 24, 1, 0.1}},
|
||||
{"2ch-48000Hz-16bps.ape", {AudioError::OK, "Monkey", 0, 48000, 16, 2, 0.1}},
|
||||
{"2ch-44100Hz-16bps.m4a", {AudioError::OK, "ALAC", 0, 44100, 16, 2, 0.1}},
|
||||
{"1ch-96000Hz-24bps.wv", {AudioError::OK, "WavPack", 0, 96000, 24, 1, 0.1}},
|
||||
{"2ch-48000Hz-16bps.wv", {AudioError::OK, "WavPack", 0, 48000, 16, 2, 0.1}},
|
||||
{"2ch-44100Hz-16bps.wav", {AudioError::OK, "PCM", 0, 44100, 16, 2, 0.1}},
|
||||
{"2ch-44100Hz-128cbr.mp3", {AudioError::OK, "MP3", 128000, 44100, 0, 2, MP3_T}},
|
||||
{"2ch-44100Hz-320cbr.mp3", {AudioError::OK, "MP3", 320000, 44100, 0, 2, MP3_T}},
|
||||
{"2ch-44100Hz-V0.mp3", {AudioError::OK, "MP3", 201329, 44100, 0, 2, MP3_T}},
|
||||
{"2ch-44100Hz-V2.mp3", {AudioError::OK, "MP3", 150124, 44100, 0, 2, MP3_T}},
|
||||
{"2ch-44100Hz-q100.m4a", {AudioError::OK, "AAC", 159649, 44100, 0, 2, AAC_T}},
|
||||
{"2ch-44100Hz-q5.ogg", {AudioError::OK, "Vorbis", 160000, 44100, 0, 2, 0.1}},
|
||||
{"2ch-44100Hz.dts", {AudioError::OK, "DCA", 1411200, 44100, 0, 2, DCA_T}},
|
||||
{"2ch-44100Hz.ac3", {AudioError::OK, "ATSC A/52", 192000, 44100, 0, 2, AC3_T}},
|
||||
{"2ch-44100Hz-std.mpc", {AudioError::OK, "Musepack", 0, 44100, 0, 2, 0.0}},
|
||||
};
|
||||
for (const auto& item : files) {
|
||||
run(
|
||||
"audio info: " + item.first,
|
||||
[&] () { test_file(item.first, item.second); }
|
||||
);
|
||||
}
|
||||
}
|
@ -1,74 +0,0 @@
|
||||
/* test-audio-read.cc
|
||||
*
|
||||
* Copyright (C) 2013 Alexander Kojevnikov <alexander@kojevnikov.com>
|
||||
*
|
||||
* Spek is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Spek 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Spek. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <map>
|
||||
|
||||
#include "spek-audio.h"
|
||||
|
||||
#include "test.h"
|
||||
|
||||
static void test_silence(const std::string& name, int samples)
|
||||
{
|
||||
Audio audio;
|
||||
auto file = audio.open(SAMPLES_DIR "/" + name);
|
||||
|
||||
int samples_read = 0;
|
||||
double power = 0.0;
|
||||
int len;
|
||||
while ((len = file->read()) > 0) {
|
||||
samples_read += len;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
float level = file->get_buffer()[i];
|
||||
power += level * level;
|
||||
}
|
||||
}
|
||||
power /= samples_read;
|
||||
|
||||
test("error", 0, len);
|
||||
test("samples", samples, samples_read);
|
||||
test("power", 0.0, power);
|
||||
}
|
||||
|
||||
void test_audio_read()
|
||||
{
|
||||
std::map<std::string, int> files = {
|
||||
{"1ch-96000Hz-24bps.flac", 1 * 96000 / 10},
|
||||
{"2ch-48000Hz-16bps.flac", 2 * 48000 / 10},
|
||||
{"1ch-96000Hz-24bps.ape", 1 * 96000 / 10},
|
||||
{"2ch-48000Hz-16bps.ape", 2 * 48000 / 10},
|
||||
{"2ch-44100Hz-16bps.m4a", 2 * 44100 / 10},
|
||||
{"1ch-96000Hz-24bps.wv", 1 * 96000 / 10},
|
||||
{"2ch-48000Hz-16bps.wv", 2 * 48000 / 10},
|
||||
{"2ch-44100Hz-16bps.wav", 2 * 44100 / 10},
|
||||
{"2ch-44100Hz-128cbr.mp3", 2 * 1152 * 4 + 94},
|
||||
{"2ch-44100Hz-320cbr.mp3", 2 * 1152 * 4 + 94},
|
||||
{"2ch-44100Hz-V0.mp3", 2 * 1152 * 4 + 94},
|
||||
{"2ch-44100Hz-V2.mp3", 2 * 1152 * 4 + 94},
|
||||
{"2ch-44100Hz-q100.m4a", 10240},
|
||||
{"2ch-44100Hz-q5.ogg", 2 * 1024 * 4 + 1152},
|
||||
{"2ch-44100Hz.dts", 10240},
|
||||
{"2ch-44100Hz.ac3", 9 * 1024},
|
||||
{"2ch-44100Hz-std.mpc", 11 * 1024 + 256},
|
||||
};
|
||||
for (const auto& item : files) {
|
||||
run(
|
||||
"audio read: " + item.first,
|
||||
[&] () { test_silence(item.first, item.second); }
|
||||
);
|
||||
}
|
||||
}
|
134
tests/test-audio.cc
Normal file
134
tests/test-audio.cc
Normal file
@ -0,0 +1,134 @@
|
||||
/* test-audio.cc
|
||||
*
|
||||
* Copyright (C) 2013 Alexander Kojevnikov <alexander@kojevnikov.com>
|
||||
*
|
||||
* Spek is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Spek 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Spek. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <map>
|
||||
|
||||
#include "spek-audio.h"
|
||||
|
||||
#include "test.h"
|
||||
|
||||
struct FileInfo
|
||||
{
|
||||
AudioError error;
|
||||
std::string codec_name;
|
||||
int bit_rate;
|
||||
int sample_rate;
|
||||
int bits_per_sample;
|
||||
int channels;
|
||||
double duration;
|
||||
int samples;
|
||||
};
|
||||
|
||||
static void test_info(AudioFile *file, const FileInfo& info)
|
||||
{
|
||||
test("error", info.error, file->get_error());
|
||||
test(file->get_codec_name(), true, !file->get_codec_name().compare(
|
||||
0, info.codec_name.length(), info.codec_name
|
||||
));
|
||||
test("bit rate", info.bit_rate, file->get_bit_rate());
|
||||
test("sample rate", info.sample_rate, file->get_sample_rate());
|
||||
test("bps", info.bits_per_sample, file->get_bits_per_sample());
|
||||
test("channels", info.channels, file->get_channels());
|
||||
test("duration", info.duration, file->get_duration());
|
||||
}
|
||||
|
||||
static void test_read(AudioFile *file, int samples)
|
||||
{
|
||||
int samples_read = 0;
|
||||
double power = 0.0;
|
||||
int len;
|
||||
while ((len = file->read()) > 0) {
|
||||
samples_read += len;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
float level = file->get_buffer()[i];
|
||||
power += level * level;
|
||||
}
|
||||
}
|
||||
|
||||
test("samples", samples, samples_read);
|
||||
|
||||
if (samples > 0) {
|
||||
power /= samples_read;
|
||||
test("error", 0, len);
|
||||
test("power", 0.0, power);
|
||||
} else {
|
||||
test("error", -1, len);
|
||||
}
|
||||
}
|
||||
|
||||
void test_audio()
|
||||
{
|
||||
const double MP3_T = 5.0 * 1152 / 44100; // 5 frames * duration per mp3 frame
|
||||
const double AAC_T = (10240 + 628) / 2.0 / 44100;
|
||||
const double DCA_T = 8.0 * 21180 / 1411216; // file size / bit rate
|
||||
const double AC3_T = 8.0 * 2490 / 190764; // file size / bit rate
|
||||
|
||||
std::map<std::string, FileInfo> files = {
|
||||
{"no.file",
|
||||
{AudioError::CANNOT_OPEN_FILE, "", 0, 0, 0, 0, 0.0, 0}},
|
||||
{"1ch-96000Hz-24bps.flac",
|
||||
{AudioError::OK, "FLAC", 0, 96000, 24, 1, 0.1, 1 * 96000 / 10}},
|
||||
{"2ch-48000Hz-16bps.flac",
|
||||
{AudioError::OK, "FLAC", 0, 48000, 16, 2, 0.1, 2 * 48000 / 10}},
|
||||
{"1ch-96000Hz-24bps.ape",
|
||||
{AudioError::OK, "Monkey", 0, 96000, 24, 1, 0.1, 1 * 96000 / 10}},
|
||||
{"2ch-48000Hz-16bps.ape",
|
||||
{AudioError::OK, "Monkey", 0, 48000, 16, 2, 0.1, 2 * 48000 / 10}},
|
||||
{"2ch-44100Hz-16bps.m4a",
|
||||
{AudioError::OK, "ALAC", 0, 44100, 16, 2, 0.1, 2 * 44100 / 10}},
|
||||
{"1ch-96000Hz-24bps.wv",
|
||||
{AudioError::OK, "WavPack", 0, 96000, 24, 1, 0.1, 1 * 96000 / 10}},
|
||||
{"2ch-48000Hz-16bps.wv",
|
||||
{AudioError::OK, "WavPack", 0, 48000, 16, 2, 0.1, 2 * 48000 / 10}},
|
||||
{"2ch-44100Hz-16bps.wav",
|
||||
{AudioError::OK, "PCM", 0, 44100, 16, 2, 0.1, 2 * 44100 / 10}},
|
||||
{"2ch-44100Hz-128cbr.mp3",
|
||||
{AudioError::OK, "MP3", 128000, 44100, 0, 2, MP3_T, 2 * 1152 * 4 + 94}},
|
||||
{"2ch-44100Hz-320cbr.mp3",
|
||||
{AudioError::OK, "MP3", 320000, 44100, 0, 2, MP3_T, 2 * 1152 * 4 + 94}},
|
||||
{"2ch-44100Hz-V0.mp3",
|
||||
{AudioError::OK, "MP3", 201329, 44100, 0, 2, MP3_T, 2 * 1152 * 4 + 94}},
|
||||
{"2ch-44100Hz-V2.mp3",
|
||||
{AudioError::OK, "MP3", 150124, 44100, 0, 2, MP3_T, 2 * 1152 * 4 + 94}},
|
||||
{"2ch-44100Hz-q100.m4a",
|
||||
{AudioError::OK, "AAC", 159649, 44100, 0, 2, AAC_T, 10240}},
|
||||
{"2ch-44100Hz-q5.ogg",
|
||||
{AudioError::OK, "Vorbis", 160000, 44100, 0, 2, 0.1, 2 * 1024 * 4 + 1152}},
|
||||
{"2ch-44100Hz.dts",
|
||||
{AudioError::OK, "DCA", 1411200, 44100, 0, 2, DCA_T, 10240}},
|
||||
{"2ch-44100Hz.ac3",
|
||||
{AudioError::OK, "ATSC A/52", 192000, 44100, 0, 2, AC3_T, 9 * 1024}},
|
||||
{"2ch-44100Hz-std.mpc",
|
||||
{AudioError::OK, "Musepack", 0, 44100, 0, 2, 0.0, 11 * 1024 + 256}},
|
||||
};
|
||||
|
||||
Audio audio;
|
||||
for (const auto& item : files) {
|
||||
auto name = item.first;
|
||||
auto info = item.second;
|
||||
auto file = audio.open(SAMPLES_DIR "/" + name);
|
||||
run(
|
||||
"audio info: " + name,
|
||||
[&] () { test_info(file.get(), info); }
|
||||
);
|
||||
run(
|
||||
"audio read: " + name,
|
||||
[&] () { test_read(file.get(), info.samples); }
|
||||
);
|
||||
}
|
||||
}
|
@ -25,8 +25,7 @@ int main()
|
||||
{
|
||||
std::cerr << "-------------" << std::endl;
|
||||
|
||||
test_audio_info();
|
||||
test_audio_read();
|
||||
test_audio();
|
||||
test_utils();
|
||||
|
||||
if (g_passes < g_total) {
|
||||
|
@ -51,8 +51,7 @@ template<class T> void test(const std::string& message, const T& expected, const
|
||||
}
|
||||
}
|
||||
|
||||
void test_audio_info();
|
||||
void test_audio_read();
|
||||
void test_audio();
|
||||
void test_utils();
|
||||
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user