File: Music.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 (386 lines) | stat: -rwxr-xr-x 9,609 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/************************************************************************************

	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 "../System/System.h"



ALboolean CheckALError();
ALboolean CheckALUTError();




//------------------------------------------------------------------------------------
//	Vorbis callback functions
//------------------------------------------------------------------------------------
size_t VorbisRead(void *ptr, size_t byteSize, size_t sizeToRead, void *datasource)
{
	// нужно возвращать кол-во прочитаных byteSize
	eFILE* vorbisData = (eFILE*)datasource;
	return vorbisData->fread(ptr, byteSize, sizeToRead);
}


int VorbisSeek(void *datasource, ogg_int64_t offset, int whence)
{
	eFILE* vorbisData = (eFILE*)datasource;
	return vorbisData->fseek(offset, whence);
}


int VorbisClose(void *UNUSED(datasource))
{// похоже, без этой функции у линукс версии vorbis проблемы, хотя по документации...
	return 1;
}


long VorbisTell(void *datasource)
{
	eFILE*	vorbisData = (eFILE*)datasource;
	return vorbisData->ftell();
}





//------------------------------------------------------------------------------------
// Читаем фрагмент ogg файла
//------------------------------------------------------------------------------------
bool eMusic::ReadOggBlock(ALuint BufID, size_t Size)
{
	// vars
	int			current_section;
	unsigned int TotalRet = 0;
	long		ret = 0;
	char		*PCM;

	if (Size < 1) return false;
	PCM = new char[Size];

	// Read loop
	while (TotalRet < Size)
	{
		ret = ov_read(mVF, PCM + TotalRet, Size - TotalRet, 0, 2, 1, &current_section);

		// if end of file or read limit exceeded
		if (ret == 0) break;
		else if (ret < 0) 		// Error in bitstream
		{
			//
		}
		else
		{
			TotalRet += ret;
		}
	}
	if (TotalRet > 0)
	{
		alBufferData(BufID, Format, PCM, TotalRet, Rate);
		CheckALError();
	}
	delete [] PCM;
	return (ret > 0);
}







//------------------------------------------------------------------------------------
// Проигрывание звука
//------------------------------------------------------------------------------------
bool eMusic::Play(const char * Name, float fVol, float fMainVol, bool Loop, const char *LoopFileName)
{
	Volume = fVol;
	MainVolume = fMainVol;

	if (LoopFileName == 0)
	{
		LoopPart[0] = 0;
	}
	else
	{
		strcpy(LoopPart, LoopFileName);
	}

	FadeInSw = false;
	FadeInStartVol = Volume;
	FadeInEndVol = Volume;
	FadeOutSw = false;
	FadeTime = 0.0f;
	FadeAge = 0.0f;
	LastTime = vw_GetTime();
	mVF = 0;

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

	alGenSources(1, &Source);
	if(!CheckALError()) return AL_FALSE;

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


	// делаем буфер...

	// Open Ogg file
	MusicFile = vw_fopen(Name);
	if (MusicFile == 0) return false;

	int	 i, DynBuffs = 1, BlockSize;

	// OggVorbis specific structures
	ov_callbacks	cb;
	// Fill cb struct
	cb.close_func	= VorbisClose;
	cb.read_func	= VorbisRead;
	cb.seek_func	= VorbisSeek;
	cb.tell_func	= VorbisTell;
	// Create OggVorbis_File struct
	mVF = new OggVorbis_File;

	// Generate local buffers
	if (ov_open_callbacks(MusicFile, mVF, NULL, 0, cb) < 0)
	{
		// This is not ogg bitstream. Return
		return false;
	}



	// Check for existance of sound
	BlockSize	= DYNBUF_SIZE;
	DynBuffs	= NUM_OF_DYNBUF;

	// Return vorbis_comment and vorbis_info structures
	mComment		= ov_comment(mVF, -1);
	mInfo			= ov_info(mVF, -1);

	// Fill buffer infos
	Rate		= mInfo->rate;
	Format 		= (mInfo->channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;

	// Create buffers
	alGenBuffers(DynBuffs, Buffers);
	if (!CheckALError()) return false;

	for (i = 0; i < DynBuffs; i++)
	{
		// Read amount (DYNBUF_SIZE) data into each buffer
		ReadOggBlock(Buffers[i], BlockSize);
		if (!CheckALError()) return false;

		alSourceQueueBuffers(Source, 1, &Buffers[i]);
		if (!CheckALError()) return false;
	}




	// запускаем на проигрывание

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

	return AL_TRUE;
}



//------------------------------------------------------------------------------------
// обновление потока
//------------------------------------------------------------------------------------
bool eMusic::Update()
{
	int	 Processed = 0;

	alGetSourcei(Source, AL_BUFFERS_PROCESSED, &Processed);

	// We still have processed buffers
	while (Processed--)
	{
		// не создаем новые буферы, просто берем уже использованный и заполняем новыми данными
		// а потом ставим его опять в очередь
		ALuint bufferID;
		alSourceUnqueueBuffers(Source, 1, &bufferID);
		if (!CheckALError()) return true;

		if (ReadOggBlock(bufferID, DYNBUF_SIZE) != 0)
		{
			alSourceQueueBuffers(Source, 1, &bufferID);
			if (!CheckALError()) return true;
		}
		else
		{

			if (LoopPart[0] != 0)
			{
				if (mVF != 0){ov_clear(mVF);delete mVF; mVF = 0;}
				if (MusicFile != 0)
				{
					vw_fclose(MusicFile);
				}

				// Open Ogg file
				MusicFile = vw_fopen(LoopPart);
				if (MusicFile != 0)
				{
					// OggVorbis specific structures
					ov_callbacks	cb;
					// Fill cb struct
					cb.close_func	= VorbisClose;
					cb.read_func	= VorbisRead;
					cb.seek_func	= VorbisSeek;
					cb.tell_func	= VorbisTell;
					// Create OggVorbis_File struct
					mVF = new OggVorbis_File;

					// Generate local buffers
					if (ov_open_callbacks(MusicFile, mVF, NULL, 0, cb) < 0)
					{
						// This is not ogg bitstream. Return
						return false;
					}

				}
				else return false;

				Looped = true;
				LoopPart[0] = 0;
			}


			// если не нужно больше повторять - выходим
			if (!Looped) return false;

			// устанавливаем на начало
			ov_pcm_seek(mVF, 0);
			alSourceQueueBuffers(Source, 1, &bufferID);
			if (!CheckALError()) return true;

		}
	}


	// обрабатываем эффекты
	float TimeDelta = vw_GetTime() - LastTime;

	if (FadeInSw)
	if (Volume < FadeInEndVol)
	{
		FadeTime += TimeDelta;
		Volume = FadeInEndVol*(FadeTime/FadeAge);
		alSourcef(Source, AL_GAIN, MainVolume*Volume );
		alGetError(); // сброс ошибок
	}

	if (FadeOutSw)
	if (Volume > 0.0f)
	{
		FadeTime += TimeDelta;
		Volume = FadeInStartVol*((FadeAge-FadeTime)/FadeAge);
		alSourcef(Source, AL_GAIN, MainVolume*Volume);
		alGetError(); // сброс ошибок
		if (Volume <= 0.0f) return false;
	}

	LastTime = vw_GetTime();



	// если по какой-то причине перестали играть...
	ALint TMPS;
	alGetSourcei(Source, AL_SOURCE_STATE, &TMPS);
	alGetError(); // сброс ошибок
	if (TMPS == AL_STOPPED) return false;

	return true;
}





//------------------------------------------------------------------------------------
// установка громкости
//------------------------------------------------------------------------------------
void eMusic::SetMainVolume(float NewMainVolume)
{
	// если это не источник, уходим
	if (!alIsSource(Source)) return;

	MainVolume = NewMainVolume;
	alSourcef(Source, AL_GAIN,     MainVolume*Volume );
}



//------------------------------------------------------------------------------------
// плавное появление
//------------------------------------------------------------------------------------
void eMusic::FadeIn(float EndVol, float Time)
{
	FadeInSw = true;
	FadeOutSw = false;
	FadeInEndVol = EndVol;
	FadeTime = 0.0f; // начинаем с нуля!
	FadeAge = Time;
	LastTime = vw_GetTime();
}



//------------------------------------------------------------------------------------
// плавное уход на 0 с текущего
//------------------------------------------------------------------------------------
void eMusic::FadeOut(float Time)
{
	FadeOutSw = true;
	FadeInSw = false;
	FadeInStartVol = Volume;
	FadeTime = 0.0f; // начинаем с нуля!
	FadeAge = Time;
	LastTime = vw_GetTime();
}