Skip to content

Input and output

The inference Session uses Stream instances for all input and output. This includes model loading, audio input and output, binary data, and large sections of text.

Stream

typedef struct SnsrStream_  *SnsrStream;
public class SnsrStream {}

SnsrStream is the TrulyNatural SDK API input and output type.

This SnsrStream abstraction decouples the inference type from the actual data sources and sinks. This, for example, allows models to load and run even when a file system isn't available.

The constructors section describes all of the stream implementations (adapters, wrappers) included in this SDK.

Stream instances support the same set of operations.

SnsrStream handles are reference-counted, with lifetimes managed by retain and release.

Best practice is to release a stream object before allowing garbage collection to reclaim the object memory. This will free all encapsulated resources immediately.

Note

The current implementation does not do any thread-level locking. Threaded access to stream handles must be protected by explicit synchronization calls.

Constructors

This section describes the predefined stream constructors.

We provide stream implementations for files, file handles, live audio sources, circular buffers, models compiled to code, memory segments, strings, and user-defined data sources or sinks.

This library also includes stream transformations for audio format conversion, stream concatenation, and limiting the extent of read or write operations.

Note

Functions in this group that take SnsrStream arguments retain these handles and release them when they are no longer used. A SnsrStream handle with a zero reference count used as an argument will therefore be deallocated when it is no longer needed. Be sure to call retain on handles that need to survive these function invocations.

fromAsset

SNSR_API SnsrStream
snsrStreamFromAsset(AAssetManager *mgr, const char *filename);

Parameters and return value

mgr
Android AAssetManager object.
filename
Path to the asset, relative to the assets/ directory.

Creates a new read-only Stream from a compressed Android asset.

filename is relative to assets/ and must not include this as part of the name.

Note

This function is available in C TrulyNatural SDK on Android only. Contact Sensory if you need access to these libraries for Android.

fromFileName

On Android, fromFileName includes support for reading files from assets/.

fromAudioDevice

SNSR_API SnsrStream
snsrStreamFromAudioDevice(SnsrStreamAudioFormat format, ...);

Parameters and return value

format
StreamAudioFormat format specifier, determines additional arguments.
...
The specific additional arguments required depend on the value of format.
A new Stream handle.

Streams live audio from an audio capture device.

Creates a new Stream attached to the specified audio device. This supports audio capture with ALSA on Linux, with Audio Queue Services on macOS and iOS, and with the Windows Multimedia Extensions Wave API on Windows.

DEFAULT sets the default capture device with the format (16-bit little-endian LPCM) and sample rate (16 kHz) required by most TrulyNatural SDK recognizers. This is the recommended format specifier.

Example

// Use a specific ALSA device, rather than the default.
// The "plughw" device typically includes sample rate conversion,
// use this if the hardware does not support sampling at 16 kHz.
SnsrStream a = snsrStreamFromAudioDevice(SNSR_ST_AF_DEVICE, "plughw:1,0");

ALSA, Audio Queue Services, Windows Multimedia Extensions

static SnsrStream fromAudioDevice();

static SnsrStream fromAudioDevice(int device, int sampleRate); // (1)!

static SnsrStream fromAudioDevice(int sampleRate);
  1. Available on Android only.

Parameters and return value

device
Android audio source specifier.
sampleRate
Audio capture sample rate.
A new Stream handle.

Streams live audio from an audio capture device.

static SnsrStream fromAudioDevice() creates a new stream attached to the default audio recording device, with the DEFAULT encoding and sample rate.

static SnsrStream fromAudioDevice(int device, int sampleRate) creates a new stream attached to the specified MediaRecorder.AudioSource and sample rate. This method is available on Android devices only.

static SnsrStream fromAudioDevice(int sampleRate) creates a new stream attached to the default audio recording device at the sample rate specified.

AudioRecord, Java Audio

fromAudioFile

SNSR_API SnsrStream
snsrStreamFromAudioFile(const char *filename, const char *mode,
                        SnsrStreamAudioFormat format, ...);

Parameters and return value

filename
The name of a file to open.
mode
r to open for reading, or w for writing.
format
StreamAudioFormat audio format.
A new Stream handle.
static SnsrStream fromAudioFile(String filename, String mode);

Parameters and return value

filename
The name of a file to open.
mode
r to open for reading, or w for writing.
A new Stream instance.

Creates a new Stream from a RIFF WAV audio file.

This is equivalent to the code snippet below, with the difference that the RIFF header is updated after each write to the stream.

SnsrStream s;
s = snsrStreamFromAudioStream(snsrStreamFromFileName(filename, mode),
                              format);
SnsrStream s;
s = SnsrStream.fromAudioStream(SnsrStream.fromFileName(filename, mode),
                               SnsrStreamAudioFormat.DEFAULT);

Only a subset of the fopen() modes are supported:

  • r for read-only access.
  • w for write-only access, truncating the file to zero on open.

This stream type supports re-opening. The behavior follows that of fopen() with the given access mode.

fromAudioStream, fromFileName

fromAudioStream

SNSR_API SnsrStream
snsrStreamFromAudioStream(SnsrStream a, SnsrStreamAudioFormat format, ...);

Parameters and return value

a
Source Stream.
format
StreamAudioFormat audio format.
A new Stream handle.
static SnsrStream fromAudioStream(SnsrStream a, SnsrStreamAudioFormat format);

Parameters and return value

