defmodule MedicalTranscription.Audio.Utilities do @moduledoc """ Holds audio processing utility functions. """ def filter_silence(samples, state) do samples |> generate_chunks(state.vad_chunk_size) |> Enum.drop_while(&(calc_volume(&1) < state.vad_threshold)) |> Enum.join() end defp generate_chunks(samples, chunk_size) when byte_size(samples) >= 2 * chunk_size do <> = samples [chunk | generate_chunks(rest, chunk_size)] end defp generate_chunks(samples, _chunk_size) do [samples] end # Calculates audio volume based on standard deviation # of the samples defp calc_volume(chunk) do samples = for <>, do: sample samples_cnt = Enum.count(samples) samples_avg = Enum.sum(samples) / samples_cnt sum_mean_square = samples |> Enum.map(&((&1 - samples_avg) ** 2)) |> Enum.sum() :math.sqrt(sum_mean_square / samples_cnt) end end