util/{Const,Writable}Buffer: add operator[]
This commit is contained in:
parent
9dc5335e3e
commit
4ba7427fa0
@ -63,7 +63,7 @@ bool TextInputStream::ReadLine(std::string &line)
|
|||||||
the current line */
|
the current line */
|
||||||
dest = buffer.Write();
|
dest = buffer.Write();
|
||||||
assert(!dest.IsEmpty());
|
assert(!dest.IsEmpty());
|
||||||
dest.data[0] = '\n';
|
dest[0] = '\n';
|
||||||
buffer.Append(1);
|
buffer.Append(1);
|
||||||
}
|
}
|
||||||
} while (p == nullptr);
|
} while (p == nullptr);
|
||||||
|
@ -85,7 +85,7 @@ pcm_resample_fallback(PcmBuffer &buffer,
|
|||||||
while (dest_pos < dest_samples) {
|
while (dest_pos < dest_samples) {
|
||||||
unsigned src_pos = dest_pos * src_rate / dest_rate;
|
unsigned src_pos = dest_pos * src_rate / dest_rate;
|
||||||
|
|
||||||
dest_buffer[dest_pos++] = src.data[src_pos];
|
dest_buffer[dest_pos++] = src[src_pos];
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
@ -93,8 +93,8 @@ pcm_resample_fallback(PcmBuffer &buffer,
|
|||||||
unsigned src_pos = dest_pos * src_rate / dest_rate;
|
unsigned src_pos = dest_pos * src_rate / dest_rate;
|
||||||
src_pos &= ~1;
|
src_pos &= ~1;
|
||||||
|
|
||||||
dest_buffer[dest_pos++] = src.data[src_pos];
|
dest_buffer[dest_pos++] = src[src_pos];
|
||||||
dest_buffer[dest_pos++] = src.data[src_pos + 1];
|
dest_buffer[dest_pos++] = src[src_pos + 1];
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -143,6 +143,17 @@ struct ConstBuffer {
|
|||||||
constexpr const_iterator cend() const {
|
constexpr const_iterator cend() const {
|
||||||
return data + size;
|
return data + size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef NDEBUG
|
||||||
|
constexpr
|
||||||
|
#endif
|
||||||
|
const T &operator[](size_type i) const {
|
||||||
|
#ifndef NDEBUG
|
||||||
|
assert(i < size);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return data[i];
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -145,6 +145,17 @@ struct WritableBuffer {
|
|||||||
constexpr const_iterator cend() const {
|
constexpr const_iterator cend() const {
|
||||||
return data + size;
|
return data + size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef NDEBUG
|
||||||
|
constexpr
|
||||||
|
#endif
|
||||||
|
T &operator[](size_type i) const {
|
||||||
|
#ifndef NDEBUG
|
||||||
|
assert(i < size);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return data[i];
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -39,7 +39,7 @@ PcmChannelsTest::TestChannels16()
|
|||||||
CPPUNIT_ASSERT_EQUAL(N, dest.size);
|
CPPUNIT_ASSERT_EQUAL(N, dest.size);
|
||||||
for (unsigned i = 0; i < N; ++i)
|
for (unsigned i = 0; i < N; ++i)
|
||||||
CPPUNIT_ASSERT_EQUAL(int16_t((src[i * 2] + src[i * 2 + 1]) / 2),
|
CPPUNIT_ASSERT_EQUAL(int16_t((src[i * 2] + src[i * 2 + 1]) / 2),
|
||||||
dest.data[i]);
|
dest[i]);
|
||||||
|
|
||||||
/* mono to stereo */
|
/* mono to stereo */
|
||||||
|
|
||||||
@ -47,8 +47,8 @@ PcmChannelsTest::TestChannels16()
|
|||||||
CPPUNIT_ASSERT(!dest.IsNull());
|
CPPUNIT_ASSERT(!dest.IsNull());
|
||||||
CPPUNIT_ASSERT_EQUAL(N * 4, dest.size);
|
CPPUNIT_ASSERT_EQUAL(N * 4, dest.size);
|
||||||
for (unsigned i = 0; i < N; ++i) {
|
for (unsigned i = 0; i < N; ++i) {
|
||||||
CPPUNIT_ASSERT_EQUAL(src[i], dest.data[i * 2]);
|
CPPUNIT_ASSERT_EQUAL(src[i], dest[i * 2]);
|
||||||
CPPUNIT_ASSERT_EQUAL(src[i], dest.data[i * 2 + 1]);
|
CPPUNIT_ASSERT_EQUAL(src[i], dest[i * 2 + 1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,7 +67,7 @@ PcmChannelsTest::TestChannels32()
|
|||||||
CPPUNIT_ASSERT_EQUAL(N, dest.size);
|
CPPUNIT_ASSERT_EQUAL(N, dest.size);
|
||||||
for (unsigned i = 0; i < N; ++i)
|
for (unsigned i = 0; i < N; ++i)
|
||||||
CPPUNIT_ASSERT_EQUAL(int32_t(((int64_t)src[i * 2] + (int64_t)src[i * 2 + 1]) / 2),
|
CPPUNIT_ASSERT_EQUAL(int32_t(((int64_t)src[i * 2] + (int64_t)src[i * 2 + 1]) / 2),
|
||||||
dest.data[i]);
|
dest[i]);
|
||||||
|
|
||||||
/* mono to stereo */
|
/* mono to stereo */
|
||||||
|
|
||||||
@ -75,7 +75,7 @@ PcmChannelsTest::TestChannels32()
|
|||||||
CPPUNIT_ASSERT(!dest.IsNull());
|
CPPUNIT_ASSERT(!dest.IsNull());
|
||||||
CPPUNIT_ASSERT_EQUAL(N * 4, dest.size);
|
CPPUNIT_ASSERT_EQUAL(N * 4, dest.size);
|
||||||
for (unsigned i = 0; i < N; ++i) {
|
for (unsigned i = 0; i < N; ++i) {
|
||||||
CPPUNIT_ASSERT_EQUAL(src[i], dest.data[i * 2]);
|
CPPUNIT_ASSERT_EQUAL(src[i], dest[i * 2]);
|
||||||
CPPUNIT_ASSERT_EQUAL(src[i], dest.data[i * 2 + 1]);
|
CPPUNIT_ASSERT_EQUAL(src[i], dest[i * 2 + 1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,7 @@ PcmFormatTest::TestFormat8to16()
|
|||||||
CPPUNIT_ASSERT_EQUAL(N, d.size);
|
CPPUNIT_ASSERT_EQUAL(N, d.size);
|
||||||
|
|
||||||
for (size_t i = 0; i < N; ++i)
|
for (size_t i = 0; i < N; ++i)
|
||||||
CPPUNIT_ASSERT_EQUAL(int(src[i]), d.data[i] >> 8);
|
CPPUNIT_ASSERT_EQUAL(int(src[i]), d[i] >> 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -54,7 +54,7 @@ PcmFormatTest::TestFormat16to24()
|
|||||||
CPPUNIT_ASSERT_EQUAL(N, d.size);
|
CPPUNIT_ASSERT_EQUAL(N, d.size);
|
||||||
|
|
||||||
for (size_t i = 0; i < N; ++i)
|
for (size_t i = 0; i < N; ++i)
|
||||||
CPPUNIT_ASSERT_EQUAL(int(src[i]), d.data[i] >> 8);
|
CPPUNIT_ASSERT_EQUAL(int(src[i]), d[i] >> 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -69,7 +69,7 @@ PcmFormatTest::TestFormat16to32()
|
|||||||
CPPUNIT_ASSERT_EQUAL(N, d.size);
|
CPPUNIT_ASSERT_EQUAL(N, d.size);
|
||||||
|
|
||||||
for (size_t i = 0; i < N; ++i)
|
for (size_t i = 0; i < N; ++i)
|
||||||
CPPUNIT_ASSERT_EQUAL(int(src[i]), d.data[i] >> 16);
|
CPPUNIT_ASSERT_EQUAL(int(src[i]), d[i] >> 16);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -84,8 +84,8 @@ PcmFormatTest::TestFormatFloat()
|
|||||||
CPPUNIT_ASSERT_EQUAL(N, f.size);
|
CPPUNIT_ASSERT_EQUAL(N, f.size);
|
||||||
|
|
||||||
for (size_t i = 0; i != f.size; ++i) {
|
for (size_t i = 0; i != f.size; ++i) {
|
||||||
CPPUNIT_ASSERT(f.data[i] >= -1.);
|
CPPUNIT_ASSERT(f[i] >= -1.);
|
||||||
CPPUNIT_ASSERT(f.data[i] <= 1.);
|
CPPUNIT_ASSERT(f[i] <= 1.);
|
||||||
}
|
}
|
||||||
|
|
||||||
PcmDither dither;
|
PcmDither dither;
|
||||||
@ -96,5 +96,5 @@ PcmFormatTest::TestFormatFloat()
|
|||||||
CPPUNIT_ASSERT_EQUAL(N, d.size);
|
CPPUNIT_ASSERT_EQUAL(N, d.size);
|
||||||
|
|
||||||
for (size_t i = 0; i < N; ++i)
|
for (size_t i = 0; i < N; ++i)
|
||||||
CPPUNIT_ASSERT_EQUAL(src[i], d.data[i]);
|
CPPUNIT_ASSERT_EQUAL(src[i], d[i]);
|
||||||
}
|
}
|
||||||
|
@ -61,8 +61,8 @@ TestVolume(G g=G())
|
|||||||
const auto _dest = ConstBuffer<value_type>::FromVoid(dest);
|
const auto _dest = ConstBuffer<value_type>::FromVoid(dest);
|
||||||
for (unsigned i = 0; i < N; ++i) {
|
for (unsigned i = 0; i < N; ++i) {
|
||||||
const auto expected = (_src[i] + 1) / 2;
|
const auto expected = (_src[i] + 1) / 2;
|
||||||
CPPUNIT_ASSERT(_dest.data[i] >= expected - 4);
|
CPPUNIT_ASSERT(_dest[i] >= expected - 4);
|
||||||
CPPUNIT_ASSERT(_dest.data[i] <= expected + 4);
|
CPPUNIT_ASSERT(_dest[i] <= expected + 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
pv.Close();
|
pv.Close();
|
||||||
@ -119,7 +119,7 @@ PcmVolumeTest::TestVolumeFloat()
|
|||||||
|
|
||||||
const auto _dest = ConstBuffer<float>::FromVoid(dest);
|
const auto _dest = ConstBuffer<float>::FromVoid(dest);
|
||||||
for (unsigned i = 0; i < N; ++i)
|
for (unsigned i = 0; i < N; ++i)
|
||||||
CPPUNIT_ASSERT_DOUBLES_EQUAL(_src[i] / 2, _dest.data[i], 1);
|
CPPUNIT_ASSERT_DOUBLES_EQUAL(_src[i] / 2, _dest[i], 1);
|
||||||
|
|
||||||
pv.Close();
|
pv.Close();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user