diff --git a/encoded.wav b/encoded.wav deleted file mode 100644 index 5daa681..0000000 Binary files a/encoded.wav and /dev/null differ diff --git a/examples/src/Main.cpp b/examples/src/Main.cpp index 72ad507..aea2ddf 100644 --- a/examples/src/Main.cpp +++ b/examples/src/Main.cpp @@ -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; diff --git a/include/libnyquist/Dither.h b/include/libnyquist/Dither.h index adc391d..519220a 100644 --- a/include/libnyquist/Dither.h +++ b/include/libnyquist/Dither.h @@ -37,25 +37,33 @@ enum DitherType DITHER_TRIANGLE }; -class TriDither +class Dither { std::uniform_real_distribution 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 - diff --git a/libnyquist.xcodeproj/xcshareddata/xcschemes/libnyquist.xcscheme b/libnyquist.xcodeproj/xcshareddata/xcschemes/libnyquist.xcscheme index 438e1db..9512d62 100644 --- a/libnyquist.xcodeproj/xcshareddata/xcschemes/libnyquist.xcscheme +++ b/libnyquist.xcodeproj/xcshareddata/xcschemes/libnyquist.xcscheme @@ -45,7 +45,7 @@ launchStyle = "0" useCustomWorkingDirectory = "YES" customWorkingDirectory = "$(PROJECT_DIR)" - buildConfiguration = "Debug" + buildConfiguration = "Release" ignoresPersistentStateOnLaunch = "NO" debugDocumentVersioning = "YES" allowLocationSimulation = "YES"> diff --git a/src/Common.cpp b/src/Common.cpp index 46db3bf..b087981 100644 --- a/src/Common.cpp +++ b/src/Common.cpp @@ -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(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(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(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(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]))); } }