a
Source Stream.
format
StreamAudioFormat audio format.
A new Stream handle.

Converts stream audio format.

Creates a new Stream on an existing one. The new stream produces audio in format, converting as needed.

This implementation reads the audio format header in the source stream upon opening. If this is a known, but unsupported format, fromAudioStream will report an error. If the format isn't known, it assumes that the data are headerless little-endian 16-bit LPCM audio samples.

The current implementation supports the 16-bit LPCM RIFF WAVE format, sampled at 16 kHz, for a single channel only. The format parameter must be set to DEFAULT.

Note

When this stream type is used for output, all stream data will be buffered in memory until the stream is closed. The audio header and the data will then be written to the encapsulated stream.

If the final destination is a file on disk, use fromAudioFile instead.

fromAudioFile

fromBuffer

SNSR_API SnsrStream
snsrStreamFromBuffer(size_t initialSizeInBytes, size_t maxSizeInBytes);

Parameters and return value

initialSizeInBytes
The minimum size of the allocated ring buffer.
maxSizeInBytes
The limit the ring buffer is allowed to expand to.
A new Stream handle.
static SnsrStream fromBuffer(long initialSizeInBytes, long maxSizeInBytes);

Parameters and return value

initialSizeInBytes
The minimum size of the allocated ring buffer.
maxSizeInBytes
The limit the ring buffer is allowed to expand to.
A new Stream handle.

Creates a new Stream on a ring buffer with FIFO semantics.

The initial buffer will be large enough to hold initialSizeInBytes bytes, and will dynamically grow up to maxSizeInBytes.

If this ring buffer cannot be expanded when needed, write will set the EOF condition on the stream.

This stream type does not support re-opening.

Note

Calls to read on streams of this type never block. read returns EOF if the buffer runs empty.

fromCode

// Opaque type used for task models converted to C code.
typedef const struct SnsrCodeModel_ *SnsrCodeModel;

SNSR_API SnsrStream
snsrStreamFromCode(SnsrCodeModel identifier);

Parameters and return value

identifier
SnsrCodeModel handle.
A new Stream handle.

Creates a new Stream on a model compiled to code.

The SnsrCodeModel handle is an identifier loaded from an object file, compiled from C code generated by a call to save with the SOURCE or SOURCE_RAM format types.

These data are typically loaded into the read-only TEXT (code) segment.

Use this on platforms where the TEXT segment is read from ROM to reduce the amount of RAM a model needs to run.

Example

extern SnsrCodeModel snsrmodel;
SnsrSession s;
snsrNew(&s);
snsrLoad(s, snsrStreamFromCode(snsrmodel));

The Java language binding does support models converted to code.

fromFILE

SNSR_API SnsrStream
snsrStreamFromFILE(FILE *handle, SnsrStreamMode mode);

Parameters and return value

handle
A standard library FILE *, typically stdout or stderr.
mode
StreamMode, open for reading or writing, but not both.
A new Stream handle.

Creates a new SnsrStream from on a <stdio.h> FILE * handle.

Calls to close on this handle will not close the encapsulated FILE *.

This stream type does not support re-opening.

Example

SnsrStream out = snsrStreamFromFILE(stderr, SNSR_ST_MODE_WRITE);
snsrStreamOpen(out);
snsrStreamPrint(out, "hello world\n");
snsrRelease(out);

fromFileName

The Java language binding does support FILE * handles.

fromFileName

SNSR_API SnsrStream
snsrStreamFromFileName(const char *filename, const char *mode);

Parameters and return value

filename
The name of a file to open.
mode
r, w, or a followed by an optional b or t.
A new Stream handle.
static SnsrStream fromFileName(String filename, String mode);

Parameters and return value

filename
The name of a file to open.
mode
r, w, or a followed by an optional b or t.
A new Stream instance.

Creates a new Stream from a file on disk.

Only a subset of the fopen() modes are supported:

  • r for read-only access.
  • w for write-only access, truncating the file to zero on open.
  • a for write-only append access.

mode supports an optional second character:

  • b for binary mode. This is the default if mode contains just one character.
  • t for text mode. On Windows this translates line-endings between \n and \r\n.

This stream type supports re-opening. The behavior follows that of fopen() with the given access mode.

Note

On Android, SnsrStream.fromFileName() includes support for reading from the the compressed application assets/ directory.

fromMemory

SNSR_API SnsrStream
snsrStreamFromMemory(void *buffer, size_t bufferSize, SnsrStreamMode mode);

Parameters and return value

buffer
Pointer to a memory segment.
bufferSize
The size of buffer in bytes.
mode
StreamMode, to open for reading or writing, but not both.
A new Stream handle.

Creates a new Stream from a segment of memory.

Stream operations are limited to the first bufferSize bytes of the segment starting at buffer.

This stream type does not support re-opening.

Example

char *data = malloc(1000);
SnsrStream b = snsrStreamFromMemory(data, 1000, SNSR_ST_MODE_WRITE);
snsrStreamOpen(b);
snsrStreamPrint(b, "hello world, data=%p\n", data);
snsrRelease(b);
printf("wrote: %s\n", data);
free(data);

fromString

static SnsrStream fromMemory(byte[] store, SnsrStreamMode mode);

Parameters and return value

store
Data made available to the Stream.
mode
StreamMode, to open for reading or writing, but not both.
A new Stream instance.

Creates a new Stream from a segment of memory.

This stream type does not support re-opening.

Note

