Jelajahi Sumber

Ability to read and set sample offset (playback position) within a Sound, as well as fetch sample length

mcc 13 tahun lalu
induk
melakukan
b7e414ee21
2 mengubah file dengan 40 tambahan dan 3 penghapusan
  1. 19 0
      Core/Contents/Include/PolySound.h
  2. 21 3
      Core/Contents/Source/PolySound.cpp

+ 19 - 0
Core/Contents/Include/PolySound.h

@@ -90,6 +90,24 @@ namespace Polycode {
 		void setSoundVelocity(Vector3 velocity);
 		void setSoundDirection(Vector3 direction);
 		
+		/**
+		* Sets the current sample offset of this sound.
+		* @param off A number 0 <= off < sound sample length
+		*/
+		void setOffset(int off);
+		
+		/**
+		* Returns the current sample offset (playback progress) of this sound.
+		* @return The sample offset if it is known, -1 otherwise.
+		*/
+		int getOffset();
+		
+		/**
+		* Returns the number of samples in the sound.
+		* @return The sample length if it is known, -1 otherwise.
+		*/
+		int getSampleLength();
+		
 		void setPositionalProperties(Number referenceDistance, Number maxDistance);
 		
 		ALuint loadBytes(const char *data, int size, int channels = 1, ALsizei freq = 44100, int bps = 16);
@@ -109,6 +127,7 @@ namespace Polycode {
 	
 		bool isPositional;
 		ALuint soundSource;
+		int sampleLength;
 		
 	};
 }

+ 21 - 3
Core/Contents/Source/PolySound.cpp

@@ -52,7 +52,7 @@ long custom_tellfunc(void *datasource) {
 	return OSBasics::tell(file);
 }
 
-Sound::Sound(const String& fileName) {
+Sound::Sound(const String& fileName) : sampleLength(-1) {
 	String extension;
 	size_t found;
 	found=fileName.rfind(".");
@@ -73,7 +73,7 @@ Sound::Sound(const String& fileName) {
 	setIsPositional(false);
 }
 
-Sound::Sound(const char *data, int size, int channels, int freq, int bps) {
+Sound::Sound(const char *data, int size, int channels, int freq, int bps) : sampleLength(-1) {
 	ALuint buffer = loadBytes(data, size, freq, channels, bps);
 	
 	soundSource = GenSource(buffer);
@@ -149,6 +149,20 @@ void Sound::setSoundDirection(Vector3 direction) {
 		alSource3f(soundSource,AL_DIRECTION, direction.x, direction.y, direction.z);
 }
 
+void Sound::setOffset(int off) {
+	alSourcei(soundSource, AL_SAMPLE_OFFSET, off);
+}
+
+int Sound::getOffset() {
+	ALint off = -1;
+	alGetSourcei(soundSource, AL_SAMPLE_OFFSET, &off);
+	return off;
+}
+
+int Sound::getSampleLength() {
+	return sampleLength;
+}
+
 void Sound::setPositionalProperties(Number referenceDistance, Number maxDistance) { 
 	alSourcef(soundSource,AL_REFERENCE_DISTANCE, referenceDistance);
 	alSourcef(soundSource,AL_MAX_DISTANCE, maxDistance);	
@@ -237,6 +251,8 @@ ALuint Sound::loadBytes(const char *data, int size, int freq, int channels, int
 	else
 		format = (bps == 8) ? AL_FORMAT_STEREO8 : AL_FORMAT_STEREO16;
 	
+	sampleLength = bps > 8 ? size / (bps/8) : -1;
+	
 	alGenBuffers(1, &buffer);
 	soundCheck(alGetError() == AL_NO_ERROR, "LoadBytes: Could not generate buffer");
 	soundCheck(AL_NONE != buffer, "LoadBytes: Could not generate buffer");
@@ -294,7 +310,9 @@ ALuint Sound::loadOGG(const String& fileName) {
 		// Append to end of buffer
 		buffer.insert(buffer.end(), array, array + bytes);
 	} while (bytes > 0);
-	ov_clear(&oggFile);	
+	ov_clear(&oggFile);
+	
+	sampleLength = buffer.size() / sizeof(uint16_t);
 	
 	alBufferData(bufferID, format, &buffer[0], static_cast<ALsizei>(buffer.size()), freq);