1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-25 03:42:48 +01:00

Prepare tests for Sound and SoundBuffer

Also refactored SoundBuffer a bit.
This commit is contained in:
Filip Gawin 2018-08-22 23:01:25 +02:00
parent 6aa9b3a1b7
commit 77e0e3a3bc
7 changed files with 108 additions and 8 deletions

View File

@ -22,8 +22,8 @@ struct Sound {
size_t id = 0;
bool isLoaded = false;
std::shared_ptr<SoundSource> source = nullptr;
std::unique_ptr<SoundBuffer> buffer = nullptr;
std::shared_ptr<SoundSource> source;
std::unique_ptr<SoundBuffer> buffer;
Sound() = default;

View File

@ -9,10 +9,7 @@
/// OpenAL tool for playing
/// sound instance.
class SoundBuffer {
friend class SoundManager;
public:
struct SoundBuffer {
SoundBuffer();
bool bufferData(SoundSource& soundSource);
@ -30,7 +27,6 @@ public:
void setGain(float gain);
void setMaxDistance(float maxDist);
private:
ALuint source;
ALuint buffer;
};

View File

@ -28,6 +28,11 @@ Sound& SoundManager::getSoundRef(const std::string& name) {
return sounds[name]; // @todo reloading, how to check is it wav/mp3?
}
SoundManager::SoundManager() {
initializeOpenAL();
initializeAVCodec();
}
SoundManager::SoundManager(GameWorld* engine) : _engine(engine) {
auto sdtPath = _engine->data->index.findFilePath("audio/sfx.SDT");
auto rawPath = _engine->data->index.findFilePath("audio/sfx.RAW");

View File

@ -29,6 +29,7 @@ class ViewCamera;
/// instances simultaneously without duplicating raw source).
class SoundManager {
public:
SoundManager();
SoundManager(GameWorld* engine);
~SoundManager();

View File

@ -9,7 +9,7 @@
/// (loading and decoding sound)
class SoundSource {
friend class SoundManager;
friend class SoundBuffer;
friend struct SoundBuffer;
public:
/// Load sound from mp3/wav file

View File

@ -28,6 +28,7 @@ set(TESTS
ScriptMachine
State
StringEncoding
Sound
Text
TrafficDirector
Vehicle

97
tests/test_Sound.cpp Normal file
View File

@ -0,0 +1,97 @@
#include <iostream>
#include <boost/test/unit_test.hpp>
#include <audio/Sound.hpp>
#include <audio/SoundManager.hpp>
BOOST_AUTO_TEST_SUITE(SoundTests)
struct F {
SoundManager manager{};
Sound sound{};
};
BOOST_FIXTURE_TEST_CASE(creates_empty_sound, F) {
// buffer and source should be empty
BOOST_REQUIRE(sound.buffer == nullptr);
BOOST_REQUIRE(sound.source == nullptr);
}
// @todo Shfil119 implement
// This test requires assets
//BOOST_AUTO_TEST_CASE(testBufferIsPlaying) {
// sound.buffer = std::make_unique<SoundBuffer>();
// BOOST_REQUIRE(sound.isPlaying() == false);
// sound.play();
// BOOST_REQUIRE(sound.isPlaying() == true);
// sound.pause();
// BOOST_REQUIRE(sound.isPaused() == true);
// sound.stop();
// BOOST_REQUIRE(sound.isStopped() == true);
//}
BOOST_FIXTURE_TEST_CASE(sound_sets_openal_source_position, F) {
sound.buffer = std::make_unique<SoundBuffer>();
glm::vec3 position = {100.f, 100.f, 100.f};
sound.setPosition(position);
float x{0.f}, y{0.f}, z{0.f};
alGetSource3f(sound.buffer->source, AL_POSITION, &x, &y, &z);
BOOST_REQUIRE(x == position.x);
BOOST_REQUIRE(y == position.y);
BOOST_REQUIRE(z == position.z);
}
BOOST_FIXTURE_TEST_CASE(sound_sets_openal_source_looping, F) {
sound.buffer = std::make_unique<SoundBuffer>();
// Default state should be false
// openAL should change value of state
int state = static_cast<bool>(true);
alGetSourcei(sound.buffer->source, AL_LOOPING, &state);
BOOST_REQUIRE(static_cast<bool>(state) == false);
sound.setLooping(true);
alGetSourcei(sound.buffer->source, AL_LOOPING, &state);
BOOST_REQUIRE(static_cast<bool>(state) == true);
}
BOOST_FIXTURE_TEST_CASE(sound_sets_openal_source_pitch, F) {
sound.buffer = std::make_unique<SoundBuffer>();
sound.setPitch(0.5f);
float pitch{0.f};
alGetSourcef(sound.buffer->source, AL_PITCH, &pitch);
BOOST_REQUIRE(pitch == 0.5f);
}
BOOST_FIXTURE_TEST_CASE(sound_sets_openal_source_gain, F) {
sound.buffer = std::make_unique<SoundBuffer>();
sound.setGain(0.5f);
float gain{0.f};
alGetSourcef(sound.buffer->source, AL_GAIN, &gain);
BOOST_REQUIRE(gain == 0.5f);
}
BOOST_FIXTURE_TEST_CASE(sound_sets_openal_source_max_distance, F) {
sound.buffer = std::make_unique<SoundBuffer>();
sound.setMaxDistance(1000.f);
float maxDistance{0.f};
alGetSourcef(sound.buffer->source, AL_MAX_DISTANCE, &maxDistance);
BOOST_REQUIRE(maxDistance == 1000.f);
}
BOOST_AUTO_TEST_SUITE_END()