Changes written through the Stream API will only be visible in store after the stream instance is released.

Read behavior is undefined if store is modified after the stream is created.

fromString

fromOpenStream

SNSR_API SnsrStream
snsrStreamFromOpenStream(SnsrStream source, size_t sizeInBytes);

Parameters and return value

source
Stream instance.
sizeInBytes
New stream read or write limit.
A new Stream handle.
static SnsrStream fromOpenStream(SnsrStream source, long sizeInBytes);

Parameters and return value

source
Stream instance.
sizeInBytes
New stream read or write limit.
A new Stream handle.

Creates a new Stream from an existing, open stream.

The new stream mode (read or write) matches that of the source stream. Read and write operations are passed through to the source stream, but the number of bytes read or written is limited to sizeInBytes.

Input and output operations will set the stream status to EOF when this limit is reached.

Closing this stream will not close source.

fromProvider

SNSR_API SnsrStream
snsrStream_alloc(SnsrStream_Vmt *def, void *data, int readable, int writable);

#define snsrStreamFromProvider(p, d, r, w) snsrStream_alloc(p, d, r, w)

Parameters and return value

def
Table of function pointers that define the Stream type.
data
User pointer for stream context.
readable
1 if the stream supports reading, 0 if not.
writable
1 if the stream supports writing, 0 if not.
A new Stream handle.

Creates a new custom Stream.

The def virtual method table defines the behavior of the new stream.

data is an arbitrary pointer that can be retrieved from the Stream handle with getData. Use this to hold data specific to the instance.

Provider

static SnsrStream fromProvider(SnsrStream.Provider custom);

static SnsrStream fromProvider(SnsrStream.Provider custom, SnsrStreamMode mode);

Parameters and return value

custom
An object that implements the Provider interface.
mode
StreamMode, open for reading or writing, but not both.
A new Stream handle.

Creates a new custom Stream instance.

static SnsrStream fromProvider(SnsrStream.Provider custom) creates a new stream that supports both reading and writing.

static SnsrStream fromProvider(SnsrStream.Provider custom, SnsrStreamMode mode) creates a new stream that supports either reading or writing, but not both.

fromStreams

SNSR_API SnsrStream
snsrStreamFromStreams(SnsrStream a, SnsrStream b);

Parameters and return value

a
First source Stream.
b
Second source Stream.
A new Stream handle.
static SnsrStream fromStreams(SnsrStream a, SnsrStream b);

Parameters and return value

a
First source Stream.
b
Second source Stream.
A new Stream handle.

Creates a new Stream by concatenating two streams.

Streams a and b will be each be opened exactly once. These should be unopened (getMeta for OPEN_COUNT must be 0) when fromStreams is called.

The new stream will read from (or write to) stream a until end-of-stream is reached, then switch to stream b until that is exhausted, at which point read or write returns EOF.

Streams a and b must have identical modes. If one stream was created for reading, and the other for writing, this function will set the error code to WRONG_MODE.

Closing this stream will close the current source stream. The next open call will open the next source stream. When no further source streams remain open returns CANNOT_REOPEN.

fromString

SNSR_API SnsrStream
snsrStreamFromString(const char *store);

Parameters and return value

store
Data made available to the new stream. Terminated with \0.
A new Stream handle.

Creates a new read-only Stream from string data.

Equivalent to: snsrStreamFromMemory(store, strlen(store), SNSR_ST_MODE_READ);

This stream type does not support re-opening.

fromMemory

static SnsrStream fromString(String store);

Parameters and return value

store
Data made available to the new stream.
A new Stream handle.

Creates a new read-only Stream from String data.

Note

Read behavior is undefined if store is modified after calling SnsrStream.fromString(store).

fromMemory

raise

typedef SnsrRC (*SnsrStreamEvent)(SnsrStream s, void *data);

typedef void (*SnsrDataRelease)(const void *data);

SNSR_API SnsrStream
snsrStreamRaise(SnsrStreamEvent e, SnsrDataRelease r, void *data);

Parameters and return value

e
Function to call when this stream is opened. Must not be NULL.
r
A function that releases data when this stream is deallocated. Use NULL if no clean-up is required.
data
User data pointer, for context.
A new Stream handle.

Inserts an event marker into a Stream.

Use this stream type with fromStreams to embed events in a stream.

Creates a new stream that is readable, but provides no data. Any read or skip operation will immediately return EOF.

Calls e(b, data) when this stream is opened. This function should return OK unless it encounters an error.

Calls r(data) when this stream is released, unless r == NULL. This function should release the data handle.

fromStreams

public interface Raise {
  public static final long OK = 0;
  public static final long EOF = -1;
  public static final long INTERRUPTED = -2;
  public static final long INVALID_ARG = -3;
  public static final long NOT_IMPLEMENTED = -4;
  public static final long ERROR = -5;
  public static final long NOT_OPEN = -6;
  public long onOpen();
}

public static SnsrStream raise(SnsrStream.Raise jevent);

Parameters and return value

jevent
An object that implements the SnsrStream.Raise interface.
A new Stream handle.

Inserts an event marker into a Stream.

Use this stream type with fromStreams to embed events in a stream.

Creates a new stream that is readable, but provides no data. Any read or skip operation will immediately return EOF.

Calls the long onOpen() interface method when this stream is opened. This method should return Raise.OK unless it encounters an error.

fromStreams

Operations

