rust-racs
rust-racs is the rust client library for RACS.
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.2"
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 Audio
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. Then PCM data is chunked into frames
and streamed to the RACS server.
use Client;
// Connect to the RACS server
let mut client = open.unwrap;
// Create a new audio stream using pipeline
client.pipeline
.create // stream-id, sample-rate, channels, bit-depth
.execute
.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
.chunk_size // 32 KB
.batch_size
.compression
.compression_level // ZSTD levels: 0-8
.execute
.unwrap;
If chunk_size, batch_size, compression and compression_level are not provided, the default values will be used.
// Stream audio data to the server
client.stream.execute.unwrap;
Stream ids stored in RACS can be queried using the list command. list takes a glob pattern and returns a list of streams ids matching the pattern.
use Client;
let client = open.unwrap;
let result = client.pipeline
.list
.execute
.unwrap;
// List([String("vocals")])
println!;
Extracting Audio
The below example extracts a 30-second PCM audio segment using the range command. It then encodes the data to MP3 and writes the resulting bytes to a file.
use Client;
use Type;
use File;
use Write;
// Connect to the RACS server
let client = open.unwrap;
// Extract PCM data
// Encode to MP3
let result = client.pipeline
.range // stream-id, start-time (seconds), duration (seconds)
.encode // mime-type
.execute
.unwrap;
// Write to a file
if let U8V = result
Metadata
Stream metadata can be retrieved using the meta command. meta 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
.meta
.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 uncompressed 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> |