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

Function encode_audio

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

Source from the content-addressed store, hash-verified

279}
280
281static void encode_audio(const std::string& audio_input_path, const std::string& encoder_model_path, std::vector<float>& encoded_audio, size_t num_threads, litert::Environment& env) {
282
283 std::vector<float> packed;
284 std::vector<float> left_ch_input;
285 std::vector<float> right_ch_input;
286
287 // Read input audio file
288 read_wav(audio_input_path, left_ch_input, right_ch_input);
289 fprintf(stderr, "Using %s as an audio input file...\n", audio_input_path.c_str());
290
291 auto encoder_options = get_litert_value(litert::Options::Create());
292 AUDIOGEN_CHECK(encoder_options.SetHardwareAccelerators(litert::HwAccelerators::kCpu));
293 auto& encoder_cpu_options = get_litert_value(encoder_options.GetCpuOptions());
294 AUDIOGEN_CHECK(encoder_cpu_options.SetNumThreads(static_cast<int>(num_threads)));
295 AUDIOGEN_CHECK(encoder_cpu_options.SetXNNPackFlags(k_xnnpack_flags_base | TFLITE_XNNPACK_DELEGATE_FLAG_FORCE_FP16));
296
297 auto autoencoder_encoder_model = get_litert_value(litert::CompiledModel::Create(env, encoder_model_path, encoder_options));
298
299 auto encoder_inputs = get_litert_value(autoencoder_encoder_model.CreateInputBuffers());
300 auto encoder_outputs = get_litert_value(autoencoder_encoder_model.CreateOutputBuffers());
301 AUDIOGEN_CHECK(encoder_inputs.size() >= 1);
302 AUDIOGEN_CHECK(encoder_outputs.size() >= 1);
303
304 auto encoder_in_type = get_litert_value(autoencoder_encoder_model.GetInputTensorType(0, 0));
305 auto encoder_out_type = get_litert_value(autoencoder_encoder_model.GetOutputTensorType(0, 0));
306
307 const size_t audio_input_dim0 = get_num_elems(encoder_in_type);
308
309 // Divided by 2 because we have two channels
310 AUDIOGEN_CHECK(left_ch_input.size() <= audio_input_dim0 / 2);
311 AUDIOGEN_CHECK(right_ch_input.size() <= audio_input_dim0 / 2);
312
313 // Resize if needed and fill with zero the newly added values
314 left_ch_input.resize(audio_input_dim0 / 2, 0);
315 right_ch_input.resize(audio_input_dim0 / 2, 0);
316
317 // Pack the data
318 {
319 auto encoder_in_lock_and_ptr = scoped_lock<float>(encoder_inputs[0],
320 litert::TensorBuffer::LockMode::kWrite);
321 auto* autoencoder_encoder_in_data = encoder_in_lock_and_ptr.second;
322 prepare_encoder_input(left_ch_input, right_ch_input, autoencoder_encoder_in_data, audio_input_dim0);
323 }
324
325 // Run the encoder
326 auto start_encoder = time_in_ms();
327 AUDIOGEN_CHECK(autoencoder_encoder_model.Run(encoder_inputs, encoder_outputs));
328 auto end_encoder = time_in_ms();
329
330 // Copy the output to the output buffer
331 const size_t encoder_output_num_elems = get_num_elems(encoder_out_type);
332 {
333 auto encoder_out_lock_and_ptr = scoped_lock<const float>(encoder_outputs[0],
334 litert::TensorBuffer::LockMode::kRead);
335 const auto* autoencoder_encoder_out_data = encoder_out_lock_and_ptr.second;
336 encoded_audio.resize(encoder_output_num_elems);
337 memcpy(encoded_audio.data(), autoencoder_encoder_out_data, encoder_output_num_elems * sizeof(float));
338 }

Callers 1

mainFunction · 0.85

Calls 6

read_wavFunction · 0.85
get_litert_valueFunction · 0.85
prepare_encoder_inputFunction · 0.85
get_num_elemsFunction · 0.70
time_in_msFunction · 0.70
RunMethod · 0.45

Tested by

no test coverage detected