These functions or methods implement basic stream operations, such as opening, reading, writing, and closing. Utility functions print formatted data to a stream, and inspect stream state.

The error state of a stream handle persists only until the next operation on the stream.

These functions do not change the stream handle reference counts. They are therefore safe to use on handles where the reference count is zero.

atEnd

SNSR_API int
snsrStreamAtEnd(SnsrStream b);

Parameters and return value

b
Stream handle.
A boolean value, 1 if at the end of the stream, 0 if not.
public int atEnd();

Parameters and return value

A boolean value, 1 if at the end of the stream, 0 if not.

Reports stream end status.

Returns 1 if the stream has reached its end, equivalent to rC == EOF.

Follows Unix semantics:

  • The end-of-stream indicator is only valid after a read attempt.
  • A read on stream b could very well return 0, even if atEnd returned 0 just before attempting the read.

rC

close

SNSR_API SnsrRC
snsrStreamClose(SnsrStream b);

Parameters and return value

b
Stream handle.
OK for success, any other value indicates failure.
SnsrRC close();

Parameters and return value

OK for success, any other value indicates failure.

Closes an open stream.

Flushes any remaining buffered output and discards any remaining buffered input. This does not destroy the Stream data structure, use release to do so.

Closing a stream that is not open is not an error.

Streams that are still open when they're released are explicitly closed before the data structure is torn down.

copy

SNSR_API size_t
snsrStreamCopy(SnsrStream dst, SnsrStream src, size_t sizeInBytes);

Parameters and return value

dst
Destination Stream.
src
Source Stream.
sizeInBytes
The number of bytes to copy from src to dst.
The number of bytes written to dst.

Copies from Stream to Stream.

Ensures that both streams are open, copies sizeInBytes bytes from src to dst, then returns the number of bytes successfully written to dst.

If an error occurred (including an end-of-stream condition on either dst or src) the return value will be less than sizeInBytes.

This function duplicates errors that occur in src to dst. Use rC to inspect these.

rC

long copy(SnsrStream src) throws java.io.IOException;

long copy(SnsrStream src, long sizeInBytes) throws java.io.IOException;

Parameters and return value

src
Source Stream.
sizeInBytes
The number of bytes to copy from src into this stream.
The number of bytes copied to this Stream instance.

Copies from Stream to Stream.

Ensures that both streams are open, copies either sizeInBytes bytes, or all available data if sizeInBytes isn't specified, from src to this stream, then returns the number of bytes successfully written.

If an error occurred (including an end-of-stream condition on either dst or src) the return value will be less than sizeInBytes.

These methods duplicate errors that occur in src to dst; use rC to inspect these.

rC

errorDetail

SNSR_API const char *
snsrStreamErrorDetail(SnsrStream b);

Parameters and return value

b
Stream handle.
A detailed message describing the most recent reason for failure in b. The memory pointed to is owned by this library and must not be released. It is not reference-counted. This pointer remains valid only until the next API call on b.
String errorDetail();

Parameters and return value

A detailed message describing the most recent reason for failure on this Stream.

Retrieves a detailed stream error message.

This human-readable error message containing the reason for failure. Use for display or logging only, as the content of the message is unspecified and system-specific.

Do not parse this to determine program flow, use the rC return code instead.

getDelim

SNSR_API size_t
snsrStreamGetDelim(SnsrStream b, void *buffer, size_t bufferSize, int delim);

Parameters and return value

b
Input stream.
buffer
Destination buffer.
bufferSize
Size of buffer in bytes.
delim
delimiter character to search for.
The number of characters read and placed into buffer.

Reads one line from a stream.

Reads a line from stream b, delimited by the character delim, into buffer. This is similar to getdelim(), except that it operates on stream and does not allocate memory for the destination buffer.

getDelim ensures that b is open, reads up to bufferSize - 1 bytes from b into buffer, stops when delim has been read (buffer will include delim). It terminates the string in buffer with \0 and returns the number of characters read (not including the terminating \0).

If the delimiter is not found after reading bufferSize - 1 bytes, the error code in b is set to DELIM_NOT_FOUND.

buffer must not be NULL and bufferSize must be >= 2.

buffer will contain all characters read and will be terminated with \0, even if an error occurs.

long getDelim(byte[] buffer, int delim) throws java.io.IOException;

Parameters and return value

buffer
Destination buffer, must be at least two bytes long.
delim
delimiter character to search for.
The number of characters read and placed into buffer.

Reads one line from a stream.

Reads a line from stream b, delimited by the character delim, into buffer. This is similar to getdelim(), except that it operates on stream and does not allocate memory for the destination buffer.

getDelim ensures that b is open, reads up to buffer.length - 1 bytes from b into buffer, stops when delim has been read (buffer will include delim). It terminates the string in buffer with \0 and returns the number of characters read (not including the terminating \0).

If the delimiter is not found after reading buffer.length - 1 bytes, the error code in b is set to DELIM_NOT_FOUND.

buffer must not be null and buffer.length must be >= 2.

buffer will contain all characters read and will be terminated with \0, even if an error occurs.

getMeta

SNSR_API size_t
snsrStreamGetMeta(SnsrStream b, SnsrStreamMeta key);

Parameters and return value

b
Stream to query.
key
StreamMeta query type.
The value for key.
long getMeta(SnsrStreamMeta key);

Parameters and return value

key
StreamMeta query type.
The value for key.

Queries stream metadata.

