audio.cpp 2.76 KB
Newer Older
Georg Schaefer's avatar
Georg Schaefer committed
1
2
3
4
5
6
#include <SDL.h>

#include <audio/audio.hpp>
#include <audio/audio_constants.hpp>
#include <audio/audio_helper.hpp>
#include <core/engine.hpp>
7
#include <util/config.hpp>
Georg Schaefer's avatar
Georg Schaefer committed
8
9
10


namespace gdw {
Georg Schaefer's avatar
Georg Schaefer committed
11
	audio::audio(engine& engine)
Georg Schaefer's avatar
Georg Schaefer committed
12
	: audio_collection_(engine) {
13
14
15
16
17
18
19
20
		auto config = engine.config_manager().load("config/settings");
		if (!config) {
			throw std::runtime_error{"could not load config from file config/settings.user or config/settings.main"};
		}

		music_volume_ = config->get<int>("music_volume", 100);
		sound_volume_ = config->get<int>("sound_volume", 100);

Georg Schaefer's avatar
Georg Schaefer committed
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
		if(Mix_Init(MIX_INIT_OGG) != MIX_INIT_OGG) {
			audio_error("sdl_mixer init");
		}

		//init SDL_mixer - use constants from SoundConstants.hpp
		if (Mix_OpenAudio(AUDIO_FREQUENCY, MIX_DEFAULT_FORMAT, AUDIO_CHANNELS, AUDIO_CHUNKSIZE)) {
			audio_error("sdl_openaudio");
		}

		//allocate channels
		Mix_AllocateChannels(50);

		is_music_playing_ = false;
		audio_collection_.load_music_collection();
		audio_collection_.load_sound_collection();
	}

	audio::~audio() {
		//free background_music_
		background_music_.reset();

		//close audio device
		Mix_CloseAudio();
	}

46
	void audio::update(float) {
Georg Schaefer's avatar
Georg Schaefer committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
		//just in case
	}


	void audio::load_music(const std::string& name, int loops, int fade_out, int fade_in) {
		loop_music_ = loops;

		//fade old music
		Mix_FadeOutMusic(fade_out);

		background_music_.reset();
		background_music_ = audio_collection_.music(name);

		if(!background_music_) {
61
			log(log_level::error) << "no music " << name << " found in audio collection" << std::endl;;
Georg Schaefer's avatar
Georg Schaefer committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
			return;
		}

		//play new music
		if (Mix_FadeInMusic(background_music_->get(), loops, fade_in) == -1) {
			audio_error("couldn't play music");
		}
	}

	void audio::pause_resume_music(int fade) {
		if (is_music_playing_) {
			Mix_FadeOutMusic(fade);
			is_music_playing_ = false;
		}
		else {
			if (Mix_FadeInMusic(background_music_->get(), loop_music_, fade) == -1) {
				audio_error("couldn't play music");
			}
			is_music_playing_ = true;
		}
	}

	void audio::stop_music() {
		Mix_HaltMusic();
		is_music_playing_ = false;
	}

	int audio::queue_sound(const std::string& name, int loop) {
90
91
92
93
94
		auto sound_ptr = audio_collection_.sound(name);
		if (!sound_ptr) {
			log(log_level::error) << "no sound " << name << " found in audio collection" << std::endl;
		}
		int channel = Mix_PlayChannel(-1, sound_ptr->get(), loop);
Georg Schaefer's avatar
Georg Schaefer committed
95
96
97
98
99
100
101
102
103
104
105
		if (channel < 0) {
			audio_error("couldn't play sound");
		}
		Mix_Volume(channel, (int)(1.28f * sound_volume_));
		return channel;
	}

	void audio::stop_sound(int channel, int fade_out) {
		if(channel < -1) {
			return;
		}
106
		Mix_FadeOutChannel(channel, fade_out + 1);
Georg Schaefer's avatar
Georg Schaefer committed
107
	}
108
109

	void audio::stereo(int channel, int left, int right) {
110
111
		left	= (Uint8) (left * 254 / 100);
		right	= (Uint8) (right * 254 / 100);
112
113
		Mix_SetPanning(channel, left, right);
	}
Georg Schaefer's avatar
Georg Schaefer committed
114
}