Skip to main content

StreamingSession

Struct StreamingSession 

Source
pub struct StreamingSession { /* private fields */ }
Expand description

流式会话的便捷封装。

包装一个 Session,自动注册内部事件收集器,把 shim 的逐事件回调 缓冲为 Vec,使调用方无需手动管理回调 + 锁。典型用法(流式 ASR):

use audio_cpp::{Backend, Model, Registry, Request, RunMode, TaskKind};
use audio_cpp::session::StreamingSession;
let session = model.create_task_session(
    TaskKind::Asr, RunMode::Streaming, Backend::Cpu, 0, 1, None,
)?;
let mut stream = StreamingSession::from_session(session);

stream.start(Request::stream_asr().option("language", "auto"))?;
// 每块音频送入后,取出该块触发的全部事件(partial_text / is_final)
let samples: Vec<f32> = vec![0.0; 512];
let events = stream.push_audio(&samples, 16000, 1, 0)?;
for ev in &events {
    if let Some(t) = &ev.partial_text {
        println!("增量: {}", t.text);
    }
}
let result = stream.finish()?;

与底层 Session::process_audio 不同,push_audio 返回该块触发的 所有事件(经内部回调收集),而不是单个事件。不消费音频块的模型族 (如流式 TTS)可跳过 push_audio,直接 start → finish。

Implementations§

Source§

impl StreamingSession

Source

pub fn from_session(session: Session) -> Self

从现有会话包装(流式任务)。

Source

pub fn from_model( model: &Model, task: TaskKind, backend: Backend, device: i32, threads: i32, session_options: Option<&str>, ) -> Result<Self, Error>

从模型创建流式会话并包装(等价于 create_task_session(Streaming) + Self::from_session)。

§Errors

底层无法创建流式会话时返回对应 Error 变体。

Source

pub fn start<R: IntoRequest>(&self, request: R) -> Result<(), Error>

开始流式任务(等价于 Session::start)。

§Errors

请求序列化失败、路径含 NUL,或 C ABI 调用失败时返回对应 Error 变体。

Source

pub fn push_audio( &self, samples: &[f32], sample_rate: i32, channels: i32, start_sample: i64, ) -> Result<Vec<StreamEvent>, Error>

送入一段音频,返回该块触发的全部流事件。

等价于先清空内部缓冲、调用 Session::process_audio、再取出缓冲 中的全部事件。samples 为 float 采样,start_sample 为相对输入 流的起始采样点。

§Errors

C ABI 调用失败时返回对应 Error 变体。

Source

pub fn finish(&self) -> Result<TaskResult, Error>

结束流式会话,返回最终结果(等价于 Session::finish)。

§Errors

C ABI 调用失败时返回对应 Error 变体。

Source

pub fn reset(&self) -> Result<(), Error>

重置会话(等价于 Session::reset),并清空内部缓冲。

§Errors

C ABI 调用失败时返回对应 Error 变体。

Source

pub fn session(&self) -> &Session

底层会话引用。

注意:经此引用调用 Session::set_event_callback 会替换 StreamingSession 的内部事件收集器,导致 push_audio 收不到事件。 如需自定义回调,请直接使用 Session 而非本包装。

Source

pub fn backend(&self) -> Backend

创建时请求的计算后端(等价于 Session::backend)。

Source

pub fn session_mut(&mut self) -> &mut Session

底层会话可变引用。

Trait Implementations§

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.