Source

love-svg / src / modules / audio / openal / Source.cpp

Full commit
Bill Meltsner f7d5b43 
Bill Meltsner 150b94a 
ba...@bartbes.at… 2c1a94f 
Bill Meltsner f7d5b43 


ba...@bartbes.at… 2c1a94f 
Bill Meltsner f7d5b43 


ba...@bartbes.at… 2c1a94f 
Bill Meltsner f7d5b43 






rude d362ae6 



rude 1401d39 

rude d362ae6 









rude 1401d39 
Bill Meltsner f7d5b43 
Bart van Strien 344e99d 

rude d362ae6 
rude 1401d39 


rude 3c389ba 





rude d362ae6 

rude 1401d39 
ba...@bartbes.at… 2c1a94f 
Bart van Strien 344e99d 

rude d362ae6 
rude 1401d39 

rude 3c389ba 





rude d362ae6 



rude 701b231 
rude 1401d39 
Bart van Strien 19ea82f 

Bill Meltsner f7d5b43 






rude d362ae6 

rude 1401d39 
rude 3c389ba 


rude 1401d39 
rude d362ae6 


vrld c9aaf9c 

rude d362ae6 
Bill Meltsner f7d5b43 
rude d362ae6 

Bill Meltsner 2e9db8c 
rude d362ae6 



Bill Meltsner 2e9db8c 
rude d362ae6 



Bill Meltsner 2e9db8c 
rude d362ae6 
Bill Meltsner f7d5b43 
rude 94ad57c 
rude d362ae6 
rude 1401d39 
rude d362ae6 
Bill Meltsner f7d5b43 

rude d362ae6 


rude 1401d39 


Bart van Strien e8c7342 











rude 1401d39 


rude d362ae6 



rude 1401d39 




Bart van Strien fd83ded 
rude 1401d39 








Bill Meltsner ed28d72 










rude 1401d39 

Bill Meltsner ed28d72 








Bart van Strien 7897f06 

rude 1401d39 

rude d362ae6 
Bill Meltsner f7d5b43 
rude d362ae6 

rude 1401d39 
rude d362ae6 



Bill Meltsner f7d5b43 
rude d362ae6 

rude 1401d39 
rude d362ae6 








Bill Meltsner f7d5b43 
rude d362ae6 

rude 1401d39 
rude d362ae6 





Bill Meltsner f7d5b43 
rude d362ae6 

rude 1401d39 
rude d362ae6 








Bill Meltsner 07245c9 
Bart van Strien f4e4760 
Bill Meltsner 07245c9 


Bart van Strien f4e4760 
Bill Meltsner 07245c9 
Bill Meltsner fa4e82e 
Bart van Strien 0848b1b 
Bill Meltsner fa4e82e 




Bart van Strien 0848b1b 
Bill Meltsner fa4e82e 



Bill Meltsner 07245c9 


Bill Meltsner fa4e82e 
Bart van Strien 0848b1b 
Bill Meltsner fa4e82e 
Bart van Strien 0848b1b 




Bill Meltsner fa4e82e 


Bill Meltsner 07245c9 

Bart van Strien 0848b1b 







Bill Meltsner 07245c9 

Bart van Strien f4e4760 




Bill Meltsner 07245c9 
Bart van Strien f4e4760 
Bill Meltsner 07245c9 


Bill Meltsner ed28d72 
Bart van Strien f4e4760 
Bill Meltsner 07245c9 

Bill Meltsner ed28d72 
Bill Meltsner 07245c9 


Bill Meltsner ed28d72 






Bill Meltsner 07245c9 





rude d362ae6 
Bart van Strien f4e4760 




rude c02164e 

rude 1401d39 
rude c02164e 
rude 3c389ba 

Bill Meltsner f7d5b43 

rude c02164e 

rude 1401d39 
rude c02164e 
rude 3c389ba 

Bill Meltsner f7d5b43 

rude c02164e 

rude 1401d39 
rude c02164e 
rude 3c389ba 

Bill Meltsner f7d5b43 

rude c02164e 

rude 1401d39 
rude c02164e 
rude 3c389ba 

Bill Meltsner f7d5b43 

rude c02164e 

rude 1401d39 
rude c02164e 
rude 3c389ba 

Bill Meltsner f7d5b43 

rude c02164e 

rude 1401d39 
rude c02164e 
rude 3c389ba 

rude c02164e 

Bill Meltsner f7d5b43 










rude 1401d39 











Bill Meltsner f7d5b43 



