MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / ReadPcmFile

Function ReadPcmFile

tensorflow/contrib/ffmpeg/default/ffmpeg_lib.cc:136–160  ·  view source on GitHub ↗

Reads a PCM file using signed little endian 16-bit encoding (s16le).

Source from the content-addressed store, hash-verified

134
135// Reads a PCM file using signed little endian 16-bit encoding (s16le).
136std::vector<float> ReadPcmFile(const string& filename) {
137 string raw_data;
138 TF_QCHECK_OK(ReadFileToString(Env::Default(), filename, &raw_data))
139 << "Could not read FFmpeg output file: " << filename;
140
141 std::vector<float> samples;
142 const int32 sample_count = raw_data.size() / sizeof(int16);
143 samples.reserve(sample_count);
144
145 for (int32 i = 0; i < sample_count; ++i) {
146 // Most of this is jumping through hoops in the standard to convert some
147 // bits into the right format. I hope that an optimizing compiler will
148 // remove almost all of this code.
149 char raw[2] = {raw_data[i * 2], raw_data[i * 2 + 1]};
150 if (!port::kLittleEndian) {
151 std::swap(raw[0], raw[1]);
152 }
153 int16 host_order;
154 ::memcpy(&host_order, raw, sizeof(host_order));
155 const double normalized =
156 static_cast<double>(host_order) / std::numeric_limits<int16>::max();
157 samples.push_back(normalized);
158 }
159 return samples;
160}
161
162template <typename UInt>
163string LittleEndianData(UInt data) {

Callers 1

ReadAudioFileFunction · 0.85

Calls 6

ReadFileToStringFunction · 0.85
DefaultFunction · 0.85
maxFunction · 0.50
sizeMethod · 0.45
reserveMethod · 0.45
push_backMethod · 0.45

Tested by

no test coverage detected