Creates the contents of a .wav file using pcm_s16le format (signed 16 bit little endian integers).
| 203 | // Creates the contents of a .wav file using pcm_s16le format (signed 16 bit |
| 204 | // little endian integers). |
| 205 | string BuildWavFile(int32 samples_per_second, int32 channel_count, |
| 206 | const std::vector<float>& samples) { |
| 207 | string data = WavHeader(samples_per_second, channel_count, samples); |
| 208 | data.reserve(data.size() + samples.size() * sizeof(int16)); |
| 209 | for (float value : samples) { |
| 210 | const int16 quantized = |
| 211 | static_cast<int16>(value * std::numeric_limits<int16>::max()); |
| 212 | char raw[2]; |
| 213 | ::memcpy(raw, &quantized, sizeof(int16)); |
| 214 | if (!port::kLittleEndian) { |
| 215 | std::swap(raw[0], raw[1]); |
| 216 | } |
| 217 | data.push_back(raw[0]); |
| 218 | data.push_back(raw[1]); |
| 219 | } |
| 220 | return data; |
| 221 | } |
| 222 | |
| 223 | Status ReadInfoFile(const string& filename, uint32* width, uint32* height, |
| 224 | uint32* frames) { |