File: SoundItem.cpp

package info (click to toggle)
spring 106.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 55,316 kB
  • sloc: cpp: 543,954; ansic: 44,800; python: 12,575; java: 12,201; awk: 5,889; sh: 1,796; asm: 1,546; xml: 655; perl: 405; php: 211; objc: 194; makefile: 76; sed: 2
file content (95 lines) | stat: -rw-r--r-- 2,256 bytes parent folder | download | duplicates (3)
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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include "SoundItem.h"

#include <algorithm>
#include <sstream>
#include <stdexcept>
#include <cfloat>

#include "SoundBuffer.h"
#include "System/GlobalRNG.h"

namespace
{
	CGlobalUnsyncedRNG randnum; // no need for strong randomness here, so default seed is ok

	template <typename T>
	inline bool MapEntryValExtract(const spring::unordered_map<std::string, std::string>& map, const std::string& key, T& t)
	{
		auto it = map.find(key);
		if (it != map.end()) {
			std::istringstream stream(it->second);
			stream >> t;
			return true;
		}

		return false;
	}

	template <typename T>
	void FitInIntervall(const T& lower, T& val, const T& upper)
	{
		val = std::max(std::min(val, upper), lower);
	}
}

SoundItem::SoundItem(size_t itemID, size_t bufferID, const spring::unordered_map<std::string, std::string>& items)
	: soundItemID(itemID)
	, soundBufferID(bufferID)
	, maxDist(FLT_MAX)
{
	if (!MapEntryValExtract(items, "name", name))
		name = (SoundBuffer::GetById(bufferID)).GetFilename();

	MapEntryValExtract(items, "gain", gain);
	MapEntryValExtract(items, "gainmod", gainMod);
	FitInIntervall(0.f, gainMod, 1.f);
	MapEntryValExtract(items, "pitch", pitch);
	MapEntryValExtract(items, "pitchmod", pitchMod);
	FitInIntervall(0.f, pitchMod, 1.f);
	MapEntryValExtract(items, "dopplerscale", dopplerScale);
	MapEntryValExtract(items, "priority", priority);
	MapEntryValExtract(items, "maxconcurrent", maxConcurrent);
	MapEntryValExtract(items, "maxdist", maxDist);
	MapEntryValExtract(items, "rolloff", rolloff);
	MapEntryValExtract(items, "in3d", in3D);
	MapEntryValExtract(items, "looptime", loopTime);
}

bool SoundItem::PlayNow()
{
	if (maxConcurrent >= currentlyPlaying) {
		currentlyPlaying++;
		return true;
	}

	return false;
}

void SoundItem::StopPlay()
{
	assert(currentlyPlaying > 0);
	--currentlyPlaying;
}

float SoundItem::GetGain() const
{
	float tgain = 0.0f;

	if (gainMod > 0.0f)
		tgain = (float(randnum(200)) / 100.0f - 1.0f) * gainMod;

	return gain * (1.0f + tgain);
}

float SoundItem::GetPitch() const
{
	float tpitch = 0.0f;

	if (pitchMod > 0.0f)
		tpitch = (float(randnum(200)) / 100.0f - 1.0f) * pitchMod;

	return pitch * (1.0f + tpitch);
}