diff options
author | Daniil Cherednik <dan.cherednik@gmail.com> | 2024-12-20 22:31:20 +0100 |
---|---|---|
committer | Daniil Cherednik <dan.cherednik@gmail.com> | 2024-12-21 16:30:07 +0100 |
commit | 74d6e04c21bddd435bd74c34dbe027b883772a76 (patch) | |
tree | 4221df9da5a17897296e5f7f841f01b1f6428c74 /src | |
parent | 78649b2f5941e0649db960cafb2997b5ca432be1 (diff) | |
download | atracdenc-master.tar.gz |
Diffstat (limited to 'src')
44 files changed, 462 insertions, 487 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b3680d5..ea0956d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -32,10 +32,6 @@ set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/modules") enable_cxx_compiler_flag_if_supported("-Wall") enable_cxx_compiler_flag_if_supported("-Wextra") -if (ATDE_USE_FLOAT) - add_compile_definitions(ATDE_USE_FLOAT) -endif() - if (WIN32) add_compile_definitions(PLATFORM_WINDOWS) add_compile_definitions(_CRT_SECURE_NO_WARNINGS) @@ -67,11 +63,7 @@ include_directories( set(SOURCE_FFT_LIB lib/fft/kissfft_impl/kiss_fft.c) -if (ATDE_USE_FLOAT) - set_source_files_properties(lib/fft/kissfft_impl/kiss_fft.c PROPERTIES COMPILE_FLAGS -Dkiss_fft_scalar=float) -else() - set_source_files_properties(lib/fft/kissfft_impl/kiss_fft.c PROPERTIES COMPILE_FLAGS -Dkiss_fft_scalar=double) -endif() +set_source_files_properties(lib/fft/kissfft_impl/kiss_fft.c PROPERTIES COMPILE_FLAGS -Dkiss_fft_scalar=float) set(SOURCE_OMA_LIB lib/liboma/src/liboma.c) set(SOURCE_BITSTREAM_LIB lib/bitstream/bitstream.cpp) diff --git a/src/atrac/atrac1.cpp b/src/atrac/atrac1.cpp index d463a1c..0c1c053 100644 --- a/src/atrac/atrac1.cpp +++ b/src/atrac/atrac1.cpp @@ -26,8 +26,8 @@ constexpr uint32_t TAtrac1Data::SpecsPerBlock[MaxBfus]; constexpr uint32_t TAtrac1Data::SpecsStartLong[MaxBfus]; constexpr uint32_t TAtrac1Data::SpecsStartShort[MaxBfus]; constexpr uint32_t TAtrac1Data::BfuAmountTab[8]; -TFloat TAtrac1Data::ScaleTable[64] = {0}; -TFloat TAtrac1Data::SineWindow[32] = {0}; +float TAtrac1Data::ScaleTable[64] = {0}; +float TAtrac1Data::SineWindow[32] = {0}; const static TAtrac1Data Atrac1Data; diff --git a/src/atrac/atrac1.h b/src/atrac/atrac1.h index 54beedc..ae1809b 100644 --- a/src/atrac/atrac1.h +++ b/src/atrac/atrac1.h @@ -78,8 +78,8 @@ public: static const uint32_t BitsPerIDWL = 4; static const uint32_t BitsPerIDSF = 6; - static TFloat ScaleTable[64]; - static TFloat SineWindow[32]; + static float ScaleTable[64]; + static float SineWindow[32]; static uint32_t BfuToBand(uint32_t i) { if (i < 20) return 0; diff --git a/src/atrac/atrac1_bitalloc.cpp b/src/atrac/atrac1_bitalloc.cpp index a4b6cd0..5b20cc8 100644 --- a/src/atrac/atrac1_bitalloc.cpp +++ b/src/atrac/atrac1_bitalloc.cpp @@ -98,7 +98,7 @@ uint32_t TBitsBooster::ApplyBoost(std::vector<uint32_t>* bitsPerEachBlock, uint3 return surplus; } -std::vector<TFloat> TAtrac1SimpleBitAlloc::ATHLong; +std::vector<float> TAtrac1SimpleBitAlloc::ATHLong; TAtrac1SimpleBitAlloc::TAtrac1SimpleBitAlloc(ICompressedOutput* container, uint32_t bfuIdxConst, bool fastBfuNumSearch) : TAtrac1BitStreamWriter(container) @@ -125,8 +125,8 @@ TAtrac1SimpleBitAlloc::TAtrac1SimpleBitAlloc(ICompressedOutput* container, uint3 vector<uint32_t> TAtrac1SimpleBitAlloc::CalcBitsAllocation(const std::vector<TScaledBlock>& scaledBlocks, const uint32_t bfuNum, - const TFloat spread, - const TFloat shift, + const float spread, + const float shift, const TBlockSize& blockSize, const float loudness) { vector<uint32_t> bitsPerEachBlock(bfuNum); @@ -138,7 +138,7 @@ vector<uint32_t> TAtrac1SimpleBitAlloc::CalcBitsAllocation(const std::vector<TSc if (!shortBlock && scaledBlocks[i].MaxEnergy < ath) { bitsPerEachBlock[i] = 0; } else { - int tmp = spread * ( (TFloat)scaledBlocks[i].ScaleFactorIndex/3.2) + (1.0 - spread) * fix - shift; + int tmp = spread * ( (float)scaledBlocks[i].ScaleFactorIndex/3.2) + (1.0 - spread) * fix - shift; if (tmp > 16) { bitsPerEachBlock[i] = 16; } else if (tmp < 2) { @@ -189,7 +189,7 @@ uint32_t TAtrac1SimpleBitAlloc::CheckBfuUsage(bool* changed, uint32_t TAtrac1SimpleBitAlloc::Write(const std::vector<TScaledBlock>& scaledBlocks, const TBlockSize& blockSize, float loudness) { uint32_t bfuIdx = BfuIdxConst ? BfuIdxConst - 1 : 7; bool autoBfu = !BfuIdxConst; - TFloat spread = AnalizeScaleFactorSpread(scaledBlocks); + float spread = AnalizeScaleFactorSpread(scaledBlocks); vector<uint32_t> bitsPerEachBlock(TAtrac1Data::BfuAmountTab[bfuIdx]); uint32_t targetBitsPerBfus; @@ -200,9 +200,9 @@ uint32_t TAtrac1SimpleBitAlloc::Write(const std::vector<TScaledBlock>& scaledBlo TAtrac1Data::BitsPerBfuAmountTabIdx - 32 - 2 - 3 - bitsPerEachBlock.size() * (TAtrac1Data::BitsPerIDWL + TAtrac1Data::BitsPerIDSF); - TFloat maxShift = 15; - TFloat minShift = -3; - TFloat shift = 3.0; + float maxShift = 15; + float minShift = -3; + float shift = 3.0; const uint32_t maxBits = bitsAvaliablePerBfus; const uint32_t minBits = bitsAvaliablePerBfus - 110; @@ -299,8 +299,8 @@ void TAtrac1BitStreamWriter::WriteBitStream(const vector<uint32_t>& bitsPerEachB if (wordLength == 0 || wordLength == 1) continue; - const TFloat multiple = ((1 << (wordLength - 1)) - 1); - for (const TFloat val : scaledBlocks[i].Values) { + const float multiple = ((1 << (wordLength - 1)) - 1); + for (const float val : scaledBlocks[i].Values) { const int tmp = lrint(val * multiple); const uint32_t testwl = bitsPerEachBlock[i] ? (bitsPerEachBlock[i] - 1) : 0; const uint32_t a = !!testwl + testwl; diff --git a/src/atrac/atrac1_bitalloc.h b/src/atrac/atrac1_bitalloc.h index 4a33cb0..ca01b13 100644 --- a/src/atrac/atrac1_bitalloc.h +++ b/src/atrac/atrac1_bitalloc.h @@ -55,11 +55,11 @@ public: class TAtrac1SimpleBitAlloc : public TAtrac1BitStreamWriter, public TBitsBooster, public virtual IAtrac1BitAlloc { std::vector<uint32_t> CalcBitsAllocation(const std::vector<TScaledBlock>& scaledBlocks, const uint32_t bfuNum, - const TFloat spread, const TFloat shift, const TBlockSize& blockSize, + const float spread, const float shift, const TBlockSize& blockSize, const float loudness); const uint32_t BfuIdxConst; const bool FastBfuNumSearch; - static std::vector<TFloat> ATHLong; + static std::vector<float> ATHLong; uint32_t GetMaxUsedBfuId(const std::vector<uint32_t>& bitsPerEachBlock); uint32_t CheckBfuUsage(bool* changed, uint32_t curBfuId, const std::vector<uint32_t>& bitsPerEachBlock); diff --git a/src/atrac/atrac1_dequantiser.cpp b/src/atrac/atrac1_dequantiser.cpp index 0b1c065..bcc087d 100644 --- a/src/atrac/atrac1_dequantiser.cpp +++ b/src/atrac/atrac1_dequantiser.cpp @@ -26,7 +26,7 @@ using namespace NBitStream; TAtrac1Dequantiser::TAtrac1Dequantiser() { } -void TAtrac1Dequantiser::Dequant(TBitStream* stream, const TBlockSize& bs, TFloat specs[512]) { +void TAtrac1Dequantiser::Dequant(TBitStream* stream, const TBlockSize& bs, float specs[512]) { uint32_t wordLens[TAtrac1Data::MaxBfus]; uint32_t idScaleFactors[TAtrac1Data::MaxBfus]; const uint32_t numBFUs = TAtrac1Data::BfuAmountTab[stream->Read(3)]; @@ -48,17 +48,17 @@ void TAtrac1Dequantiser::Dequant(TBitStream* stream, const TBlockSize& bs, TFloa for (uint32_t bfuNum = TAtrac1Data::BlocksPerBand[bandNum]; bfuNum < TAtrac1Data::BlocksPerBand[bandNum + 1]; bfuNum++) { const uint32_t numSpecs = TAtrac1Data::SpecsPerBlock[bfuNum]; const uint32_t wordLen = !!wordLens[bfuNum] + wordLens[bfuNum]; - const TFloat scaleFactor = TAtrac1Data::ScaleTable[idScaleFactors[bfuNum]]; + const float scaleFactor = TAtrac1Data::ScaleTable[idScaleFactors[bfuNum]]; const uint32_t startPos = bs.LogCount[bandNum] ? TAtrac1Data::SpecsStartShort[bfuNum] : TAtrac1Data::SpecsStartLong[bfuNum]; if (wordLen) { - TFloat maxQuant = 1.0 / (TFloat)((1 << (wordLen - 1)) - 1); + float maxQuant = 1.0 / (float)((1 << (wordLen - 1)) - 1); //cout << "BFU ("<< bfuNum << ") :" << "wordLen " << wordLen << " maxQuant " << maxQuant << " scaleFactor " << scaleFactor << " id " << idScaleFactors[bfuNum] << " num Specs " << numSpecs << " short: "<< (int)bs.LogCount[bandNum] << endl; for (uint32_t i = 0; i < numSpecs; i++ ) { specs[startPos + i] = scaleFactor * maxQuant * MakeSign(stream->Read(wordLen), wordLen); } } else { - memset(&specs[startPos], 0, numSpecs * sizeof(TFloat)); + memset(&specs[startPos], 0, numSpecs * sizeof(float)); } } diff --git a/src/atrac/atrac1_dequantiser.h b/src/atrac/atrac1_dequantiser.h index 95fd250..0b57c08 100644 --- a/src/atrac/atrac1_dequantiser.h +++ b/src/atrac/atrac1_dequantiser.h @@ -27,7 +27,7 @@ namespace NAtrac1 { class TAtrac1Dequantiser { public: TAtrac1Dequantiser(); - void Dequant(NBitStream::TBitStream* stream, const TBlockSize& bs, TFloat specs[512]); + void Dequant(NBitStream::TBitStream* stream, const TBlockSize& bs, float specs[512]); }; } //namespace NAtrac1 diff --git a/src/atrac/atrac1_qmf.h b/src/atrac/atrac1_qmf.h index d8fdae3..0276160 100644 --- a/src/atrac/atrac1_qmf.h +++ b/src/atrac/atrac1_qmf.h @@ -28,18 +28,18 @@ class Atrac1AnalysisFilterBank { const static int delayComp = 39; TQmf<TIn, nInSamples> Qmf1; TQmf<TIn, nInSamples / 2> Qmf2; - std::vector<TFloat> MidLowTmp; - std::vector<TFloat> DelayBuf; + std::vector<float> MidLowTmp; + std::vector<float> DelayBuf; public: Atrac1AnalysisFilterBank() { MidLowTmp.resize(512); DelayBuf.resize(delayComp + 512); } - void Analysis(TIn* pcm, TFloat* low, TFloat* mid, TFloat* hi) { - memcpy(&DelayBuf[0], &DelayBuf[256], sizeof(TFloat) * delayComp); + void Analysis(TIn* pcm, float* low, float* mid, float* hi) { + memcpy(&DelayBuf[0], &DelayBuf[256], sizeof(float) * delayComp); Qmf1.Analysis(pcm, &MidLowTmp[0], &DelayBuf[delayComp]); Qmf2.Analysis(&MidLowTmp[0], low, mid); - memcpy(hi, &DelayBuf[0], sizeof(TFloat) * 256); + memcpy(hi, &DelayBuf[0], sizeof(float) * 256); } }; @@ -49,16 +49,16 @@ class Atrac1SynthesisFilterBank { const static int delayComp = 39; TQmf<TOut, nInSamples> Qmf1; TQmf<TOut, nInSamples / 2> Qmf2; - std::vector<TFloat> MidLowTmp; - std::vector<TFloat> DelayBuf; + std::vector<float> MidLowTmp; + std::vector<float> DelayBuf; public: Atrac1SynthesisFilterBank() { MidLowTmp.resize(512); DelayBuf.resize(delayComp + 512); } - void Synthesis(TOut* pcm, TFloat* low, TFloat* mid, TFloat* hi) { - memcpy(&DelayBuf[0], &DelayBuf[256], sizeof(TFloat) * delayComp); - memcpy(&DelayBuf[delayComp], hi, sizeof(TFloat) * 256); + void Synthesis(TOut* pcm, float* low, float* mid, float* hi) { + memcpy(&DelayBuf[0], &DelayBuf[256], sizeof(float) * delayComp); + memcpy(&DelayBuf[delayComp], hi, sizeof(float) * 256); Qmf2.Synthesis(&MidLowTmp[0], &low[0], &mid[0]); Qmf1.Synthesis(&pcm[0], &MidLowTmp[0], &DelayBuf[0]); } diff --git a/src/atrac/atrac3.cpp b/src/atrac/atrac3.cpp index 363a32e..1e5b8e9 100644 --- a/src/atrac/atrac3.cpp +++ b/src/atrac/atrac3.cpp @@ -24,7 +24,7 @@ namespace NAtrac3 { constexpr uint32_t TAtrac3Data::BlockSizeTab[33]; constexpr uint32_t TAtrac3Data::ClcLengthTab[8]; -constexpr double TAtrac3Data::MaxQuant[8]; +constexpr float TAtrac3Data::MaxQuant[8]; constexpr uint32_t TAtrac3Data::BlocksPerBand[4 + 1]; constexpr uint32_t TAtrac3Data::SpecsPerBlock[33]; constexpr TAtrac3Data::THuffEntry TAtrac3Data::HuffTable1[HuffTable1Sz]; @@ -36,11 +36,11 @@ constexpr TAtrac3Data::THuffEntry TAtrac3Data::HuffTable7[HuffTable7Sz]; constexpr TAtrac3Data::THuffTablePair TAtrac3Data::HuffTables[7]; constexpr TContainerParams TAtrac3Data::ContainerParams[8]; -double TAtrac3Data::EncodeWindow[256] = {0}; -double TAtrac3Data::DecodeWindow[256] = {0}; -double TAtrac3Data::ScaleTable[64] = {0}; -double TAtrac3Data::GainLevel[16]; -double TAtrac3Data::GainInterpolation[31]; +float TAtrac3Data::EncodeWindow[256] = {0}; +float TAtrac3Data::DecodeWindow[256] = {0}; +float TAtrac3Data::ScaleTable[64] = {0}; +float TAtrac3Data::GainLevel[16]; +float TAtrac3Data::GainInterpolation[31]; static const TAtrac3Data Atrac3Data; diff --git a/src/atrac/atrac3.h b/src/atrac/atrac3.h index 6ce0283..103f6fc 100644 --- a/src/atrac/atrac3.h +++ b/src/atrac/atrac3.h @@ -56,11 +56,11 @@ public: static constexpr uint32_t NumSamples = 1024; static const uint32_t MDCTSz = 512; - static double ScaleTable[64]; - static double EncodeWindow[256]; - static double DecodeWindow[256]; - static double GainLevel[16]; - static double GainInterpolation[31]; + static float ScaleTable[64]; + static float EncodeWindow[256]; + static float DecodeWindow[256]; + static float GainLevel[16]; + static float GainInterpolation[31]; static constexpr int32_t ExponentOffset = 4; static constexpr int32_t LocScale = 3; static constexpr int32_t LocSz = 1 << LocScale; @@ -68,7 +68,7 @@ public: static constexpr uint32_t NumSpecs = NumSamples; static const uint32_t frameSz = 152; - static constexpr double MaxQuant[8] = { + static constexpr float MaxQuant[8] = { 0.0, 1.5, 2.5, 3.5, 4.5, 7.5, 15.5, 31.5 }; diff --git a/src/atrac/atrac3_bitstream.cpp b/src/atrac/atrac3_bitstream.cpp index e3194b1..9bada6f 100644 --- a/src/atrac/atrac3_bitstream.cpp +++ b/src/atrac/atrac3_bitstream.cpp @@ -40,7 +40,7 @@ static const uint32_t FixedBitAllocTable[TAtrac3Data::MaxBfus] = { 1, 0 }; -std::vector<TFloat> TAtrac3BitStreamWriter::ATH; +std::vector<float> TAtrac3BitStreamWriter::ATH; TAtrac3BitStreamWriter::TAtrac3BitStreamWriter(ICompressedOutput* container, const TContainerParams& params, uint32_t bfuIdxConst) : Container(container) , Params(params) @@ -120,7 +120,7 @@ uint32_t TAtrac3BitStreamWriter::VLCEnc(const uint32_t selector, const int manti return bitsUsed; } -static inline int ToInt(double x) { +static inline int ToInt(float x) { #if defined(_MSC_VER) && !defined(_WIN64) int n; __asm { @@ -133,7 +133,7 @@ static inline int ToInt(double x) { #endif } -static inline void CalcMantisas(const TFloat* values, const uint32_t first, const uint32_t last, const TFloat mul, int* mantisas) { +static inline void CalcMantisas(const float* values, const uint32_t first, const uint32_t last, const float mul, int* mantisas) { for (uint32_t j = 0, f = first; f < last; f++, j++) { mantisas[f] = ToInt(values[j] * mul); } @@ -156,9 +156,9 @@ std::pair<uint8_t, uint32_t> TAtrac3BitStreamWriter::CalcSpecsBitsConsumption(co const uint32_t first = TAtrac3Data::BlockSizeTab[i]; const uint32_t last = TAtrac3Data::BlockSizeTab[i+1]; const uint32_t blockSize = last - first; - const TFloat mul = TAtrac3Data::MaxQuant[std::min(precisionPerEachBlocks[i], (uint32_t)7)]; + const float mul = TAtrac3Data::MaxQuant[std::min(precisionPerEachBlocks[i], (uint32_t)7)]; if (calcMant) { - const TFloat* values = scaledBlocks[i].Values.data(); + const float* values = scaledBlocks[i].Values.data(); CalcMantisas(values, first, last, mul, mantisas); } bits += clcMode ? CLCEnc(precisionPerEachBlocks[i], mantisas + first, blockSize, nullptr) : @@ -197,7 +197,7 @@ std::pair<uint8_t, vector<uint32_t>> TAtrac3BitStreamWriter::CreateAllocation(co return DUMMY_ALLOC; } - TFloat spread = AnalizeScaleFactorSpread(scaledBlocks); + float spread = AnalizeScaleFactorSpread(scaledBlocks); uint16_t numBfu = BfuIdxConst ? BfuIdxConst : 32; @@ -215,10 +215,10 @@ std::pair<uint8_t, vector<uint32_t>> TAtrac3BitStreamWriter::CreateAllocation(co bool cont = true; while (cont) { precisionPerEachBlocks.resize(numBfu); - TFloat maxShift = 20; - TFloat minShift = -8; + double maxShift = 20; + double minShift = -8; for (;;) { - TFloat shift = (maxShift + minShift) / 2; + double shift = (maxShift + minShift) / 2; const vector<uint32_t>& tmpAlloc = CalcBitsAllocation(scaledBlocks, numBfu, spread, shift, laudness); auto consumption = CalcSpecsBitsConsumption(sce, tmpAlloc, mt); @@ -451,7 +451,7 @@ uint16_t TAtrac3BitStreamWriter::EncodeTonalComponents(const TSingleChannelEleme ASSERT(curGroup.SubGroupPtr[k]->ScaledBlock.Values.size() < 8); int mantisas[256]; - const TFloat mul = TAtrac3Data::MaxQuant[std::min((uint32_t)(i>>3), (uint32_t)7)]; + const float mul = TAtrac3Data::MaxQuant[std::min((uint32_t)(i>>3), (uint32_t)7)]; ASSERT(codedValues == curGroup.SubGroupPtr[k]->ScaledBlock.Values.size()); for (uint32_t z = 0; z < curGroup.SubGroupPtr[k]->ScaledBlock.Values.size(); ++z) { mantisas[z] = lrint(curGroup.SubGroupPtr[k]->ScaledBlock.Values[z] * mul); @@ -477,9 +477,9 @@ uint16_t TAtrac3BitStreamWriter::EncodeTonalComponents(const TSingleChannelEleme vector<uint32_t> TAtrac3BitStreamWriter::CalcBitsAllocation(const std::vector<TScaledBlock>& scaledBlocks, const uint32_t bfuNum, - const TFloat spread, - const TFloat shift, - const TFloat loudness) + const float spread, + const float shift, + const float loudness) { vector<uint32_t> bitsPerEachBlock(bfuNum); for (size_t i = 0; i < bitsPerEachBlock.size(); ++i) { @@ -489,7 +489,7 @@ vector<uint32_t> TAtrac3BitStreamWriter::CalcBitsAllocation(const std::vector<TS bitsPerEachBlock[i] = 0; } else { const uint32_t fix = FixedBitAllocTable[i]; - int tmp = spread * ( (TFloat)scaledBlocks[i].ScaleFactorIndex/3.2) + (1.0 - spread) * fix - shift; + int tmp = spread * ( (float)scaledBlocks[i].ScaleFactorIndex/3.2) + (1.0 - spread) * fix - shift; if (tmp > 7) { bitsPerEachBlock[i] = 7; } else if (tmp < 0) { @@ -514,8 +514,8 @@ void WriteJsParams(NBitStream::TBitStream* bs) // 0.5 - M only (mono) // 0.0 - Uncorrelated // -0.5 - S only -static TFloat CalcMSRatio(TFloat mEnergy, TFloat sEnergy) { - TFloat total = sEnergy + mEnergy; +static float CalcMSRatio(float mEnergy, float sEnergy) { + float total = sEnergy + mEnergy; if (total > 0) return mEnergy / total - 0.5; @@ -535,7 +535,7 @@ static int32_t CalcMSBytesShift(uint32_t frameSz, if (elements[1].ScaledBlocks.empty()) { return maxAllowedShift; } else { - TFloat ratio = CalcMSRatio(elements[0].Loudness, elements[1].Loudness); + float ratio = CalcMSRatio(elements[0].Loudness, elements[1].Loudness); //std::cerr << ratio << std::endl; return std::max(std::min(ToInt(frameSz * ratio), maxAllowedShift), -maxAllowedShift); } diff --git a/src/atrac/atrac3_bitstream.h b/src/atrac/atrac3_bitstream.h index 75a39ea..93ee72e 100644 --- a/src/atrac/atrac3_bitstream.h +++ b/src/atrac/atrac3_bitstream.h @@ -45,10 +45,10 @@ public: TAtrac3Data::SubbandInfo SubbandInfo; std::vector<TTonalBlock> TonalBlocks; std::vector<TScaledBlock> ScaledBlocks; - TFloat Loudness; + float Loudness; }; private: - static std::vector<TFloat> ATH; + static std::vector<float> ATH; struct TTonalComponentsSubGroup { std::vector<uint8_t> SubGroupMap; @@ -66,7 +66,7 @@ private: const uint32_t blockSize, NBitStream::TBitStream* bitStream); std::vector<uint32_t> CalcBitsAllocation(const std::vector<TScaledBlock>& scaledBlocks, - uint32_t bfuNum, TFloat spread, TFloat shift, TFloat loudness); + uint32_t bfuNum, float spread, float shift, float loudness); std::pair<uint8_t, std::vector<uint32_t>> CreateAllocation(const TSingleChannelElement& sce, uint16_t targetBits, int mt[TAtrac3Data::MaxSpecs], float laudness); diff --git a/src/atrac/atrac3_qmf.h b/src/atrac/atrac3_qmf.h index 2fc33fa..1ed137e 100644 --- a/src/atrac/atrac3_qmf.h +++ b/src/atrac/atrac3_qmf.h @@ -28,14 +28,14 @@ class Atrac3AnalysisFilterBank { TQmf<TIn, nInSamples> Qmf1; TQmf<TIn, nInSamples / 2> Qmf2; TQmf<TIn, nInSamples / 2> Qmf3; - std::vector<TFloat> Buf1; - std::vector<TFloat> Buf2; + std::vector<float> Buf1; + std::vector<float> Buf2; public: Atrac3AnalysisFilterBank() { Buf1.resize(nInSamples); Buf2.resize(nInSamples); } - void Analysis(TIn* pcm, TFloat* subs[4]) { + void Analysis(TIn* pcm, float* subs[4]) { Qmf1.Analysis(pcm, Buf1.data(), Buf2.data()); Qmf2.Analysis(Buf1.data(), subs[0], subs[1]); Qmf3.Analysis(Buf2.data(), subs[3], subs[2]); diff --git a/src/atrac/atrac_psy_common.cpp b/src/atrac/atrac_psy_common.cpp index a034a78..186915e 100644 --- a/src/atrac/atrac_psy_common.cpp +++ b/src/atrac/atrac_psy_common.cpp @@ -99,15 +99,15 @@ namespace NAtracDEnc { using std::vector; //returns 1 for tone-like, 0 - noise-like -TFloat AnalizeScaleFactorSpread(const vector<TScaledBlock>& scaledBlocks) +float AnalizeScaleFactorSpread(const vector<TScaledBlock>& scaledBlocks) { - TFloat s = 0.0; + float s = 0.0; for (size_t i = 0; i < scaledBlocks.size(); ++i) { s += scaledBlocks[i].ScaleFactorIndex; } s /= scaledBlocks.size(); - TFloat sigma = 0.0; - TFloat t = 0.0; + float sigma = 0.0; + float t = 0.0; for (size_t i = 0; i < scaledBlocks.size(); ++i) { t = (scaledBlocks[i].ScaleFactorIndex - s); t *= t; diff --git a/src/atrac/atrac_psy_common.h b/src/atrac/atrac_psy_common.h index 854dfd1..c433b90 100644 --- a/src/atrac/atrac_psy_common.h +++ b/src/atrac/atrac_psy_common.h @@ -21,7 +21,7 @@ namespace NAtracDEnc { -TFloat AnalizeScaleFactorSpread(const std::vector<TScaledBlock>& scaledBlocks); +float AnalizeScaleFactorSpread(const std::vector<TScaledBlock>& scaledBlocks); std::vector<float> CalcATH(int len, int sampleRate); inline float TrackLoudness(float prevLoud, float l0, float l1) diff --git a/src/atrac/atrac_scale.cpp b/src/atrac/atrac_scale.cpp index fffabbe..d6f5960 100644 --- a/src/atrac/atrac_scale.cpp +++ b/src/atrac/atrac_scale.cpp @@ -33,7 +33,7 @@ using std::endl; using std::abs; -static const TFloat MAX_SCALE = 1.0; +static const float MAX_SCALE = 1.0; template<class TBaseData> TScaler<TBaseData>::TScaler() { @@ -43,10 +43,10 @@ TScaler<TBaseData>::TScaler() { } template<class TBaseData> -TScaledBlock TScaler<TBaseData>::Scale(const TFloat* in, uint16_t len) { - TFloat maxAbsSpec = 0; +TScaledBlock TScaler<TBaseData>::Scale(const float* in, uint16_t len) { + float maxAbsSpec = 0; for (uint16_t i = 0; i < len; ++i) { - const TFloat absSpec = abs(in[i]); + const float absSpec = abs(in[i]); if (absSpec > maxAbsSpec) { maxAbsSpec = absSpec; } @@ -55,17 +55,19 @@ TScaledBlock TScaler<TBaseData>::Scale(const TFloat* in, uint16_t len) { cerr << "Scale error: absSpec > MAX_SCALE, val: " << maxAbsSpec << endl; maxAbsSpec = MAX_SCALE; } - const map<TFloat, uint8_t>::const_iterator scaleIter = ScaleIndex.lower_bound(maxAbsSpec); - const TFloat scaleFactor = scaleIter->first; + const map<float, uint8_t>::const_iterator scaleIter = ScaleIndex.lower_bound(maxAbsSpec); + const float scaleFactor = scaleIter->first; const uint8_t scaleFactorIndex = scaleIter->second; TScaledBlock res(scaleFactorIndex); - TFloat maxEnergy = 0.0; + float maxEnergy = 0.0; for (uint16_t i = 0; i < len; ++i) { - TFloat scaledValue = in[i] / scaleFactor; - TFloat energy = in[i] * in[i]; + float scaledValue = in[i] / scaleFactor; + float energy = in[i] * in[i]; maxEnergy = std::max(maxEnergy, energy); if (abs(scaledValue) >= 1.0) { - cerr << "got "<< scaledValue << " it is wrong scalling" << endl; + if (abs(scaledValue) > 1.0) { + cerr << "clipping, scaled value: "<< scaledValue << endl; + } scaledValue = (scaledValue > 0) ? 0.99999 : -0.99999; } res.Values.push_back(scaledValue); @@ -75,7 +77,7 @@ TScaledBlock TScaler<TBaseData>::Scale(const TFloat* in, uint16_t len) { } template<class TBaseData> -vector<TScaledBlock> TScaler<TBaseData>::ScaleFrame(const vector<TFloat>& specs, const TBlockSize& blockSize) { +vector<TScaledBlock> TScaler<TBaseData>::ScaleFrame(const vector<float>& specs, const TBlockSize& blockSize) { vector<TScaledBlock> scaledBlocks; scaledBlocks.reserve(TBaseData::MaxBfus); for (uint8_t bandNum = 0; bandNum < TBaseData::NumQMF; ++bandNum) { diff --git a/src/atrac/atrac_scale.h b/src/atrac/atrac_scale.h index 6cea7a9..059360d 100644 --- a/src/atrac/atrac_scale.h +++ b/src/atrac/atrac_scale.h @@ -30,19 +30,19 @@ namespace NAtracDEnc { struct TScaledBlock { TScaledBlock(uint8_t sfi) : ScaleFactorIndex(sfi) {} /* const */ uint8_t ScaleFactorIndex = 0; - std::vector<TFloat> Values; - TFloat MaxEnergy; + std::vector<float> Values; + float MaxEnergy; }; class TBlockSize; template <class TBaseData> class TScaler { - std::map<TFloat, uint8_t> ScaleIndex; + std::map<float, uint8_t> ScaleIndex; public: TScaler(); - TScaledBlock Scale(const TFloat* in, uint16_t len); - std::vector<TScaledBlock> ScaleFrame(const std::vector<TFloat>& specs, const TBlockSize& blockSize); + TScaledBlock Scale(const float* in, uint16_t len); + std::vector<TScaledBlock> ScaleFrame(const std::vector<float>& specs, const TBlockSize& blockSize); }; class TBlockSize { diff --git a/src/atrac1denc.cpp b/src/atrac1denc.cpp index 05e940b..0b0ca07 100644 --- a/src/atrac1denc.cpp +++ b/src/atrac1denc.cpp @@ -45,8 +45,8 @@ TAtrac1Decoder::TAtrac1Decoder(TCompressedInputPtr&& aea) { } -static void vector_fmul_window(TFloat *dst, const TFloat *src0, - const TFloat *src1, const TFloat *win, int len) +static void vector_fmul_window(float *dst, const float *src0, + const float *src1, const float *win, int len) { int i, j; @@ -55,36 +55,36 @@ static void vector_fmul_window(TFloat *dst, const TFloat *src0, src0 += len; for (i = -len, j = len - 1; i < 0; i++, j--) { - TFloat s0 = src0[i]; - TFloat s1 = src1[j]; - TFloat wi = win[i]; - TFloat wj = win[j]; + float s0 = src0[i]; + float s1 = src1[j]; + float wi = win[i]; + float wj = win[j]; dst[i] = s0 * wj - s1 * wi; dst[j] = s0 * wi + s1 * wj; } } -void TAtrac1MDCT::Mdct(TFloat Specs[512], TFloat* low, TFloat* mid, TFloat* hi, const TBlockSize& blockSize) { +void TAtrac1MDCT::Mdct(float Specs[512], float* low, float* mid, float* hi, const TBlockSize& blockSize) { uint32_t pos = 0; for (uint32_t band = 0; band < TAtrac1Data::NumQMF; band++) { const uint32_t numMdctBlocks = 1 << blockSize.LogCount[band]; - TFloat* srcBuf = (band == 0) ? low : (band == 1) ? mid : hi; + float* srcBuf = (band == 0) ? low : (band == 1) ? mid : hi; uint32_t bufSz = (band == 2) ? 256 : 128; const uint32_t blockSz = (numMdctBlocks == 1) ? bufSz : 32; uint32_t winStart = (numMdctBlocks == 1) ? ((band == 2) ? 112 : 48) : 0; //compensate level for 3rd band in case of short window - const TFloat multiple = (numMdctBlocks != 1 && band == 2) ? 2.0 : 1.0; - vector<TFloat> tmp(512); + const float multiple = (numMdctBlocks != 1 && band == 2) ? 2.0 : 1.0; + vector<float> tmp(512); uint32_t blockPos = 0; for (size_t k = 0; k < numMdctBlocks; ++k) { - memcpy(&tmp[winStart], &srcBuf[bufSz], 32 * sizeof(TFloat)); + memcpy(&tmp[winStart], &srcBuf[bufSz], 32 * sizeof(float)); for (size_t i = 0; i < 32; i++) { srcBuf[bufSz + i] = TAtrac1Data::SineWindow[i] * srcBuf[blockPos + blockSz - 32 + i]; srcBuf[blockPos + blockSz - 32 + i] = TAtrac1Data::SineWindow[31 - i] * srcBuf[blockPos + blockSz - 32 + i]; } - memcpy(&tmp[winStart+32], &srcBuf[blockPos], blockSz * sizeof(TFloat)); - const vector<TFloat>& sp = (numMdctBlocks == 1) ? ((band == 2) ? Mdct512(&tmp[0]) : Mdct256(&tmp[0])) : Mdct64(&tmp[0]); + memcpy(&tmp[winStart+32], &srcBuf[blockPos], blockSz * sizeof(float)); + const vector<float>& sp = (numMdctBlocks == 1) ? ((band == 2) ? Mdct512(&tmp[0]) : Mdct256(&tmp[0])) : Mdct64(&tmp[0]); for (size_t i = 0; i < sp.size(); i++) { Specs[blockPos + pos + i] = sp[i] * multiple; } @@ -97,7 +97,7 @@ void TAtrac1MDCT::Mdct(TFloat Specs[512], TFloat* low, TFloat* mid, TFloat* hi, pos += bufSz; } } -void TAtrac1MDCT::IMdct(TFloat Specs[512], const TBlockSize& mode, TFloat* low, TFloat* mid, TFloat* hi) { +void TAtrac1MDCT::IMdct(float Specs[512], const TBlockSize& mode, float* low, float* mid, float* hi) { uint32_t pos = 0; for (size_t band = 0; band < TAtrac1Data::NumQMF; band++) { const uint32_t numMdctBlocks = 1 << mode.LogCount[band]; @@ -105,15 +105,15 @@ void TAtrac1MDCT::IMdct(TFloat Specs[512], const TBlockSize& mode, TFloat* low, const uint32_t blockSz = (numMdctBlocks == 1) ? bufSz : 32; uint32_t start = 0; - TFloat* dstBuf = (band == 0) ? low : (band == 1) ? mid : hi; + float* dstBuf = (band == 0) ? low : (band == 1) ? mid : hi; - vector<TFloat> invBuf(512); - TFloat* prevBuf = &dstBuf[bufSz * 2 - 16]; + vector<float> invBuf(512); + float* prevBuf = &dstBuf[bufSz * 2 - 16]; for (uint32_t block = 0; block < numMdctBlocks; block++) { if (band) { SwapArray(&Specs[pos], blockSz); } - vector<TFloat> inv = (numMdctBlocks != 1) ? Midct64(&Specs[pos]) : (bufSz == 128) ? Midct256(&Specs[pos]) : Midct512(&Specs[pos]); + vector<float> inv = (numMdctBlocks != 1) ? Midct64(&Specs[pos]) : (bufSz == 128) ? Midct256(&Specs[pos]) : Midct512(&Specs[pos]); for (size_t i = 0; i < (inv.size()/2); i++) { invBuf[start+i] = inv[i + inv.size()/4]; } @@ -125,7 +125,7 @@ void TAtrac1MDCT::IMdct(TFloat Specs[512], const TBlockSize& mode, TFloat* low, pos += blockSz; } if (numMdctBlocks == 1) - memcpy(dstBuf + 32, &invBuf[16], ((band == 2) ? 240 : 112) * sizeof(TFloat)); + memcpy(dstBuf + 32, &invBuf[16], ((band == 2) ? 240 : 112) * sizeof(float)); for (size_t j = 0; j < 16; j++) { dstBuf[bufSz*2 - 16 + j] = invBuf[bufSz - 16 + j]; @@ -133,9 +133,9 @@ void TAtrac1MDCT::IMdct(TFloat Specs[512], const TBlockSize& mode, TFloat* low, } } -TPCMEngine<TFloat>::TProcessLambda TAtrac1Decoder::GetLambda() { - return [this](TFloat* data, const TPCMEngine<TFloat>::ProcessMeta& /*meta*/) { - TFloat sum[512]; +TPCMEngine::TProcessLambda TAtrac1Decoder::GetLambda() { + return [this](float* data, const TPCMEngine::ProcessMeta& /*meta*/) { + float sum[512]; const uint32_t srcChannels = Aea->GetChannelNum(); for (uint32_t channel = 0; channel < srcChannels; channel++) { std::unique_ptr<ICompressedIO::TFrame> frame(Aea->ReadFrame()); @@ -144,7 +144,7 @@ TPCMEngine<TFloat>::TProcessLambda TAtrac1Decoder::GetLambda() { TBlockSize mode(&bitstream); TAtrac1Dequantiser dequantiser; - vector<TFloat> specs; + vector<float> specs; specs.resize(512);; dequantiser.Dequant(&bitstream, mode, &specs[0]); @@ -164,7 +164,7 @@ TPCMEngine<TFloat>::TProcessLambda TAtrac1Decoder::GetLambda() { } -TPCMEngine<TFloat>::TProcessLambda TAtrac1Encoder::GetLambda() { +TPCMEngine::TProcessLambda TAtrac1Encoder::GetLambda() { const uint32_t srcChannels = Aea->GetChannelNum(); vector<IAtrac1BitAlloc*> bitAlloc(srcChannels); @@ -178,19 +178,19 @@ TPCMEngine<TFloat>::TProcessLambda TAtrac1Encoder::GetLambda() { , Loudness(0.0) {} - vector<TFloat> Specs; + vector<float> Specs; float Loudness; }; using TData = vector<TChannelData>; auto buf = std::make_shared<TData>(srcChannels); - return [this, srcChannels, bitAlloc, buf](TFloat* data, const TPCMEngine<TFloat>::ProcessMeta& /*meta*/) { + return [this, srcChannels, bitAlloc, buf](float* data, const TPCMEngine::ProcessMeta& /*meta*/) { TBlockSize blockSz[2]; uint32_t windowMasks[2] = {0}; for (uint32_t channel = 0; channel < srcChannels; channel++) { - TFloat src[TAtrac1Data::NumSamples]; + float src[TAtrac1Data::NumSamples]; for (size_t i = 0; i < TAtrac1Data::NumSamples; ++i) { src[i] = data[i * srcChannels + channel]; } @@ -201,10 +201,10 @@ TPCMEngine<TFloat>::TProcessLambda TAtrac1Encoder::GetLambda() { if (Settings.GetWindowMode() == TAtrac1EncodeSettings::EWindowMode::EWM_AUTO) { windowMask |= (uint32_t)TransientDetectors.GetDetector(channel, 0).Detect(&PcmBufLow[channel][0]); - const vector<TFloat>& invMid = InvertSpectr<128>(&PcmBufMid[channel][0]); + const vector<float>& invMid = InvertSpectr<128>(&PcmBufMid[channel][0]); windowMask |= (uint32_t)TransientDetectors.GetDetector(channel, 1).Detect(&invMid[0]) << 1; - const vector<TFloat>& invHi = InvertSpectr<256>(&PcmBufHi[channel][0]); + const vector<float>& invHi = InvertSpectr<256>(&PcmBufHi[channel][0]); windowMask |= (uint32_t)TransientDetectors.GetDetector(channel, 2).Detect(&invHi[0]) << 2; //std::cout << "trans: " << windowMask << std::endl; diff --git a/src/atrac1denc.h b/src/atrac1denc.h index 841e3e5..86b8546 100644 --- a/src/atrac1denc.h +++ b/src/atrac1denc.h @@ -44,8 +44,8 @@ class TAtrac1MDCT { NMDCT::TMIDCT<256> Midct256; NMDCT::TMIDCT<64> Midct64; public: - void IMdct(TFloat specs[512], const TBlockSize& mode, TFloat* low, TFloat* mid, TFloat* hi); - void Mdct(TFloat specs[512], TFloat* low, TFloat* mid, TFloat* hi, const TBlockSize& blockSize); + void IMdct(float specs[512], const TBlockSize& mode, float* low, float* mid, float* hi); + void Mdct(float specs[512], float* low, float* mid, float* hi, const TBlockSize& blockSize); TAtrac1MDCT() : Mdct512(1) , Mdct256(0.5) @@ -56,15 +56,15 @@ public: {} }; -class TAtrac1Encoder : public IProcessor<TFloat>, public TAtrac1MDCT { +class TAtrac1Encoder : public IProcessor, public TAtrac1MDCT { TCompressedOutputPtr Aea; const NAtrac1::TAtrac1EncodeSettings Settings; - TFloat PcmBufLow[2][256 + 16]; - TFloat PcmBufMid[2][256 + 16]; - TFloat PcmBufHi[2][512 + 16]; + float PcmBufLow[2][256 + 16]; + float PcmBufMid[2][256 + 16]; + float PcmBufHi[2][512 + 16]; - Atrac1AnalysisFilterBank<TFloat> AnalysisFilterBank[2]; + Atrac1AnalysisFilterBank<float> AnalysisFilterBank[2]; const std::vector<float> LoudnessCurve; @@ -103,24 +103,24 @@ class TAtrac1Encoder : public IProcessor<TFloat>, public TAtrac1MDCT { public: TAtrac1Encoder(TCompressedOutputPtr&& aea, NAtrac1::TAtrac1EncodeSettings&& settings); - TPCMEngine<TFloat>::TProcessLambda GetLambda() override; + TPCMEngine::TProcessLambda GetLambda() override; }; -class TAtrac1Decoder : public IProcessor<TFloat>, public TAtrac1MDCT { +class TAtrac1Decoder : public IProcessor, public TAtrac1MDCT { TCompressedInputPtr Aea; const NAtrac1::TAtrac1EncodeSettings Settings; - TFloat PcmBufLow[2][256 + 16]; - TFloat PcmBufMid[2][256 + 16]; - TFloat PcmBufHi[2][512 + 16]; + float PcmBufLow[2][256 + 16]; + float PcmBufMid[2][256 + 16]; + float PcmBufHi[2][512 + 16]; int32_t PcmValueMax = 1; int32_t PcmValueMin = -1; - Atrac1SynthesisFilterBank<TFloat> SynthesisFilterBank[2]; + Atrac1SynthesisFilterBank<float> SynthesisFilterBank[2]; public: TAtrac1Decoder(TCompressedInputPtr&& aea); - TPCMEngine<TFloat>::TProcessLambda GetLambda() override; + TPCMEngine::TProcessLambda GetLambda() override; }; } diff --git a/src/atrac3denc.cpp b/src/atrac3denc.cpp index 39d1909..f49f1bd 100644 --- a/src/atrac3denc.cpp +++ b/src/atrac3denc.cpp @@ -29,26 +29,26 @@ using namespace NMDCT; using namespace NAtrac3; using std::vector; -void TAtrac3MDCT::Mdct(TFloat specs[1024], TFloat* bands[4], TFloat maxLevels[4], TGainModulatorArray gainModulators) +void TAtrac3MDCT::Mdct(float specs[1024], float* bands[4], float maxLevels[4], TGainModulatorArray gainModulators) { for (int band = 0; band < 4; ++band) { - TFloat* srcBuff = bands[band]; - TFloat* const curSpec = &specs[band*256]; + float* srcBuff = bands[band]; + float* const curSpec = &specs[band*256]; TGainModulator modFn = gainModulators[band]; - TFloat tmp[512]; - memcpy(&tmp[0], srcBuff, 256 * sizeof(TFloat)); + float tmp[512]; + memcpy(&tmp[0], srcBuff, 256 * sizeof(float)); if (modFn) { modFn(&tmp[0], &srcBuff[256]); } - TFloat max = 0.0; + float max = 0.0; for (int i = 0; i < 256; i++) { max = std::max(max, std::abs(srcBuff[256+i])); srcBuff[i] = TAtrac3Data::EncodeWindow[i] * srcBuff[256+i]; tmp[256+i] = TAtrac3Data::EncodeWindow[255-i] * srcBuff[256+i]; } - const vector<TFloat>& sp = Mdct512(&tmp[0]); + const vector<float>& sp = Mdct512(&tmp[0]); assert(sp.size() == 256); - memcpy(curSpec, sp.data(), 256 * sizeof(TFloat)); + memcpy(curSpec, sp.data(), 256 * sizeof(float)); if (band & 1) { SwapArray(curSpec, 256); } @@ -56,23 +56,23 @@ void TAtrac3MDCT::Mdct(TFloat specs[1024], TFloat* bands[4], TFloat maxLevels[4] } } -void TAtrac3MDCT::Mdct(TFloat specs[1024], TFloat* bands[4], TGainModulatorArray gainModulators) +void TAtrac3MDCT::Mdct(float specs[1024], float* bands[4], TGainModulatorArray gainModulators) { - static TFloat dummy[4]; + static float dummy[4]; Mdct(specs, bands, dummy, gainModulators); } -void TAtrac3MDCT::Midct(TFloat specs[1024], TFloat* bands[4], TGainDemodulatorArray gainDemodulators) +void TAtrac3MDCT::Midct(float specs[1024], float* bands[4], TGainDemodulatorArray gainDemodulators) { for (int band = 0; band < 4; ++band) { - TFloat* dstBuff = bands[band]; - TFloat* curSpec = &specs[band*256]; - TFloat* prevBuff = dstBuff + 256; + float* dstBuff = bands[band]; + float* curSpec = &specs[band*256]; + float* prevBuff = dstBuff + 256; TAtrac3GainProcessor::TGainDemodulator demodFn = gainDemodulators[band]; if (band & 1) { SwapArray(curSpec, 256); } - vector<TFloat> inv = Midct512(curSpec); + vector<float> inv = Midct512(curSpec); assert(inv.size()/2 == 256); for (int j = 0; j < 256; ++j) { inv[j] *= /*2 */ TAtrac3Data::DecodeWindow[j]; @@ -85,7 +85,7 @@ void TAtrac3MDCT::Midct(TFloat specs[1024], TFloat* bands[4], TGainDemodulatorAr dstBuff[j] = inv[j] + prevBuff[j]; } } - memcpy(prevBuff, &inv[256], sizeof(TFloat)*256); + memcpy(prevBuff, &inv[256], sizeof(float)*256); } } @@ -130,9 +130,9 @@ TAtrac3MDCT::TGainModulatorArray TAtrac3MDCT::MakeGainModulatorArray(const TAtra } } -TFloat TAtrac3Encoder::LimitRel(TFloat x) +float TAtrac3Encoder::LimitRel(float x) { - return std::min(std::max((double)x, TAtrac3Data::GainLevel[15]), TAtrac3Data::GainLevel[0]); + return std::min(std::max(x, TAtrac3Data::GainLevel[15]), TAtrac3Data::GainLevel[0]); } void TAtrac3Encoder::ResetTransientParamsHistory(int channel, int band) @@ -150,18 +150,18 @@ const TAtrac3Encoder::TTransientParam& TAtrac3Encoder::GetTransientParamsHistory return TransientParamsHistory[channel][band]; } -TAtrac3Encoder::TTransientParam TAtrac3Encoder::CalcTransientParam(const std::vector<TFloat>& gain, const TFloat lastMax) +TAtrac3Encoder::TTransientParam TAtrac3Encoder::CalcTransientParam(const std::vector<float>& gain, const float lastMax) { int32_t attack0Location = -1; // position where gain is risen up, -1 - no attack - TFloat attack0Relation = 1; + float attack0Relation = 1; - const TFloat attackThreshold = 2; + const float attackThreshold = 2; { // pre-echo searching // relative to previous half frame for (uint32_t i = 0; i < gain.size(); i++) { - const TFloat tmp = gain[i] / lastMax; + const float tmp = gain[i] / lastMax; if (tmp > attackThreshold) { attack0Relation = tmp; attack0Location = i; @@ -171,13 +171,13 @@ TAtrac3Encoder::TTransientParam TAtrac3Encoder::CalcTransientParam(const std::ve } int32_t attack1Location = -1; - TFloat attack1Relation = 1; + float attack1Relation = 1; { // pre-echo searching // relative to previous subsamples block - TFloat q = gain[0]; + float q = gain[0]; for (uint32_t i = 1; i < gain.size(); i++) { - const TFloat tmp = gain[i] / q; + const float tmp = gain[i] / q; if (tmp > attackThreshold) { attack1Relation = tmp; attack1Location = i; @@ -187,15 +187,15 @@ TAtrac3Encoder::TTransientParam TAtrac3Encoder::CalcTransientParam(const std::ve } int32_t releaseLocation = -1; // position where gain is fallen down, -1 - no release - TFloat releaseRelation = 1; + float releaseRelation = 1; - const TFloat releaseTreshold = 2; + const float releaseTreshold = 2; { // post-echo searching // relative to current frame - TFloat q = gain.back(); + float q = gain.back(); for (uint32_t i = gain.size() - 2; i > 0; --i) { - const TFloat tmp = gain[i] / q; + const float tmp = gain[i] / q; if (tmp > releaseTreshold) { releaseRelation = tmp; releaseLocation = i; @@ -208,24 +208,24 @@ TAtrac3Encoder::TTransientParam TAtrac3Encoder::CalcTransientParam(const std::ve return {attack0Location, attack0Relation, attack1Location, attack1Relation, releaseLocation, releaseRelation}; } -void TAtrac3Encoder::CreateSubbandInfo(TFloat* in[4], +void TAtrac3Encoder::CreateSubbandInfo(float* in[4], uint32_t channel, TAtrac3Data::SubbandInfo* subbandInfo) { - auto relToIdx = [](TFloat rel) { + auto relToIdx = [](float rel) { rel = 1.0/rel; return (uint32_t)(RelationToIdx(rel)); }; for (int band = 0; band < 4; ++band) { - const TFloat* srcBuff = in[band]; + const float* srcBuff = in[band]; - const TFloat* const lastMax = &PrevPeak[channel][band]; + const float* const lastMax = &PrevPeak[channel][band]; std::vector<TAtrac3Data::SubbandInfo::TGainPoint> curve; - const std::vector<TFloat> gain = AnalyzeGain(srcBuff, 256, 32, false); + const std::vector<float> gain = AnalyzeGain(srcBuff, 256, 32, false); auto transientParam = CalcTransientParam(gain, *lastMax); bool hasTransient = false; @@ -277,8 +277,8 @@ void TAtrac3Encoder::CreateSubbandInfo(TFloat* in[4], void TAtrac3Encoder::Matrixing() { for (uint32_t subband = 0; subband < 4; subband++) { - TFloat* pair[2] = {PcmBuffer.GetSecond(subband * 2), PcmBuffer.GetSecond(subband * 2 + 1)}; - TFloat tmp[2]; + float* pair[2] = {PcmBuffer.GetSecond(subband * 2), PcmBuffer.GetSecond(subband * 2 + 1)}; + float tmp[2]; for (uint32_t sample = 0; sample < 256; sample++) { tmp[0] = pair[0][sample]; tmp[1] = pair[1][sample]; @@ -288,7 +288,7 @@ void TAtrac3Encoder::Matrixing() } } -TPCMEngine<TFloat>::TProcessLambda TAtrac3Encoder::GetLambda() +TPCMEngine::TProcessLambda TAtrac3Encoder::GetLambda() { std::shared_ptr<TAtrac3BitStreamWriter> bitStreamWriter(new TAtrac3BitStreamWriter(Oma.get(), *Params.ConteinerParams, Params.BfuIdxConst)); @@ -297,24 +297,24 @@ TPCMEngine<TFloat>::TProcessLambda TAtrac3Encoder::GetLambda() : Specs(TAtrac3Data::NumSamples) {} - vector<TFloat> Specs; + vector<float> Specs; }; using TData = vector<TChannelData>; auto buf = std::make_shared<TData>(2); - return [this, bitStreamWriter, buf](TFloat* data, const TPCMEngine<TFloat>::ProcessMeta& meta) { + return [this, bitStreamWriter, buf](float* data, const TPCMEngine::ProcessMeta& meta) { using TSce = TAtrac3BitStreamWriter::TSingleChannelElement; for (uint32_t channel = 0; channel < meta.Channels; channel++) { - TFloat src[TAtrac3Data::NumSamples]; + float src[TAtrac3Data::NumSamples]; for (size_t i = 0; i < TAtrac3Data::NumSamples; ++i) { src[i] = data[i * meta.Channels + channel] / 4.0; } { - TFloat* p[4] = {PcmBuffer.GetSecond(channel), PcmBuffer.GetSecond(channel+2), PcmBuffer.GetSecond(channel+4), PcmBuffer.GetSecond(channel+6)}; + float* p[4] = {PcmBuffer.GetSecond(channel), PcmBuffer.GetSecond(channel+2), PcmBuffer.GetSecond(channel+4), PcmBuffer.GetSecond(channel+6)}; AnalysisFilterBank[channel].Analysis(&src[0], p); } } @@ -329,14 +329,14 @@ TPCMEngine<TFloat>::TProcessLambda TAtrac3Encoder::GetLambda() sce->SubbandInfo.Reset(); if (!Params.NoGainControll) { - TFloat* p[4] = {PcmBuffer.GetSecond(channel), PcmBuffer.GetSecond(channel+2), PcmBuffer.GetSecond(channel+4), PcmBuffer.GetSecond(channel+6)}; + float* p[4] = {PcmBuffer.GetSecond(channel), PcmBuffer.GetSecond(channel+2), PcmBuffer.GetSecond(channel+4), PcmBuffer.GetSecond(channel+6)}; CreateSubbandInfo(p, channel, &sce->SubbandInfo); //4 detectors per band } - TFloat* maxOverlapLevels = PrevPeak[channel]; + float* maxOverlapLevels = PrevPeak[channel]; { - TFloat* p[4] = {PcmBuffer.GetFirst(channel), PcmBuffer.GetFirst(channel+2), PcmBuffer.GetFirst(channel+4), PcmBuffer.GetFirst(channel+6)}; + float* p[4] = {PcmBuffer.GetFirst(channel), PcmBuffer.GetFirst(channel+2), PcmBuffer.GetFirst(channel+4), PcmBuffer.GetFirst(channel+6)}; Mdct(specs.data(), p, maxOverlapLevels, MakeGainModulatorArray(sce->SubbandInfo)); } diff --git a/src/atrac3denc.h b/src/atrac3denc.h index 97f6797..fb06f15 100644 --- a/src/atrac3denc.h +++ b/src/atrac3denc.h @@ -39,12 +39,12 @@ namespace NAtracDEnc { /////////////////////////////////////////////////////////////////////////////// -inline uint16_t RelationToIdx(TFloat x) { +inline uint16_t RelationToIdx(float x) { if (x <= 0.5) { - x = 1.0 / std::max(x, (TFloat)0.00048828125); + x = 1.0 / std::max(x, (float)0.00048828125); return 4 + GetFirstSetBit((int32_t)std::trunc(x)); } else { - x = std::min(x, (TFloat)16.0); + x = std::min(x, (float)16.0); return 4 - GetFirstSetBit((int32_t)std::trunc(x)); } } @@ -66,41 +66,41 @@ public: using TGainDemodulator = TAtrac3GainProcessor::TGainDemodulator; typedef std::array<TGainDemodulator, 4> TGainDemodulatorArray; typedef std::array<TGainModulator, 4> TGainModulatorArray; - void Mdct(TFloat specs[1024], - TFloat* bands[4], - TFloat maxLevels[4], + void Mdct(float specs[1024], + float* bands[4], + float maxLevels[4], TGainModulatorArray gainModulators); - void Mdct(TFloat specs[1024], - TFloat* bands[4], + void Mdct(float specs[1024], + float* bands[4], TGainModulatorArray gainModulators = TGainModulatorArray()); - void Midct(TFloat specs[1024], - TFloat* bands[4], + void Midct(float specs[1024], + float* bands[4], TGainDemodulatorArray gainDemodulators = TGainDemodulatorArray()); protected: TAtrac3MDCT::TGainModulatorArray MakeGainModulatorArray(const TAtrac3Data::SubbandInfo& si); }; -class TAtrac3Encoder : public IProcessor<TFloat>, public TAtrac3MDCT { +class TAtrac3Encoder : public IProcessor, public TAtrac3MDCT { using TAtrac3Data = NAtrac3::TAtrac3Data; TCompressedOutputPtr Oma; const NAtrac3::TAtrac3EncoderSettings Params; const std::vector<float> LoudnessCurve; - TDelayBuffer<TFloat, 8, 256> PcmBuffer; //8 = 2 channels * 4 bands + TDelayBuffer<float, 8, 256> PcmBuffer; //8 = 2 channels * 4 bands - TFloat PrevPeak[2][4]; //2 channel, 4 band - peak level (after windowing), used to check overflow during scalling + float PrevPeak[2][4]; //2 channel, 4 band - peak level (after windowing), used to check overflow during scalling - Atrac3AnalysisFilterBank<TFloat> AnalysisFilterBank[2]; + Atrac3AnalysisFilterBank<float> AnalysisFilterBank[2]; TScaler<TAtrac3Data> Scaler; std::vector<NAtrac3::TAtrac3BitStreamWriter::TSingleChannelElement> SingleChannelElements; public: struct TTransientParam { int32_t Attack0Location; // Attack position relative to previous frame - TFloat Attack0Relation; + float Attack0Relation; int32_t Attack1Location; // Attack position relative to previous sample - TFloat Attack1Relation; + float Attack1Relation; int32_t ReleaseLocation; - TFloat ReleaseRelation; + float ReleaseRelation; }; private: std::vector<std::vector<TTransientParam>> TransientParamsHistory; @@ -109,9 +109,9 @@ private: #ifdef ATRAC_UT_PUBLIC public: #endif - TFloat LimitRel(TFloat x); - TTransientParam CalcTransientParam(const std::vector<TFloat>& gain, TFloat lastMax); - void CreateSubbandInfo(TFloat* in[4], uint32_t channel, + float LimitRel(float x); + TTransientParam CalcTransientParam(const std::vector<float>& gain, float lastMax); + void CreateSubbandInfo(float* in[4], uint32_t channel, TAtrac3Data::SubbandInfo* subbandInfo); void ResetTransientParamsHistory(int channel, int band); void SetTransientParamsHistory(int channel, int band, const TTransientParam& params); @@ -121,6 +121,6 @@ public: public: TAtrac3Encoder(TCompressedOutputPtr&& oma, NAtrac3::TAtrac3EncoderSettings&& encoderSettings); ~TAtrac3Encoder(); - TPCMEngine<TFloat>::TProcessLambda GetLambda() override; + TPCMEngine::TProcessLambda GetLambda() override; }; } diff --git a/src/atrac3denc_ut.cpp b/src/atrac3denc_ut.cpp index 8578736..6979c92 100644 --- a/src/atrac3denc_ut.cpp +++ b/src/atrac3denc_ut.cpp @@ -27,14 +27,14 @@ using std::vector; using namespace NAtracDEnc; using namespace NAtrac3; -static void GenerateSignal(TFloat* buf, size_t n, TFloat f, TFloat a) { +static void GenerateSignal(float* buf, size_t n, float f, float a) { for (size_t i = 0; i < n; ++i) { buf[i] = a * sin((M_PI/2) * i * f); } } -static void GenerateSignalWithTransient(TFloat* buf, size_t n, TFloat f, TFloat a, - size_t transientPos, size_t transientLen, TFloat transientLev) { +static void GenerateSignalWithTransient(float* buf, size_t n, float f, float a, + size_t transientPos, size_t transientLen, float transientLev) { assert(transientPos + transientLen < n); GenerateSignal(buf, n, f, a); GenerateSignal(buf+transientPos, transientLen, f, transientLev); @@ -47,11 +47,11 @@ class TWindowTest { public: void RunTest() { for (size_t i = 0; i < 256; i++) { - const TFloat ha1 = TAtrac3Data::EncodeWindow[i] / 2.0; //compensation - const TFloat hs1 = TAtrac3Data::DecodeWindow[i]; - const TFloat hs2 = TAtrac3Data::DecodeWindow[255-i]; - const TFloat res = hs1 / (hs1 * hs1 + hs2 * hs2); - EXPECT_NEAR(ha1, res, 0.000000001); + const float ha1 = TAtrac3Data::EncodeWindow[i] / 2.0; //compensation + const double hs1 = TAtrac3Data::DecodeWindow[i]; + const double hs2 = TAtrac3Data::DecodeWindow[255-i]; + const float res = hs1 / (hs1 * hs1 + hs2 * hs2); + EXPECT_NEAR(ha1, res, 1.0 / (1 << 24)); } } }; @@ -93,18 +93,18 @@ public: TEST(TAtrac3MDCT, TAtrac3MDCTZeroOneBlock) { TAtrac3MDCT mdct; - TAtrac3MDCTWorkBuff<TFloat> buff; - size_t workSz = TAtrac3MDCTWorkBuff<TFloat>::BandBuffSz; + TAtrac3MDCTWorkBuff<float> buff; + size_t workSz = TAtrac3MDCTWorkBuff<float>::BandBuffSz; - vector<TFloat> specs(1024); + vector<float> specs(1024); - TFloat* p[4] = { buff.Band0, buff.Band1, buff.Band2, buff.Band3 }; + float* p[4] = { buff.Band0, buff.Band1, buff.Band2, buff.Band3 }; mdct.Mdct(specs.data(), p); for(auto s: specs) EXPECT_NEAR(s, 0.0, 0.0000000001); - TFloat* t[4] = { buff.Band0Res, buff.Band1Res, buff.Band2Res, buff.Band3Res }; + float* t[4] = { buff.Band0Res, buff.Band1Res, buff.Band2Res, buff.Band3Res }; mdct.Midct(specs.data(), p); for(size_t i = 0; i < workSz; ++i) @@ -124,25 +124,25 @@ TEST(TAtrac3MDCT, TAtrac3MDCTZeroOneBlock) { /* TEST(TAtrac3MDCT, TAtrac3MDCTSignal) { TAtrac3MDCT mdct; - TAtrac3MDCTWorkBuff<TFloat> buff; - size_t workSz = TAtrac3MDCTWorkBuff<TFloat>::BandBuffSz; + TAtrac3MDCTWorkBuff<float> buff; + size_t workSz = TAtrac3MDCTWorkBuff<float>::BandBuffSz; const size_t len = 1024; - vector<TFloat> signal(len); - vector<TFloat> signalRes(len); + vector<float> signal(len); + vector<float> signalRes(len); GenerateSignal(signal.data(), signal.size(), 0.25, 32768); for (size_t pos = 0; pos < len; pos += workSz) { - vector<TFloat> specs(1024); - memcpy(buff.Band0 + workSz, signal.data() + pos, workSz * sizeof(TFloat)); + vector<float> specs(1024); + memcpy(buff.Band0 + workSz, signal.data() + pos, workSz * sizeof(float)); - TFloat* p[4] = { buff.Band0, buff.Band1, buff.Band2, buff.Band3 }; + float* p[4] = { buff.Band0, buff.Band1, buff.Band2, buff.Band3 }; mdct.Mdct(specs.data(), p); - TFloat* t[4] = { buff.Band0Res, buff.Band1Res, buff.Band2Res, buff.Band3Res }; + float* t[4] = { buff.Band0Res, buff.Band1Res, buff.Band2Res, buff.Band3Res }; mdct.Midct(specs.data(), t); - memcpy(signalRes.data() + pos, buff.Band0Res, workSz * sizeof(TFloat)); + memcpy(signalRes.data() + pos, buff.Band0Res, workSz * sizeof(float)); } for (int i = workSz; i < len; ++i) @@ -151,19 +151,19 @@ TEST(TAtrac3MDCT, TAtrac3MDCTSignal) { TEST(TAtrac3MDCT, TAtrac3MDCTSignalWithGainCompensation) { TAtrac3MDCT mdct; - TAtrac3MDCTWorkBuff<TFloat> buff; - size_t workSz = TAtrac3MDCTWorkBuff<TFloat>::BandBuffSz; + TAtrac3MDCTWorkBuff<float> buff; + size_t workSz = TAtrac3MDCTWorkBuff<float>::BandBuffSz; const size_t len = 4096; - vector<TFloat> signal(len, 8000); - vector<TFloat> signalRes(len); + vector<float> signal(len, 8000); + vector<float> signalRes(len); GenerateSignal(signal.data() + 1024, signal.size()-1024, 0.25, 32768); for (size_t pos = 0; pos < len; pos += workSz) { - vector<TFloat> specs(1024); - memcpy(buff.Band0 + workSz, signal.data() + pos, workSz * sizeof(TFloat)); + vector<float> specs(1024); + memcpy(buff.Band0 + workSz, signal.data() + pos, workSz * sizeof(float)); - TFloat* p[4] = { buff.Band0, buff.Band1, buff.Band2, buff.Band3 }; + float* p[4] = { buff.Band0, buff.Band1, buff.Band2, buff.Band3 }; if (pos == 256) { //apply gain modulation TAtrac3Data::SubbandInfo siCur; @@ -195,7 +195,7 @@ TEST(TAtrac3MDCT, TAtrac3MDCTSignalWithGainCompensation) { mdct.Mdct(specs.data(), p); } - TFloat* t[4] = { buff.Band0Res, buff.Band1Res, buff.Band2Res, buff.Band3Res }; + float* t[4] = { buff.Band0Res, buff.Band1Res, buff.Band2Res, buff.Band3Res }; if (pos == 256) { //restore gain modulation TAtrac3Data::SubbandInfo siCur; @@ -268,7 +268,7 @@ TEST(TAtrac3MDCT, TAtrac3MDCTSignalWithGainCompensation) { } else { mdct.Midct(specs.data(), t); } - memcpy(signalRes.data() + pos, buff.Band0Res, workSz * sizeof(TFloat)); + memcpy(signalRes.data() + pos, buff.Band0Res, workSz * sizeof(float)); } for (int i = workSz; i < len; ++i) { //std::cout << "res: " << i << " " << signalRes[i] << std::endl; @@ -278,21 +278,21 @@ TEST(TAtrac3MDCT, TAtrac3MDCTSignalWithGainCompensation) { TEST(TAtrac3MDCT, TAtrac3MDCTSignalWithGainCompensationAndManualTransient) { TAtrac3MDCT mdct; - TAtrac3MDCTWorkBuff<TFloat> buff; - size_t workSz = TAtrac3MDCTWorkBuff<TFloat>::BandBuffSz; + TAtrac3MDCTWorkBuff<float> buff; + size_t workSz = TAtrac3MDCTWorkBuff<float>::BandBuffSz; const size_t len = 1024; - vector<TFloat> signal(len); - vector<TFloat> signalRes(len); + vector<float> signal(len); + vector<float> signalRes(len); GenerateSignalWithTransient(signal.data(), signal.size(), 0.03125, 512.0, 640, 64, 32768.0); const std::vector<TAtrac3Data::SubbandInfo::TGainPoint> curve1 = {{6, 13}, {4, 14}}; for (size_t pos = 0; pos < len; pos += workSz) { - vector<TFloat> specs(1024); - memcpy(buff.Band0 + workSz, signal.data() + pos, workSz * sizeof(TFloat)); + vector<float> specs(1024); + memcpy(buff.Band0 + workSz, signal.data() + pos, workSz * sizeof(float)); - TFloat* p[4] = { buff.Band0, buff.Band1, buff.Band2, buff.Band3 }; + float* p[4] = { buff.Band0, buff.Band1, buff.Band2, buff.Band3 }; //for (int i = 0; i < 256; i++) { // std::cout << i + pos << " " << buff.Band0[i] << std::endl; //} @@ -315,7 +315,7 @@ TEST(TAtrac3MDCT, TAtrac3MDCTSignalWithGainCompensationAndManualTransient) { if (i > 240 && i < 256) specs[i] /= 1.9; } - TFloat* t[4] = { buff.Band0Res, buff.Band1Res, buff.Band2Res, buff.Band3Res }; + float* t[4] = { buff.Band0Res, buff.Band1Res, buff.Band2Res, buff.Band3Res }; if (pos == 512) { //restore gain modulation TAtrac3Data::SubbandInfo siCur; TAtrac3Data::SubbandInfo siNext; @@ -337,7 +337,7 @@ TEST(TAtrac3MDCT, TAtrac3MDCTSignalWithGainCompensationAndManualTransient) { mdct.Midct(specs.data(), t); } - memcpy(signalRes.data() + pos, buff.Band0Res, workSz * sizeof(TFloat)); + memcpy(signalRes.data() + pos, buff.Band0Res, workSz * sizeof(float)); } for (int i = workSz; i < len; ++i) { //std::cout << "res: " << i << " " << signalRes[i] << std::endl; diff --git a/src/atracdenc_ut.cpp b/src/atracdenc_ut.cpp index efcd3df..416b972 100644 --- a/src/atracdenc_ut.cpp +++ b/src/atracdenc_ut.cpp @@ -24,7 +24,7 @@ using std::vector; using namespace NAtracDEnc; -void CheckResult128(const vector<TFloat>& a, const vector<TFloat>& b) { +void CheckResult128(const vector<float>& a, const vector<float>& b) { float m = 0.0; for (int i = 0; i < a.size(); i++) { m = fmax(m, (float)a[i]); @@ -37,7 +37,7 @@ void CheckResult128(const vector<TFloat>& a, const vector<TFloat>& b) { } } -void CheckResult256(const vector<TFloat>& a, const vector<TFloat>& b) { +void CheckResult256(const vector<float>& a, const vector<float>& b) { float m = 0.0; for (int i = 0; i < a.size(); i++) { m = fmax(m, (float)a[i]); @@ -53,14 +53,14 @@ void CheckResult256(const vector<TFloat>& a, const vector<TFloat>& b) { TEST(TAtrac1MDCT, TAtrac1MDCTLongEncDec) { TAtrac1MDCT mdct; - vector<TFloat> low(128 * 2); - vector<TFloat> mid(128 * 2); - vector<TFloat> hi(256 * 2); - vector<TFloat> specs(512 * 2); - - vector<TFloat> lowRes(128 * 2); - vector<TFloat> midRes(128 * 2); - vector<TFloat> hiRes(256 * 2); + vector<float> low(128 * 2); + vector<float> mid(128 * 2); + vector<float> hi(256 * 2); + vector<float> specs(512 * 2); + + vector<float> lowRes(128 * 2); + vector<float> midRes(128 * 2); + vector<float> hiRes(256 * 2); for (int i = 0; i < 128; i++) { low[i] = mid[i] = i; @@ -82,25 +82,25 @@ TEST(TAtrac1MDCT, TAtrac1MDCTLongEncDec) { TEST(TAtrac1MDCT, TAtrac1MDCTShortEncDec) { TAtrac1MDCT mdct; - vector<TFloat> low(128 * 2); - vector<TFloat> mid(128 * 2); - vector<TFloat> hi(256 * 2); - vector<TFloat> specs(512 * 2); - - vector<TFloat> lowRes(128 * 2); - vector<TFloat> midRes(128 * 2); - vector<TFloat> hiRes(256 * 2); + vector<float> low(128 * 2); + vector<float> mid(128 * 2); + vector<float> hi(256 * 2); + vector<float> specs(512 * 2); + + vector<float> lowRes(128 * 2); + vector<float> midRes(128 * 2); + vector<float> hiRes(256 * 2); for (int i = 0; i < 128; i++) { low[i] = mid[i] = i; } - const vector<TFloat> lowCopy = low; //in case of short wondow AtracMDCT changed input buffer during calculation - const vector<TFloat> midCopy = mid; + const vector<float> lowCopy = low; //in case of short wondow AtracMDCT changed input buffer during calculation + const vector<float> midCopy = mid; for (int i = 0; i < 256; i++) { hi[i] = i; } - const vector<TFloat> hiCopy = hi; + const vector<float> hiCopy = hi; const TBlockSize blockSize(true, true, true); //short diff --git a/src/config.h b/src/config.h index d3635df..901f584 100644 --- a/src/config.h +++ b/src/config.h @@ -22,13 +22,7 @@ #define NOMINMAX #endif -#ifdef ATDE_USE_FLOAT -# define kiss_fft_scalar float -typedef float TFloat; -#else -# define kiss_fft_scalar double -typedef double TFloat; -#endif +#define kiss_fft_scalar float #ifndef M_PI #define M_PI (3.14159265358979323846) diff --git a/src/gain_processor.h b/src/gain_processor.h index f43531f..c77536a 100644 --- a/src/gain_processor.h +++ b/src/gain_processor.h @@ -24,7 +24,7 @@ template<class T> class TGainProcessor : public T { public: - typedef std::function<void(TFloat* out, TFloat* cur, TFloat* prev)> TGainDemodulator; + typedef std::function<void(float* out, float* cur, float* prev)> TGainDemodulator; /* * example GainModulation: * PCMinput: @@ -41,13 +41,13 @@ public: * (i.e the input buffer started at b point) * so next transformation (mdct #3) gets modulated first part */ - typedef std::function<void(TFloat* bufCur, TFloat* bufNext)> TGainModulator; - static TFloat GetGainInc(uint32_t levelIdxCur) + typedef std::function<void(float* bufCur, float* bufNext)> TGainModulator; + static float GetGainInc(uint32_t levelIdxCur) { const int incPos = T::ExponentOffset - levelIdxCur + T::GainInterpolationPosShift; return T::GainInterpolation[incPos]; } - static TFloat GetGainInc(uint32_t levelIdxCur, uint32_t levelIdxNext) + static float GetGainInc(uint32_t levelIdxCur, uint32_t levelIdxNext) { const int incPos = levelIdxNext - levelIdxCur + T::GainInterpolationPosShift; return T::GainInterpolation[incPos]; @@ -57,17 +57,17 @@ public: TGainDemodulator Demodulate(const std::vector<typename T::SubbandInfo::TGainPoint>& giNow, const std::vector<typename T::SubbandInfo::TGainPoint>& giNext) { - return [=](TFloat* out, TFloat* cur, TFloat* prev) { + return [=](float* out, float* cur, float* prev) { uint32_t pos = 0; - const TFloat scale = giNext.size() ? T::GainLevel[giNext[0].Level] : 1; + const float scale = giNext.size() ? T::GainLevel[giNext[0].Level] : 1; for (uint32_t i = 0; i < giNow.size(); ++i) { uint32_t lastPos = giNow[i].Location << T::LocScale; const uint32_t levelPos = giNow[i].Level; assert(levelPos < sizeof(T::GainLevel)/sizeof(T::GainLevel[0])); - TFloat level = T::GainLevel[levelPos]; + float level = T::GainLevel[levelPos]; const int incPos = ((i + 1) < giNow.size() ? giNow[i + 1].Level : T::ExponentOffset) - giNow[i].Level + T::GainInterpolationPosShift; - TFloat gainInc = T::GainInterpolation[incPos]; + float gainInc = T::GainInterpolation[incPos]; for (; pos < lastPos; pos++) { //std::cout << "pos: " << pos << " scale: " << scale << " level: " << level << std::endl; out[pos] = (cur[pos] * scale + prev[pos]) * level; @@ -87,17 +87,17 @@ public: TGainModulator Modulate(const std::vector<typename T::SubbandInfo::TGainPoint>& giCur) { if (giCur.empty()) return {}; - return [=](TFloat* bufCur, TFloat* bufNext) { + return [=](float* bufCur, float* bufNext) { uint32_t pos = 0; - const TFloat scale = T::GainLevel[giCur[0].Level]; + const float scale = T::GainLevel[giCur[0].Level]; for (uint32_t i = 0; i < giCur.size(); ++i) { uint32_t lastPos = giCur[i].Location << T::LocScale; const uint32_t levelPos = giCur[i].Level; assert(levelPos < sizeof(T::GainLevel)/sizeof(T::GainLevel[0])); - TFloat level = T::GainLevel[levelPos]; + float level = T::GainLevel[levelPos]; const int incPos = ((i + 1) < giCur.size() ? giCur[i + 1].Level : T::ExponentOffset) - giCur[i].Level + T::GainInterpolationPosShift; - TFloat gainInc = T::GainInterpolation[incPos]; + float gainInc = T::GainInterpolation[incPos]; for (; pos < lastPos; pos++) { //std::cout << "mod pos: " << pos << " scale: " << scale << " bufCur: " << bufCur[pos] << " level: " << level << " bufNext: " << bufNext[pos] << std::endl; bufCur[pos] /= scale; diff --git a/src/lib/mdct/mdct.cpp b/src/lib/mdct/mdct.cpp index 74b6d91..c2872db 100644 --- a/src/lib/mdct/mdct.cpp +++ b/src/lib/mdct/mdct.cpp @@ -22,11 +22,11 @@ namespace NMDCT { -static std::vector<TFloat> CalcSinCos(size_t n, TFloat scale) +static std::vector<float> CalcSinCos(size_t n, float scale) { - std::vector<TFloat> tmp(n >> 1); - const TFloat alpha = 2.0 * M_PI / (8.0 * n); - const TFloat omiga = 2.0 * M_PI / n; + std::vector<float> tmp(n >> 1); + const float alpha = 2.0 * M_PI / (8.0 * n); + const float omiga = 2.0 * M_PI / n; scale = sqrt(scale/n); for (size_t i = 0; i < (n >> 2); ++i) { tmp[2 * i + 0] = scale * cos(omiga * i + alpha); @@ -35,7 +35,7 @@ static std::vector<TFloat> CalcSinCos(size_t n, TFloat scale) return tmp; } -TMDCTBase::TMDCTBase(size_t n, TFloat scale) +TMDCTBase::TMDCTBase(size_t n, float scale) : N(n) , SinCos(CalcSinCos(n, scale)) { diff --git a/src/lib/mdct/mdct.h b/src/lib/mdct/mdct.h index 988d1e9..114af30 100644 --- a/src/lib/mdct/mdct.h +++ b/src/lib/mdct/mdct.h @@ -25,21 +25,21 @@ namespace NMDCT { -static_assert(sizeof(kiss_fft_scalar) == sizeof(TFloat), "size of fft_scalar is not equal to size of TFloat"); +static_assert(sizeof(kiss_fft_scalar) == sizeof(float), "size of fft_scalar is not equal to size of float"); class TMDCTBase { protected: const size_t N; - const std::vector<TFloat> SinCos; + const std::vector<float> SinCos; kiss_fft_cpx* FFTIn; kiss_fft_cpx* FFTOut; kiss_fft_cfg FFTPlan; - TMDCTBase(size_t n, TFloat scale); + TMDCTBase(size_t n, float scale); virtual ~TMDCTBase(); }; -template<size_t TN, typename TIO = TFloat> +template<size_t TN, typename TIO = float> class TMDCT : public TMDCTBase { std::vector<TIO> Buf; public: @@ -54,15 +54,15 @@ public: const size_t n4 = N >> 2; const size_t n34 = 3 * n4; const size_t n54 = 5 * n4; - const TFloat* cos = &SinCos[0]; - const TFloat* sin = &SinCos[1]; + const float* cos = &SinCos[0]; + const float* sin = &SinCos[1]; - TFloat *xr, *xi, r0, i0; - TFloat c, s; + float *xr, *xi, r0, i0; + float c, s; size_t n; - xr = (TFloat*)FFTIn; - xi = (TFloat*)FFTIn + 1; + xr = (float*)FFTIn; + xi = (float*)FFTIn + 1; for (n = 0; n < n4; n += 2) { r0 = in[n34 - 1 - n] + in[n34 + n]; i0 = in[n4 + n] - in[n4 - 1 - n]; @@ -87,8 +87,8 @@ public: kiss_fft(FFTPlan, FFTIn, FFTOut); - xr = (TFloat*)FFTOut; - xi = (TFloat*)FFTOut + 1; + xr = (float*)FFTOut; + xi = (float*)FFTOut + 1; for (n = 0; n < n2; n += 2) { r0 = xr[n]; i0 = xi[n]; @@ -104,7 +104,7 @@ public: } }; -template<size_t TN, typename TIO = TFloat> +template<size_t TN, typename TIO = float> class TMIDCT : public TMDCTBase { std::vector<TIO> Buf; public: @@ -118,15 +118,15 @@ public: const size_t n4 = N >> 2; const size_t n34 = 3 * n4; const size_t n54 = 5 * n4; - const TFloat* cos = &SinCos[0]; - const TFloat* sin = &SinCos[1]; + const float* cos = &SinCos[0]; + const float* sin = &SinCos[1]; - TFloat *xr, *xi, r0, i0, r1, i1; - TFloat c, s; + float *xr, *xi, r0, i0, r1, i1; + float c, s; size_t n; - xr = (TFloat*)FFTIn; - xi = (TFloat*)FFTIn + 1; + xr = (float*)FFTIn; + xi = (float*)FFTIn + 1; for (n = 0; n < n2; n += 2) { r0 = in[n]; @@ -141,8 +141,8 @@ public: kiss_fft(FFTPlan, FFTIn, FFTOut); - xr = (TFloat*)FFTOut; - xi = (TFloat*)FFTOut + 1; + xr = (float*)FFTOut; + xi = (float*)FFTOut + 1; for (n = 0; n < n4; n += 2) { r0 = xr[n]; diff --git a/src/lib/mdct/mdct_ut.cpp b/src/lib/mdct/mdct_ut.cpp index 6f49b59..d997b08 100644 --- a/src/lib/mdct/mdct_ut.cpp +++ b/src/lib/mdct/mdct_ut.cpp @@ -26,24 +26,24 @@ using std::vector; using namespace NMDCT; -static vector<TFloat> mdct(TFloat* x, int N) { - vector<TFloat> res; +static vector<float> mdct(float* x, int N) { + vector<float> res; for (int k = 0; k < N; k++) { - TFloat sum = 0; + float sum = 0; for (int n = 0; n < 2 * N; n++) - sum += x[n]* cos((M_PI/N) * ((TFloat)n + 0.5 + N/2) * ((TFloat)k + 0.5)); + sum += x[n]* cos((M_PI/N) * ((float)n + 0.5 + N/2) * ((float)k + 0.5)); res.push_back(sum); } return res; } -static vector<TFloat> midct(TFloat* x, int N) { - vector<TFloat> res; +static vector<float> midct(float* x, int N) { + vector<float> res; for (int n = 0; n < 2 * N; n++) { - TFloat sum = 0; + float sum = 0; for (int k = 0; k < N; k++) - sum += (x[k] * cos((M_PI/N) * ((TFloat)n + 0.5 + N/2) * ((TFloat)k + 0.5))); + sum += (x[k] * cos((M_PI/N) * ((float)n + 0.5 + N/2) * ((float)k + 0.5))); res.push_back(sum); } @@ -53,12 +53,12 @@ static vector<TFloat> midct(TFloat* x, int N) { TEST(TMdctTest, MDCT32) { const int N = 32; TMDCT<N> transform(N); - vector<TFloat> src(N); + vector<float> src(N); for (int i = 0; i < N; i++) { src[i] = i; } - const vector<TFloat> res1 = mdct(&src[0], N/2); - const vector<TFloat> res2 = transform(&src[0]); + const vector<float> res1 = mdct(&src[0], N/2); + const vector<float> res2 = transform(&src[0]); EXPECT_EQ(res1.size(), res2.size()); auto eps = CalcEps(N); for (int i = 0; i < res1.size(); i++) { @@ -69,12 +69,12 @@ TEST(TMdctTest, MDCT32) { TEST(TMdctTest, MDCT64) { const int N = 64; TMDCT<N> transform(N); - vector<TFloat> src(N); + vector<float> src(N); for (int i = 0; i < N; i++) { src[i] = i; } - const vector<TFloat> res1 = mdct(&src[0], N/2); - const vector<TFloat> res2 = transform(&src[0]); + const vector<float> res1 = mdct(&src[0], N/2); + const vector<float> res2 = transform(&src[0]); EXPECT_EQ(res1.size(), res2.size()); auto eps = CalcEps(N); for (int i = 0; i < res1.size(); i++) { @@ -85,12 +85,12 @@ TEST(TMdctTest, MDCT64) { TEST(TMdctTest, MDCT128) { const int N = 128; TMDCT<N> transform(N); - vector<TFloat> src(N); + vector<float> src(N); for (int i = 0; i < N; i++) { src[i] = i; } - const vector<TFloat> res1 = mdct(&src[0], N/2); - const vector<TFloat> res2 = transform(&src[0]); + const vector<float> res1 = mdct(&src[0], N/2); + const vector<float> res2 = transform(&src[0]); EXPECT_EQ(res1.size(), res2.size()); auto eps = CalcEps(N * 4); for (int i = 0; i < res1.size(); i++) { @@ -101,12 +101,12 @@ TEST(TMdctTest, MDCT128) { TEST(TMdctTest, MDCT256) { const int N = 256; TMDCT<N> transform(N); - vector<TFloat> src(N); + vector<float> src(N); for (int i = 0; i < N; i++) { src[i] = i; } - const vector<TFloat> res1 = mdct(&src[0], N/2); - const vector<TFloat> res2 = transform(&src[0]); + const vector<float> res1 = mdct(&src[0], N/2); + const vector<float> res2 = transform(&src[0]); EXPECT_EQ(res1.size(), res2.size()); auto eps = CalcEps(N * 4); for (int i = 0; i < res1.size(); i++) { @@ -117,14 +117,14 @@ TEST(TMdctTest, MDCT256) { TEST(TMdctTest, MDCT256_RAND) { const int N = 256; TMDCT<N> transform(N); - vector<TFloat> src(N); - TFloat m = 0.0; + vector<float> src(N); + float m = 0.0; for (int i = 0; i < N; i++) { src[i] = rand(); m = std::max(m, src[i]); } - const vector<TFloat> res1 = mdct(&src[0], N/2); - const vector<TFloat> res2 = transform(&src[0]); + const vector<float> res1 = mdct(&src[0], N/2); + const vector<float> res2 = transform(&src[0]); EXPECT_EQ(res1.size(), res2.size()); auto eps = CalcEps(m * 8); for (int i = 0; i < res1.size(); i++) { @@ -135,12 +135,12 @@ TEST(TMdctTest, MDCT256_RAND) { TEST(TMdctTest, MIDCT32) { const int N = 32; TMIDCT<N> transform; - vector<TFloat> src(N); + vector<float> src(N); for (int i = 0; i < N/2; i++) { src[i] = i; } - const vector<TFloat> res1 = midct(&src[0], N/2); - const vector<TFloat> res2 = transform(&src[0]); + const vector<float> res1 = midct(&src[0], N/2); + const vector<float> res2 = transform(&src[0]); EXPECT_EQ(res1.size(), res2.size()); auto eps = CalcEps(N); for (int i = 0; i < N; i++) { @@ -151,12 +151,12 @@ TEST(TMdctTest, MIDCT32) { TEST(TMdctTest, MIDCT64) { const int N = 64; TMIDCT<N> transform; - vector<TFloat> src(N); + vector<float> src(N); for (int i = 0; i < N/2; i++) { src[i] = i; } - const vector<TFloat> res1 = midct(&src[0], N/2); - const vector<TFloat> res2 = transform(&src[0]); + const vector<float> res1 = midct(&src[0], N/2); + const vector<float> res2 = transform(&src[0]); EXPECT_EQ(res1.size(), res2.size()); auto eps = CalcEps(N); for (int i = 0; i < N; i++) { @@ -167,12 +167,12 @@ TEST(TMdctTest, MIDCT64) { TEST(TMdctTest, MIDCT128) { const int N = 128; TMIDCT<N> transform(N); - vector<TFloat> src(N); + vector<float> src(N); for (int i = 0; i < N/2; i++) { src[i] = i; } - const vector<TFloat> res1 = midct(&src[0], N/2); - const vector<TFloat> res2 = transform(&src[0]); + const vector<float> res1 = midct(&src[0], N/2); + const vector<float> res2 = transform(&src[0]); EXPECT_EQ(res1.size(), res2.size()); auto eps = CalcEps(N); for (int i = 0; i < N; i++) { @@ -183,12 +183,12 @@ TEST(TMdctTest, MIDCT128) { TEST(TMdctTest, MIDCT256) { const int N = 256; TMIDCT<N> transform(N); - vector<TFloat> src(N); + vector<float> src(N); for (int i = 0; i < N/2; i++) { src[i] = i; } - const vector<TFloat> res1 = midct(&src[0], N/2); - const vector<TFloat> res2 = transform(&src[0]); + const vector<float> res1 = midct(&src[0], N/2); + const vector<float> res2 = transform(&src[0]); EXPECT_EQ(res1.size(), res2.size()); auto eps = CalcEps(N * 2); for (int i = 0; i < N; i++) { @@ -199,14 +199,14 @@ TEST(TMdctTest, MIDCT256) { TEST(TMdctTest, MIDCT256_RAND) { const int N = 256; TMIDCT<N> transform(N); - vector<TFloat> src(N); - TFloat m = 0.0; + vector<float> src(N); + float m = 0.0; for (int i = 0; i < N/2; i++) { src[i] = rand(); m = std::max(m, src[i]); } - const vector<TFloat> res1 = midct(&src[0], N/2); - const vector<TFloat> res2 = transform(&src[0]); + const vector<float> res1 = midct(&src[0], N/2); + const vector<float> res2 = transform(&src[0]); EXPECT_EQ(res1.size(), res2.size()); auto eps = CalcEps(m * 4); for (int i = 0; i < N; i++) { diff --git a/src/lib/mdct/mdct_ut_common.h b/src/lib/mdct/mdct_ut_common.h index 5c1096c..060e2d0 100644 --- a/src/lib/mdct/mdct_ut_common.h +++ b/src/lib/mdct/mdct_ut_common.h @@ -21,8 +21,8 @@ #include "mdct.h" // Calculate value of error for given magnitude -inline TFloat CalcEps(TFloat magn) { - const float snr = (sizeof(TFloat) == 4) ? -114.0 : -240.0; +inline float CalcEps(float magn) { + const float snr = (sizeof(float) == 4) ? -114.0 : -240.0; return magn * pow(10, (snr / 20.0)); } diff --git a/src/main.cpp b/src/main.cpp index 4b40024..faf88e9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -46,8 +46,8 @@ using std::stoi; using namespace NAtracDEnc; -typedef std::unique_ptr<TPCMEngine<TFloat>> TPcmEnginePtr; -typedef std::unique_ptr<IProcessor<TFloat>> TAtracProcessorPtr; +typedef std::unique_ptr<TPCMEngine> TPcmEnginePtr; +typedef std::unique_ptr<IProcessor> TAtracProcessorPtr; static void printUsage(const char* myName, const string& err = string()) { @@ -145,9 +145,9 @@ static void PrepareAtrac1Encoder(const string& inFile, "the result will be incorrect" << std::endl; } TCompressedOutputPtr aeaIO = CreateAeaOutput(outFile, "test", numChannels, (uint32_t)numFrames); - pcmEngine->reset(new TPCMEngine<TFloat>(4096, + pcmEngine->reset(new TPCMEngine(4096, numChannels, - TPCMEngine<TFloat>::TReaderPtr((*wavIO)->GetPCMReader<TFloat>()))); + TPCMEngine::TReaderPtr((*wavIO)->GetPCMReader()))); if (!noStdOut) cout << "Input\n Filename: " << inFile << "\n Channels: " << (int)numChannels @@ -177,9 +177,9 @@ static void PrepareAtrac1Decoder(const string& inFile, << "\n Codec: PCM" << endl; wavIO->reset(new TWav(outFile, aeaIO->GetChannelNum(), 44100)); - pcmEngine->reset(new TPCMEngine<TFloat>(4096, + pcmEngine->reset(new TPCMEngine(4096, aeaIO->GetChannelNum(), - TPCMEngine<TFloat>::TWriterPtr((*wavIO)->GetPCMWriter<TFloat>()))); + TPCMEngine::TWriterPtr((*wavIO)->GetPCMWriter()))); atracProcessor->reset(new TAtrac1Decoder(std::move(aeaIO))); } @@ -237,9 +237,9 @@ static void PrepareAtrac3Encoder(const string& inFile, << "\n Bitrate: " << encoderSettings.ConteinerParams->Bitrate << endl; - pcmEngine->reset(new TPCMEngine<TFloat>(4096, + pcmEngine->reset(new TPCMEngine(4096, numChannels, - TPCMEngine<TFloat>::TReaderPtr(wavIO->GetPCMReader<TFloat>()))); + TPCMEngine::TReaderPtr(wavIO->GetPCMReader()))); atracProcessor->reset(new TAtrac3Encoder(std::move(omaIO), std::move(encoderSettings))); } diff --git a/src/pcm_io_sndfile.cpp b/src/pcm_io_sndfile.cpp index 52737c0..4da29e9 100644 --- a/src/pcm_io_sndfile.cpp +++ b/src/pcm_io_sndfile.cpp @@ -62,10 +62,10 @@ public: size_t GetTotalSamples() const override { return File.frames(); } - size_t Read(TPCMBuffer<TFloat>& buf, size_t sz) override { + size_t Read(TPCMBuffer& buf, size_t sz) override { return File.readf(buf[0], sz); } - size_t Write(const TPCMBuffer<TFloat>& buf, size_t sz) override { + size_t Write(const TPCMBuffer& buf, size_t sz) override { return File.writef(buf[0], sz); } private: diff --git a/src/pcmengin.h b/src/pcmengin.h index 69e2899..d5d9644 100644 --- a/src/pcmengin.h +++ b/src/pcmengin.h @@ -47,9 +47,8 @@ class TEndOfRead : public std::exception { } }; -template <class T> class TPCMBuffer { - std::vector<T> Buf_; + std::vector<float> Buf_; size_t NumChannels; public: @@ -63,7 +62,7 @@ public: return Buf_.size() / NumChannels; } - T* operator[](size_t pos) { + float* operator[](size_t pos) { size_t rpos = pos * NumChannels; if (rpos >= Buf_.size()) { std::cerr << "attempt to access out of buffer pos: " << pos << std::endl; @@ -72,7 +71,7 @@ public: return &Buf_[rpos]; } - const T* operator[](size_t pos) const { + const float* operator[](size_t pos) const { size_t rpos = pos * NumChannels; if (rpos >= Buf_.size()) { std::cerr << "attempt to access out of buffer pos: " << pos << std::endl; @@ -91,32 +90,29 @@ public: } }; -template <class T> class IPCMWriter { public: - virtual void Write(const TPCMBuffer<T>& data , const uint32_t size) const = 0; + virtual void Write(const TPCMBuffer& data , const uint32_t size) const = 0; IPCMWriter() {}; virtual ~IPCMWriter() {}; }; -template <class T> class IPCMReader { public: - virtual void Read(TPCMBuffer<T>& data , const uint32_t size) const = 0; + virtual void Read(TPCMBuffer& data , const uint32_t size) const = 0; IPCMReader() {}; virtual ~IPCMReader() {}; }; -template<class T> class TPCMEngine { public: - typedef std::unique_ptr<IPCMWriter<T>> TWriterPtr; - typedef std::unique_ptr<IPCMReader<T>> TReaderPtr; + typedef std::unique_ptr<IPCMWriter> TWriterPtr; + typedef std::unique_ptr<IPCMReader> TReaderPtr; struct ProcessMeta { const uint16_t Channels; }; private: - TPCMBuffer<T> Buffer; + TPCMBuffer Buffer; TWriterPtr Writer; TReaderPtr Reader; uint64_t Processed = 0; @@ -141,7 +137,7 @@ public: , Reader(std::move(reader)) { } - typedef std::function<void(T* data, const ProcessMeta& meta)> TProcessLambda; + typedef std::function<void(float* data, const ProcessMeta& meta)> TProcessLambda; uint64_t ApplyProcess(size_t step, TProcessLambda lambda) { if (step > Buffer.Size()) { @@ -170,9 +166,8 @@ public: } }; -template<class T> class IProcessor { public: - virtual typename TPCMEngine<T>::TProcessLambda GetLambda() = 0; + virtual typename TPCMEngine::TProcessLambda GetLambda() = 0; virtual ~IProcessor() {} }; diff --git a/src/platform/win/pcm_io/mf/pcm_io_mf.cpp b/src/platform/win/pcm_io/mf/pcm_io_mf.cpp index 3f9ff3a..a4e550b 100644 --- a/src/platform/win/pcm_io/mf/pcm_io_mf.cpp +++ b/src/platform/win/pcm_io/mf/pcm_io_mf.cpp @@ -69,8 +69,8 @@ static std::wstring Utf8ToMultiByte(const std::string& in) { } // TODO: add dither, noise shape? -static inline int16_t FloatToInt16(TFloat in) { - return std::min((int)INT16_MAX, std::max((int)INT16_MIN, (int)lrint(in * (TFloat)INT16_MAX))); +static inline int16_t FloatToInt16(float in) { + return std::min((int)INT16_MAX, std::max((int)INT16_MIN, (int)lrint(in * (float)INT16_MAX))); } static HRESULT WriteToFile(HANDLE hFile, void* p, DWORD cb) { @@ -350,7 +350,7 @@ public: return static_cast<size_t>(totalSamples); } - size_t Read(TPCMBuffer<TFloat>& buf, size_t sz) override { + size_t Read(TPCMBuffer& buf, size_t sz) override { HRESULT hr = S_OK; const size_t sizeBytes = sz * BytesPerSample_; @@ -447,7 +447,7 @@ public: return curPos; } - size_t Write(const TPCMBuffer<TFloat>& buf, size_t sz) override { + size_t Write(const TPCMBuffer& buf, size_t sz) override { const size_t samples = ChannelsNum_ * sz; Buf_.resize(samples * 2); for (size_t i = 0; i < samples; i++) { @@ -480,4 +480,4 @@ IPCMProviderImpl* CreatePCMIOMFReadImpl(const std::string& path) { IPCMProviderImpl* CreatePCMIOMFWriteImpl(const std::string& path, int channels, int sampleRate) { return new TPCMIOMediaFoundationFile(path, channels, sampleRate); -}
\ No newline at end of file +} diff --git a/src/platform/win/pcm_io/pcm_io.cpp b/src/platform/win/pcm_io/pcm_io.cpp index bd266ea..452036d 100644 --- a/src/platform/win/pcm_io/pcm_io.cpp +++ b/src/platform/win/pcm_io/pcm_io.cpp @@ -26,28 +26,28 @@ #include <windows.h> -void ConvertToPcmBufferFromLE(const BYTE* audioData, TPCMBuffer<TFloat>& buf, size_t sz, size_t shift, size_t channelsNum) { +void ConvertToPcmBufferFromLE(const BYTE* audioData, TPCMBuffer& buf, size_t sz, size_t shift, size_t channelsNum) { if (channelsNum == 1) { for (size_t i = 0; i < sz; i++) { - *(buf[i + shift] + 0) = (*(int16_t*)(audioData + i * 2 + 0)) / (TFloat)32768.0; + *(buf[i + shift] + 0) = (*(int16_t*)(audioData + i * 2 + 0)) / (float)32768.0; } } else { for (size_t i = 0; i < sz; i++) { - *(buf[i + shift] + 0) = (*(int16_t*)(audioData + i * 4 + 0)) / (TFloat)32768.0; - *(buf[i + shift] + 1) = (*(int16_t*)(audioData + i * 4 + 2)) / (TFloat)32768.0; + *(buf[i + shift] + 0) = (*(int16_t*)(audioData + i * 4 + 0)) / (float)32768.0; + *(buf[i + shift] + 1) = (*(int16_t*)(audioData + i * 4 + 2)) / (float)32768.0; } } } -void ConvertToPcmBufferFromBE(const BYTE* audioData, TPCMBuffer<TFloat>& buf, size_t sz, size_t shift, size_t channelsNum) { +void ConvertToPcmBufferFromBE(const BYTE* audioData, TPCMBuffer& buf, size_t sz, size_t shift, size_t channelsNum) { if (channelsNum == 1) { for (size_t i = 0; i < sz; i++) { - *(buf[i + shift] + 0) = conv_ntoh((*(int16_t*)(audioData + i * 2 + 0))) / (TFloat)32768.0; + *(buf[i + shift] + 0) = conv_ntoh((*(int16_t*)(audioData + i * 2 + 0))) / (float)32768.0; } } else { for (size_t i = 0; i < sz; i++) { - *(buf[i + shift] + 0) = conv_ntoh((*(int16_t*)(audioData + i * 4 + 0))) / (TFloat)32768.0; - *(buf[i + shift] + 1) = conv_ntoh((*(int16_t*)(audioData + i * 4 + 2))) / (TFloat)32768.0; + *(buf[i + shift] + 0) = conv_ntoh((*(int16_t*)(audioData + i * 4 + 0))) / (float)32768.0; + *(buf[i + shift] + 1) = conv_ntoh((*(int16_t*)(audioData + i * 4 + 2))) / (float)32768.0; } } } diff --git a/src/platform/win/pcm_io/pcm_io_impl.h b/src/platform/win/pcm_io/pcm_io_impl.h index 117ebe0..6c7d840 100644 --- a/src/platform/win/pcm_io/pcm_io_impl.h +++ b/src/platform/win/pcm_io/pcm_io_impl.h @@ -22,5 +22,5 @@ #include <windows.h> -void ConvertToPcmBufferFromLE(const BYTE* audioData, TPCMBuffer<TFloat>& buf, size_t sz, size_t shift, size_t channelsNum); -void ConvertToPcmBufferFromBE(const BYTE* audioData, TPCMBuffer<TFloat>& buf, size_t sz, size_t shift, size_t channelsNum);
\ No newline at end of file +void ConvertToPcmBufferFromLE(const BYTE* audioData, TPCMBuffer& buf, size_t sz, size_t shift, size_t channelsNum); +void ConvertToPcmBufferFromBE(const BYTE* audioData, TPCMBuffer& buf, size_t sz, size_t shift, size_t channelsNum); diff --git a/src/platform/win/pcm_io/win32/pcm_io_win32.cpp b/src/platform/win/pcm_io/win32/pcm_io_win32.cpp index 7eee5b0..bd1849c 100644 --- a/src/platform/win/pcm_io/win32/pcm_io_win32.cpp +++ b/src/platform/win/pcm_io/win32/pcm_io_win32.cpp @@ -82,7 +82,7 @@ public: } } - size_t Read(TPCMBuffer<TFloat>& buf, size_t sz) override { + size_t Read(TPCMBuffer& buf, size_t sz) override { if (Finished_) return 0; @@ -108,7 +108,7 @@ public: return toConvert; } - size_t Write(const TPCMBuffer<TFloat>& buf, size_t sz) override { + size_t Write(const TPCMBuffer& buf, size_t sz) override { abort(); return 0; } diff --git a/src/qmf/qmf.h b/src/qmf/qmf.h index 2e71444..81977f3 100644 --- a/src/qmf/qmf.h +++ b/src/qmf/qmf.h @@ -24,10 +24,10 @@ template<class TPCM, int nIn> class TQmf { static const float TapHalf[24]; - TFloat QmfWindow[48]; + float QmfWindow[48]; TPCM PcmBuffer[nIn + 46]; - TFloat PcmBufferMerge[nIn + 46]; - TFloat DelayBuff[46]; + float PcmBufferMerge[nIn + 46]; + float DelayBuff[46]; public: TQmf() { const int sz = sizeof(QmfWindow)/sizeof(QmfWindow[0]); @@ -41,8 +41,8 @@ public: } } - void Analysis(TPCM* in, TFloat* lower, TFloat* upper) { - TFloat temp; + void Analysis(TPCM* in, float* lower, float* upper) { + float temp; for (size_t i = 0; i < 46; i++) PcmBuffer[i] = PcmBuffer[nIn + i]; @@ -61,9 +61,9 @@ public: } } - void Synthesis(TPCM* out, TFloat* lower, TFloat* upper) { - memcpy(&PcmBufferMerge[0], &DelayBuff[0], 46*sizeof(TFloat)); - TFloat* newPart = &PcmBufferMerge[46]; + void Synthesis(TPCM* out, float* lower, float* upper) { + memcpy(&PcmBufferMerge[0], &DelayBuff[0], 46*sizeof(float)); + float* newPart = &PcmBufferMerge[46]; for (int i = 0; i < nIn; i+=4) { newPart[i+0] = lower[i/2] + upper[i/2]; newPart[i+1] = lower[i/2] - upper[i/2]; @@ -71,10 +71,10 @@ public: newPart[i+3] = lower[i/2 + 1] - upper[i/2 + 1]; } - TFloat* winP = &PcmBufferMerge[0]; + float* winP = &PcmBufferMerge[0]; for (size_t j = nIn/2; j != 0; j--) { - TFloat s1 = 0; - TFloat s2 = 0; + float s1 = 0; + float s2 = 0; for (size_t i = 0; i < 48; i+=2) { s1 += winP[i] * QmfWindow[i]; s2 += winP[i+1] * QmfWindow[i+1]; @@ -84,7 +84,7 @@ public: winP += 2; out += 2; } - memcpy(&DelayBuff[0], &PcmBufferMerge[nIn], 46*sizeof(TFloat)); + memcpy(&DelayBuff[0], &PcmBufferMerge[nIn], 46*sizeof(float)); } }; diff --git a/src/transient_detector.cpp b/src/transient_detector.cpp index ca96cd4..f090104 100644 --- a/src/transient_detector.cpp +++ b/src/transient_detector.cpp @@ -26,8 +26,8 @@ namespace NAtracDEnc { using std::vector; -static TFloat calculateRMS(const TFloat* in, uint32_t n) { - TFloat s = 0; +static float calculateRMS(const float* in, uint32_t n) { + float s = 0; for (uint32_t i = 0; i < n; i++) { s += (in[i] * in[i]); } @@ -35,41 +35,41 @@ static TFloat calculateRMS(const TFloat* in, uint32_t n) { return sqrt(s); } -static TFloat calculatePeak(const TFloat* in, uint32_t n) { - TFloat s = 0; +static float calculatePeak(const float* in, uint32_t n) { + float s = 0; for (uint32_t i = 0; i < n; i++) { - TFloat absVal = std::abs(in[i]); + float absVal = std::abs(in[i]); if (absVal > s) s = absVal; } return s; } -void TTransientDetector::HPFilter(const TFloat* in, TFloat* out) { - static const TFloat fircoef[] = { +void TTransientDetector::HPFilter(const float* in, float* out) { + static const float fircoef[] = { -8.65163e-18 * 2.0, -0.00851586 * 2.0, -6.74764e-18 * 2.0, 0.0209036 * 2.0, -3.36639e-17 * 2.0, -0.0438162 * 2.0, -1.54175e-17 * 2.0, 0.0931738 * 2.0, -5.52212e-17 * 2.0, -0.313819 * 2.0 }; - memcpy(HPFBuffer.data() + PrevBufSz, in, BlockSz * sizeof(TFloat)); - const TFloat* inBuf = HPFBuffer.data(); + memcpy(HPFBuffer.data() + PrevBufSz, in, BlockSz * sizeof(float)); + const float* inBuf = HPFBuffer.data(); for (size_t i = 0; i < BlockSz; ++i) { - TFloat s = inBuf[i + 10]; - TFloat s2 = 0; + float s = inBuf[i + 10]; + float s2 = 0; for (size_t j = 0; j < ((FIRLen - 1) / 2) - 1 ; j += 2) { s += fircoef[j] * (inBuf[i + j] + inBuf[i + FIRLen - j]); s2 += fircoef[j + 1] * (inBuf[i + j + 1] + inBuf[i + FIRLen - j - 1]); } out[i] = (s + s2)/2; } - memcpy(HPFBuffer.data(), in + (BlockSz - PrevBufSz), PrevBufSz * sizeof(TFloat)); + memcpy(HPFBuffer.data(), in + (BlockSz - PrevBufSz), PrevBufSz * sizeof(float)); } -bool TTransientDetector::Detect(const TFloat* buf) { +bool TTransientDetector::Detect(const float* buf) { const uint16_t nBlocksToAnalize = NShortBlocks + 1; - TFloat* rmsPerShortBlock = reinterpret_cast<TFloat*>(alloca(sizeof(TFloat) * nBlocksToAnalize)); - std::vector<TFloat> filtered(BlockSz); + float* rmsPerShortBlock = reinterpret_cast<float*>(alloca(sizeof(float) * nBlocksToAnalize)); + std::vector<float> filtered(BlockSz); HPFilter(buf, filtered.data()); bool trans = false; rmsPerShortBlock[0] = LastEnergy; @@ -88,11 +88,11 @@ bool TTransientDetector::Detect(const TFloat* buf) { return trans; } -std::vector<TFloat> AnalyzeGain(const TFloat* in, const uint32_t len, const uint32_t maxPoints, bool useRms) { - vector<TFloat> res; +std::vector<float> AnalyzeGain(const float* in, const uint32_t len, const uint32_t maxPoints, bool useRms) { + vector<float> res; const uint32_t step = len / maxPoints; for (uint32_t pos = 0; pos < len; pos += step) { - TFloat rms = useRms ? calculateRMS(in + pos, step) : calculatePeak(in + pos, step); + float rms = useRms ? calculateRMS(in + pos, step) : calculatePeak(in + pos, step); res.emplace_back(rms); } return res; diff --git a/src/transient_detector.h b/src/transient_detector.h index 0f94cc6..241861b 100644 --- a/src/transient_detector.h +++ b/src/transient_detector.h @@ -31,9 +31,9 @@ class TTransientDetector { const uint16_t NShortBlocks; static const uint16_t PrevBufSz = 20; static const uint16_t FIRLen = 21; - void HPFilter(const TFloat* in, TFloat* out); - std::vector<TFloat> HPFBuffer; - TFloat LastEnergy = 0.0; + void HPFilter(const float* in, float* out); + std::vector<float> HPFBuffer; + float LastEnergy = 0.0; uint16_t LastTransientPos = 0; public: TTransientDetector(uint16_t shortSz, uint16_t blockSz) @@ -43,10 +43,10 @@ public: { HPFBuffer.resize(BlockSz + FIRLen); } - bool Detect(const TFloat* buf); + bool Detect(const float* buf); uint32_t GetLastTransientPos() const { return LastTransientPos; } }; -std::vector<TFloat> AnalyzeGain(const TFloat* in, uint32_t len, uint32_t maxPoints, bool useRms); +std::vector<float> AnalyzeGain(const float* in, uint32_t len, uint32_t maxPoints, bool useRms); } diff --git a/src/transient_detector_ut.cpp b/src/transient_detector_ut.cpp index e41d4ac..95495ae 100644 --- a/src/transient_detector_ut.cpp +++ b/src/transient_detector_ut.cpp @@ -26,7 +26,7 @@ using std::vector; using namespace NAtracDEnc; TEST(AnalyzeGain, AnalyzeGainSimple) { - TFloat in[256]; + float in[256]; for (int i = 0; i < 256; ++i) { if (i <= 24) { in[i] = 1.0; @@ -38,10 +38,10 @@ TEST(AnalyzeGain, AnalyzeGainSimple) { in[i] = 0.5; } } - vector<TFloat> res = AnalyzeGain(in, 256, 32, false); + vector<float> res = AnalyzeGain(in, 256, 32, false); EXPECT_EQ(res.size(), 32); -// for (TFloat v : res) +// for (float v : res) // std::cout << v << std::endl; for (int i = 0; i < 3; ++i) EXPECT_EQ(res[i], 1.0); @@ -44,15 +44,15 @@ inline void SwapArray(T* p, const size_t len) { } template<size_t N> -inline void InvertSpectrInPlase(TFloat* in) { +inline void InvertSpectrInPlase(float* in) { for (size_t i = 0; i < N; i+=2) in[i] *= -1; } template<size_t N> -inline std::vector<TFloat> InvertSpectr(const TFloat* in) { - std::vector<TFloat> buf(N); - std::memcpy(&buf[0], in, N * sizeof(TFloat)); +inline std::vector<float> InvertSpectr(const float* in) { + std::vector<float> buf(N); + std::memcpy(&buf[0], in, N * sizeof(float)); InvertSpectrInPlase<N>(&buf[0]); return buf; } diff --git a/src/util_ut.cpp b/src/util_ut.cpp index 9fb61c4..9028c1e 100644 --- a/src/util_ut.cpp +++ b/src/util_ut.cpp @@ -22,10 +22,10 @@ #include <vector> TEST(Util, SwapArrayTest) { - TFloat arr[8] = { 0, 1, 2, 3, 4, 5, 6, 7 }; + float arr[8] = { 0, 1, 2, 3, 4, 5, 6, 7 }; SwapArray(arr, 8); for (size_t i = 0; i < 8; ++i) { - EXPECT_NEAR((TFloat)i, arr[7-i], 0.000000000001); + EXPECT_NEAR((float)i, arr[7-i], 0.000000000001); } } @@ -43,10 +43,10 @@ TEST(Util, GetFirstSetBitTest) { TEST(Util, CalcEnergy) { - EXPECT_NEAR((TFloat)0.0, CalcEnergy(std::vector<TFloat>{0.0}), 0.000000000001); - EXPECT_NEAR((TFloat)1.0, CalcEnergy(std::vector<TFloat>{1.0}), 0.000000000001); - EXPECT_NEAR((TFloat)2.0, CalcEnergy(std::vector<TFloat>{1.0, 1.0}), 0.000000000001); - EXPECT_NEAR((TFloat)5.0, CalcEnergy(std::vector<TFloat>{2.0, 1.0}), 0.000000000001); - EXPECT_NEAR((TFloat)5.0, CalcEnergy(std::vector<TFloat>{1.0, 2.0}), 0.000000000001); - EXPECT_NEAR((TFloat)8.0, CalcEnergy(std::vector<TFloat>{2.0, 2.0}), 0.000000000001); + EXPECT_NEAR((float)0.0, CalcEnergy(std::vector<float>{0.0}), 0.000000000001); + EXPECT_NEAR((float)1.0, CalcEnergy(std::vector<float>{1.0}), 0.000000000001); + EXPECT_NEAR((float)2.0, CalcEnergy(std::vector<float>{1.0, 1.0}), 0.000000000001); + EXPECT_NEAR((float)5.0, CalcEnergy(std::vector<float>{2.0, 1.0}), 0.000000000001); + EXPECT_NEAR((float)5.0, CalcEnergy(std::vector<float>{1.0, 2.0}), 0.000000000001); + EXPECT_NEAR((float)8.0, CalcEnergy(std::vector<float>{2.0, 2.0}), 0.000000000001); } diff --git a/src/wav.cpp b/src/wav.cpp index c182dbc..f62bbb3 100644 --- a/src/wav.cpp +++ b/src/wav.cpp @@ -43,6 +43,31 @@ TWav::TWav(const std::string& path, size_t channels, size_t sampleRate) TWav::~TWav() { } +IPCMReader* TWav::GetPCMReader() const { + return new TWavPcmReader([this](TPCMBuffer& data, const uint32_t size) { + if (data.Channels() != Impl->GetChannelsNum()) + throw TWrongReadBuffer(); + + size_t read; + if ((read = Impl->Read(data, size)) != size) { + if (!read) + throw TNoDataToRead(); + + data.Zero(read, size - read); + } + }); +} + +IPCMWriter* TWav::GetPCMWriter() { + return new TWavPcmWriter([this](const TPCMBuffer& data, const uint32_t size) { + if (data.Channels() != Impl->GetChannelsNum()) + throw TWrongReadBuffer(); + if (Impl->Write(data, size) != size) { + fprintf(stderr, "can't write block\n"); + } + }); +} + uint64_t TWav::GetTotalSamples() const { return Impl->GetTotalSamples(); } @@ -31,28 +31,26 @@ class TFileAlreadyExists : public std::exception { class TNoDataToRead : public std::exception { }; -template<class T> -class TWavPcmReader : public IPCMReader<T> { +class TWavPcmReader : public IPCMReader { public: - typedef std::function<void(TPCMBuffer<T>& data, const uint32_t size)> TLambda; + typedef std::function<void(TPCMBuffer& data, const uint32_t size)> TLambda; TLambda Lambda; TWavPcmReader(TLambda lambda) : Lambda(lambda) {} - void Read(TPCMBuffer<T>& data , const uint32_t size) const override { + void Read(TPCMBuffer& data , const uint32_t size) const override { Lambda(data, size); } }; -template<class T> -class TWavPcmWriter : public IPCMWriter<T> { +class TWavPcmWriter : public IPCMWriter { public: - typedef std::function<void(const TPCMBuffer<T>& data, const uint32_t size)> TLambda; + typedef std::function<void(const TPCMBuffer& data, const uint32_t size)> TLambda; TLambda Lambda; TWavPcmWriter(TLambda lambda) : Lambda(lambda) {} - void Write(const TPCMBuffer<T>& data , const uint32_t size) const override { + void Write(const TPCMBuffer& data , const uint32_t size) const override { Lambda(data, size); } }; @@ -63,8 +61,8 @@ public: virtual size_t GetChannelsNum() const = 0; virtual size_t GetSampleRate() const = 0; virtual size_t GetTotalSamples() const = 0; - virtual size_t Read(TPCMBuffer<TFloat>& buf, size_t sz) = 0; - virtual size_t Write(const TPCMBuffer<TFloat>& buf, size_t sz) = 0; + virtual size_t Read(TPCMBuffer& buf, size_t sz) = 0; + virtual size_t Write(const TPCMBuffer& buf, size_t sz) = 0; }; //TODO: split for reader/writer @@ -82,40 +80,9 @@ public: size_t GetSampleRate() const; uint64_t GetTotalSamples() const; - template<class T> - IPCMReader<T>* GetPCMReader() const; + IPCMReader* GetPCMReader() const; - template<class T> - IPCMWriter<T>* GetPCMWriter(); + IPCMWriter* GetPCMWriter(); }; typedef std::unique_ptr<TWav> TWavPtr; - -template<class T> -IPCMReader<T>* TWav::GetPCMReader() const { - return new TWavPcmReader<T>([this](TPCMBuffer<T>& data, const uint32_t size) { - if (data.Channels() != Impl->GetChannelsNum()) - throw TWrongReadBuffer(); - - size_t read; - if ((read = Impl->Read(data, size)) != size) { - if (!read) - throw TNoDataToRead(); - - data.Zero(read, size - read); - } - }); -} - -template<class T> -IPCMWriter<T>* TWav::GetPCMWriter() { - return new TWavPcmWriter<T>([this](const TPCMBuffer<T>& data, const uint32_t size) { - if (data.Channels() != Impl->GetChannelsNum()) - throw TWrongReadBuffer(); - if (Impl->Write(data, size) != size) { - fprintf(stderr, "can't write block\n"); - } - }); -} - - |