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¶
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¶
Parameters and return value
-
mgr - Android AAssetManager object.
-
filename - Path to the asset, relative to the
assets/directory.
On Android, fromFileName includes support for reading files from assets/.
fromAudioDevice¶
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
static SnsrStream fromAudioDevice();
static SnsrStream fromAudioDevice(int device, int sampleRate); // (1)!
static SnsrStream fromAudioDevice(int sampleRate);
- 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.
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 rto open for reading, orwfor writing.-
format - StreamAudioFormat audio format.
- A new Stream handle.
Parameters and return value
-
filename - The name of a file to open.
-
mode rto open for reading, orwfor 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.
Only a subset of the fopen() modes are supported:
rfor read-only access.wfor 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¶
Parameters and return value
-
a - Source Stream.
-
format - StreamAudioFormat audio format.
- A new Stream handle.
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.
fromBuffer¶
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.
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.
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 SnsrCodeModelhandle.- 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.
The Java language binding does support models converted to code.
fromFILE¶
Parameters and return value
-
handle - A standard library
FILE *, typicallystdoutorstderr. -
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
The Java language binding does support FILE * handles.
fromFileName¶
Creates a new Stream from a file on disk.
Only a subset of the fopen() modes are supported:
rfor read-only access.wfor write-only access, truncating the file to zero on open.afor write-only append access.
mode supports an optional second character:
bfor binary mode. This is the default ifmodecontains just one character.tfor text mode. On Windows this translates line-endings between\nand\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¶
Parameters and return value
-
buffer - Pointer to a memory segment.
-
bufferSize - The size of
bufferin bytes. -
mode - StreamMode, to open for reading or writing, but not both.
- A new Stream handle.
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.
fromOpenStream¶
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 1if the stream supports reading,0if not.-
writable 1if the stream supports writing,0if 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.
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¶
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¶
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.
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
datawhen this stream is deallocated. UseNULLif 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.
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.Raiseinterface. - 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.
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¶
Parameters and return value
-
b - Stream handle.
- A boolean value,
1if at the end of the stream,0if not.
close¶
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¶
Parameters and return value
-
dst - Destination Stream.
-
src - Source
Stream. -
sizeInBytes - The number of bytes to copy from
srctodst. - The number of bytes written to
dst.
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.
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
srcinto this stream. - The number of bytes copied to this
Streaminstance.
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.
errorDetail¶
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 onb.
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¶
Parameters and return value
-
b - Input stream.
-
buffer - Destination buffer.
-
bufferSize - Size of
bufferin 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.
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¶
Parameters and return value
-
b - Stream to query.
-
key - StreamMeta query type.
- The value for
key.
Parameters and return value
-
key - StreamMeta query type.
- The value for
key.
open¶
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¶
rC¶
Retrieves the most recent return code for a stream.
read¶
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.
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
intvalue of the single byte read, orInteger.MIN_VALUEif an error occurred.-
buffer - Destination buffer where read data are copied to.
-
offset - Number of bytes from the beginning of
bufferwhere writing will start. -
count - Number of bytes to read from the Stream.
longthe 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.
release¶
The C language binding uses reference counting to manage object lifetimes, see retain and release in the memory management section.
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.
skip¶
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.
write¶
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.)
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
bufferwhere 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.)
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.
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;
- optional open Opens the stream
- optional close Closes the stream
- optional release Releases stream data
- optional read Reads from the encapsulated data source
- 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.
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)!
}
- open Opens the stream
- close Closes the stream
- release Releases stream data
- read Reads from the encapsulated data source
- write Writes to the encapsulated data sink
- Operation successful
- End-of-stream reached
- The read or write operation was interrupted
- Argument validation failed
- This operation is not implemented, e.g. write operation on a read-only stream
- All errors that are not
EOF,INTERRUPTED,INVALID_ARG,NOT_IMPLEMENTED, orNOT_OPEN - 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.
open¶
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.
Parameters and return value
Provider.OKon success,Provider.NOT_OPENon 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.
close¶
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.
Parameters and return value
Provider.OKon success orProvidererror 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.
release¶
Parameters and return value
-
b - Stream handle.
read¶
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:
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.
write¶
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.
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.
getData¶
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.
Not available in the Java language binding.
Use the custom object that implements the Provider interface instead.
getVmt¶
Gets the virtual method table from a custom Stream.
Returns the def argument passed to the fromProvider call that created stream b.
Not available in the Java language binding.
Use the custom object that implements the Provider interface instead.
setDetail¶
Parameters and return value
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.
_Not available in the Java language binding.
Use throw new IOException(message); instead.
setRC¶
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¶
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.
| 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. |
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. |
DEVICE_LOW_LATENCY | Low-latency default format with a user-specified device. |
DEVICE_RATE_MODE | 16-bit LE LPCM with user-specified device, sample rate, and mode. snsrStreamFromAudioDevice() parameters
|
DEVICE_RATE_MODE_LOW_LATENCY | Low-latency 16-bit LE LPCM with user-specified device, sample rate, and mode. snsrStreamFromAudioDevice() parameters
|
DEVID | Default audio format with a user-specified device. snsrStreamFromAudioDevice() parameters
On Windows, |
DEVID_LOW_LATENCY | Low-latency default audio format with a user-specified device. snsrStreamFromAudioDevice() parameters
On Windows, |
DEVID_RATE_MODE | 16-bit LE LPCM with user-specified device, sample rate, and mode. snsrStreamFromAudioDevice() parameters
On Windows, |
DEVID_RATE_MODE_LOW_LATENCY | Low-latency 16-bit LE LPCM with user-specified device, sample rate, and mode. snsrStreamFromAudioDevice() parameters
On Windows, |
StreamMeta¶
StreamMode¶
Stream input or output mode.