reconocimiento elixir desde cero

This commit is contained in:
2025-06-19 09:27:15 -03:00
parent ba9ecfcff4
commit ebf491bc1f
49 changed files with 2682 additions and 0 deletions

View File

@ -0,0 +1,36 @@
defmodule Recognition_VAD.Application do
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
@moduledoc false
use Application
@impl true
def start(_type, _args) do
children = [
Recognition_VADWeb.Telemetry,
{DNSCluster, query: Application.get_env(:recognition_VAD, :dns_cluster_query) || :ignore},
{Phoenix.PubSub, name: Recognition_VAD.PubSub},
Recognition_VAD.AudioProcessor,
# Start the Finch HTTP client for sending emails
{Finch, name: Recognition_VAD.Finch},
# Start a worker by calling: Recognition_VAD.Worker.start_link(arg)
# {Recognition_VAD.Worker, arg},
# Start to serve requests, typically the last entry
Recognition_VADWeb.Endpoint
]
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Recognition_VAD.Supervisor]
Supervisor.start_link(children, opts)
end
# Tell Phoenix to update the endpoint configuration
# whenever the application is updated.
@impl true
def config_change(changed, _new, removed) do
Recognition_VADWeb.Endpoint.config_change(changed, removed)
:ok
end
end

View File

@ -0,0 +1,32 @@
defmodule Recognition_VAD.AudioProcessor do
use GenServer
require Logger
def start_link(_opts) do
GenServer.start_link(__MODULE__, %{}, name: __MODULE__)
end
def init(_) do
{:ok, %{buffer: [], sample_rate: 0}}
end
def handle_cast({:chunk, binary_audio, sample_rate}, state) do
# 👇 Guardamos el chunk en el buffer
new_buffer = [binary_audio | state.buffer] |> Enum.take(100) # máximo 100 chunks
Logger.info("🟡 Recibido chunk de #{byte_size(binary_audio)} bytes a #{sample_rate} Hz")
{:noreply, %{state | buffer: new_buffer, sample_rate: sample_rate}}
end
def handle_cast(:save_wav, state) do
timestamp = DateTime.utc_now() |> DateTime.to_unix()
filename = "recording_#{timestamp}.wav"
Recognition_VAD.WavWriter.write_pcm_chunks_to_wav(state.buffer, state.sample_rate, filename)
Logger.info("💾 Guardado archivo: #{filename}")
{:noreply, state}
end
end

View File

@ -0,0 +1,3 @@
defmodule Recognition_VAD.Mailer do
use Swoosh.Mailer, otp_app: :recognition_VAD
end

View File

@ -0,0 +1,29 @@
defmodule Recognition_VAD.WavWriter do
def write_pcm_chunks_to_wav(chunks, sample_rate, path) do
data = IO.iodata_to_binary(Enum.reverse(chunks))
num_channels = 1
bits_per_sample = 16
byte_rate = sample_rate * num_channels * div(bits_per_sample, 8)
block_align = div(bits_per_sample * num_channels, 8)
header =
<<
"RIFF",
36 + byte_size(data)::little-size(32),
"WAVE",
"fmt ",
16::little-size(32),
1::little-size(16),
num_channels::little-size(16),
sample_rate::little-size(32),
byte_rate::little-size(32),
block_align::little-size(16),
bits_per_sample::little-size(16),
"data",
byte_size(data)::little-size(32)
>>
File.write!(path, header <> data)
end
end