rust-racs
rust-racs is the rust client library for RACS. It provides access to all the RACS commands through a low-level API.
Installation
Run the following Cargo command in your project directory:
cargo add racs
Or add the following line to your Cargo.toml:
racs = "0.1.0"
Basic Operations
To open a connection, simply create a client using open.
use Client;
let client = open.unwrap;
The open function creates a client with a default connection pool size of 3.
To specify the connection pool size, use open_with_pool_size.
use Client;
let client = open_with_pool_size.unrwap;
Streaming
The pipeline function is used to chain together multiple RACS commands and execute them sequentially.
In the below example, a new audio stream is created and opened. Then PCM data is chunked into frames
and streamed to the RACS server.
use Client;
// Connect to a RACS server
let client = open.unwrap;
// Create a new audio stream and open it using pipeline
client
.pipeline
.create
.open
.execute // execute all pipelined commands
.unwrap;
// Prepare PCM samples (interleaved L/R, 16- or 24-bit integers)
let samples: = /* your PCM audio data */;
// Stream audio data to the server
client
.stream
.unwrap;
// Close the stream when finished
client
.pipeline
.close
.execute
.unwrap;
Extracting and Formating
The below example retrieves a reference timestamp, and uses it to extract an audio segment based on the given range. It then converts the extracted PCM data into MP3 format and writes the resulting bytes to a file.
use ;
use Client;
use Type;
use File;
use Write;
// Connect to the RACS server
let client = open.unwrap;
// Get the reference timestamp (in milliseconds)
let result = client
.pipeline
.info
.execute;
if let Int = result.unwrap
To extract PCM data without formating, do the following instead:
// Extract PCM data between `from` and `to`
let result = client
.pipeline
.extract
.execute;
if let S32V = result.unwrap
Querying Streams and Metadata
Stream ids stored in RACS can be queried using the search function.
search takes a glob pattern and returns a vector of streams ids matching the pattern.
use Client;
use Type;
// Connect to the RACS server
let client = open.unwrap;
// Run list command matching "*" pattern
let result = client
.pipeline
.search
.execute;
// Print the list of stream ids
if let List = result.unwrap
[!NOTE]
Stringis the only element type currently supported forType::List
Stream metadata can be queried using the info function.
info takes the stream id and metadata attribute as parameters.
use Client;
use Type;
// Connect to the RACS server
let client = open.unwrap;
// Get sample rate attribute for stream
let result = client
.pipeline
.info
.execute;
// Print the sample rate
if let Int = result.unwrap
i64 is returned for all metadata attributes. The supported attributes are:
| Attribute | Description |
|---|---|
channels |
Channel count of the audio stream. |
sample_rate |
Sample rate of the audio stream (Hz). |
bit_depth |
Bit depth of the audio stream. |
ref |
Reference timestamp (milliseconds UTC). |
size |
Size of audio stream in bytes. |
Raw Command Execution
To execute raw command strings, use the execute_command function.
use Client;
use Type;
let client = open.unwrap;
let result = client.execute_command;
if let Int = result.unwrap
Refer to the documentation in RACS for the commands.
Type Conversions
Below is a table of conversions for the Type enum between RACS and rust:
| RACS | Rust |
|---|---|
Type::Int |
i64 |
Type::Float |
f64 |
Type::Bool |
bool |
Type::String |
String |
Type::Error |
Err |
Type::Null |
N/A |
Type::U8V |
Vec<u8> |
Type::U16V |
Vec<u16> |
Type::S16V |
Vec<i16> |
Type::U32V |
Vec<u32> |
Type::S32V |
Vec<i32> |
Type::C64V |
Vec<Complex32> |
Type::List |
Vec<Type> |