mirror of
https://github.com/love2d/love.git
synced 2026-08-17 11:00:31 +02:00
Fixed 8-bit SoundData samples when used in a love.audio Source
This commit is contained in:
@@ -52,7 +52,7 @@ SoundData::SoundData(Decoder *decoder)
|
|||||||
{
|
{
|
||||||
while (bufferSize < (size_t) size + decoded)
|
while (bufferSize < (size_t) size + decoded)
|
||||||
bufferSize <<= 1;
|
bufferSize <<= 1;
|
||||||
data = (int8 *) realloc(data, bufferSize);
|
data = (uint8 *) realloc(data, bufferSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!data)
|
if (!data)
|
||||||
@@ -76,7 +76,7 @@ SoundData::SoundData(Decoder *decoder)
|
|||||||
|
|
||||||
// Shrink buffer if necessary.
|
// Shrink buffer if necessary.
|
||||||
if (data && bufferSize > (size_t) size)
|
if (data && bufferSize > (size_t) size)
|
||||||
data = (int8 *) realloc(data, size);
|
data = (uint8 *) realloc(data, size);
|
||||||
|
|
||||||
channels = decoder->getChannels();
|
channels = decoder->getChannels();
|
||||||
bitDepth = decoder->getBitDepth();
|
bitDepth = decoder->getBitDepth();
|
||||||
@@ -139,14 +139,14 @@ void SoundData::load(int samples, int sampleRate, int bitDepth, int channels, vo
|
|||||||
if (realsize > INT_MAX)
|
if (realsize > INT_MAX)
|
||||||
throw love::Exception("Data is too big!");
|
throw love::Exception("Data is too big!");
|
||||||
|
|
||||||
data = (int8 *) malloc(size);
|
data = (uint8 *) malloc(size);
|
||||||
if (!data)
|
if (!data)
|
||||||
throw love::Exception("Not enough memory.");
|
throw love::Exception("Not enough memory.");
|
||||||
|
|
||||||
if (newData)
|
if (newData)
|
||||||
memcpy(data, newData, size);
|
memcpy(data, newData, size);
|
||||||
else
|
else
|
||||||
memset(data, 0, size);
|
memset(data, bitDepth == 8 ? 128 : 0, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void *SoundData::getData() const
|
void *SoundData::getData() const
|
||||||
@@ -192,12 +192,14 @@ void SoundData::setSample(int i, float sample)
|
|||||||
|
|
||||||
if (bitDepth == 16)
|
if (bitDepth == 16)
|
||||||
{
|
{
|
||||||
|
// 16-bit sample values are signed.
|
||||||
int16 *s = (int16 *) data;
|
int16 *s = (int16 *) data;
|
||||||
s[i] = (int16) (sample * (float) LOVE_INT16_MAX);
|
s[i] = (int16) (sample * (float) LOVE_INT16_MAX);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
data[i] = (int8) (sample * (float) LOVE_INT8_MAX);
|
// 8-bit sample values are unsigned internally.
|
||||||
|
data[i] = (uint8) ((sample * 127.0f) + 128.0f);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -209,12 +211,14 @@ float SoundData::getSample(int i) const
|
|||||||
|
|
||||||
if (bitDepth == 16)
|
if (bitDepth == 16)
|
||||||
{
|
{
|
||||||
|
// 16-bit sample values are signed.
|
||||||
int16 *s = (int16 *) data;
|
int16 *s = (int16 *) data;
|
||||||
return (float) s[i] / (float) LOVE_INT16_MAX;
|
return (float) s[i] / (float) LOVE_INT16_MAX;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return (float) data[i] / (float) LOVE_INT8_MAX;
|
// 8-bit sample values are unsigned internally.
|
||||||
|
return ((float) data[i] - 128.0f) / 127.0f;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ private:
|
|||||||
|
|
||||||
void load(int samples, int sampleRate, int bitDepth, int channels, void *newData = 0);
|
void load(int samples, int sampleRate, int bitDepth, int channels, void *newData = 0);
|
||||||
|
|
||||||
int8 *data;
|
uint8 *data;
|
||||||
int size;
|
int size;
|
||||||
|
|
||||||
int sampleRate;
|
int sampleRate;
|
||||||
|
|||||||
Reference in New Issue
Block a user