diff options
| author | Carl Hetherington <cth@carlh.net> | 2020-11-29 20:57:09 +0100 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2020-11-29 20:57:09 +0100 |
| commit | 54d45efaa9913191806144d99868a6edbe8c488c (patch) | |
| tree | 342cc1a9b936a12fa0b7628ec2dc46f8cd06cae2 | |
| parent | 48a720448779e4519b2cccae7c41ab1a618224a7 (diff) | |
Fix SoundFrame::get().
The old version did not deal with signed-ness correctly. I think
this version is slightly dodgy in that it assumes 2s complement,
but that's probably not so bad.
| -rw-r--r-- | src/sound_frame.cc | 3 | ||||
| -rw-r--r-- | test/sound_frame_test.cc | 9 |
2 files changed, 11 insertions, 1 deletions
diff --git a/src/sound_frame.cc b/src/sound_frame.cc index b5a52d02..53d00b38 100644 --- a/src/sound_frame.cc +++ b/src/sound_frame.cc @@ -50,7 +50,8 @@ int32_t SoundFrame::get (int channel, int frame) const { uint8_t const * d = data() + (frame * _channels * 3) + (channel * 3); - return d[0] | (d[1] << 8) | (d[2] << 16); + /* This is slightly dubious I think */ + return (d[0] << 8 | (d[1] << 16) | (d[2] << 24)) >> 8; } int diff --git a/test/sound_frame_test.cc b/test/sound_frame_test.cc index d5491a78..a9088e26 100644 --- a/test/sound_frame_test.cc +++ b/test/sound_frame_test.cc @@ -64,6 +64,7 @@ BOOST_AUTO_TEST_CASE (sound_frame_test) int const read = sf_readf_int (sndfile, ref_data, frame_length); BOOST_REQUIRE_EQUAL (read, frame_length); + /* Check raw data is as we expect */ uint8_t const * p = frame->data (); for (int i = 0; i < (frame_length * channels); ++i) { int x = ref_data[i] >> 8; @@ -74,6 +75,14 @@ BOOST_AUTO_TEST_CASE (sound_frame_test) BOOST_REQUIRE_EQUAL (x, y); p += 3; } + + /* Check SoundFrame::get() */ + int* ref = ref_data; + for (int sample = 0; sample < frame_length; ++sample) { + for (int channel = 0; channel < channels; ++channel) { + BOOST_REQUIRE_EQUAL ((*ref++) >> 8, frame->get(channel, sample)); + } + } } BOOST_AUTO_TEST_CASE (sound_frame_test2) |