Returns Stream metadata, selected by key. If an error occurs this function returns 0 and sets the error state in the stream. Use rC to check the error state.

rC, StreamMeta

open

SNSR_API SnsrRC
snsrStreamOpen(SnsrStream b);

Parameters and return value

b
Stream to open.
OK for success, any other value indicates failure.
SnsrRC open() throws java.io.IOException;

Parameters and return value

OK for success, any other value indicates failure.

Opens a stream.

Read and write operations are valid only on open streams, and newly created streams are not open.

Some stream types can be closed and re-opened. See the constructors section for details. If re-opening is not supported open will return CANNOT_REOPEN when an attempt is made to open it a second time.

print

SNSR_API size_t
snsrStreamPrint(SnsrStream b, const char *format, ...);

Parameters and return value

b
Output Stream.
format
printf() style format string.
...
Arguments specified by format.
The number of bytes written to b, or 0 if an error occurred.
long print(String format, Object... args) throws java.io.IOException;

Parameters and return value

format
printf() style format string.
args
Arguments specified by format.
The number of bytes written to the Stream, or 0 if an error occurred.

Prints a formatted string to a stream.

This function is similar to printf(), but operates on a Stream instead of a FILE *.

print will open the output stream if it's not already open.

If an error occurs this function returns 0 and sets the error state in b. Use rC to check the error state.

rC, printf()

rC

SNSR_API SnsrRC
snsrStreamRC(SnsrStream b);

Parameters and return value

b
Stream handle.
The current RC code for b.
SnsrRC rC();

Parameters and return value

The current RC code for this Stream.

Retrieves the most recent return code for a stream.

atEnd

read

SNSR_API size_t
snsrStreamRead(SnsrStream a, void *buffer, size_t size, size_t nitems);

Parameters and return value

a
Stream to read from.
buffer
Memory to write to.
size
The size of an item, in bytes.
nitems
The number of items to read.
The number of items read.

Reads binary data from a stream.

Opens the stream if it is not already open, reads nitems items, each size bytes long, into buffer and returns the number of items read.

If either size or nitems is zero this function returns 0 without reading anything.

A short read (fewer than nitems) will occur only if the end of the stream was reached before nitems were read, or if an error occurred. The stream return code will be set appropriately. Use atEnd or rC to distinguish between these conditions.

If the end of the stream is encountered in the middle of an object, it returns the number of complete items read. The partial object is discarded and the stream remains in the end-of-stream condition.

Read operations block until all the requested data have been read, or an error or end-of-stream occurs.

atEnd, rC, skip

int read() throws java.io.IOException;

long read(byte[] buffer) throws java.io.IOException;

long read(byte[] buffer, long offset, long count) throws java.io.IOException;

Parameters and return value

int value of the single byte read, or Integer.MIN_VALUE if an error occurred.
buffer
Destination buffer where read data are copied to.
offset
Number of bytes from the beginning of buffer where writing will start.
count
Number of bytes to read from the Stream.
long the number of bytes read.

Reads binary data from a stream.

These methods open the Stream if it's not already open.

int read() reads a single byte from the stream.

long read(byte[] buffer) attempts to fill the entire buffer with data from the stream and returns the number of bytes read.

long read(byte[] buffer, long offset, long count) reads up to count bytes for the stream into buffer starting at offset bytes from the start. It returns the number of bytes read. A short read (fewer than count bytes) will occur if buffer fills up first, the stream ends, or an error occurs. Use atEnd or rC to distinguish between these conditions.

Read operations block until all the requested data have been read, or an error or end-of-stream occurs.

atEnd, rC, skip

release

The C language binding uses reference counting to manage object lifetimes, see retain and release in the memory management section.

public void release();

Releases the native library handle encapsulated by the SnsrStream class.

This method releases native handles immediately. Use this to free up resources without having to depend on garbage collection cycle eventually running.

No instance methods must be invoked after calling this method.

Example

SnsrStream s = SnsrStream.fromAudioDevice();
// ...
s.release();
s = null;

skip

SNSR_API size_t
snsrStreamSkip(SnsrStream a, size_t size, size_t nitems);

Parameters and return value

a
Stream to read from.
size
The size of an item, in bytes.
nitems
The number of items to read and discard.
The number of items read and discarded.
long skip(long count) throws java.io.IOException;

long skip(long size, long nitems) throws java.io.IOException;

Parameters and return value

size
The size of an item, in bytes. size == 1 if not specified.
nitems
The number of items to read and discard.
The number of items read and discarded.

Reads from a stream and discards the results.

This function is similar to read, but discards the read data instead of writing it to a memory buffer.

atEnd, rC, read

write

SNSR_API size_t
snsrStreamWrite(SnsrStream a, const void *buffer, size_t size, size_t nitems);

Parameters and return value

a
Stream to write to.
buffer
Memory to read from.
size
The size of an item, in bytes.
nitems
The number of items to write.
The number of items written.

Writes from a buffer to a Stream.

Opens the stream if it is not already open, then writes nitems from buffer, each size bytes long, to the stream. Returns the number of items written.

Returns less than nitems only if an error occurred. Use rC to obtain the error code.

Write operations block until the requested data have been transferred to the underlying implementation, but may return well before the data have reached their final destination (written to disk, played from a speaker, etc.)

rC, read

long write(int oneByte) throws java.io.IOException;

long write(byte[] buffer) throws java.io.IOException;

long write(byte[] buffer, long offset, long count) throws java.io.IOException;

