From f473e87d87df636521139699e55d740d6638c1a5 Mon Sep 17 00:00:00 2001 From: Dimitri Diakopoulos Date: Sun, 17 May 2015 17:43:38 -0700 Subject: [PATCH] more sanity checks and helper functions --- include/libnyquist/Common.h | 2 ++ include/libnyquist/Dither.h | 6 ++++++ include/libnyquist/RiffUtils.h | 10 ++++++---- src/Common.cpp | 22 ++++++++++++++++++++++ src/WavEncoder.cpp | 18 ++++++++++++++---- 5 files changed, 50 insertions(+), 8 deletions(-) diff --git a/include/libnyquist/Common.h b/include/libnyquist/Common.h index 4ca5c71..7e9101d 100644 --- a/include/libnyquist/Common.h +++ b/include/libnyquist/Common.h @@ -245,6 +245,8 @@ struct NyquistFileBuffer }; NyquistFileBuffer ReadFile(std::string pathToFile); + +int GetFormatBitsPerSample(PCMFormat f); } // end namespace nqr diff --git a/include/libnyquist/Dither.h b/include/libnyquist/Dither.h index 07b92f3..5a9864b 100644 --- a/include/libnyquist/Dither.h +++ b/include/libnyquist/Dither.h @@ -33,6 +33,12 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. namespace nqr { +enum DitherType +{ + DITHER_ONE, + DITHER_TWO +}; + } // end namespace nqr #endif diff --git a/include/libnyquist/RiffUtils.h b/include/libnyquist/RiffUtils.h index 0bdae07..31eab26 100644 --- a/include/libnyquist/RiffUtils.h +++ b/include/libnyquist/RiffUtils.h @@ -28,6 +28,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "Common.h" #include "WavDecoder.h" +#include "Dither.h" namespace nqr { @@ -38,10 +39,11 @@ namespace nqr struct EncoderParams { - int channels; - int samplerate; - int bit_depth; - PCMFormat fmt; + int channelCount; + int sampleRate; + int bitDepth; + PCMFormat targetFormat; + DitherType dither; }; struct ChunkHeaderInfo diff --git a/src/Common.cpp b/src/Common.cpp index 367e9fd..33c5a60 100644 --- a/src/Common.cpp +++ b/src/Common.cpp @@ -145,3 +145,25 @@ void nqr::ConvertToFloat32(float * dst, const int32_t * src, const size_t N, PCM dst[i] = int32_to_float32(Read32(src[i])); } } + +int nqr::GetFormatBitsPerSample(PCMFormat f) +{ + switch(f) + { + case PCM_U8: + case PCM_S8: + return 8; + case PCM_16: + return 16; + case PCM_24: + return 24; + case PCM_32: + case PCM_FLT: + return 32; + case PCM_64: + case PCM_DBL: + return 64; + default: + return 0; + } +} diff --git a/src/WavEncoder.cpp b/src/WavEncoder.cpp index 06234dc..03a54aa 100644 --- a/src/WavEncoder.cpp +++ b/src/WavEncoder.cpp @@ -67,12 +67,18 @@ int WavEncoder::WriteFile(const EncoderParams p, const AudioData * d, const std: { return EncoderError::BufferTooBig; } - + + // No resampling + if (d->sampleRate != p.sampleRate) + { + return EncoderError::UnsupportedSamplerate; + } + std::ofstream fout(path.c_str(), std::ios::out | std::ios::binary); if (!fout.is_open()) { - throw std::runtime_error("File cannot be opened"); + return EncoderError::FileIOError; } char * chunkSizeBuff = new char[4]; @@ -94,12 +100,14 @@ int WavEncoder::WriteFile(const EncoderParams p, const AudioData * d, const std: fout.write(GenerateChunkCodeChar('d', 'a', 't', 'a'), 4); // + data chunk size - auto numSamplesBytes = data.size() * sizeof(float); + auto numSamplesBytes = d->samples.size() * sizeof(float); toBytes(numSamplesBytes, chunkSizeBuff); fout.write(chunkSizeBuff, 4); // Debugging -- assume IEEE_Float - fout.write(reinterpret_cast(data.data()), numSamplesBytes); + + auto LookupB + fout.write(reinterpret_cast(d->samples.data()), numSamplesBytes); // Find size long totalSize = fout.tellp(); @@ -113,4 +121,6 @@ int WavEncoder::WriteFile(const EncoderParams p, const AudioData * d, const std: fout.write(chunkSizeBuff, 4); delete[] chunkSizeBuff; + + return EncoderError::NoError; }