File: Sound.cpp

package info (click to toggle)
astromenace 1.3.2%2Brepack-7.1
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 4,496 kB
  • sloc: cpp: 61,665; makefile: 25; sh: 19
file content (209 lines) | stat: -rwxr-xr-x 6,159 bytes parent folder | download | duplicates (5)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/************************************************************************************

	AstroMenace (Hardcore 3D space shooter with spaceship upgrade possibilities)
	Copyright © 2006-2013 Michael Kurinnoy, Viewizard


	AstroMenace is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	AstroMenace is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with AstroMenace. If not, see <http://www.gnu.org/licenses/>.


	Web Site: http://www.viewizard.com/
	Project: http://sourceforge.net/projects/openastromenace/
	E-mail: viewizard@viewizard.com

*************************************************************************************/


#include "Sound.h"
#include "Buffer.h"
#include "../System/System.h"


ALboolean CheckALError();
ALboolean CheckALUTError();





//------------------------------------------------------------------------------------
// Проигрывание звука
//------------------------------------------------------------------------------------
bool eSound::Play(const char *Name, float fVol, float fMainVol, float x, float y, float z, bool Relative, bool Loop, bool NeedReleaseStatus, int AtType)
{
	Source = 0;
	Age = 0.0f;
	LastUpdateTime = vw_GetTime();
	Group = 0;
	GroupCount = 0;
	SubGroup = 0;
	SubGroupCount = 0;
	Priority = 0;
	FileName = new char[strlen(Name)+1];
	strcpy(FileName,Name);


	DestroyTime = -1.0f;
	DestroyTimeStart = -1.0f;

	NeedRelease = NeedReleaseStatus;


	// находим существующий или делаем новый буфер
	ALuint Buffer = vw_FindBufferIDByName(Name);
	// у нас еще нет такого буфера, его надо попытаться создать
	if (Buffer == 0)
	{
		if( vw_TestFileExtension( Name, "wav" ) || vw_TestFileExtension( Name, "WAV" ))
		{
			Buffer = vw_CreateSoundBufferFromWAV(Name);
		}
		else
		{
			if( vw_TestFileExtension( Name, "ogg" ) || vw_TestFileExtension( Name, "OGG" ))
				Buffer = vw_CreateSoundBufferFromOGG(Name);
		}
	}

	if (Buffer == 0) return AL_FALSE;

	Volume = fVol;
	MainVolume = fMainVol;

	// Position of the source sound.
	ALfloat SourcePos[] = { x, y, z }; // -1.0 1.0 по иксам это баланс
	// Velocity of the source sound.
	ALfloat SourceVel[] = { 0.0f, 0.0f, 0.0f };

	// Bind the buffer with the source.
	alGenSources(1, &Source);
	if(!CheckALError())return AL_FALSE;

	alSourcei (Source, AL_BUFFER,   Buffer   );

	alSourcef (Source, AL_GAIN,     fVol*fMainVol	 ); // фактически громкость
	alSourcefv(Source, AL_POSITION, SourcePos);
	alSourcefv(Source, AL_VELOCITY, SourceVel);
	alSourcei (Source, AL_LOOPING,  Loop     );
	alSourcei (Source, AL_SOURCE_RELATIVE, Relative);
	alGetError(); // сброс ошибок



	// параметры затухания сигнала от источника
	if (AtType == 1)
	{
		alSourcef (Source, AL_REFERENCE_DISTANCE,  30.0f     );
		alSourcef (Source, AL_MAX_DISTANCE,  250.0f     );
		alSourcef (Source, AL_ROLLOFF_FACTOR,  0.5f     );
		alGetError(); // сброс ошибок
	}
	else
	if (AtType == 2)
	{
		alSourcef (Source, AL_REFERENCE_DISTANCE,  150.0f     );
		alSourcef (Source, AL_MAX_DISTANCE,  600.0f     );
		alSourcef (Source, AL_ROLLOFF_FACTOR,  0.2f     );
		alGetError(); // сброс ошибок
	}



	alSourcePlay(Source);
	if(!CheckALError()) return AL_FALSE;

	return AL_TRUE;
}



//------------------------------------------------------------------------------------
// перезапуск
//------------------------------------------------------------------------------------
void eSound::Replay()
{
	// если это не источник, уходим
	if (!alIsSource(Source)) return;

	alSourceRewind(Source);
	alSourcePlay(Source);
}




//------------------------------------------------------------------------------------
// остановка
//------------------------------------------------------------------------------------
void eSound::Stop(float StopDelay)
{
	// если это не источник, уходим
	if (!alIsSource(Source)) return;

	if (StopDelay < 0.0f) StopDelay = 0.0f;

	if (StopDelay == 0.0f)
	{
		alSourceStop(Source);
		alGetError(); // сброс ошибок
	}
	else
	{
		DestroyTimeStart = DestroyTime = StopDelay;
	}
}




//------------------------------------------------------------------------------------
// для 3д звука установка положения (баланса)
//------------------------------------------------------------------------------------
void eSound::SetLocation(float x, float y, float z)
{
	// если это не источник, уходим
	if (!alIsSource(Source)) return;

	ALfloat SourcePos[] = { x, y, z };
	alSourcefv(Source, AL_POSITION, SourcePos);
	alGetError(); // сброс ошибок
}



//------------------------------------------------------------------------------------
// установка громкости
//------------------------------------------------------------------------------------
void eSound::SetMainVolume(float NewMainVolume)
{
	// если это не источник, уходим
	if (!alIsSource(Source)) return;
	MainVolume = NewMainVolume;
	alSourcef (Source, AL_GAIN,     MainVolume*Volume );
	alGetError(); // сброс ошибок
}



//------------------------------------------------------------------------------------
// установка информации о звуке
//------------------------------------------------------------------------------------
void eSound::SetInfo(int NewGroup, int NewGroupCount, int NewSubGroup, int NewSubGroupCount, int NewPriority)
{
	Group = NewGroup;
	GroupCount = NewGroupCount;
	SubGroup = NewSubGroup;
	SubGroupCount = NewSubGroupCount;
	Priority = NewPriority;
}