MCPcopy Create free account
hub / github.com/Arm-Examples/ML-examples / save_as_wav

Function save_as_wav

kleidiai-examples/audiogen/app/audiogen.cpp:344–381  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

342}
343
344static void save_as_wav(const std::string& path, const float* left_ch, const float* right_ch, size_t buffer_sz) {
345
346 constexpr uint16_t audio_format = 3; // IEEE float
347
348 const int32_t byte_rate = k_audio_sr * k_audio_num_channels * (k_bits_per_sample / 8);
349 const int32_t block_align = k_audio_num_channels * (k_bits_per_sample / 8);
350 const int32_t data_chunk_sz = buffer_sz * 2 * sizeof(float);
351 const int32_t fmt_chunk_sz = 16;
352 const int32_t header_sz = 44;
353 const int32_t file_sz = header_sz + data_chunk_sz - 8;
354
355 std::ofstream out_file(path, std::ios::binary);
356
357 // Prepare the header
358 // RIFF header
359 out_file.write("RIFF", 4);
360 out_file.write(reinterpret_cast<const char*>(&file_sz), 4);
361 out_file.write("WAVE", 4);
362 out_file.write("fmt ", 4);
363 out_file.write(reinterpret_cast<const char*>(&fmt_chunk_sz), 4);
364 out_file.write(reinterpret_cast<const char*>(&audio_format), 2);
365 out_file.write(reinterpret_cast<const char*>(&k_audio_num_channels), 2);
366 out_file.write(reinterpret_cast<const char*>(&k_audio_sr), 4);
367 out_file.write(reinterpret_cast<const char*>(&byte_rate), 4);
368 out_file.write(reinterpret_cast<const char*>(&block_align), 2);
369 out_file.write(reinterpret_cast<const char*>(&k_bits_per_sample), 2);
370
371 // Store the data in interleaved format (L0, R0, L1, R1,....)
372 out_file.write("data", 4);
373 out_file.write(reinterpret_cast<const char*>(&data_chunk_sz), 4);
374
375 for (size_t i = 0; i < buffer_sz; ++i) {
376 out_file.write(reinterpret_cast<const char*>(&left_ch[i]), sizeof(float));
377 out_file.write(reinterpret_cast<const char*>(&right_ch[i]), sizeof(float));
378 }
379
380 out_file.close();
381}
382
383static void fill_random_norm_dist(float* buff, size_t buff_sz, size_t seed) {
384 std::random_device rd{};

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected