aboutsummaryrefslogtreecommitdiff
path: root/src/test/AudioTest.cpp
blob: 2a2d0e1bf38982b22301a40819baef95a432fd6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "util/Log.h"
#include <gtest/gtest.h>
#include <future>

#include <crepe/manager/ComponentManager.h>
#include <crepe/manager/ResourceManager.h>
#include <crepe/api/AudioSource.h>
#include <crepe/api/GameObject.h>
#include <crepe/system/AudioSystem.h>

using namespace std;
using namespace std::chrono_literals;
using namespace crepe;
using namespace testing;

class AudioTest : public Test {
	Mediator mediator;
public:
	ComponentManager component_manager{mediator};
	ResourceManager resource_manager{mediator};
	AudioSystem system {mediator};

private:
	GameObject entity = component_manager.new_object("name");
public:
	AudioSource & bgm = entity.add_component<AudioSource>("mwe/audio/bgm.ogg");
	AudioSource & sfx1 = entity.add_component<AudioSource>("mwe/audio/sfx1.wav");
	AudioSource & sfx2 = entity.add_component<AudioSource>("mwe/audio/sfx2.wav");
	AudioSource & sfx3 = entity.add_component<AudioSource>("mwe/audio/sfx3.wav");

	void SetUp() override {
		bgm.play_on_awake = true;
	}
};

TEST_F(AudioTest, Default) {
	bool example_done = false;

	future example = async([&](){
		// Start the background track. This happens automatically due to the play_on_awake property
		// being true. The following call is optional and doesn't start two simultanious voices if
		// left in:
		// bgm.play();

		// Play each sample sequentially while pausing and resuming the background track
		this_thread::sleep_for(500ms);
		sfx1.play();
		this_thread::sleep_for(500ms);
		sfx2.play();
		bgm.active = false;
		this_thread::sleep_for(500ms);
		sfx3.play();
		bgm.active = true;
		this_thread::sleep_for(500ms);

		// Play all samples simultaniously
		sfx1.play();
		sfx2.play();
		sfx3.play();
		this_thread::sleep_for(1000ms);
	});

	future system_loop = async([&](){
		while (!example_done) {
			auto next = chrono::steady_clock::now() + 25ms;
			system.update();
			this_thread::sleep_until(next);
		}
	});

	example.wait();
	example_done = true;
	system_loop.wait();
}