Examples#
Each example below is a complete, runnable program. Set LIBRARY_PATH to the path of the .so file
inside your extracted application package directory before running.
The C examples are desktop Linux programs. The tarball ships each one in its examples/docs
directory, and the build commands run from the tarball’s top directory. To stay short, they use
session.h and asr_transcript.h from the examples/common directory.
The session helper loads the package as a shared library, reads the weights and the license from the filesystem, and blocks inside each call. If your target cannot do that, as on most embedded platforms, start from the event loop the helper wraps.
Live microphone#
Transcribes audio from a live microphone and displays the transcript in the terminal as speech is recognized. Each overwrite is applied in place, so the transcript refines on the same line as later revisions arrive.
Run with:
arecord -f S16_LE -c 1 -r 16000 -t raw -q | python microphone.py
import sys
from abr_sdk.asr import Asr, AsrChunk
LIBRARY_PATH = "/path/to/niagara-38m-live.en-linux-x86_64/libniagara_38m_live.so"
CHUNK_BYTES = 16000 * 2 // 10 # 100 ms of 16 kHz mono S16_LE audio
buf = bytearray()
def on_chunk(chunk: AsrChunk) -> None:
"""Apply the chunk to the transcript and redraw the line."""
chunk.update(buf)
print(f"\r{buf.decode('utf-8')}", end="", flush=True)
with Asr(LIBRARY_PATH) as asr:
print("Listening... press Ctrl+C to stop.", flush=True)
try:
while chunk := sys.stdin.buffer.read(CHUNK_BYTES):
asr.push(chunk, on_chunk=on_chunk)
except KeyboardInterrupt:
pass
asr.wait_for_completion()
print()
Build and run with:
cc -std=c99 -Iabr_sdk -Iexamples abr_sdk/*.c examples/common/*.c examples/docs/microphone.c -o microphone
arecord -f S16_LE -c 1 -r 16000 -t raw -q | ./microphone
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include "common/asr_transcript.h"
#include "common/session.h"
#define LIBRARY_PATH \
"/path/to/niagara-38m-live.en-linux-x86_64/libniagara_38m_live.so"
#define CHUNK_BYTES (16000 * 2 / 10) // 100 ms of 16 kHz mono S16_LE audio
static char text[ABR_ASR_TRANSCRIPT_MIN_SIZE];
static abr_asr_transcript_t transcript;
static abr_asr_printer_t printer;
static volatile sig_atomic_t interrupted;
static void on_sigint(int signum) {
(void)signum;
interrupted = 1;
}
/**
* Applies the chunk to the transcript and redraws the line.
*/
static void on_chunk(void *user_data, const uint8_t *data, size_t n_bytes) {
(void)user_data;
(void)n_bytes;
abr_asr_text_chunk_t chunk;
memcpy(&chunk, data, sizeof(chunk));
abr_asr_transcript_edit_t edit;
abr_asr_transcript_apply(&transcript, &chunk, &edit);
abr_asr_printer_show(&printer, &edit);
}
int main(void) {
abr_asr_transcript_init(&transcript, text, sizeof(text));
abr_asr_printer_init(&printer);
abr_session_t session;
if (abr_session_open(&session, LIBRARY_PATH, NULL, 0) != ABR_OK) {
return 1;
}
signal(SIGINT, on_sigint);
printf("Listening... press Ctrl+C to stop.\n");
uint8_t chunk[CHUNK_BYTES];
size_t n_bytes;
while (!interrupted &&
(n_bytes = fread(chunk, 1, sizeof(chunk), stdin)) > 0) {
abr_session_push(&session, chunk, n_bytes, on_chunk, NULL);
}
abr_session_wait_for_completion(&session, on_chunk, NULL);
abr_asr_printer_end_line(&printer);
abr_session_close(&session);
return session.status == ABR_OK ? 0 : 1;
}
Audio file via ffmpeg#
Transcribes any audio file by piping it through ffmpeg. ffmpeg handles format conversion, so
this works with WAV, MP3, FLAC, or any other format it supports.
Run with:
ffmpeg -loglevel quiet -i recording.wav -f s16le -ar 16000 -ac 1 - | python stream_file.py
import sys
from abr_sdk.asr import Asr, AsrTranscript
LIBRARY_PATH = "/path/to/niagara-38m-live.en-linux-x86_64/libniagara_38m_live.so"
CHUNK_BYTES = 16000 * 2 // 10 # 100 ms of 16 kHz mono S16_LE audio
transcript = AsrTranscript()
with Asr(LIBRARY_PATH) as asr:
while chunk := sys.stdin.buffer.read(CHUNK_BYTES):
asr.push(chunk, on_chunk=transcript.chunks.append)
asr.wait_for_completion()
print(transcript.text)
Build and run with:
cc -std=c99 -Iabr_sdk -Iexamples abr_sdk/*.c examples/common/*.c examples/docs/stream_file.c -o stream_file
ffmpeg -loglevel quiet -i recording.wav -f s16le -ar 16000 -ac 1 - | ./stream_file
#include <stdio.h>
#include "common/asr_transcript.h"
#include "common/session.h"
#define LIBRARY_PATH \
"/path/to/niagara-38m-live.en-linux-x86_64/libniagara_38m_live.so"
#define CHUNK_BYTES (16000 * 2 / 10) // 100 ms of 16 kHz mono S16_LE audio
static char text[1 << 20];
static abr_asr_transcript_t transcript;
int main(void) {
abr_asr_transcript_init(&transcript, text, sizeof(text));
abr_session_t session;
if (abr_session_open(&session, LIBRARY_PATH, NULL, 0) != ABR_OK) {
return 1;
}
uint8_t chunk[CHUNK_BYTES];
size_t n_bytes;
while ((n_bytes = fread(chunk, 1, sizeof(chunk), stdin)) > 0) {
abr_session_push(&session, chunk, n_bytes, abr_asr_transcript_on_output,
&transcript);
}
abr_session_wait_for_completion(&session, abr_asr_transcript_on_output,
&transcript);
abr_session_close(&session);
printf("%s\n", transcript.text);
return session.status == ABR_OK ? 0 : 1;
}
WAV file#
Reads a WAV file and streams it through the ASR pipeline in 100-millisecond chunks, after confirming the file is already in the required format. See Input format for format requirements.
The standard library wave module reads the header and the frames.
Run with:
python from_wav.py
import wave
from abr_sdk.asr import Asr, AsrTranscript
LIBRARY_PATH = "/path/to/niagara-38m-live.en-linux-x86_64/libniagara_38m_live.so"
CHUNK_FRAMES = 1600 # 100 ms at 16 kHz
transcript = AsrTranscript()
with wave.open("recording.wav", "rb") as wf:
assert wf.getsampwidth() == 2, "expected 16-bit PCM"
assert wf.getnchannels() == 1, "expected mono"
assert wf.getframerate() == 16000, "expected 16 kHz"
with Asr(LIBRARY_PATH) as asr:
while frames := wf.readframes(CHUNK_FRAMES):
asr.push(bytes(frames), on_chunk=transcript.chunks.append)
asr.wait_for_completion()
print(transcript.text)
The standard library has no WAV reader, so open_wav walks the file’s chunks itself: it checks the
format chunk and stops at the start of the data chunk, whose size bounds how much is streamed.
Build and run with:
cc -std=c99 -Iabr_sdk -Iexamples abr_sdk/*.c examples/common/*.c examples/docs/from_wav.c -o from_wav
./from_wav
#include <stdio.h>
#include <string.h>
#include "common/asr_transcript.h"
#include "common/session.h"
#define LIBRARY_PATH \
"/path/to/niagara-38m-live.en-linux-x86_64/libniagara_38m_live.so"
#define CHUNK_BYTES (16000 * 2 / 10) // 100 ms of 16 kHz mono S16_LE audio
static char text[1 << 20];
static abr_asr_transcript_t transcript;
static uint32_t le32(const uint8_t *p) {
return (uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 |
(uint32_t)p[3] << 24;
}
static uint16_t le16(const uint8_t *p) { return (uint16_t)(p[0] | p[1] << 8); }
/**
* Checks the format chunk and positions the file at the start of the samples.
*
* @return The number of sample bytes, or 0 if the file is not 16-bit mono PCM
* at 16 kHz.
*/
static uint32_t open_wav(FILE *file) {
uint8_t riff[12];
if (fread(riff, 1, sizeof(riff), file) != sizeof(riff) ||
memcmp(riff, "RIFF", 4) != 0 || memcmp(riff + 8, "WAVE", 4) != 0) {
return 0;
}
bool format_ok = false;
uint8_t header[8];
while (fread(header, 1, sizeof(header), file) == sizeof(header)) {
const uint32_t size = le32(header + 4);
if (memcmp(header, "fmt ", 4) == 0) {
uint8_t fmt[16];
if (size < sizeof(fmt) ||
fread(fmt, 1, sizeof(fmt), file) != sizeof(fmt)) {
return 0;
}
format_ok = le16(fmt) == 1 && // PCM
le16(fmt + 2) == 1 && // mono
le32(fmt + 4) == 16000 && // 16 kHz
le16(fmt + 14) == 16; // 16-bit
fseek(file, (long)(size - sizeof(fmt) + (size & 1)), SEEK_CUR);
} else if (memcmp(header, "data", 4) == 0) {
return format_ok ? size : 0;
} else {
fseek(file, (long)size + (size & 1), SEEK_CUR);
}
}
return 0;
}
int main(void) {
abr_asr_transcript_init(&transcript, text, sizeof(text));
FILE *file = fopen("recording.wav", "rb");
if (file == NULL) {
fprintf(stderr, "could not open recording.wav\n");
return 1;
}
uint32_t remaining = open_wav(file);
if (remaining == 0) {
fprintf(stderr, "expected a 16-bit mono 16 kHz WAV file\n");
fclose(file);
return 1;
}
abr_session_t session;
if (abr_session_open(&session, LIBRARY_PATH, NULL, 0) != ABR_OK) {
fclose(file);
return 1;
}
uint8_t chunk[CHUNK_BYTES];
while (remaining > 0) {
const size_t max_bytes =
remaining < sizeof(chunk) ? remaining : sizeof(chunk);
const size_t n_bytes = fread(chunk, 1, max_bytes, file);
abr_session_push(&session, chunk, n_bytes, abr_asr_transcript_on_output,
&transcript);
if (n_bytes < max_bytes) {
break;
}
remaining -= (uint32_t)n_bytes;
}
abr_session_wait_for_completion(&session, abr_asr_transcript_on_output,
&transcript);
abr_session_close(&session);
fclose(file);
printf("%s\n", transcript.text);
return session.status == ABR_OK ? 0 : 1;
}
WAV or FLAC file with soundfile#
Reads a WAV or FLAC file using the soundfile library. soundfile returns the file’s native sample
rate alongside the data, which is used here to size the chunks. The SDK expects 16,000 Hz audio and
does not resample: if the file is not at 16,000 Hz, resample it before passing.
Run with:
python from_soundfile.py
import soundfile as sf
from abr_sdk.asr import Asr, AsrTranscript
LIBRARY_PATH = "/path/to/niagara-38m-live.en-linux-x86_64/libniagara_38m_live.so"
data, sample_rate = sf.read("recording.wav", dtype="int16", always_2d=False)
assert data.ndim == 1, "expected mono audio"
CHUNK_FRAMES = sample_rate // 10 # 100 ms
transcript = AsrTranscript()
with Asr(LIBRARY_PATH) as asr:
for i in range(0, len(data), CHUNK_FRAMES):
asr.push(
data[i : i + CHUNK_FRAMES].tobytes(), on_chunk=transcript.chunks.append
)
asr.wait_for_completion()
print(transcript.text)
Language model feed#
Text already delivered can still be revised, so a downstream language model should receive the
final transcript. This example collects chunks into an AsrTranscriptAsrTranscript and sends
its text after wait_for_completion()abr_session_wait_for_completion() has flushed the last words.
This example handles a single turn and then exits. A voice assistant repeats these steps on the
same AsrAsr instance, ending each turn with its own end-of-speech detection or push-to-talk
key, and speaks each reply with Overview before listening again.
Run with:
arecord -f S16_LE -c 1 -r 16000 -t raw -q | python llm_feed.py
import sys
from abr_sdk.asr import Asr, AsrTranscript
LIBRARY_PATH = "/path/to/niagara-38m-live.en-linux-x86_64/libniagara_38m_live.so"
CHUNK_BYTES = 16000 * 2 // 10 # 100 ms of 16 kHz mono S16_LE audio
def send_to_llm(text: str) -> None:
"""Replace with your LLM handler."""
print(text)
transcript = AsrTranscript()
with Asr(LIBRARY_PATH) as asr:
try:
while chunk := sys.stdin.buffer.read(CHUNK_BYTES):
asr.push(chunk, on_chunk=transcript.chunks.append)
except KeyboardInterrupt:
pass
asr.wait_for_completion()
send_to_llm(transcript.text)
Build and run with:
cc -std=c99 -Iabr_sdk -Iexamples abr_sdk/*.c examples/common/*.c examples/docs/llm_feed.c -o llm_feed
arecord -f S16_LE -c 1 -r 16000 -t raw -q | ./llm_feed
#include <signal.h>
#include <stdio.h>
#include "common/asr_transcript.h"
#include "common/session.h"
#define LIBRARY_PATH \
"/path/to/niagara-38m-live.en-linux-x86_64/libniagara_38m_live.so"
#define CHUNK_BYTES (16000 * 2 / 10) // 100 ms of 16 kHz mono S16_LE audio
static char text[1 << 20];
static abr_asr_transcript_t transcript;
static volatile sig_atomic_t interrupted;
static void on_sigint(int signum) {
(void)signum;
interrupted = 1;
}
/**
* Replace with your LLM handler.
*/
static void send_to_llm(const char *turn) { printf("%s\n", turn); }
int main(void) {
abr_asr_transcript_init(&transcript, text, sizeof(text));
abr_session_t session;
if (abr_session_open(&session, LIBRARY_PATH, NULL, 0) != ABR_OK) {
return 1;
}
signal(SIGINT, on_sigint);
uint8_t chunk[CHUNK_BYTES];
size_t n_bytes;
while (!interrupted &&
(n_bytes = fread(chunk, 1, sizeof(chunk), stdin)) > 0) {
abr_session_push(&session, chunk, n_bytes, abr_asr_transcript_on_output,
&transcript);
}
abr_session_wait_for_completion(&session, abr_asr_transcript_on_output,
&transcript);
abr_session_close(&session);
send_to_llm(transcript.text);
return session.status == ABR_OK ? 0 : 1;
}
Subtitle display#
Redraws a single subtitle line as each revision arrives, so the line settles on the punctuated, spell-corrected text.
Run with:
arecord -f S16_LE -c 1 -r 16000 -t raw -q | python subtitle.py
import sys
from abr_sdk.asr import Asr, AsrChunk
LIBRARY_PATH = "/path/to/niagara-38m-live.en-linux-x86_64/libniagara_38m_live.so"
CHUNK_BYTES = 16000 * 2 // 10 # 100 ms of 16 kHz mono S16_LE audio
buf = bytearray()
def on_chunk(chunk: AsrChunk) -> None:
"""Redraw the subtitle line after each revision."""
chunk.update(buf)
print(buf.decode("utf-8"), end="\r", flush=True)
with Asr(LIBRARY_PATH) as asr:
try:
while chunk := sys.stdin.buffer.read(CHUNK_BYTES):
asr.push(chunk, on_chunk=on_chunk)
except KeyboardInterrupt:
pass
asr.wait_for_completion()
print()
Build and run with:
cc -std=c99 -Iabr_sdk -Iexamples abr_sdk/*.c examples/common/*.c examples/docs/subtitle.c -o subtitle
arecord -f S16_LE -c 1 -r 16000 -t raw -q | ./subtitle
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include "common/asr_transcript.h"
#include "common/session.h"
#define LIBRARY_PATH \
"/path/to/niagara-38m-live.en-linux-x86_64/libniagara_38m_live.so"
#define CHUNK_BYTES (16000 * 2 / 10) // 100 ms of 16 kHz mono S16_LE audio
static char text[ABR_ASR_TRANSCRIPT_MIN_SIZE];
static abr_asr_transcript_t transcript;
static volatile sig_atomic_t interrupted;
static void on_sigint(int signum) {
(void)signum;
interrupted = 1;
}
/**
* Redraws the subtitle line after each revision.
*/
static void on_chunk(void *user_data, const uint8_t *data, size_t n_bytes) {
(void)user_data;
(void)n_bytes;
abr_asr_text_chunk_t chunk;
memcpy(&chunk, data, sizeof(chunk));
abr_asr_transcript_apply(&transcript, &chunk, NULL);
printf("%s\r", transcript.text);
fflush(stdout);
}
int main(void) {
abr_asr_transcript_init(&transcript, text, sizeof(text));
abr_session_t session;
if (abr_session_open(&session, LIBRARY_PATH, NULL, 0) != ABR_OK) {
return 1;
}
signal(SIGINT, on_sigint);
uint8_t chunk[CHUNK_BYTES];
size_t n_bytes;
while (!interrupted &&
(n_bytes = fread(chunk, 1, sizeof(chunk), stdin)) > 0) {
abr_session_push(&session, chunk, n_bytes, on_chunk, NULL);
}
abr_session_wait_for_completion(&session, on_chunk, NULL);
abr_session_close(&session);
printf("\n");
return session.status == ABR_OK ? 0 : 1;
}
Terminal live display with rich#
Uses the rich library to display the full transcript in the terminal as it arrives, showing each
revision. The transcript visibly improves as the model output (ACCURATE by default) is overwritten
by post-processed text.
Run with:
arecord -f S16_LE -c 1 -r 16000 -t raw -q | python rich_display.py
import sys
from rich.live import Live
from rich.text import Text
from abr_sdk.asr import Asr, AsrChunk
LIBRARY_PATH = "/path/to/niagara-38m-live.en-linux-x86_64/libniagara_38m_live.so"
CHUNK_BYTES = 16000 * 2 // 10 # 100 ms of 16 kHz mono S16_LE audio
buf = bytearray()
def on_chunk(chunk: AsrChunk) -> None:
"""Apply the chunk to the transcript buffer."""
chunk.update(buf)
with Asr(LIBRARY_PATH) as asr, Live() as live:
try:
while chunk := sys.stdin.buffer.read(CHUNK_BYTES):
asr.push(chunk, on_chunk=on_chunk)
live.update(Text(buf.decode("utf-8")))
except KeyboardInterrupt:
pass
asr.wait_for_completion()
live.update(Text(buf.decode("utf-8")))
Benchmarking#
For benchmarking the model or batch-processing a collection of pre-loaded audio clips, push each complete clip and wait for completion to get its final transcript. The example sets ACCURATE mode explicitly, because accuracy benchmarks should run in ACCURATE mode and FAST is not faster for batch processing (see Benchmarking).
process()buf_push takes a complete clip as a single bytes object and returns the final
AsrTranscriptAsrTranscript. It blocks until the neural network has finished.
Run with:
python benchmark.py
from pathlib import Path
from abr_sdk.asr import Asr, AsrMode
LIBRARY_PATH = "/path/to/niagara-38m-live.en-linux-x86_64/libniagara_38m_live.so"
CLIP_PATHS = ["clip1.pcm", "clip2.pcm"]
# ACCURATE mode (the default, set explicitly here) is the right mode for
# accuracy benchmarks; FAST trades accuracy for latency and inflates WER/CER.
with Asr(LIBRARY_PATH, mode=AsrMode.ACCURATE) as asr:
for path in CLIP_PATHS:
transcript = asr.process(Path(path).read_bytes())
print(transcript.text)
Build and run with:
cc -std=c99 -Iabr_sdk -Iexamples abr_sdk/*.c examples/common/*.c examples/docs/benchmark.c -o benchmark
./benchmark
#include <stdio.h>
#include <stdlib.h>
#include "common/asr_transcript.h"
#include "common/session.h"
#define LIBRARY_PATH \
"/path/to/niagara-38m-live.en-linux-x86_64/libniagara_38m_live.so"
static const char *const CLIP_PATHS[] = {"clip1.pcm", "clip2.pcm"};
static char text[1 << 20];
static abr_asr_transcript_t transcript;
/**
* Reads a whole file into a buffer the caller frees.
*/
static uint8_t *read_file(const char *path, size_t *n_bytes) {
FILE *file = fopen(path, "rb");
if (file == NULL) {
return NULL;
}
fseek(file, 0, SEEK_END);
*n_bytes = (size_t)ftell(file);
fseek(file, 0, SEEK_SET);
uint8_t *data = malloc(*n_bytes);
if (data != NULL && fread(data, 1, *n_bytes, file) != *n_bytes) {
free(data);
data = NULL;
}
fclose(file);
return data;
}
int main(void) {
// Accurate mode (the default, set explicitly here) is the right mode for
// accuracy benchmarks; fast trades accuracy for latency and inflates
// WER/CER.
abr_config_t cfg;
cfg.key = "asr_mode";
cfg.value.type = ABR_VAR_STR;
cfg.value.value.str = "accurate";
abr_session_t session;
if (abr_session_open(&session, LIBRARY_PATH, &cfg, 1) != ABR_OK) {
return 1;
}
for (size_t i = 0; i < sizeof(CLIP_PATHS) / sizeof(CLIP_PATHS[0]); i++) {
size_t n_bytes;
uint8_t *clip = read_file(CLIP_PATHS[i], &n_bytes);
if (clip == NULL) {
fprintf(stderr, "cannot read %s\n", CLIP_PATHS[i]);
break;
}
abr_asr_transcript_init(&transcript, text, sizeof(text));
abr_session_push(&session, clip, n_bytes, abr_asr_transcript_on_output,
&transcript);
abr_session_wait_for_completion(&session, abr_asr_transcript_on_output,
&transcript);
free(clip);
printf("%s\n", transcript.text);
}
abr_session_close(&session);
return session.status == ABR_OK ? 0 : 1;
}
Each .pcm file must contain raw PCM audio in the required format:
little-endian 16-bit mono at 16,000 Hz. For batch workloads, process clip after clip on the same
application; each clip is independent.
Without the session helper#
The session helper wraps a short event loop over the binding’s own calls. Write that loop yourself
when the helper does not fit your program. This is the loop from the tarball’s examples/asr.c,
which reads audio from a file descriptor. It waits on an event set, pushes audio whenever the input
buffer has room, and pulls one text chunk whenever the output buffer holds one. ERR jumps to the
cleanup code on a failure, and the json branch prints the raw chunks.
abr_buf_t *buf_in = lib.app_get_buffer(app, 0);
abr_buf_t *buf_out = lib.app_get_buffer(app, 1);
abr_evset_t *evset = lib.evset_create(app);
abr_event_t *ev_in =
lib.event_create_buf_level_event(evset, buf_in, 128, 0);
abr_event_t *ev_out = lib.event_create_buf_level_event(
evset, buf_out, sizeof(abr_asr_text_chunk_t), 0);
while (!done) {
// Wait for any event to happen. Ctrl-C interrupts the wait and sets
// `done`, which ends the loop.
abr_status_t status = lib.evset_poll(evset, -1);
if (status == ABR_ERR_INTERRUPT) {
continue;
}
if (status != ABR_OK) {
abr_log(&log, ABR_LOG_ERROR, "Waiting for events failed: %s",
abr_status_str(status));
retval = 1;
goto fail;
}
// If the network is ready to accept data from stdin, then read data
// from stdin.
if (lib.event_is_enabled(ev_in) && lib.event_is_triggered(ev_in)) {
uint8_t buf[128];
ssize_t res = read(input_fd, buf, sizeof(buf));
if (res > 0) {
int32_t n_bytes_written = lib.buf_push(buf_in, buf, res, false);
if (n_bytes_written < 0) {
abr_log(&log, ABR_LOG_ERROR, "Writing the input failed: %s",
abr_status_str((abr_status_t)n_bytes_written));
retval = 1;
goto fail;
}
if (n_bytes_written != res) {
abr_log(&log, ABR_LOG_ERROR,
"Input buffer overflow: wrote %d of %zd bytes.",
n_bytes_written, res);
retval = 1;
goto fail;
}
abr_log(&log, ABR_LOG_DEBUG, "Wrote %d bytes to network input",
n_bytes_written);
} else {
// Done reading from stdin, append a "flush" to the input data.
lib.buf_push(buf_in, NULL, 0, true);
abr_log(&log, ABR_LOG_INFO,
"stdin is done. Waiting for remaining output data.");
lib.event_disable(ev_in);
}
}
// Print any output data.
if (lib.event_is_triggered(ev_out)) {
abr_asr_text_chunk_t chunk;
// A pull stops at a segment boundary, so an empty read is normal
// whenever the application ends a segment without emitting a
// chunk. Anything in between is a partially written chunk.
bool flush;
int32_t n_bytes_read =
lib.buf_pull(buf_out, (uint8_t *)&chunk,
sizeof(abr_asr_text_chunk_t), &flush);
if (n_bytes_read < 0) {
abr_log(&log, ABR_LOG_ERROR, "Reading the output failed: %s",
abr_status_str((abr_status_t)n_bytes_read));
retval = 1;
goto fail;
}
if ((n_bytes_read != 0) &&
((size_t)n_bytes_read != sizeof(abr_asr_text_chunk_t))) {
abr_log(&log, ABR_LOG_ERROR,
"Read a partial text chunk: %d of %zu bytes.",
n_bytes_read, sizeof(abr_asr_text_chunk_t));
retval = 1;
goto fail;
}
if ((n_bytes_read > 0) && !json) {
abr_asr_transcript_edit_t edit;
ERR(abr_asr_transcript_apply(&transcript, &chunk, &edit));
abr_asr_printer_show(&printer, &edit);
} else if (n_bytes_read > 0) {
abr_log(&log, ABR_LOG_DEBUG,
"Read %d bytes from the network output", n_bytes_read);
// Print the received text chunk to the terminal.
fprintf(stdout, "{\"type\": ");
switch (chunk.type) {
case ABR_ASR_TEXT_CHUNK_CAUSAL:
fprintf(stdout, "\"causal\"");
break;
case ABR_ASR_TEXT_CHUNK_NONCAUSAL:
fprintf(stdout, "\"noncausal\"");
break;
case ABR_ASR_TEXT_CHUNK_POSTPROC:
fprintf(stdout, "\"postproc\"");
break;
default:
fprintf(stdout, "null");
break;
}
fprintf(stdout, ", \"replace_byte_offs_begin\": %4d, ",
(int)chunk.replace_byte_offs_begin);
fprintf(stdout, "\"end\": %4d, \"data\": \"",
(int)chunk.replace_byte_offs_end);
fwrite(chunk.data, 1, chunk.n_bytes, stdout);
fprintf(stdout, "\"}\n");
fflush(stdout);
}
// We're done as soon as the "flush" flag appears on the other
// side.
if (flush) {
abr_log(&log, ABR_LOG_INFO, "All data processed. Exiting.");
break;
}
// Try reading more data from the output buffers.
continue;
}
}
On an embedded target without shared libraries or a filesystem, the loop stays the same and the setup around it changes:
Library: link the application library into your firmware and pass
NULLas the path toabr_lib_open().Weights: pass the
resources_addrconfiguration key toabr_lib.app_createin place ofresources_dir. It holds the address of the application data container in memory, such as its location in flash.License: pass the license itself in the
license_envelopekey in place oflicense_dir.Audio: replace the
readcall with your audio driver, and push only as many bytes as the driver has ready.Blocking:
abr_lib.evset_pollblocks the calling thread, so run the loop in a thread of its own. Pass a timeout of zero to check for events without blocking, or callabr_lib.evset_interruptfrom another thread to wake it.
abr_lib.app_create documents every configuration key.