test-audio.cc (4362B)
1 #include <map> 2 3 #include "spek-audio.h" 4 5 #include "test.h" 6 7 struct FileInfo 8 { 9 AudioError error; 10 std::string codec_name; 11 int bit_rate; 12 int sample_rate; 13 int bits_per_sample; 14 int channels; 15 double duration; 16 int samples; 17 }; 18 19 static void test_info(AudioFile *file, const FileInfo& info) 20 { 21 test("error", info.error, file->get_error()); 22 test(file->get_codec_name(), true, !file->get_codec_name().compare( 23 0, info.codec_name.length(), info.codec_name 24 )); 25 test("bit rate", info.bit_rate, file->get_bit_rate()); 26 test("sample rate", info.sample_rate, file->get_sample_rate()); 27 test("bps", info.bits_per_sample, file->get_bits_per_sample()); 28 test("channels", info.channels, file->get_channels()); 29 test("duration", info.duration, file->get_duration()); 30 } 31 32 static void test_read(AudioFile *file, int samples) 33 { 34 if (!file->get_error()) { 35 file->start(0, 1024); 36 } 37 38 int samples_read = 0; 39 double power = 0.0; 40 int len; 41 while ((len = file->read()) > 0) { 42 samples_read += len; 43 for (int i = 0; i < len; ++i) { 44 float level = file->get_buffer()[i]; 45 power += level * level; 46 } 47 } 48 49 test("samples", samples, samples_read); 50 51 if (samples > 0) { 52 power /= samples_read; 53 test("error", 0, len); 54 test("power", 0.0, power); 55 } else { 56 test("error", -1, len); 57 } 58 } 59 60 void test_audio() 61 { 62 const double MP3_T = 5.0 * 1152 / 44100; // 5 frames * duration per mp3 frame 63 const double AAC_T = (10240 + 628) / 2.0 / 44100; 64 const double DCA_T = 8.0 * 21180 / 1411216; // file size / bit rate 65 const double AC3_T = 8.0 * 2490 / 190764; // file size / bit rate 66 67 std::map<std::string, FileInfo> files = { 68 {"no.file", 69 {AudioError::CANNOT_OPEN_FILE, "", 0, 0, 0, 0, 0.0, 0}}, 70 {"1ch-96000Hz-24bps.flac", 71 {AudioError::OK, "FLAC", 0, 96000, 24, 1, 0.1, 96000 / 10}}, 72 {"2ch-48000Hz-16bps.flac", 73 {AudioError::OK, "FLAC", 0, 48000, 16, 2, 0.1, 48000 / 10}}, 74 {"1ch-96000Hz-24bps.ape", 75 {AudioError::OK, "Monkey", 0, 96000, 24, 1, 0.1, 96000 / 10}}, 76 {"2ch-48000Hz-16bps.ape", 77 {AudioError::OK, "Monkey", 0, 48000, 16, 2, 0.1, 48000 / 10}}, 78 {"2ch-44100Hz-16bps.m4a", 79 {AudioError::OK, "ALAC", 0, 44100, 16, 2, 0.1, 44100 / 10}}, 80 {"1ch-96000Hz-24bps.wv", 81 {AudioError::OK, "WavPack", 0, 96000, 24, 1, 0.1, 96000 / 10}}, 82 {"2ch-48000Hz-16bps.wv", 83 {AudioError::OK, "WavPack", 0, 48000, 16, 2, 0.1, 48000 / 10}}, 84 {"2ch-44100Hz-16bps.wav", 85 {AudioError::OK, "PCM", 0, 44100, 16, 2, 0.1, 44100 / 10}}, 86 {"2ch-44100Hz-128cbr.mp3", 87 {AudioError::OK, "MP3", 128000, 44100, 0, 2, MP3_T, 44100 / 10}}, 88 {"2ch-44100Hz-320cbr.mp3", 89 {AudioError::OK, "MP3", 320000, 44100, 0, 2, MP3_T, 44100 / 10}}, 90 {"2ch-44100Hz-V0.mp3", 91 {AudioError::OK, "MP3", 201329, 44100, 0, 2, MP3_T, 44100 / 10}}, 92 {"2ch-44100Hz-V2.mp3", 93 {AudioError::OK, "MP3", 150124, 44100, 0, 2, MP3_T, 44100 / 10}}, 94 {"2ch-44100Hz-q100.m4a", 95 {AudioError::OK, "AAC", 159649, 44100, 0, 2, AAC_T, 5120}}, 96 {"2ch-44100Hz-q5.ogg", 97 {AudioError::OK, "Vorbis", 160000, 44100, 0, 2, 0.1, 4672}}, 98 {"2ch-44100Hz.dts", 99 {AudioError::OK, "DCA", 1411200, 44100, 0, 2, DCA_T, 5120}}, 100 {"2ch-44100Hz.ac3", 101 {AudioError::OK, "ATSC A/52", 192000, 44100, 0, 2, AC3_T, 4608}}, 102 {"2ch-44100Hz-std.mpc", 103 {AudioError::OK, "Musepack", 0, 44100, 0, 2, 0.0, 6912}}, 104 {"2ch-44100Hz-v1.wma", 105 {AudioError::OK, "Windows Media Audio 1", 128000, 44100, 0, 2, 0.138, 6 * 1024}}, 106 {"2ch-44100Hz-v2.wma", 107 {AudioError::OK, "Windows Media Audio 2", 128000, 44100, 0, 2, 0.138, 6 * 1024}}, 108 }; 109 110 Audio audio; 111 for (const auto& item : files) { 112 auto name = item.first; 113 auto info = item.second; 114 auto file = audio.open(SAMPLES_DIR "/" + name, 0); 115 run( 116 "audio info: " + name, 117 [&] () { test_info(file.get(), info); } 118 ); 119 run( 120 "audio read: " + name, 121 [&] () { test_read(file.get(), info.samples); } 122 ); 123 } 124 }