From 6499bc745595cc53ab101987bc5e527d9a22053c Mon Sep 17 00:00:00 2001 From: Dimitri Diakopoulos Date: Sat, 16 May 2015 23:06:20 -0700 Subject: [PATCH] Break out chunk/riff helpers into separate files --- include/libnyquist/Common.h | 21 ----------------- include/libnyquist/Dither.h | 1 + include/libnyquist/RiffUtils.h | 34 ++++++++++++++++++++++++++++ libnyquist.xcodeproj/project.pbxproj | 6 +++++ src/Common.cpp | 20 ---------------- src/RiffUtils.cpp | 23 +++++++++++++++++++ 6 files changed, 64 insertions(+), 41 deletions(-) create mode 100644 include/libnyquist/RiffUtils.h create mode 100644 src/RiffUtils.cpp diff --git a/include/libnyquist/Common.h b/include/libnyquist/Common.h index 82e6e98..5ac6036 100644 --- a/include/libnyquist/Common.h +++ b/include/libnyquist/Common.h @@ -271,27 +271,6 @@ inline void ConvertToFloat32(float * dst, const int32_t * src, const size_t N, P } } -/////////////////////////////////////////// -// Chunk utilities (move somewhere else) // -/////////////////////////////////////////// - -struct ChunkHeaderInfo -{ - uint32_t offset; // Byte offset into chunk - uint32_t size; // Size of the chunk in bytes -}; - -inline uint32_t GenerateChunkCode(uint8_t a, uint8_t b, uint8_t c, uint8_t d) -{ - #ifdef ARCH_CPU_LITTLE_ENDIAN - return ((uint32_t) ((a) | ((b) << 8) | ((c) << 16) | (((uint32_t) (d)) << 24))); - #else - return ((uint32_t) ((((uint32_t) (a)) << 24) | ((b) << 16) | ((c) << 8) | (d))); - #endif -} - -ChunkHeaderInfo ScanForChunk(const std::vector & fileData, uint32_t chunkMarker); - ////////////////////////// // User Data + File Ops // ////////////////////////// diff --git a/include/libnyquist/Dither.h b/include/libnyquist/Dither.h index 02fe0fe..b00ea4d 100644 --- a/include/libnyquist/Dither.h +++ b/include/libnyquist/Dither.h @@ -13,3 +13,4 @@ namespace nqr } // end namespace nqr #endif + diff --git a/include/libnyquist/RiffUtils.h b/include/libnyquist/RiffUtils.h new file mode 100644 index 0000000..5d03e0a --- /dev/null +++ b/include/libnyquist/RiffUtils.h @@ -0,0 +1,34 @@ +#pragma comment(user, "license") + +#ifndef RIFF_UTILS_H +#define RIFF_UTILS_H + +#include "Common.h" + +namespace nqr +{ + +///////////////////// +// Chunk utilities // +///////////////////// + +struct ChunkHeaderInfo +{ + uint32_t offset; // Byte offset into chunk + uint32_t size; // Size of the chunk in bytes +}; + +inline uint32_t GenerateChunkCode(uint8_t a, uint8_t b, uint8_t c, uint8_t d) +{ + #ifdef ARCH_CPU_LITTLE_ENDIAN + return ((uint32_t) ((a) | ((b) << 8) | ((c) << 16) | (((uint32_t) (d)) << 24))); + #else + return ((uint32_t) ((((uint32_t) (a)) << 24) | ((b) << 16) | ((c) << 8) | (d))); + #endif +} + +ChunkHeaderInfo ScanForChunk(const std::vector & fileData, uint32_t chunkMarker); + +} // end namespace nqr + +#endif diff --git a/libnyquist.xcodeproj/project.pbxproj b/libnyquist.xcodeproj/project.pbxproj index 0d25d3a..56f68d4 100644 --- a/libnyquist.xcodeproj/project.pbxproj +++ b/libnyquist.xcodeproj/project.pbxproj @@ -10,6 +10,7 @@ 0804D13F1AE69F0100F4B1FD /* OpusDependencies.c in Sources */ = {isa = PBXBuildFile; fileRef = 0804D13E1AE69F0100F4B1FD /* OpusDependencies.c */; }; 0804D1451AE6BA5100F4B1FD /* celt_decoder.c in Sources */ = {isa = PBXBuildFile; fileRef = 0804D1431AE6BA5100F4B1FD /* celt_decoder.c */; }; 0804D1461AE6BA5100F4B1FD /* celt_encoder.c in Sources */ = {isa = PBXBuildFile; fileRef = 0804D1441AE6BA5100F4B1FD /* celt_encoder.c */; }; + 080A58581B0866F600302850 /* RiffUtils.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 080A58571B0866F600302850 /* RiffUtils.cpp */; }; 081FFB191ADF803800673073 /* FlacDependencies.c in Sources */ = {isa = PBXBuildFile; fileRef = 081FFB181ADF803800673073 /* FlacDependencies.c */; }; 086DADAD1AE029860031F793 /* VorbisDependencies.c in Sources */ = {isa = PBXBuildFile; fileRef = 086DADAB1ADF9DF30031F793 /* VorbisDependencies.c */; }; 08894F291AEAA5D7007AAF90 /* bits.c in Sources */ = {isa = PBXBuildFile; fileRef = 08894F1E1AEAA5D7007AAF90 /* bits.c */; }; @@ -56,6 +57,8 @@ 0804D13E1AE69F0100F4B1FD /* OpusDependencies.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = OpusDependencies.c; path = src/OpusDependencies.c; sourceTree = SOURCE_ROOT; }; 0804D1431AE6BA5100F4B1FD /* celt_decoder.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = celt_decoder.c; path = third_party/opus/celt/celt_decoder.c; sourceTree = SOURCE_ROOT; }; 0804D1441AE6BA5100F4B1FD /* celt_encoder.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = celt_encoder.c; path = third_party/opus/celt/celt_encoder.c; sourceTree = SOURCE_ROOT; }; + 080A58561B0866AA00302850 /* RiffUtils.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = RiffUtils.h; path = include/libnyquist/RiffUtils.h; sourceTree = SOURCE_ROOT; }; + 080A58571B0866F600302850 /* RiffUtils.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = RiffUtils.cpp; path = src/RiffUtils.cpp; sourceTree = SOURCE_ROOT; }; 081FFB181ADF803800673073 /* FlacDependencies.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = FlacDependencies.c; path = src/FlacDependencies.c; sourceTree = SOURCE_ROOT; }; 086DADAB1ADF9DF30031F793 /* VorbisDependencies.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = VorbisDependencies.c; path = src/VorbisDependencies.c; sourceTree = SOURCE_ROOT; }; 08894F1E1AEAA5D7007AAF90 /* bits.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = bits.c; path = third_party/wavpack/src/bits.c; sourceTree = SOURCE_ROOT; }; @@ -221,6 +224,7 @@ 08B91D881AC73B8000335131 /* FlacDecoder.h */, 08B91D891AC73B8000335131 /* VorbisDecoder.h */, 08B91D8A1AC73B8000335131 /* OpusDecoder.h */, + 080A58561B0866AA00302850 /* RiffUtils.h */, 08B91D8B1AC73B8000335131 /* PostProcess.h */, 08B91D8C1AC73B8000335131 /* RingBuffer.h */, 08B91D8E1AC73B8000335131 /* WavDecoder.h */, @@ -237,6 +241,7 @@ 08B91D911AC73B8A00335131 /* AudioDecoder.cpp */, 08B91D921AC73B8A00335131 /* AudioDevice.cpp */, 08B91D931AC73B8A00335131 /* Common.cpp */, + 080A58571B0866F600302850 /* RiffUtils.cpp */, 08B91D941AC73B8A00335131 /* FlacDecoder.cpp */, 08B91D951AC73B8A00335131 /* Main.cpp */, 08B91D961AC73B8A00335131 /* VorbisDecoder.cpp */, @@ -321,6 +326,7 @@ 08B91DA21AC73B8A00335131 /* WavDecoder.cpp in Sources */, 08B91D9A1AC73B8A00335131 /* CafDecoder.cpp in Sources */, 08894F2F1AEAA5D7007AAF90 /* tags.c in Sources */, + 080A58581B0866F600302850 /* RiffUtils.cpp in Sources */, 08B91DA11AC73B8A00335131 /* OpusDecoder.cpp in Sources */, 08894F2D1AEAA5D7007AAF90 /* metadata.c in Sources */, 08B91D9E1AC73B8A00335131 /* FlacDecoder.cpp in Sources */, diff --git a/src/Common.cpp b/src/Common.cpp index e567a93..ac12f9c 100644 --- a/src/Common.cpp +++ b/src/Common.cpp @@ -2,26 +2,6 @@ using namespace nqr; -ChunkHeaderInfo nqr::ScanForChunk(const std::vector & fileData, uint32_t chunkMarker) -{ - // D[n] aligned to 16 bytes now - const uint16_t * d = reinterpret_cast(fileData.data()); - - for (size_t i = 0; i < fileData.size() / sizeof(uint16_t); i++) - { - // This will be in machine endianess - uint32_t m = Pack(Read16(d[i]), Read16(d[i+1])); - - if (m == chunkMarker) - { - uint32_t cSz = Pack(Read16(d[i+2]), Read16(d[i+3])); - return {(uint32_t (i * sizeof(uint16_t))), cSz}; // return i in bytes to the start of the data - } - else continue; - } - return {0, 0}; -}; - NyquistFileBuffer nqr::ReadFile(std::string pathToFile) { std::cout << "[Debug] Open: " << pathToFile << std::endl; diff --git a/src/RiffUtils.cpp b/src/RiffUtils.cpp new file mode 100644 index 0000000..cf46677 --- /dev/null +++ b/src/RiffUtils.cpp @@ -0,0 +1,23 @@ +#include "RiffUtils.h" + +using namespace nqr; + +ChunkHeaderInfo nqr::ScanForChunk(const std::vector & fileData, uint32_t chunkMarker) +{ + // D[n] aligned to 16 bytes now + const uint16_t * d = reinterpret_cast(fileData.data()); + + for (size_t i = 0; i < fileData.size() / sizeof(uint16_t); i++) + { + // This will be in machine endianess + uint32_t m = Pack(Read16(d[i]), Read16(d[i+1])); + + if (m == chunkMarker) + { + uint32_t cSz = Pack(Read16(d[i+2]), Read16(d[i+3])); + return {(uint32_t (i * sizeof(uint16_t))), cSz}; // return i in bytes to the start of the data + } + else continue; + } + return {0, 0}; +}; \ No newline at end of file