SameedAtif

SameedAtif

How to pass Audio from Phoenix.Channel to Membrane.Source?

I have a simple Phoenix.Channel that sends Audio to a Membrane Pipeline. How do I send the Audio from the Pipeline to the source?

the channel:

defmodule AudioTranscriptionWeb.AudioChannel do
  use Phoenix.Channel

  def join("audio:stream", _payload, socket) do
    {:ok, socket}
  end

  def handle_in("new_audio", %{"data" => audio_data}, socket) do
    # Send the audio data to the Membrane Pipeline
    AudioTranscription.StreamToFile.send_audio_data(audio_data)
    {:noreply, socket}
  end
end

Membrane Pipeline

defmodule AudioTranscription.StreamToFile do
  use Membrane.Pipeline

  alias Membrane.File.Sink

  @impl true
  def handle_init(_) do
    spec =
      child(AudioTranscription.Source)
      |> child(%Membrane.File.Sink{location: "output.wav"})

      {[spec: spec], %{}}
  end

  @impl true
  def notify_child(AudioTranscription.Source, data) do
    # How do I notify the Source
  end

  @impl true
  def send_audio_data(audio_data) do
    # Process the audio data
    notify_child(AudioTranscription.Source, audio_data) # not working
    # AudioTranscription.AudioSource.receive_audio_data(audio_data)
  end
end

Membrane.Source

defmodule AudioTranscription.Source do
  @moduledoc """
  Membrane element counting incoming buffers.

  Count of buffers divided by `divisor` (passed via `:input` pad options)
  is sent as a `{:counter, number}` notification once every `interval`
  (passed via element options).
  """
  use Membrane.Source

  alias Membrane.Buffer
  alias Membrane.RawAudio

  def_output_pad :output,
    availability: :always,
    flow_control: :manual,
    accepted_format: _any

  @impl true
  def handle_init(_) do
    {:ok, %{buffer: []}}
  end

  @impl true
  def handle_demand(:output, _size, _unit, _ctx, state) do
    {{:ok, buffer: state.buffer}, state}
  end

  @impl true
  def handle_playing(_ctx, state) do
    {{:ok, []}, state}
  end

  def handle_push_buffer(buffer, _ctx, state) do
    {:ok, state}
  end

  def receive_audio_data(audio_data) do
    send(self(), {:new_audio_data, audio_data})
  end

  @impl true
  def handle_info({:new_audio_data, audio_data}, _ctx, state) do
    buffer = %Buffer{payload: audio_data}
    {{:ok, buffer: {:output, buffer}}, state}
  end

  def handle_parent_notification(notification, _context, _state) do
    IO.inspect(notification)
  end
end

Or should I use something else to achieve this? Please help, thank you.

Most Liked

mat-hek

mat-hek

Membrane Core Team

For the record, this was resolved on our Discord

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New

We're in Beta

About us Mission Statement