Add tests for floating point sample conversion

This commit is contained in:
Thomas Goyne 2014-07-10 10:27:48 -07:00
parent 63de3232f7
commit a5c2ef273f
2 changed files with 43 additions and 6 deletions

View File

@ -93,12 +93,9 @@ public:
else else
expanded = static_cast<Target>(src_buf[i] * std::numeric_limits<Target>::max()); expanded = static_cast<Target>(src_buf[i] * std::numeric_limits<Target>::max());
if (expanded < std::numeric_limits<Target>::min()) dest[i] = expanded < std::numeric_limits<Target>::min() ? std::numeric_limits<Target>::min() :
dest[i] = std::numeric_limits<Target>::min(); expanded > std::numeric_limits<Target>::max() ? std::numeric_limits<Target>::max() :
else if (expanded > std::numeric_limits<Target>::max()) static_cast<Target>(expanded);
dest[i] = std::numeric_limits<Target>::max();
else
dest[i] = static_cast<Target>(expanded);
} }
} }
}; };

View File

@ -265,6 +265,46 @@ TEST(lagi_audio, stereo_downmix) {
EXPECT_EQ(i, samples[i]); EXPECT_EQ(i, samples[i]);
} }
template<typename Float>
struct FloatAudioProvider : agi::AudioProvider {
FloatAudioProvider() {
channels = 1;
num_samples = 90 * 480000;
decoded_samples = num_samples;
sample_rate = 480000;
bytes_per_sample = sizeof(Float);
float_samples = true;
}
void FillBuffer(void *buf, int64_t start, int64_t count) const override {
auto out = static_cast<Float *>(buf);
for (int64_t end = start + count; start < end; ++start) {
auto shifted = start + SHRT_MIN;
*out++ = (Float)(1.0 * shifted / (shifted < 0 ? -SHRT_MIN : SHRT_MAX));
}
}
};
TEST(lagi_audio, float_conversion) {
auto provider = agi::CreateConvertAudioProvider(agi::make_unique<FloatAudioProvider<float>>());
EXPECT_FALSE(provider->AreSamplesFloat());
int16_t samples[1 << 16];
provider->GetAudio(samples, 0, 1 << 16);
for (int i = 0; i < (1 << 16); ++i)
ASSERT_EQ(i + SHRT_MIN, samples[i]);
}
TEST(lagi_audio, double_conversion) {
auto provider = agi::CreateConvertAudioProvider(agi::make_unique<FloatAudioProvider<double>>());
EXPECT_FALSE(provider->AreSamplesFloat());
int16_t samples[1 << 16];
provider->GetAudio(samples, 0, 1 << 16);
for (int i = 0; i < (1 << 16); ++i)
ASSERT_EQ(i + SHRT_MIN, samples[i]);
}
TEST(lagi_audio, pcm_simple) { TEST(lagi_audio, pcm_simple) {
auto path = agi::Path().Decode("?temp/pcm_simple"); auto path = agi::Path().Decode("?temp/pcm_simple");
{ {