Upload Audio_Effects_SDK/samples/utils/wave_reader/waveReadWrite.hpp with huggingface_hub
Browse files
Audio_Effects_SDK/samples/utils/wave_reader/waveReadWrite.hpp
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/*
|
| 2 |
+
* Copyright (c) 2017-2022, NVIDIA Corporation. All rights reserved.
|
| 3 |
+
*
|
| 4 |
+
* NVIDIA Corporation and its licensors retain all intellectual property
|
| 5 |
+
* and proprietary rights in and to this software, related documentation
|
| 6 |
+
* and any modifications thereto. Any use, reproduction, disclosure or
|
| 7 |
+
* distribution of this software and related documentation without an express
|
| 8 |
+
* license agreement from NVIDIA Corporation is strictly prohibited.
|
| 9 |
+
*/
|
| 10 |
+
|
| 11 |
+
#pragma once
|
| 12 |
+
|
| 13 |
+
#include <stdint.h>
|
| 14 |
+
#include <string.h>
|
| 15 |
+
#include <assert.h>
|
| 16 |
+
|
| 17 |
+
#include <memory>
|
| 18 |
+
#include <string>
|
| 19 |
+
#include <vector>
|
| 20 |
+
|
| 21 |
+
#include "wave.hpp"
|
| 22 |
+
|
| 23 |
+
#define MAKEFOURCC(a, b, c, d) ( (uint32_t) (((d) << 24) | ((c) << 16) | ((b) << 8) | (a)) )
|
| 24 |
+
|
| 25 |
+
struct RiffHeader {
|
| 26 |
+
uint32_t chunkId;
|
| 27 |
+
uint32_t chunkSize;
|
| 28 |
+
uint32_t fileTag;
|
| 29 |
+
};
|
| 30 |
+
|
| 31 |
+
struct RiffChunk {
|
| 32 |
+
uint32_t chunkId;
|
| 33 |
+
uint32_t chunkSize;
|
| 34 |
+
};
|
| 35 |
+
|
| 36 |
+
struct WaveFileInfo {
|
| 37 |
+
waveFormat_ext wfx;
|
| 38 |
+
uint8_t* audioData;
|
| 39 |
+
uint32_t audioDataSize;
|
| 40 |
+
};
|
| 41 |
+
|
| 42 |
+
enum WaveFileFlags {
|
| 43 |
+
READ_WAVEFILE = 0,
|
| 44 |
+
WRITE_WAVEFILE = 1
|
| 45 |
+
};
|
| 46 |
+
|
| 47 |
+
class CWaveFileRead {
|
| 48 |
+
public:
|
| 49 |
+
explicit CWaveFileRead(std::string wavFile);
|
| 50 |
+
uint32_t GetSampleRate() const { return m_WaveFormatEx.nSamplesPerSec; }
|
| 51 |
+
uint32_t GetRawPCMDataSizeInBytes() const { return m_WaveDataSize; }
|
| 52 |
+
uint32_t GetNumSamples() const { return m_nNumSamples; }
|
| 53 |
+
uint32_t GetNumAlignedSamples() const { return m_NumAlignedSamples; }
|
| 54 |
+
uint8_t* GetRawPCMData() { return m_WaveData.get(); }
|
| 55 |
+
const float *GetFloatPCMData();
|
| 56 |
+
const float *GetFloatPCMDataAligned(int alignSamples);
|
| 57 |
+
waveFormat_ext& GetWaveFormat() { return m_WaveFormatEx; }
|
| 58 |
+
int GetBitsPerSample();
|
| 59 |
+
bool isValid() const { return validFile; }
|
| 60 |
+
std::vector<float>* GetFloatVector();
|
| 61 |
+
|
| 62 |
+
private:
|
| 63 |
+
const RiffChunk* FindChunk(const uint8_t* data, size_t sizeBytes, uint32_t fourcc);
|
| 64 |
+
int readPCM(const char* szFileName);
|
| 65 |
+
|
| 66 |
+
private:
|
| 67 |
+
std::string m_wavFile;
|
| 68 |
+
uint32_t m_nNumSamples;
|
| 69 |
+
bool validFile;
|
| 70 |
+
std::unique_ptr<uint8_t[]> m_WaveData;
|
| 71 |
+
std::vector<float> m_floatWaveData;
|
| 72 |
+
uint32_t m_WaveDataSize;
|
| 73 |
+
std::unique_ptr<float[]> m_floatWaveDataAligned;
|
| 74 |
+
waveFormat_ext m_WaveFormatEx;
|
| 75 |
+
uint32_t m_NumAlignedSamples;
|
| 76 |
+
};
|
| 77 |
+
|
| 78 |
+
class CWaveFileWrite {
|
| 79 |
+
public:
|
| 80 |
+
CWaveFileWrite(std::string wavFile, uint32_t samplesPerSec, uint32_t numChannels,
|
| 81 |
+
uint16_t bitsPerSample, bool isFloat);
|
| 82 |
+
~CWaveFileWrite();
|
| 83 |
+
bool initFile();
|
| 84 |
+
// can be called 'n' times.
|
| 85 |
+
bool writeChunk(const void *data, uint32_t len);
|
| 86 |
+
bool commitFile();
|
| 87 |
+
uint32_t getWrittenCount() { return m_cumulativeCount; }
|
| 88 |
+
std::string getFileName() { return m_wavFile; }
|
| 89 |
+
private:
|
| 90 |
+
bool m_validState = false;
|
| 91 |
+
std::string m_wavFile;
|
| 92 |
+
FILE *m_fp = nullptr;
|
| 93 |
+
uint32_t m_cumulativeCount = 0;
|
| 94 |
+
waveFormat_ext wfx;
|
| 95 |
+
bool m_commitDone = false;
|
| 96 |
+
};
|
| 97 |
+
|
| 98 |
+
bool ReadWavFile(const std::string& filename, uint32_t expected_sample_rate,
|
| 99 |
+
std::vector<float>** data, unsigned* original_num_samples,
|
| 100 |
+
std::vector<int>* file_end_offset, int align_samples = -1);
|