Reads a PCM file using signed little endian 16-bit encoding (s16le).
| 134 | |
| 135 | // Reads a PCM file using signed little endian 16-bit encoding (s16le). |
| 136 | std::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 | |
| 162 | template <typename UInt> |
| 163 | string LittleEndianData(UInt data) { |
no test coverage detected