Skip to content

Commit

Permalink
Handle case when audio converted does not return immediately
Browse files Browse the repository at this point in the history
  • Loading branch information
mickel8 committed Aug 8, 2024
1 parent de48673 commit d4854d6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions c_src/xav/reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ int reader_next_frame(struct Reader *reader) {
ret = avcodec_receive_frame(reader->c, reader->frame);

if (ret == 0) {
XAV_LOG_DEBUG("Successfully received frame");
frame_ready = 1;
} else if (ret == AVERROR_EOF) {
XAV_LOG_DEBUG("EOF");
Expand Down
9 changes: 7 additions & 2 deletions lib/reader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,16 @@ defmodule Xav.Reader do
Reads and decodes the next frame.
"""
@spec next_frame(t()) :: {:ok, Xav.Frame.t()} | {:error, :eof}
def next_frame(%__MODULE__{reader: reader}) do
case Xav.Reader.NIF.next_frame(reader) do
def next_frame(%__MODULE__{reader: ref} = reader) do
case Xav.Reader.NIF.next_frame(ref) do
{:ok, {data, format, width, height, pts}} ->
{:ok, Xav.Frame.new(data, format, width, height, pts)}

{:ok, {"", _format, _samples, _pts}} ->
# Sometimes, audio converter might not return data immediately.
# Hence, call until we succeed.
next_frame(reader)

{:ok, {data, format, samples, pts}} ->
{:ok, Xav.Frame.new(data, format, samples, pts)}

Expand Down
9 changes: 7 additions & 2 deletions test/reader_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,20 @@ defmodule Xav.ReaderTest do

test "speech to text" do
for {path, expected_output} <- [
# This file has been downloaded from https://audio-samples.github.io/
# Section: Samples from the model without biasing or priming.
{"./test/fixtures/stt/melnet_sample_0.mp3",
"""
My thought, I have nobody by a beauty and will as you poured. \
Mr. Rochester has served and that so don't find a simple and \
devoted aboud to what might in a\
"""},
{"./test/fixtures/stt/harvard.wav",
"""
The stale smell of old beer lingers. It takes heat to bring out the odor. \
A cold dip restores health in zest. A salt pickle tastes fine with ham. \
Tacos all pastora are my favorite. A zestful food is the hot cross bun.\
"""},
# This mp3 file results in an empty buffer after first conversion.
{"./test/fixtures/stt/harvard.mp3",
"""
The stale smell of old beer lingers. It takes heat to bring out the odor. \
A cold dip restores health in zest. A salt pickle tastes fine with ham. \
Expand Down

0 comments on commit d4854d6

Please sign in to comment.