File: IAudioPlayer.cpp

package info (click to toggle)
jazz2-native 3.5.0-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 16,912 kB
  • sloc: cpp: 172,557; xml: 113; python: 36; makefile: 5; sh: 2
file content (175 lines) | stat: -rw-r--r-- 4,961 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#if defined(WITH_AUDIO)
#	define NCINE_INCLUDE_OPENAL
#	include "../CommonHeaders.h"
#endif

#include "IAudioPlayer.h"
#include "IAudioDevice.h"
#include "../CommonConstants.h"
#include "../ServiceLocator.h"
#include "../Primitives/Vector3.h"

namespace nCine
{
	IAudioPlayer::IAudioPlayer(ObjectType type)
		: Object(type), sourceId_(IAudioDevice::UnavailableSource), state_(PlayerState::Stopped), flags_(PlayerFlags::None),
			gain_(1.0f), pitch_(1.0f), lowPass_(1.0f), position_(0.0f, 0.0f, 0.0f), filterHandle_(0)
	{
	}

	IAudioPlayer::~IAudioPlayer()
	{
#if defined(WITH_AUDIO) && defined(OPENAL_FILTERS_SUPPORTED)
		if (filterHandle_ != 0) {
			alDeleteFilters(1, &filterHandle_);
			filterHandle_ = 0;
		}
#endif
	}

	std::int32_t IAudioPlayer::sampleOffset() const
	{
#if defined(WITH_AUDIO)
		ALint byteOffset = 0;
		alGetSourcei(sourceId_, AL_SAMPLE_OFFSET, &byteOffset);
		return byteOffset;
#else
		return 0;
#endif
	}

	void IAudioPlayer::setSampleOffset(std::int32_t byteOffset)
	{
#if defined(WITH_AUDIO)
		alSourcei(sourceId_, AL_SAMPLE_OFFSET, byteOffset);
#endif
	}

	/*! The change is applied to the OpenAL source only when playing. */
	void IAudioPlayer::setSourceRelative(bool value)
	{
		if (GetFlags(PlayerFlags::SourceRelative) != value) {
			SetFlags(PlayerFlags::SourceRelative, value);
#if defined(WITH_AUDIO)
			if (state_ == PlayerState::Playing) {
				alSourcei(sourceId_, AL_SOURCE_RELATIVE, value ? AL_TRUE : AL_FALSE);
			}
#endif
		}
	}

	/*! The change is applied to the OpenAL source only when playing. */
	void IAudioPlayer::setGain(float gain)
	{
		gain_ = gain;
#if defined(WITH_AUDIO)
		if (state_ == PlayerState::Playing) {
			alSourcef(sourceId_, AL_GAIN, gain_);
		}
#endif
	}

	/*! The change is applied to the OpenAL source only when playing. */
	void IAudioPlayer::setPitch(float pitch)
	{
		pitch_ = pitch;
#if defined(WITH_AUDIO)
		if (state_ == PlayerState::Playing) {
			alSourcef(sourceId_, AL_PITCH, pitch_);
		}
#endif
	}

	void IAudioPlayer::setLowPass(float value)
	{
		if (lowPass_ != value) {
			lowPass_ = value;
			if (state_ == PlayerState::Playing) {
				updateFilters();
			}
		}
	}

	/*! The change is applied to the OpenAL source only when playing. */
	void IAudioPlayer::setPosition(const Vector3f& position)
	{
		position_ = position;
		if (state_ == PlayerState::Playing) {
			IAudioDevice& device = theServiceLocator().GetAudioDevice();
			setPositionInternal(getAdjustedPosition(device, position_, GetFlags(PlayerFlags::SourceRelative), GetFlags(PlayerFlags::As2D)));
		}
	}

	void IAudioPlayer::updateFilters()
	{
#if defined(WITH_AUDIO) && defined(OPENAL_FILTERS_SUPPORTED)
		if (lowPass_ < 1.0f) {
			if (filterHandle_ == 0) {
				alGenFilters(1, &filterHandle_);
				alFilteri(filterHandle_, AL_FILTER_TYPE, AL_FILTER_LOWPASS);
				alFilterf(filterHandle_, AL_LOWPASS_GAIN, 1.0f);
			}
			if (filterHandle_ != 0) {
				alFilterf(filterHandle_, AL_LOWPASS_GAINHF, lowPass_);
				alSourcei(sourceId_, AL_DIRECT_FILTER, filterHandle_);
			}
		} else {
			if (filterHandle_ != 0) {
				alFilterf(filterHandle_, AL_LOWPASS_GAINHF, 1.0f);
			}
			alSourcei(sourceId_, AL_DIRECT_FILTER, 0);
		}
#endif
	}

	void IAudioPlayer::setPositionInternal(const Vector3f& position)
	{
#if defined(WITH_AUDIO)
		alSource3f(sourceId_, AL_POSITION, position.X, position.Y, position.Z);
#endif
	}

	Vector3f IAudioPlayer::getAdjustedPosition(IAudioDevice& device, const Vector3f& pos, bool isSourceRelative, bool isAs2D)
	{
		if (isAs2D) {
			// Let's do a +/- 30° panning for 2D audio, locked to front
			Vector2f panningPos = Vector2f::FromAngleLength(30.0f * fDegToRad * pos.X, 1.0f);
			return Vector3(panningPos.X, 0.0f, -std::abs(panningPos.Y));
		}

		Vector3f listenerPos;
		Vector3f adjustedPos = Vector3f(pos.X * IAudioDevice::LengthToPhysical, pos.Y * -IAudioDevice::LengthToPhysical, pos.Z * -IAudioDevice::LengthToPhysical);

		if (!isSourceRelative) {
			listenerPos = device.getListenerPosition();
			listenerPos.X *= IAudioDevice::LengthToPhysical;
			listenerPos.Y *= -IAudioDevice::LengthToPhysical;
			listenerPos.Z *= -IAudioDevice::LengthToPhysical;

			adjustedPos -= listenerPos;
		}

		// Flatten depth position a little, so far away sounds that can still be seen appear louder
		adjustedPos.Z *= 0.5f;

		// Normalize audio position for smooth panning when near. Do it in physical units, so this remains constant regardless of unit changes.
		constexpr float SmoothPanRadius = 26.0f;
		float listenerSpaceDist = adjustedPos.Length();
		if (listenerSpaceDist < SmoothPanRadius) {
			float panningActive = listenerSpaceDist / SmoothPanRadius;
			adjustedPos = Vector3f::Lerp(
								Vector3(0.0f, 0.0f, 1.0f + (SmoothPanRadius - 1.0f) * panningActive),
								adjustedPos,
								panningActive);
		}

		// Ensure the source is always at the front
		adjustedPos.Z = -std::abs(adjustedPos.Z);

		if (!isSourceRelative) {
			adjustedPos += listenerPos;
		}

		return adjustedPos;
	}
}