Commits

Alex Szpakowski committed f5b196c

Fixed 8-bit SoundData samples when used in a love.audio Source

Comments (0)

Files changed (2)

src/modules/sound/SoundData.cpp

 		{
 			while (bufferSize < (size_t) size + decoded)
 				bufferSize <<= 1;
-			data = (int8 *) realloc(data, bufferSize);
+			data = (uint8 *) realloc(data, bufferSize);
 		}
 
 		if (!data)
 
 	// Shrink buffer if necessary.
 	if (data && bufferSize > (size_t) size)
-		data = (int8 *) realloc(data, size);
+		data = (uint8 *) realloc(data, size);
 
 	channels = decoder->getChannels();
 	bitDepth = decoder->getBitDepth();
 	if (realsize > INT_MAX)
 		throw love::Exception("Data is too big!");
 
-	data = (int8 *) malloc(size);
+	data = (uint8 *) malloc(size);
 	if (!data)
 		throw love::Exception("Not enough memory.");
 
 	if (newData)
 		memcpy(data, newData, size);
 	else
-		memset(data, 0, size);
+		memset(data, bitDepth == 8 ? 128 : 0, size);
 }
 
 void *SoundData::getData() const
 
 	if (bitDepth == 16)
 	{
+		// 16-bit sample values are signed.
 		int16 *s = (int16 *) data;
 		s[i] = (int16) (sample * (float) LOVE_INT16_MAX);
 	}
 	else
 	{
-		data[i] = (int8) (sample * (float) LOVE_INT8_MAX);
+		// 8-bit sample values are unsigned internally.
+		data[i] = (uint8) ((sample * 127.0f) + 128.0f);
 	}
 }
 
 
 	if (bitDepth == 16)
 	{
+		// 16-bit sample values are signed.
 		int16 *s = (int16 *) data;
 		return (float) s[i] / (float) LOVE_INT16_MAX;
 	}
 	else
 	{
-		return (float) data[i] / (float) LOVE_INT8_MAX;
+		// 8-bit sample values are unsigned internally.
+		return ((float) data[i] - 128.0f) / 127.0f;
 	}
 }
 

src/modules/sound/SoundData.h

 
 	void load(int samples, int sampleRate, int bitDepth, int channels, void *newData = 0);
 
-	int8 *data;
+	uint8 *data;
 	int size;
 
 	int sampleRate;