Bart van Strien 7897f06 
Bill Meltsner f7d5b43 



rude 1401d39 








Bart van Strien bddc1d0 


rude 1401d39 

Bart van Strien 0848b1b 
rude 1401d39 













Bill Meltsner f7d5b43 



rude 1401d39 


rude 3c389ba 
rude 1401d39 
Bart van Strien 0848b1b 

rude 1401d39 

Bill Meltsner f7d5b43 
rude 1401d39 
























Bill Meltsner ed28d72 

rude 1401d39 




rude 3c389ba 











rude 1401d39 






















Bart van Strien fd83ded 
rude 1401d39 

Bart van Strien 7897f06 
Bart van Strien 344e99d 






rude 1401d39 
Bill Meltsner ed28d72 
rude 1401d39 
Bart van Strien 344e99d 








rude 1401d39 


ba...@bartbes.at… 2c1a94f 




rude d362ae6 

  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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/**
* Copyright (c) 2006-2011 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty.  In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
*    claim that you wrote the original software. If you use this software
*    in a product, an acknowledgment in the product documentation would be
*    appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
*    misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/

#include "Source.h"

#include "Pool.h"

// STD
#include <iostream>

namespace love
{
namespace audio
{
namespace openal
{

	Source::Source(Pool * pool, love::sound::SoundData * soundData)
		: love::audio::Source(Source::TYPE_STATIC), pool(pool), valid(false),
		pitch(1.0f), volume(1.0f), looping(false), offsetSamples(0), offsetSeconds(0),
		decoder(0), toLoop(0)
	{
		alGenBuffers(1, buffers);
		ALenum fmt = getFormat(soundData->getChannels(), soundData->getBits());
		alBufferData(buffers[0], fmt, soundData->getData(), soundData->getSize(), soundData->getSampleRate());

		static float z[3] = {0, 0, 0};

		setFloatv(position, z);
		setFloatv(velocity, z);
		setFloatv(direction, z);
	}

	Source::Source(Pool * pool, love::sound::Decoder * decoder)
		: love::audio::Source(Source::TYPE_STREAM), pool(pool), valid(false),
		pitch(1.0f), volume(1.0f), looping(false), offsetSamples(0), offsetSeconds(0),
		decoder(decoder), toLoop(0)
	{
		decoder->retain();
		alGenBuffers(MAX_BUFFERS, buffers);

		static float z[3] = {0, 0, 0};

		setFloatv(position, z);
		setFloatv(velocity, z);
		setFloatv(direction, z);
	}

	Source::~Source()
	{
		stop();
		alDeleteBuffers((type == TYPE_STATIC) ? 1 : MAX_BUFFERS, buffers);
		if (decoder)
			decoder->release();
	}

	love::audio::Source * Source::copy()
	{
		return 0;
	}

	void Source::play()
	{
		valid = pool->play(this, source);

		if(valid)
			reset(source);
	}

	void Source::stop()
	{
		if (!isStopped())
			pool->stop(this);
	}

	void Source::pause()
	{
		pool->pause(this);
	}

	void Source::resume()
	{
		pool->resume(this);
	}

	void Source::rewind()
	{
		pool->rewind(this);
	}

	bool Source::isStopped() const
	{
		if(valid)
		{
			ALenum state;
			alGetSourcei(source, AL_SOURCE_STATE, &state);
			return (state == AL_STOPPED);
		}

		return true;
	}

	bool Source::isPaused() const
	{
		if(valid)
		{
			ALenum state;
			alGetSourcei(source, AL_SOURCE_STATE, &state);
			return (state == AL_PAUSED);
		}

		return false;
	}

	bool Source::isFinished() const
	{
		return type == TYPE_STATIC ? isStopped() : isStopped() && !isLooping() && decoder->isFinished();
	}

	void Source::update()
	{
		if(valid && type == TYPE_STATIC)
		{
			// Looping mode could have changed.
			alSourcei(source, AL_LOOPING, isLooping() ? AL_TRUE : AL_FALSE);
		}
		else if(valid && type == TYPE_STREAM && !(!isLooping() && isFinished()))
		{
			// Number of processed buffers.
			ALint processed;

			alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed);

			while(processed--)
			{
				ALuint buffer;
				
				float curOffsetSamples, curOffsetSecs;
				
				alGetSourcef(source, AL_SAMPLE_OFFSET, &curOffsetSamples);
				
				ALint b;
				alGetSourcei(source, AL_BUFFER, &b);
				int freq;
				alGetBufferi(b, AL_FREQUENCY, &freq);
				curOffsetSecs = curOffsetSamples / freq;
				
				// Get a free buffer.
				alSourceUnqueueBuffers(source, 1, &buffer);
				
				float newOffsetSamples, newOffsetSecs;
				
				alGetSourcef(source, AL_SAMPLE_OFFSET, &newOffsetSamples);
				newOffsetSecs = newOffsetSamples / freq;
				
				offsetSamples += (curOffsetSamples - newOffsetSamples);
				offsetSeconds += (curOffsetSecs - newOffsetSecs);
				
				streamAtomic(buffer, decoder);
				alSourceQueueBuffers(source, 1, &buffer);
			}
		}
	}

	void Source::setPitch(float pitch)
	{
		if(valid)
			alSourcef(source, AL_PITCH, pitch);

		this->pitch = pitch;
	}

	float Source::getPitch() const
	{
		if(valid)
		{
			ALfloat f;
			alGetSourcef(source, AL_PITCH, &f);
			return f;
		}

		// In case the Source isn't playing.
		return pitch;
	}

	void Source::setVolume(float volume)
	{
		if(valid)
		{
			alSourcef(source, AL_GAIN, volume);
		}

		this->volume = volume;
	}

	float Source::getVolume() const
	{
		if(valid)
		{
			ALfloat f;
			alGetSourcef(source, AL_GAIN, &f);
			return f;
		}

		// In case the Source isn't playing.
		return volume;
	}
	
	void Source::seekAtomic(float offset, void * unit)
	{
		if (valid)
		{
			switch (*((Source::Unit*) unit)) {
				case Source::UNIT_SAMPLES:
					if (type == TYPE_STREAM) {
						offsetSamples = offset;
						ALint buffer;
						alGetSourcei(source, AL_BUFFER, &buffer);
						int freq;
						alGetBufferi(buffer, AL_FREQUENCY, &freq);
						offset /= freq;
						offsetSeconds = offset;
						decoder->seek(offset);
					} else {
						alSourcef(source, AL_SAMPLE_OFFSET, offset);
					}
					break;
				case Source::UNIT_SECONDS:	
				default:
					if (type == TYPE_STREAM) {
						offsetSeconds = offset;
						decoder->seek(offset);
						ALint buffer;
						alGetSourcei(source, AL_BUFFER, &buffer);
						int freq;
						alGetBufferi(buffer, AL_FREQUENCY, &freq);
						offsetSamples = offset*freq;
					} else {
						alSourcef(source, AL_SEC_OFFSET, offset);
					}
					break;
			}
			if (type == TYPE_STREAM)
			{
				// Because we still have old data
				// from before the seek in the buffers
				// let's empty them.
				stopAtomic(false);
				playAtomic();
			}
		}
	}

	void Source::seek(float offset, Source::Unit unit)
	{
		return pool->seek(this, offset, &unit);
	}
	
	float Source::tellAtomic(void * unit) const
	{
		if (valid)
		{
			float offset;
			switch (*((Source::Unit*) unit)) {
				case Source::UNIT_SAMPLES:
					alGetSourcef(source, AL_SAMPLE_OFFSET, &offset);
					if (type == TYPE_STREAM) offset += offsetSamples;
					break;
				case Source::UNIT_SECONDS:
				default:
					alGetSourcef(source, AL_SAMPLE_OFFSET, &offset);
					ALint buffer;
					alGetSourcei(source, AL_BUFFER, &buffer);
					int freq;
					alGetBufferi(buffer, AL_FREQUENCY, &freq);
					offset /= freq;
					if (type == TYPE_STREAM) offset += offsetSeconds;
					break;
			}
			return offset;
		}
		return 0.0f;
	}

	float Source::tell(Source::Unit unit)
	{
		return pool->tell(this, &unit);
	}

	void Source::setPosition(float * v)
	{
		if(valid)
			alSourcefv(source, AL_POSITION, v);

		setFloatv(position, v);
	}

	void Source::getPosition(float * v) const
	{
		if(valid)
			alGetSourcefv(source, AL_POSITION, v);
		else
			setFloatv(v, position);
	}

	void Source::setVelocity(float * v)
	{
		if(valid)
			alSourcefv(source, AL_VELOCITY, v);

		setFloatv(velocity, v);
	}

	void Source::getVelocity(float * v) const
	{
		if(valid)
			alGetSourcefv(source, AL_VELOCITY, v);
		else
			setFloatv(v, velocity);
	}

	void Source::setDirection(float * v)
	{
		if(valid)
			alSourcefv(source, AL_DIRECTION, v);
		else
			setFloatv(direction, v);
	}

	void Source::getDirection(float * v) const
	{
		if(valid)
			alGetSourcefv(source, AL_DIRECTION, v);
		else
			setFloatv(v, direction);
	}

	void Source::setLooping(bool looping)
	{
		if(valid && type == TYPE_STATIC)
			alSourcei(source, AL_LOOPING, looping ? AL_TRUE : AL_FALSE);

		this->looping = looping;
	}

	bool Source::isLooping() const
	{
		return looping;
	}

	void Source::playAtomic()
	{
		if(type == TYPE_STATIC)
		{
			alSourcei(source, AL_BUFFER, buffers[0]);
		}
		else if(type == TYPE_STREAM)
		{
			int usedBuffers = 0;

			for(unsigned int i = 0; i < MAX_BUFFERS; i++)
			{
				int decoded = streamAtomic(buffers[i], decoder);
				++usedBuffers;
				if(decoder->isFinished())
					break;
			}

			if(usedBuffers > 0)
				alSourceQueueBuffers(source, usedBuffers, buffers);
		}

		// Set these properties. These may have changed while we've
		// been without an AL source.
		alSourcef(source, AL_PITCH, pitch);
		alSourcef(source, AL_GAIN, volume);

		alSourcePlay(source);

		valid = true; //if it fails it will be set to false again
		//but this prevents a horrible, horrible bug
	}

	void Source::stopAtomic(bool rewind)
	{
		if(valid)
		{
			if(type == TYPE_STATIC)
			{
				alSourceStop(source);
			}
			else if(type == TYPE_STREAM)
			{
				alSourceStop(source);

				int queued = 0;
				alGetSourcei(source, AL_BUFFERS_QUEUED, &queued);

				while(queued--)
				{
					ALuint buffer;
					alSourceUnqueueBuffers(source, 1, &buffer);
				}
			}

			alSourcei(source, AL_BUFFER, AL_NONE);
		}
		if (rewind)
			rewindAtomic();
		valid = false;
	}

	void Source::pauseAtomic()
	{
		if(valid)
		{
			alSourcePause(source);
		}
	}

	void Source::resumeAtomic()
	{
		if(valid)
		{
			alSourcePlay(source);
		}
	}

	void Source::rewindAtomic()
	{
		if(valid && type == TYPE_STATIC)
		{
			alSourceRewind(source);
		}
		else if(valid && type == TYPE_STREAM)
		{
			decoder->rewind();
			offsetSamples = 0;
			offsetSeconds = 0;
		}
	}

	void Source::reset(ALenum source)
	{
		alSourcefv(source, AL_POSITION, position);
		alSourcefv(source, AL_VELOCITY, velocity);
		alSourcefv(source, AL_DIRECTION, direction);
		alSourcef(source, AL_PITCH, pitch);
		alSourcef(source, AL_GAIN, volume);
	}

	void Source::setFloatv(float * dst, const float * src) const
	{
		dst[0] = src[0];
		dst[1] = src[1];
		dst[2] = src[2];
	}

	ALenum Source::getFormat(int channels, int bits) const
	{
		if(channels == 1 && bits == 8)
			return AL_FORMAT_MONO8;
		else if(channels == 1 && bits == 16)
			return AL_FORMAT_MONO16;
		else if(channels == 2 && bits == 8)
			return AL_FORMAT_STEREO8;
		else if(channels == 2 && bits == 16)
			return AL_FORMAT_STEREO16;
		else
			return 0;
	}

	int Source::streamAtomic(ALuint buffer, love::sound::Decoder * d)
	{
		// Get more sound data.
		int decoded = d->decode();

		int fmt = getFormat(d->getChannels(), d->getBits());

		if(fmt != 0)
			alBufferData(buffer, fmt, d->getBuffer(), decoded, d->getSampleRate());

		if(decoder->isFinished() && isLooping()) {
			int queued, processed;
			alGetSourcei(source, AL_BUFFERS_QUEUED, &queued);
			alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed);
			if (queued > processed)
				toLoop = queued-processed;
			else
				toLoop = MAX_BUFFERS-processed;
			d->rewind();
		}

		if (toLoop > 0)
		{
			if (--toLoop == 0)
			{
				offsetSamples = 0;
				offsetSeconds = 0;
			}
		}

		return decoded;
	}

	bool Source::isStatic() const
	{
		return (type == TYPE_STATIC);
	}

} // openal
} // audio
} // love