Browse Source

Audio: apply mosra's suggestions.

pull/342/head
Guillaume Jacquemin 7 years ago
parent
commit
1e8601d00c
  1. 12
      src/Magnum/Audio/Buffer.cpp
  2. 62
      src/Magnum/Audio/Buffer.h
  3. 85
      src/Magnum/Audio/Test/BufferALTest.cpp

12
src/Magnum/Audio/Buffer.cpp

@ -31,19 +31,9 @@
namespace Magnum { namespace Audio { namespace Magnum { namespace Audio {
Buffer& Buffer::setLoopPoints(Int loopStart, Int loopEnd) { 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] = { ALint loopPoints[2] = {
Math::max<ALint>(0, loopStart), Math::max<ALint>(0, loopStart),
Math::min<ALint>(loopEnd, lengthInSamples) Math::min<ALint>(loopEnd, length())
}; };
alBufferiv(_id, AL_LOOP_POINTS_SOFT, &loopPoints[0]); alBufferiv(_id, AL_LOOP_POINTS_SOFT, &loopPoints[0]);

62
src/Magnum/Audio/Buffer.h

@ -104,23 +104,75 @@ class MAGNUM_AUDIO_EXPORT Buffer {
return *this; 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<ALint, ALint> loopPoints() {
std::pair<ALint, ALint> points{0, 0};
alGetBufferiv(_id, AL_LOOP_POINTS_SOFT, &(points.first));
return points;
}
/** /**
* @brief Set buffer loop points * @brief Set buffer loop points
* @param loopStart The loop's start point in samples * @param loopStart The loop's start point in samples
* @param loopEnd The loop's end point in samples * @param loopEnd The loop's end point in samples
* @return Reference to self (for method chaining) * @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} * @requires_al_extension Extension @al_extension{SOFT,loop_points}
*/ */
Buffer& setLoopPoints(Int loopStart, Int loopEnd); Buffer& setLoopPoints(Int loopStart, Int loopEnd);
/** /**
* @brief Set buffer to loop from the beginning until a certain point * @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 * Equivalent to calling @ref setLoopPoints() with @p loopStart equal to
* 0. * 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} * @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 * @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 * Equivalent to calling @ref setLoopPoints() with @p loopEnd equal to
* @p INT_MAX. * @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} * @requires_al_extension Extension @al_extension{SOFT,loop_points}
*/ */
@ -144,10 +196,10 @@ class MAGNUM_AUDIO_EXPORT Buffer {
/** /**
* @brief Resets the loop points * @brief Resets the loop points
* @return Reference to self (for method chaining)
* *
* Equivalent to calling @ref setLoopPoints() with @p loopStart equal to * Equivalent to calling @ref setLoopPoints() with @p loopStart equal to
* 0, and @p loopEnd equal to @p INT_MAX. * 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} * @requires_al_extension Extension @al_extension{SOFT,loop_points}
*/ */

85
src/Magnum/Audio/Test/BufferALTest.cpp

@ -40,7 +40,11 @@ struct BufferALTest: TestSuite::Tester {
void construct(); void construct();
void setData(); void size();
void channels();
void bitDepth();
void length();
void loopPoints();
void setLoopPoints(); void setLoopPoints();
void setLoopSince(); void setLoopSince();
void setLoopUntil(); void setLoopUntil();
@ -52,7 +56,11 @@ struct BufferALTest: TestSuite::Tester {
BufferALTest::BufferALTest() { BufferALTest::BufferALTest() {
addTests({&BufferALTest::construct, addTests({&BufferALTest::construct,
&BufferALTest::setData, &BufferALTest::size,
&BufferALTest::channels,
&BufferALTest::bitDepth,
&BufferALTest::length,
&BufferALTest::loopPoints,
&BufferALTest::setLoopPoints, &BufferALTest::setLoopPoints,
&BufferALTest::setLoopSince, &BufferALTest::setLoopSince,
&BufferALTest::setLoopUntil, &BufferALTest::setLoopUntil,
@ -64,15 +72,52 @@ void BufferALTest::construct() {
CORRADE_VERIFY(buf.id() != 0); CORRADE_VERIFY(buf.id() != 0);
} }
void BufferALTest::setData() { void BufferALTest::size() {
Buffer buf; Buffer buf;
constexpr char data[] { 25, 17, 24, 122, 67, 24, 48, 96 }; constexpr char data[] { 25, 17, 24, 122, 67, 24, 48, 96 };
buf.setData(BufferFormat::Mono8, data, 22050); buf.setData(BufferFormat::Mono8, data, 22050);
ALint bufSize; CORRADE_COMPARE(buf.size(), 8);
alGetBufferi(buf.id(), AL_SIZE, &bufSize); }
CORRADE_VERIFY(bufSize == 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<Audio::Extensions::AL::SOFT::loop_points>()) {
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() { void BufferALTest::setLoopPoints() {
@ -84,12 +129,7 @@ void BufferALTest::setLoopPoints() {
constexpr char data[] { 25, 17, 24, 122, 67, 24, 48, 96 }; constexpr char data[] { 25, 17, 24, 122, 67, 24, 48, 96 };
buf.setData(BufferFormat::Mono8, data, 22050).setLoopPoints(1, 6); buf.setData(BufferFormat::Mono8, data, 22050).setLoopPoints(1, 6);
CORRADE_COMPARE(buf.loopPoints(), std::make_pair(1, 6));
Containers::Array<ALint> points{Containers::InPlaceInit, { 0, 0 }};
alGetBufferiv(buf.id(), AL_LOOP_POINTS_SOFT, points.data());
CORRADE_COMPARE_AS(points,
(Containers::Array<ALint>{Containers::InPlaceInit, { 1, 6 }}),
TestSuite::Compare::Container<Containers::ArrayView<const ALint>>);
} }
void BufferALTest::setLoopSince() { void BufferALTest::setLoopSince() {
@ -101,12 +141,7 @@ void BufferALTest::setLoopSince() {
constexpr char data[] { 25, 17, 24, 122, 67, 24, 48, 96 }; constexpr char data[] { 25, 17, 24, 122, 67, 24, 48, 96 };
buf.setData(BufferFormat::Mono8, data, 22050).setLoopSince(3); buf.setData(BufferFormat::Mono8, data, 22050).setLoopSince(3);
CORRADE_COMPARE(buf.loopPoints(), std::make_pair(3, 8));
Containers::Array<ALint> points{Containers::InPlaceInit, { 0, 0 }};
alGetBufferiv(buf.id(), AL_LOOP_POINTS_SOFT, points.data());
CORRADE_COMPARE_AS(points,
(Containers::Array<ALint>{Containers::InPlaceInit, { 3, 8 }}),
TestSuite::Compare::Container<Containers::ArrayView<const ALint>>);
} }
void BufferALTest::setLoopUntil() { void BufferALTest::setLoopUntil() {
@ -118,12 +153,7 @@ void BufferALTest::setLoopUntil() {
constexpr char data[] { 25, 17, 24, 122, 67, 24, 48, 96 }; constexpr char data[] { 25, 17, 24, 122, 67, 24, 48, 96 };
buf.setData(BufferFormat::Mono8, data, 22050).setLoopUntil(5); buf.setData(BufferFormat::Mono8, data, 22050).setLoopUntil(5);
CORRADE_COMPARE(buf.loopPoints(), std::make_pair(0, 5));
Containers::Array<ALint> points{Containers::InPlaceInit, { 0, 0 }};
alGetBufferiv(buf.id(), AL_LOOP_POINTS_SOFT, points.data());
CORRADE_COMPARE_AS(points,
(Containers::Array<ALint>{Containers::InPlaceInit, { 0, 5 }}),
TestSuite::Compare::Container<Containers::ArrayView<const ALint>>);
} }
void BufferALTest::resetLoopPoints() { void BufferALTest::resetLoopPoints() {
@ -135,12 +165,7 @@ void BufferALTest::resetLoopPoints() {
constexpr char data[] { 25, 17, 24, 122, 67, 24, 48, 96 }; constexpr char data[] { 25, 17, 24, 122, 67, 24, 48, 96 };
buf.setData(BufferFormat::Mono8, data, 22050).resetLoopPoints(); buf.setData(BufferFormat::Mono8, data, 22050).resetLoopPoints();
CORRADE_COMPARE(buf.loopPoints(), std::make_pair(0, 8));
Containers::Array<ALint> points{Containers::InPlaceInit, { 0, 0 }};
alGetBufferiv(buf.id(), AL_LOOP_POINTS_SOFT, points.data());
CORRADE_COMPARE_AS(points,
(Containers::Array<ALint>{Containers::InPlaceInit, { 0, 8 }}),
TestSuite::Compare::Container<Containers::ArrayView<const ALint>>);
} }
}}}} }}}}

Loading…
Cancel
Save