diff --git a/src/Magnum/Audio/Buffer.cpp b/src/Magnum/Audio/Buffer.cpp index ffe8f969a..d89c96f08 100644 --- a/src/Magnum/Audio/Buffer.cpp +++ b/src/Magnum/Audio/Buffer.cpp @@ -31,19 +31,9 @@ namespace Magnum { namespace Audio { Buffer& Buffer::setLoopPoints(Int loopStart, Int loopEnd) { - ALint sizeInBytes; - ALint channels; - ALint bits; - - alGetBufferi(_id, AL_SIZE, &sizeInBytes); - alGetBufferi(_id, AL_CHANNELS, &channels); - alGetBufferi(_id, AL_BITS, &bits); - - ALint lengthInSamples = sizeInBytes * 8 / (channels * bits); - ALint loopPoints[2] = { Math::max(0, loopStart), - Math::min(loopEnd, lengthInSamples) + Math::min(loopEnd, length()) }; alBufferiv(_id, AL_LOOP_POINTS_SOFT, &loopPoints[0]); diff --git a/src/Magnum/Audio/Buffer.h b/src/Magnum/Audio/Buffer.h index 51c9b0f90..40b5ebb1e 100644 --- a/src/Magnum/Audio/Buffer.h +++ b/src/Magnum/Audio/Buffer.h @@ -104,23 +104,75 @@ class MAGNUM_AUDIO_EXPORT Buffer { return *this; } + /** + * @brief Get buffer size + * @return The buffer's size in bytes + */ + ALint size() { + ALint size; + alGetBufferi(_id, AL_SIZE, &size); + return size; + } + + /** + * @brief Get buffer channels + * @return The buffer's number of channels + */ + ALint channels() { + ALint channels; + alGetBufferi(_id, AL_CHANNELS, &channels); + return channels; + } + + /** + * @brief Get buffer bit depth + * @return The buffer's bit depth + */ + ALint bitDepth() { + ALint bitDepth; + alGetBufferi(_id, AL_BITS, &bitDepth); + return bitDepth; + } + + /** + * @brief Get buffer length + * @return The buffer's length in samples + */ + ALint length() { + return size() * 8 / (channels() * bitDepth()); + } + + /** + * @brief Get buffer loop points + * @return A @ref std::pair containing the start and end loop points + * + * @requires_al_extension Extension @al_extension{SOFT,loop_points} + */ + std::pair loopPoints() { + std::pair points{0, 0}; + alGetBufferiv(_id, AL_LOOP_POINTS_SOFT, &(points.first)); + return points; + } + /** * @brief Set buffer loop points * @param loopStart The loop's start point in samples * @param loopEnd The loop's end point in samples * @return Reference to self (for method chaining) * + * The buffer needs to not be attached to a source for this operation to + * succeed. * @requires_al_extension Extension @al_extension{SOFT,loop_points} */ Buffer& setLoopPoints(Int loopStart, Int loopEnd); /** * @brief Set buffer to loop from the beginning until a certain point + * @param loopEnd The loop's end point in samples + * @return Reference to self (for method chaining) * * Equivalent to calling @ref setLoopPoints() with @p loopStart equal to * 0. - * @param loopEnd The loop's end point in samples - * @return Reference to self (for method chaining) * * @requires_al_extension Extension @al_extension{SOFT,loop_points} */ @@ -130,11 +182,11 @@ class MAGNUM_AUDIO_EXPORT Buffer { /** * @brief Set buffer to loop from the a certain point until the end + * @param loopStart The loop's start point in samples + * @return Reference to self (for method chaining) * * Equivalent to calling @ref setLoopPoints() with @p loopEnd equal to * @p INT_MAX. - * @param loopStart The loop's start point in samples - * @return Reference to self (for method chaining) * * @requires_al_extension Extension @al_extension{SOFT,loop_points} */ @@ -144,10 +196,10 @@ class MAGNUM_AUDIO_EXPORT Buffer { /** * @brief Resets the loop points + * @return Reference to self (for method chaining) * * Equivalent to calling @ref setLoopPoints() with @p loopStart equal to * 0, and @p loopEnd equal to @p INT_MAX. - * @return Reference to self (for method chaining) * * @requires_al_extension Extension @al_extension{SOFT,loop_points} */ diff --git a/src/Magnum/Audio/Test/BufferALTest.cpp b/src/Magnum/Audio/Test/BufferALTest.cpp index 952b401fd..f0359299b 100644 --- a/src/Magnum/Audio/Test/BufferALTest.cpp +++ b/src/Magnum/Audio/Test/BufferALTest.cpp @@ -40,7 +40,11 @@ struct BufferALTest: TestSuite::Tester { void construct(); - void setData(); + void size(); + void channels(); + void bitDepth(); + void length(); + void loopPoints(); void setLoopPoints(); void setLoopSince(); void setLoopUntil(); @@ -52,7 +56,11 @@ struct BufferALTest: TestSuite::Tester { BufferALTest::BufferALTest() { addTests({&BufferALTest::construct, - &BufferALTest::setData, + &BufferALTest::size, + &BufferALTest::channels, + &BufferALTest::bitDepth, + &BufferALTest::length, + &BufferALTest::loopPoints, &BufferALTest::setLoopPoints, &BufferALTest::setLoopSince, &BufferALTest::setLoopUntil, @@ -64,15 +72,52 @@ void BufferALTest::construct() { CORRADE_VERIFY(buf.id() != 0); } -void BufferALTest::setData() { +void BufferALTest::size() { Buffer buf; constexpr char data[] { 25, 17, 24, 122, 67, 24, 48, 96 }; buf.setData(BufferFormat::Mono8, data, 22050); - ALint bufSize; - alGetBufferi(buf.id(), AL_SIZE, &bufSize); - CORRADE_VERIFY(bufSize == 8); + CORRADE_COMPARE(buf.size(), 8); +} + +void BufferALTest::channels() { + Buffer buf; + constexpr char data[] { 25, 17, 24, 122, 67, 24, 48, 96 }; + + buf.setData(BufferFormat::Mono8, data, 22050); + + CORRADE_COMPARE(buf.channels(), 1); +} + +void BufferALTest::bitDepth() { + Buffer buf; + constexpr char data[] { 25, 17, 24, 122, 67, 24, 48, 96 }; + + buf.setData(BufferFormat::Mono8, data, 22050); + + CORRADE_COMPARE(buf.bitDepth(), 8); +} + +void BufferALTest::length() { + Buffer buf; + constexpr char data[] { 25, 17, 24, 122, 67, 24, 48, 96 }; + + buf.setData(BufferFormat::Mono8, data, 22050); + + CORRADE_COMPARE(buf.length(), 8); +} + +void BufferALTest::loopPoints() { + if(!_context.isExtensionSupported()) { + CORRADE_SKIP("Extension AL_SOFT_loop_points isn't supported on this system."); + } + + Buffer buf; + constexpr char data[] { 25, 17, 24, 122, 67, 24, 48, 96 }; + + buf.setData(BufferFormat::Mono8, data, 22050); + CORRADE_COMPARE(buf.loopPoints(), std::make_pair(0, 8)); } void BufferALTest::setLoopPoints() { @@ -84,12 +129,7 @@ void BufferALTest::setLoopPoints() { constexpr char data[] { 25, 17, 24, 122, 67, 24, 48, 96 }; buf.setData(BufferFormat::Mono8, data, 22050).setLoopPoints(1, 6); - - Containers::Array points{Containers::InPlaceInit, { 0, 0 }}; - alGetBufferiv(buf.id(), AL_LOOP_POINTS_SOFT, points.data()); - CORRADE_COMPARE_AS(points, - (Containers::Array{Containers::InPlaceInit, { 1, 6 }}), - TestSuite::Compare::Container>); + CORRADE_COMPARE(buf.loopPoints(), std::make_pair(1, 6)); } void BufferALTest::setLoopSince() { @@ -101,12 +141,7 @@ void BufferALTest::setLoopSince() { constexpr char data[] { 25, 17, 24, 122, 67, 24, 48, 96 }; buf.setData(BufferFormat::Mono8, data, 22050).setLoopSince(3); - - Containers::Array points{Containers::InPlaceInit, { 0, 0 }}; - alGetBufferiv(buf.id(), AL_LOOP_POINTS_SOFT, points.data()); - CORRADE_COMPARE_AS(points, - (Containers::Array{Containers::InPlaceInit, { 3, 8 }}), - TestSuite::Compare::Container>); + CORRADE_COMPARE(buf.loopPoints(), std::make_pair(3, 8)); } void BufferALTest::setLoopUntil() { @@ -118,12 +153,7 @@ void BufferALTest::setLoopUntil() { constexpr char data[] { 25, 17, 24, 122, 67, 24, 48, 96 }; buf.setData(BufferFormat::Mono8, data, 22050).setLoopUntil(5); - - Containers::Array points{Containers::InPlaceInit, { 0, 0 }}; - alGetBufferiv(buf.id(), AL_LOOP_POINTS_SOFT, points.data()); - CORRADE_COMPARE_AS(points, - (Containers::Array{Containers::InPlaceInit, { 0, 5 }}), - TestSuite::Compare::Container>); + CORRADE_COMPARE(buf.loopPoints(), std::make_pair(0, 5)); } void BufferALTest::resetLoopPoints() { @@ -135,12 +165,7 @@ void BufferALTest::resetLoopPoints() { constexpr char data[] { 25, 17, 24, 122, 67, 24, 48, 96 }; buf.setData(BufferFormat::Mono8, data, 22050).resetLoopPoints(); - - Containers::Array points{Containers::InPlaceInit, { 0, 0 }}; - alGetBufferiv(buf.id(), AL_LOOP_POINTS_SOFT, points.data()); - CORRADE_COMPARE_AS(points, - (Containers::Array{Containers::InPlaceInit, { 0, 8 }}), - TestSuite::Compare::Container>); + CORRADE_COMPARE(buf.loopPoints(), std::make_pair(0, 8)); } }}}}