Skip to content

Commit

Permalink
fix: fix fatal error in set_timestamp api in Python binding (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunxilin authored Oct 15, 2024
1 parent d16dce7 commit d674568
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
7 changes: 2 additions & 5 deletions core/src/ten_runtime/binding/python/native/msg/audio_frame.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,10 @@ PyObject *ten_py_audio_frame_set_timestamp(PyObject *self, PyObject *args) {
"Invalid argument.");

int64_t timestamp = 0;

if (!PyLong_Check(args)) {
return NULL;
if (!PyArg_ParseTuple(args, "L", &timestamp)) {
return ten_py_raise_py_value_error_exception("Invalid timestamp.");
}

timestamp = PyLong_AsLongLong(args);

ten_audio_frame_set_timestamp(py_audio_frame->msg.c_msg, timestamp);

Py_RETURN_NONE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Licensed under the Apache License, Version 2.0, with certain conditions.
# Refer to the "LICENSE" file in the root directory for more information.
#
import time
from ten import (
Extension,
TenEnv,
Expand Down Expand Up @@ -32,6 +33,8 @@ def on_start(self, ten_env: TenEnv) -> None:
)
print("audio duration: ", len(self.audio))

self.start_time = int(time.time() * 1000)

ten_env.on_start_done()

def on_cmd(self, ten_env: TenEnv, cmd: Cmd) -> None:
Expand All @@ -50,6 +53,10 @@ def on_cmd(self, ten_env: TenEnv, cmd: Cmd) -> None:
audio_frame.set_sample_rate(16000)
audio_frame.set_number_of_channels(1)
audio_frame.set_samples_per_channel(160)

timestamp = int(time.time() * 1000)
audio_frame.set_timestamp(timestamp)

audio_frame.alloc_buf(len(pcm_data))
buf = audio_frame.lock_buf()
buf[: len(pcm_data)] = pcm_data
Expand All @@ -64,6 +71,11 @@ def on_audio_frame(self, ten_env: TenEnv, audio_frame: AudioFrame) -> None:
assert audio_frame.get_number_of_channels() == 1
assert audio_frame.get_samples_per_channel() == 160

timestamp = audio_frame.get_timestamp()

current_time = int(time.time() * 1000)
assert current_time >= timestamp >= self.start_time

buf = audio_frame.get_buf()

assert buf[: len(buf)] == self.audio[0:10].raw_data
Expand Down

0 comments on commit d674568

Please sign in to comment.