File: ISound.cpp

package info (click to toggle)
spring 104.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 47,512 kB
  • sloc: cpp: 391,093; ansic: 79,943; python: 12,356; java: 12,201; awk: 5,889; sh: 1,826; xml: 655; makefile: 486; perl: 405; php: 211; objc: 194; sed: 2
file content (128 lines) | stat: -rw-r--r-- 4,290 bytes parent folder | download
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include "ISound.h"

#ifndef   NO_SOUND
#include "OpenAL/Sound.h"
#endif // NO_SOUND
#include "Null/NullSound.h"

#include "SoundLog.h"
#include "System/Config/ConfigHandler.h"

#include "ISoundChannels.h"
#include "Null/NullAudioChannel.h"
#ifndef NO_SOUND
#include "OpenAL/AudioChannel.h"
#endif

#include "System/TimeProfiler.h"
#include "System/SafeUtil.h"

CONFIG(bool, Sound).defaultValue(true).description("Select the Sound driver, true = OpenAL, false = NullAudio");

// defined here so spring-headless contains them, too (default & headless should contain the same set of configtags!)
CONFIG(int, MaxSounds).defaultValue(128).headlessValue(1).minimumValue(1).description("Maximum parallel played sounds.");
CONFIG(bool, PitchAdjust).defaultValue(false).description("When enabled adjust sound speed/pitch to game speed.");
CONFIG(int, snd_volmaster).defaultValue(60).minimumValue(0).maximumValue(200).description("Master sound volume.");
CONFIG(int, snd_volgeneral).defaultValue(100).minimumValue(0).maximumValue(200).description("Volume for \"general\" sound channel.");
CONFIG(int, snd_volunitreply).defaultValue(100).minimumValue(0).maximumValue(200).description("Volume for \"unit reply\" sound channel.");
CONFIG(int, snd_volbattle).defaultValue(100).minimumValue(0).maximumValue(200).description("Volume for \"battle\" sound channel.");
CONFIG(int, snd_volui).defaultValue(100).minimumValue(0).maximumValue(200).description("Volume for \"ui\" sound channel.");
CONFIG(int, snd_volmusic).defaultValue(100).minimumValue(0).maximumValue(200).description("Volume for \"music\" sound channel.");
CONFIG(std::string, snd_device).defaultValue("").description("Sets the used output device. See \"Available Devices\" section in infolog.txt.");

CONFIG(float, snd_airAbsorption).defaultValue(0.1f);
CONFIG(bool, UseEFX).defaultValue(true).safemodeValue(false);


ISound* ISound::singleton = nullptr;

ISound::ISound()
	: numEmptyPlayRequests(0)
	, numAbortedPlays(0)
{
}

void ISound::Initialize(bool forceNullSound)
{
	if (singleton != nullptr) {
		LOG_L(L_WARNING, "[ISound::%s] already initialized!", __func__);
		return;
	}

#ifndef NO_SOUND
	if (!IsNullAudio() && !forceNullSound) {
		Channels::BGMusic = new AudioChannel();
		Channels::General = new AudioChannel();
		Channels::Battle = new AudioChannel();
		Channels::UnitReply = new AudioChannel();
		Channels::UserInterface = new AudioChannel();

		{
			ScopedOnceTimer timer("ISound::Init::New");
			singleton = new CSound();
		}
		{
			ScopedOnceTimer timer("ISound::Init::Dev");

			// sound device is initialized in a thread, must wait
			// for it to finish (otherwise LoadSoundDefs can fail)
			while (!singleton->CanLoadSoundDefs()) {
				LOG("[ISound::%s] spawning sound-thread (%.1fms)", __func__, (timer.GetDuration()).toMilliSecsf());

				if (singleton->SoundThreadQuit()) {
					// no device or context found, fallback
					ChangeOutput(true); break;
				}

				spring_sleep(spring_msecs(100));
			}
		}
	} else
#endif // NO_SOUND
	{
		Channels::BGMusic = new NullAudioChannel();
		Channels::General = new NullAudioChannel();
		Channels::Battle = new NullAudioChannel();
		Channels::UnitReply = new NullAudioChannel();
		Channels::UserInterface = new NullAudioChannel();
		singleton = new NullSound();
	}
}

void ISound::Shutdown()
{
	spring::SafeDelete(singleton);

	spring::SafeDelete(Channels::BGMusic);
	spring::SafeDelete(Channels::General);
	spring::SafeDelete(Channels::Battle);
	spring::SafeDelete(Channels::UnitReply);
	spring::SafeDelete(Channels::UserInterface);
}


bool ISound::IsNullAudio()
{
	return !configHandler->GetBool("Sound");
}


bool ISound::ChangeOutput(bool forceNullSound)
{
	// FIXME: on reload, sound-ids change (depends on order when they are requested, see GetSoundId()/GetSoundItem()
	if (IsNullAudio())
		LOG_L(L_ERROR, "[ISound::%s] re-enabling sound isn't supported yet, expect problems!", __func__);

	Shutdown();
	configHandler->Set("Sound", IsNullAudio() || forceNullSound);
	Initialize(forceNullSound);
	return (IsNullAudio() || forceNullSound);
}

bool ISound::LoadSoundDefs(const std::string& filename, const std::string& modes)
{
	return (singleton->LoadSoundDefsImpl(filename, modes));
}