Browse Source

Source:tell actually works now

--HG--
branch : minor
Bill Meltsner 14 years ago
parent
commit
2faf8aa442
2 changed files with 42 additions and 7 deletions
  1. 39 7
      src/modules/audio/openal/Source.cpp
  2. 3 0
      src/modules/audio/openal/Source.h

+ 39 - 7
src/modules/audio/openal/Source.cpp

@@ -34,7 +34,7 @@ namespace openal
 
 
 	Source::Source(Pool * pool, love::sound::SoundData * soundData)
 	Source::Source(Pool * pool, love::sound::SoundData * soundData)
 		: love::audio::Source(Source::TYPE_STATIC), pool(pool), valid(false),
 		: love::audio::Source(Source::TYPE_STATIC), pool(pool), valid(false),
-		pitch(1.0f), volume(1.0f), looping(false), decoder(0)
+		pitch(1.0f), volume(1.0f), looping(false), offsetSamples(0), offsetSeconds(0), decoder(0)
 	{
 	{
 		alGenBuffers(1, buffers);
 		alGenBuffers(1, buffers);
 		ALenum fmt = getFormat(soundData->getChannels(), soundData->getBits());
 		ALenum fmt = getFormat(soundData->getChannels(), soundData->getBits());
@@ -49,7 +49,7 @@ namespace openal
 
 
 	Source::Source(Pool * pool, love::sound::Decoder * decoder)
 	Source::Source(Pool * pool, love::sound::Decoder * decoder)
 		: love::audio::Source(Source::TYPE_STREAM), pool(pool), valid(false),
 		: love::audio::Source(Source::TYPE_STREAM), pool(pool), valid(false),
-		pitch(1.0f), volume(1.0f), looping(false), decoder(decoder)
+		pitch(1.0f), volume(1.0f), looping(false), offsetSamples(0), offsetSeconds(0), decoder(decoder)
 	{
 	{
 		decoder->retain();
 		decoder->retain();
 		alGenBuffers(MAX_BUFFERS, buffers);
 		alGenBuffers(MAX_BUFFERS, buffers);
@@ -147,10 +147,28 @@ namespace openal
 			while(processed--)
 			while(processed--)
 			{
 			{
 				ALuint buffer;
 				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.
 				// Get a free buffer.
 				alSourceUnqueueBuffers(source, 1, &buffer);
 				alSourceUnqueueBuffers(source, 1, &buffer);
-
+				
+				float newOffsetSamples, newOffsetSecs;
+				
+				alGetSourcef(source, AL_SAMPLE_OFFSET, &newOffsetSamples);
+				newOffsetSecs = newOffsetSamples / freq;
+				
+				offsetSamples += (curOffsetSamples - newOffsetSamples);
+				offsetSeconds += (curOffsetSecs - newOffsetSecs);
+				
 				if(streamAtomic(buffer, decoder) > 0)
 				if(streamAtomic(buffer, decoder) > 0)
 					alSourceQueueBuffers(source, 1, &buffer);
 					alSourceQueueBuffers(source, 1, &buffer);
 			}
 			}
@@ -207,10 +225,12 @@ namespace openal
 		{
 		{
 			switch (unit) {
 			switch (unit) {
 				case Source::UNIT_SAMPLES:
 				case Source::UNIT_SAMPLES:
+					if (type == TYPE_STREAM) offset -= offsetSamples;
 					alSourcef(source, AL_SAMPLE_OFFSET, offset);
 					alSourcef(source, AL_SAMPLE_OFFSET, offset);
 					break;
 					break;
 				case Source::UNIT_SECONDS:	
 				case Source::UNIT_SECONDS:	
 				default:
 				default:
+					if (type == TYPE_STREAM) offset -= offsetSeconds;
 					alSourcef(source, AL_SEC_OFFSET, offset);
 					alSourcef(source, AL_SEC_OFFSET, offset);
 					break;
 					break;
 			}
 			}
@@ -221,14 +241,21 @@ namespace openal
 	{
 	{
 		if (valid)
 		if (valid)
 		{
 		{
-			ALfloat offset;
+			float offset;
 			switch (unit) {
 			switch (unit) {
 				case Source::UNIT_SAMPLES:
 				case Source::UNIT_SAMPLES:
 					alGetSourcef(source, AL_SAMPLE_OFFSET, &offset);
 					alGetSourcef(source, AL_SAMPLE_OFFSET, &offset);
+					if (type == TYPE_STREAM) offset += offsetSamples;
 					break;
 					break;
 				case Source::UNIT_SECONDS:
 				case Source::UNIT_SECONDS:
 				default:
 				default:
-					alGetSourcef(source, AL_SEC_OFFSET, &offset);
+					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;
 					break;
 			}
 			}
 			return offset;
 			return offset;
@@ -383,6 +410,8 @@ namespace openal
 		else if(valid && type == TYPE_STREAM)
 		else if(valid && type == TYPE_STREAM)
 		{
 		{
 			decoder->rewind();
 			decoder->rewind();
+			offsetSamples = 0;
+			offsetSeconds = 0;
 		}
 		}
 	}
 	}
 
 
@@ -426,8 +455,11 @@ namespace openal
 		if(decoded > 0 && fmt != 0)
 		if(decoded > 0 && fmt != 0)
 			alBufferData(buffer, fmt, d->getBuffer(), decoded, d->getSampleRate());
 			alBufferData(buffer, fmt, d->getBuffer(), decoded, d->getSampleRate());
 
 
-		if(decoded < d->getSize() && isLooping())
+		if(decoded < d->getSize() && isLooping()) {
+			offsetSamples = 0;
+			offsetSeconds = 0;
 			d->rewind();
 			d->rewind();
+		}
 
 
 		return decoded;
 		return decoded;
 	}
 	}

+ 3 - 0
src/modules/audio/openal/Source.h

@@ -62,6 +62,9 @@ namespace openal
 		float velocity[3];
 		float velocity[3];
 		float direction[3];
 		float direction[3];
 		bool looping;
 		bool looping;
+		
+		float offsetSamples;
+		float offsetSeconds;
 
 
 		love::sound::Decoder * decoder;
 		love::sound::Decoder * decoder;