Parameters and return value

oneByte
A single source byte to write.
buffer
Source buffer data are read from.
offset
Number of bytes from the beginning of buffer where reading will start.
count
Number of bytes to write to the Stream.
The number of bytes written.

Writes data to a stream.

long write(int oneByte) writes a single byte to this SnsrStream and returns 1, or 0 in case of an error.

long write(byte[] buffer) writes buffer.length bytes from buffer to the stream and returns the number of bytes written.

long write(byte[] buffer, long offset, long count) writes count bytes from buffer starting at offset bytes from the beginning to the stream. It returns the number of bytes written.

These methods open the stream if it's not already open. The number of bytes written will be fewer than the requested amount only if an error occurs. Use rC to identify such errors.

Write operations block until the requested data have been transferred to the underlying implementation, but may return well before the data have reached their final destination (written to disk, played from a speaker, etc.)

rC, read

User-defined

This section describes the provider data structure used with fromProvider to create custom stream types. Each stream must implement at least a subset of the open, close, release, read, and write methods.

The data argument to fromProvider should point to a struct that holds the entire context needed for each instance of a custom stream. Retrieve this pointer in the implementation functions with a call to getData.

Use setRC and setDetail to report any errors encountered.

The custom argument to fromProvider is an object that implements the SnsrStream.Provider interface. This object should hold the entire context needed for each instance of a custom stream.

Provider

typedef struct {
  const char *name; // Stream identifier
  SnsrRC (*open)(SnsrStream b); // (1)!
  SnsrRC (*close)(SnsrStream b); // (2)!
  void   (*release)(SnsrStream b); // (3)!
  size_t (*read)(SnsrStream b, void *data, size_t sizeInBytes); // (4)!
  size_t (*write)(SnsrStream b, const void *data, size_t sizeInBytes); // (5)!
} SnsrStream_Vmt;
  1. optional open Opens the stream
  2. optional close Closes the stream
  3. optional release Releases stream data
  4. optional read Reads from the encapsulated data source
  5. optional write Writes to the encapsulated data sink

Stream virtual method table.

This method table defines stream behavior when used as an argument to fromProvider. This struct must remain valid for the entire life of all the streams of this type. We recommend static allocation.

When one of these functions return (open, close) or set (read, write) an RC code other than OK, and additional error detail is available, it should also set a detailed human-readable error message using setDetail.

Set the fields for optional members without implementations to NULL.

fromProvider

public interface Provider {
  public static final long OK = 0; // (6)!
  public static final long EOF = -1; // (7)!
  public static final long INTERRUPTED = -2; // (8)!
  public static final long INVALID_ARG = -3; // (9)!
  public static final long NOT_IMPLEMENTED = -4; // (10)!
  public static final long ERROR = -5; // (11)!
  public static final long NOT_OPEN = -6; // (12)!

  public long onOpen() throws IOException; // (1)!
  public long onClose() throws IOException; // (2)!
  public void onRelease(); // (3)!
  public long onRead(byte[] buffer) throws IOException; // (4)!
  public long onWrite(byte[] buffer) throws IOException; // (5)!
}
  1. open Opens the stream
  2. close Closes the stream
  3. release Releases stream data
  4. read Reads from the encapsulated data source
  5. write Writes to the encapsulated data sink
  6. Operation successful
  7. End-of-stream reached
  8. The read or write operation was interrupted
  9. Argument validation failed
  10. This operation is not implemented, e.g. write operation on a read-only stream
  11. All errors that are not EOF, INTERRUPTED, INVALID_ARG, NOT_IMPLEMENTED, or NOT_OPEN
  12. Attempt was made to use a stream that is not open

Custom Stream interface specification.

Use this interface to add new stream types.

Each Stream type exposes a single constructor used to create a new instances of this type. The constructor function sets up the required data structures and copies arguments into these structures for use by the open method.

The constructor must not open the underlying data stream. The open method will do this instead. Delaying the open operation allows for function compositions, such as a stream that is a concatenation of other streams.

Use fromProvider to create an instance from an object that implements this SnsrStream.Provider interface.

fromProvider

open
SnsrRC (*open)(SnsrStream b);

Parameters and return value

b
Stream handle.

Opens the underlying data stream.

Called in response to an open API call, and only on Stream handles that are not already open.

Must open the underlying stream in binary mode, unless documented otherwise.

Must return OK if the operation succeeded, and an error code if the resource could not be opened.

Provider

public long onOpen() throws IOException;

Parameters and return value

Provider.OK on success, Provider.NOT_OPEN on failure.

Opens the underlying data stream.

Called in response to an open API call, and only on Stream handles that are not already open.

Must open the underlying stream in binary mode, unless documented otherwise.

Must return Provider.OK if the operation succeeded, or Provider.NOT_OPEN if the resource could not be opened.

Provider

close
SnsrRC (*close)(SnsrStream b);

Parameters and return value

b
Stream handle.

Closes the underlying data stream.

Called in response to a close API call, and only on Stream handles that are currently open.

This function must flush buffered output and discard any remaining buffered input.

Must return OK if the operation succeeded, and an error code if the underlying resource could not be closed.

Provider

long onClose() throws IOException;

Parameters and return value

Provider.OK on success or Provider error code on failure.

Closes the underlying data stream.

Called in response to a close API call, and only on Stream handles that are currently open.

This function must flush buffered output and discard any remaining buffered input.

