From 2893e8f2b0e54ba67c21d1a232f375e2ec2f1918 Mon Sep 17 00:00:00 2001 From: Dimitri Diakopoulos Date: Sun, 17 May 2015 18:14:44 -0700 Subject: [PATCH] bugfixes and prepare for dithering impl --- examples/src/Main.cpp | 2 +- include/libnyquist/Dither.h | 1 + src/RiffUtils.cpp | 12 ++++++------ src/WavEncoder.cpp | 26 +++++++++++++++++--------- 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/examples/src/Main.cpp b/examples/src/Main.cpp index 8de96ae..ec3ea53 100644 --- a/examples/src/Main.cpp +++ b/examples/src/Main.cpp @@ -98,7 +98,7 @@ int main() } // Test wav file encoder - encoder.WriteFile({2, 44100, 32, PCM_FLT}, fileData->samples, "encoded.wav"); + encoder.WriteFile({2, 44100, 32, PCM_FLT, DITHER_NONE}, fileData, "encoded.wav"); return 0; } \ No newline at end of file diff --git a/include/libnyquist/Dither.h b/include/libnyquist/Dither.h index 5a9864b..f3ff662 100644 --- a/include/libnyquist/Dither.h +++ b/include/libnyquist/Dither.h @@ -35,6 +35,7 @@ namespace nqr enum DitherType { + DITHER_NONE, DITHER_ONE, DITHER_TWO }; diff --git a/src/RiffUtils.cpp b/src/RiffUtils.cpp index 36039db..7c7fc52 100644 --- a/src/RiffUtils.cpp +++ b/src/RiffUtils.cpp @@ -55,12 +55,12 @@ WaveChunkHeader nqr::MakeWaveHeader(const EncoderParams param) header.fmt_id = GenerateChunkCode('f', 'm', 't', ' '); header.chunk_size = 16; - header.format = (param.fmt <= PCMFormat::PCM_32) ? WaveFormatCode::FORMAT_PCM : WaveFormatCode::FORMAT_IEEE; - header.channel_count = param.channels; - header.sample_rate = param.samplerate; - header.data_rate = param.samplerate * param.channels * (param.bit_depth / 8); - header.frame_size = param.channels * (param.bit_depth / 8); - header.bit_depth = param.bit_depth; + header.format = (param.targetFormat <= PCMFormat::PCM_32) ? WaveFormatCode::FORMAT_PCM : WaveFormatCode::FORMAT_IEEE; + header.channel_count = param.channelCount; + header.sample_rate = param.sampleRate; + header.data_rate = param.sampleRate * param.channelCount * (param.bitDepth / 8); + header.frame_size = param.channelCount * (param.bitDepth / 8); + header.bit_depth = param.bitDepth; return header; } \ No newline at end of file diff --git a/src/WavEncoder.cpp b/src/WavEncoder.cpp index 2784912..c2417ea 100644 --- a/src/WavEncoder.cpp +++ b/src/WavEncoder.cpp @@ -48,7 +48,6 @@ WavEncoder::~WavEncoder() int WavEncoder::WriteFile(const EncoderParams p, const AudioData * d, const std::string & path) { - if (d->samples.size() <= 32) { return EncoderError::InsufficientSampleData; @@ -86,28 +85,37 @@ int WavEncoder::WriteFile(const EncoderParams p, const AudioData * d, const std: // Initial size toBytes(36, chunkSizeBuff); - // RIFF File Header + // RIFF file header fout.write(GenerateChunkCodeChar('R', 'I', 'F', 'F'), 4); fout.write(chunkSizeBuff, 4); fout.write(GenerateChunkCodeChar('W', 'A', 'V', 'E'), 4); - // Fmt Header + // Fmt header auto header = MakeWaveHeader(p); fout.write(reinterpret_cast(&header), sizeof(WaveChunkHeader)); - // Data Header + // Data header fout.write(GenerateChunkCodeChar('d', 'a', 't', 'a'), 4); // + data chunk size auto numSamplesBytes = d->samples.size() * sizeof(float); - toBytes(numSamplesBytes, chunkSizeBuff); + toBytes(samplesSizeInBytes, chunkSizeBuff); fout.write(chunkSizeBuff, 4); // Debugging -- assume IEEE_Float - - auto what = GetFormatBitsPerSample(d->sourceFmt); - fout.write(reinterpret_cast(d->samples.data()), numSamplesBytes); + auto sourceBits = GetFormatBitsPerSample(d->sourceFormat); + auto targetBits = GetFormatBitsPerSample(p.targetFormat); + + // Apply dithering + if (sourceBits != targetBits && p.dither != DITHER_NONE) + { + + } + else + { + fout.write(reinterpret_cast(d->samples.data()), samplesSizeInBytes); + } // Find size long totalSize = fout.tellp(); @@ -115,7 +123,7 @@ int WavEncoder::WriteFile(const EncoderParams p, const AudioData * d, const std: // Modify RIFF header fout.seekp(4); - // Total size of the file, less 8 for the RIFF header + // Total size of the file, less 8 bytes for the RIFF header toBytes(totalSize - 8 , chunkSizeBuff); fout.write(chunkSizeBuff, 4);