change dithering calls to something a bit easier to read

adpcm
Dimitri Diakopoulos 11 years ago
parent ae86aed48f
commit e2e2bab052

Binary file not shown.

@ -92,7 +92,7 @@ int main()
}
// Test wav file encoder
int encoderStatus = encoder.WriteFile({1, PCM_16, DITHER_NONE}, fileData, "encoded.wav");
int encoderStatus = encoder.WriteFile({2, PCM_16, DITHER_NONE}, fileData, "encoded.wav");
std::cout << "Encoder Status: " << encoderStatus << std::endl;
return 0;

@ -37,25 +37,33 @@ enum DitherType
DITHER_TRIANGLE
};
class TriDither
class Dither
{
std::uniform_real_distribution<float> distribution;
std::mt19937 rndGen;
float prev = 0.0f;
DitherType d;
public:
TriDither() : distribution(-0.5f, +0.5f) {}
Dither(DitherType d) : distribution(-0.5f, +0.5f), d(d) {}
float operator()(float s)
{
const float value = distribution(rndGen);
s = s + value - prev;
prev = value;
return s;
if (d == DITHER_TRIANGLE)
{
const float value = distribution(rndGen);
s = s + value - prev;
prev = value;
return s;
}
else
{
return s;
}
}
};
} // end namespace nqr
#endif

@ -45,7 +45,7 @@
launchStyle = "0"
useCustomWorkingDirectory = "YES"
customWorkingDirectory = "$(PROJECT_DIR)"
buildConfiguration = "Debug"
buildConfiguration = "Release"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
allowLocationSimulation = "YES">

@ -148,29 +148,28 @@ void nqr::ConvertToFloat32(float * dst, const int32_t * src, const size_t N, PCM
void nqr::ConvertFromFloat32(uint8_t * dst, const float * src, const size_t N, PCMFormat f, DitherType t)
{
//@todo implement dither
assert(f != PCM_END);
TriDither dither;
Dither dither(t);
if (f == PCM_U8)
{
uint8_t * destPtr = reinterpret_cast<uint8_t *>(dst);
for (size_t i = 0; i < N; ++i)
destPtr[i] = (t == DITHER_TRIANGLE) ? (uint8_t) lroundf(dither(float32_to_uint8(src[i]))) : (uint8_t) float32_to_uint8(src[i]);
destPtr[i] = (uint8_t) dither(lroundf(float32_to_uint8(src[i])));
}
else if (f == PCM_S8)
{
int8_t * destPtr = reinterpret_cast<int8_t *>(dst);
for (size_t i = 0; i < N; ++i)
destPtr[i] = (t == DITHER_TRIANGLE) ? (int8_t) lroundf(dither(float32_to_int8(src[i]))) : (int8_t) float32_to_int8(src[i]);
destPtr[i] = (int8_t) dither(lroundf(float32_to_int8(src[i])));
}
else if (f == PCM_16)
{
int16_t * destPtr = reinterpret_cast<int16_t *>(dst);
for (size_t i = 0; i < N; ++i)
destPtr[i] = (t == DITHER_TRIANGLE) ? (int16_t) lroundf(dither(float32_to_int16(src[i]))) : (int16_t) float32_to_int16(src[i]);
destPtr[i] =(int16_t) dither(lroundf(float32_to_int16(src[i])));
}
else if (f == PCM_24)
{
@ -178,7 +177,7 @@ void nqr::ConvertFromFloat32(uint8_t * dst, const float * src, const size_t N, P
size_t c = 0;
for (size_t i = 0; i < N; ++i)
{
int32_t sample = (t == DITHER_TRIANGLE) ? (int32_t) lroundf(dither(float32_to_int24(src[i]))) : (int32_t) float32_to_int24(src[i]);
int32_t sample = (int32_t) dither(lroundf(float32_to_int24(src[i])));
auto unpacked = Unpack(sample); // Handles endian swap
destPtr[c] = unpacked[0];
destPtr[c+1] = unpacked[1];
@ -190,7 +189,7 @@ void nqr::ConvertFromFloat32(uint8_t * dst, const float * src, const size_t N, P
{
int32_t * destPtr = reinterpret_cast<int32_t *>(dst);
for (size_t i = 0; i < N; ++i)
destPtr[i] = (t == DITHER_TRIANGLE) ? (int32_t) lroundf(dither(float32_to_int32(src[i]))) : (int32_t) float32_to_int32(src[i]);
destPtr[i] = (int32_t) dither(lroundf(float32_to_int32(src[i])));
}
}

Loading…
Cancel
Save