Must return Provider.OK if the operation succeeded, and an error code if the underlying resource could not be closed.

Provider

release
void (*release)(SnsrStream b);

Parameters and return value

b
Stream handle.
void onRelease();

Releases private Stream data.

Called just before the stream handle is de-allocated. The stream will have been closed when this function is called.

Must release all the resources allocated by the open function.

Provider

read
size_t (*read)(SnsrStream b, void *data, size_t sizeInBytes);

Parameters and return value

data
Memory location to write to.
sizeInBytes
Number of bytes to read from the encapsulated data source.
b
Stream handle.

Reads data from the encapsulated source.

Called in response to a read API call.

The handle will be open when this function is called, and sizeInBytes will be larger than 0.

Must read sizeInBytes bytes into the buffer pointed to by data.

The read must block (i.e. not return) until all the requested data have been read, or an error occurs.

Must return the number of actual bytes read. This number of bytes read must be sizeInBytes, unless:

  • An error occurred, in which case it must call setRC with an appropriate error code.
  • The end of the stream was reached, in which case it must call setRC with EOF.

Provider

long onRead(byte[] buffer) throws IOException;

Parameters and return value

buffer
Destination buffer.
Number of bytes read from the stream.

Reads data from the encapsulated source.

Called in response to a read API call.

The handle will be open when this function is called.

Must read buffer.length bytes into buffer.

The read must block (i.e. not return) until all the requested data have been read, or an error occurs.

Must return the number of actual bytes read, unless an error occurred. Returning fewer than buffer.length bytes indicates that the end of the stream was reached.

Must return Provider.INTERRUPTED if the read was interrupted.

Provider

write
size_t (*write)(SnsrStream b, const void *data, size_t sizeInBytes);

Parameters and return value

data
Memory location to read from.
sizeInBytes
Number of bytes to write to the encapsulated data source.
b
Stream handle.
The number of bytes written.

Writes data to the encapsulated data stream.

Called in response to a write API call.

The stream handle will be open when this function is called, and sizeInBytes will be larger than 0.

Must write sizeInBytes bytes, reading them from the buffer pointed to by data.

The write must block (i.e. not return) until all of the requested data have been written to the encapsulated sink, or an error occurs.

Must return the number of bytes actually written. This number of bytes must be sizeInBytes, unless an error occurred, in which case it must call setRC with an appropriate error code.

Provider

long onWrite(byte[] buffer) throws IOException;

Parameters and return value

buffer
Source buffer to read from.
The number of bytes written to the stream.

Writes data to the encapsulated data stream.

Called in response to a write API call.

The stream handle will be open when this function is called.

Must read buffer.length bytes from buffer and write them to the encapsulated Stream.

The write must block (i.e. not return) until all of the requested data have been written to the encapsulated sink, or an error occurs.

Must return the number of bytes actually written.

Return Provider.INTERRUPTED if the write operation was interrupted.

Provider

getData

SNSR_API void *
snsrStream_getData(SnsrStream b);

Parameters and return value

b
A custom Stream handle.
A pointer to user data.

Gets the user data pointer from a custom Stream.

Returns the data argument passed to the fromProvider call that created stream b.

fromProvider, Provider

Not available in the Java language binding.

Use the custom object that implements the Provider interface instead.

getVmt

SNSR_API SnsrStream_Vmt *
snsrStream_getVmt(SnsrStream b);

Parameters and return value

b
A custom Stream handle.
A pointer to the Provider struct for b.

Gets the virtual method table from a custom Stream.

Returns the def argument passed to the fromProvider call that created stream b.

fromProvider, Provider

Not available in the Java language binding.

Use the custom object that implements the Provider interface instead.

setDetail

SNSR_API void
snsrStream_setDetail(SnsrStream b, const char *format, ...);

Parameters and return value

b
A custom Stream handle.
format
printf() style format string.
...
Arguments specified by format.

Sets a detailed Stream error message.

Sets the detailed human-readable error message in stream b to format, which is a standard C library printf() format string.

This detailed error message is made available through the errorDetail API function.

errorDetail, setRC, Provider

_Not available in the Java language binding.

Use throw new IOException(message); instead.

setRC

SNSR_API void
snsrStream_setDetail(SnsrStream b, const char *format, ...);

Parameters and return value

b
A custom Stream handle.
code
RC return code, OK for success.

This sets the return code for custom Stream b to code.

Note

Only a subset of the RC codes are valid for streams: code must be in the range from OK to FORMAT_NOT_SUPPORTED.

setDetail, Provider

Not available in the Java language binding.

Return one of the SnsrStream.Provider error codes (e.g. return Provider.NOT_OPEN;) instead.

Enumerations

Stream functions and methods use the enumerations below to select from a small set of alternate behaviors.

StreamAudioFormat

typedef enum {
    SNSR_ST_AF_{name},
    ...
} SnsrStreamAudioFormat;

// Where {name} is from the table below, e.g.: SNSR_ST_AF_DEFAULT
public enum SnsrStreamAudioFormat {
  {name},
  ...
}

// Where {name} is from the table below, e.g.: SnsrStreamAudioFormat.DEFAULT

Audio format specifier for Stream.

The default format is 16-bit little-endian LPCM sampled at 16 kHz. Most Sensory models expect this format, making the DEFAULT and DEFAULT_LOW_LATENCY the most salient.

fromAudioDevice, fromAudioStream

