#binding #pulse #pulseaudio #audio #pulse-simple

libpulse-simple-binding

A Rust language binding for the PulseAudio libpulse-simple library

19 stable releases

new 2.6.1 Apr 11, 2019
2.6.0 Mar 11, 2019
2.5.0 Dec 22, 2018
2.4.0 Nov 28, 2018
1.0.2 Feb 9, 2018

#24 in Audio

Download history 4/week @ 2018-12-28 24/week @ 2019-01-04 10/week @ 2019-01-11 9/week @ 2019-01-18 8/week @ 2019-01-25 29/week @ 2019-02-01 4/week @ 2019-02-08 3/week @ 2019-02-15 21/week @ 2019-02-22 40/week @ 2019-03-01 17/week @ 2019-03-08 41/week @ 2019-03-15 86/week @ 2019-03-22 3/week @ 2019-03-29 26/week @ 2019-04-05

100 downloads per month
Used in 1 crate

LGPL-2.1+

695KB
8K SLoC

libpulse_simple_binding

A Rust language binding for the PulseAudio libpulse-simple library.

Usage

Add this to your Cargo.toml:

[dependencies]
libpulse-binding = "2.0"
libpulse-simple-binding = "2.0"

and this to your crate root:

extern crate libpulse_binding as pulse;
extern crate libpulse_simple_binding as psimple;

lib.rs:

PulseAudio Rust language binding library for the ‘simple’ component

PulseAudio ‘simple’ provides a simple but limited synchronous playback and recording API. This is a synchronous, simplified wrapper around the standard asynchronous API.

About

This library is a binding that allows Rust code to connect to the PulseAudio sound server via PulseAudio’s existing C API. This binding provides a safe(r) Rust interface which might be preferred over the raw C API provided by the underlying sys linking crate.

This crate provides an interface to PulseAudio’s ‘simple’ component, and should be used in addition to the general libpulse_binding crate.

Introduction

The simple API is designed for applications with very basic sound playback or capture needs. It can only support a single stream per connection and has no support for handling of complex features like events, channel mappings and volume control. It is, however, very simple to use and quite sufficient for many programs.

Usage

Firstly, add a dependency on the crate in your program’s Cargo.toml file. Secondly, import the crate along with the general libpulse_binding crate to the root of your program:

extern crate libpulse_binding as pulse;
extern crate libpulse_simple_binding as psimple;

Finally, establish a connection, as below.

Connecting

The first step before using the sound system is to connect to the server. This is normally done this way:

# extern crate libpulse_binding as pulse;
# extern crate libpulse_simple_binding as psimple;
#
use psimple::Simple;
use pulse::stream::Direction;

# fn main() {
let spec = pulse::sample::Spec {
    format: pulse::sample::SAMPLE_S16NE,
    channels: 2,
    rate: 44100,
};
assert!(spec.is_valid());

let s = Simple::new(
    None,                // Use the default server
    "FooApp",            // Our application’s name
    Direction::Playback, // We want a playback stream
    None,                // Use the default device
    "Music",             // Description of our stream
    &spec,               // Our sample format
    None,                // Use default channel map
    None                 // Use default buffering attributes
).unwrap();
# }

Transferring data

Once the connection is established to the server, data can start flowing. Using the connection is very similar to the normal read() and write() system calls using read and write methods of the Simple object. Note that these operations always block.

Buffer control

  • Simple::get_latency: Will return the total latency of the playback or record pipeline, respectively.
  • Simple::flush: Will throw away all data currently in buffers.

If a playback stream is used then the following operation is available:

Cleanup

Once playback or capture is complete, the connection should be closed and resources freed. This is done automatically once the object is dropped.

Dependencies