Name Description
DEFAULT recommended Default audio stream format.
DEFAULT_LOW_LATENCY Low latency, at the expense of higher CPU overhead.
LPCM_S16_16K 16-bit little-endian LPCM at 16 kHz (default).
LPCM_S16_16K_SAMPLES

WAV file sample count.

snsrStreamFromAudioDevice() parameters

SNSR_API SnsrStream
snsrStreamFromAudioDevice(
  SNSR_ST_AF_LPCM_S16_16K_SAMPLES,
  size_t *samples
);

samples
Number of audio samples read from the RIFF header.
LPCM_S16_16K_LOW_LATENCY

Low latency, at the expense of higher CPU overhead.

DEFAULT_LOW_LATENCY
DEVICE

Default format with a user-specified device.

snsrStreamFromAudioDevice() parameters

SNSR_API SnsrStream
snsrStreamFromAudioDevice(
  SNSR_ST_AF_DEVICE,
  const char *device
);

device
Platform-specific device selector.
DEVICE_LOW_LATENCY

Low-latency default format with a user-specified device.

snsrStreamFromAudioDevice() parameters

SNSR_API SnsrStream
snsrStreamFromAudioDevice(
  SNSR_ST_AF_DEVICE_LOW_LATENCY,
  const char *device
);

device
Platform-specific device selector.
DEVICE_RATE_MODE

16-bit LE LPCM with user-specified device, sample rate, and mode.

snsrStreamFromAudioDevice() parameters

SNSR_API SnsrStream
snsrStreamFromAudioDevice(
  SNSR_ST_AF_DEVICE_RATE_MODE,
  const char *device,
  unsigned rate,
  SnsrStreamMode mode
);

device
Platform-specific device selector.
rate
Sample rate in Hz.
mode
Read or write.
StreamMode
DEVICE_RATE_MODE_LOW_LATENCY

Low-latency 16-bit LE LPCM with user-specified device, sample rate, and mode.

snsrStreamFromAudioDevice() parameters

SNSR_API SnsrStream
snsrStreamFromAudioDevice(
  SNSR_ST_AF_DEVICE_RATE_MODE_LOW_LATENCY,
  const char *device,
  unsigned it rate,
  SnsrStreamMode mode
);

device
Platform-specific device selector.
rate
Sample rate in Hz.
mode
Read or write.
StreamMode
DEVID

Default audio format with a user-specified device.

snsrStreamFromAudioDevice() parameters

SNSR_API SnsrStream
snsrStreamFromAudioDevice(
  SNSR_ST_AF_DEVID,
  int devid
);

devid
Platform-specific device selector.

On Windows, devid is the device ID used with the Multimedia Extensions, such as waveInGetDevCaps().

DEVID_LOW_LATENCY

Low-latency default audio format with a user-specified device.

snsrStreamFromAudioDevice() parameters

SNSR_API SnsrStream
snsrStreamFromAudioDevice(
  SNSR_ST_AF_DEVID,
  int devid
);

devid
Platform-specific device selector.

On Windows, devid is the device ID used with the Multimedia Extensions, such as waveInGetDevCaps().

DEVID_RATE_MODE

16-bit LE LPCM with user-specified device, sample rate, and mode.

snsrStreamFromAudioDevice() parameters

SNSR_API SnsrStream
snsrStreamFromAudioDevice(
  SNSR_ST_AF_DEVID,
  int devid,
  unsigned int rate,
  SnsrStreamMode mode
);

devid
Platform-specific device selector.
rate
Sample rate in Hz.
mode
Read or write.

On Windows, devid is the device ID used with the Multimedia Extensions, such as waveInGetDevCaps().

StreamMode
DEVID_RATE_MODE_LOW_LATENCY

Low-latency 16-bit LE LPCM with user-specified device, sample rate, and mode.

snsrStreamFromAudioDevice() parameters

SNSR_API SnsrStream
snsrStreamFromAudioDevice(
  SNSR_ST_AF_DEVID,
  int devid,
  unsigned int rate,
  SnsrStreamMode mode
);

devid
Platform-specific device selector.
rate
Sample rate in Hz.
mode
Read or write.

On Windows, devid is the device ID used with the Multimedia Extensions, such as waveInGetDevCaps().

StreamMode

StreamMeta

typedef enum {
    SNSR_ST_META_{name},
    ...
} SnsrStreamMeta;

// Where {name} is from the table below, e.g.: SNSR_ST_META_BYTES_READ
public enum SnsrStreamMeta {
  {name},
  ...
}

// Where {name} is from the table below, e.g.: SnsrStreamMeta.BYTES_READ

Stream metadata introspection key.

getMeta

Name Description
BYTES_READ Number of bytes read from the stream.
BYTES_WRITTEN Number of bytes written to the stream.
IS_OPEN Boolean, 1 if the stream is open, 0 if not.
IS_READABLE Boolean, 1 if the stream is readable, 0 if not.
IS_WRITABLE Boolean, 1 if the stream is writable, 0 if not.
OPEN_COUNT Number of times the stream has been opened.

StreamMode

typedef enum {
    SNSR_ST_MODE_{name},
    ...
} SnsrStreamMode;

// Where {name} is from the table below, e.g.: SNSR_ST_MODE_READ
public enum SnsrStreamMode {
  {name},
  ...
}

// Where {name} is from the table below, e.g.: SnsrStreamMode.READ

Stream input or output mode.

Name Description
READ Read from this input stream.
WRITE Write to this output stream.