libpulse_sys/
sample.rs

1// Copyright 2017 Lyndon Brown
2//
3// This file is part of the PulseAudio Rust language linking library.
4//
5// Licensed under the MIT license or the Apache license (version 2.0), at your option. You may not
6// copy, modify, or distribute this file except in compliance with said license. You can find copies
7// of these licenses either in the LICENSE-MIT and LICENSE-APACHE files, or alternatively at
8// <http://opensource.org/licenses/MIT> and <http://www.apache.org/licenses/LICENSE-2.0>
9// respectively.
10//
11// Portions of documentation are copied from the LGPL 2.1+ licensed PulseAudio C headers on a
12// fair-use basis, as discussed in the overall project readme (available in the git repository).
13
14//! Constants and routines for sample type handling.
15
16use std::os::raw::c_char;
17use num_derive::{FromPrimitive, ToPrimitive};
18
19/// Maximum number of allowed channels.
20pub const PA_CHANNELS_MAX: u8 = 32;
21
22/// Maximum allowed sample rate.
23pub const PA_RATE_MAX: u32 = 48000 * 16;
24
25/// Sample format.
26#[repr(C)]
27#[derive(Debug, Copy, Clone, PartialEq, Eq)]
28#[derive(FromPrimitive, ToPrimitive)]
29#[allow(non_camel_case_types)]
30pub enum pa_sample_format_t {
31    U8,
32    ALaw,
33    ULaw,
34    S16le,
35    S16be,
36    F32le,
37    F32be,
38    S32le,
39    S32be,
40    S24le,
41    S24be,
42    S24_32le,
43    S24_32be,
44
45    Invalid = -1,
46}
47
48pub const PA_SAMPLE_MAX: usize = 13;
49
50pub const PA_SAMPLE_U8:        pa_sample_format_t = pa_sample_format_t::U8;
51pub const PA_SAMPLE_ALAW:      pa_sample_format_t = pa_sample_format_t::ALaw;
52pub const PA_SAMPLE_ULAW:      pa_sample_format_t = pa_sample_format_t::ULaw;
53pub const PA_SAMPLE_S16LE:     pa_sample_format_t = pa_sample_format_t::S16le;
54pub const PA_SAMPLE_S16BE:     pa_sample_format_t = pa_sample_format_t::S16be;
55pub const PA_SAMPLE_FLOAT32LE: pa_sample_format_t = pa_sample_format_t::F32le;
56pub const PA_SAMPLE_FLOAT32BE: pa_sample_format_t = pa_sample_format_t::F32be;
57pub const PA_SAMPLE_S32LE:     pa_sample_format_t = pa_sample_format_t::S32le;
58pub const PA_SAMPLE_S32BE:     pa_sample_format_t = pa_sample_format_t::S32be;
59pub const PA_SAMPLE_S24LE:     pa_sample_format_t = pa_sample_format_t::S24le;
60pub const PA_SAMPLE_S24BE:     pa_sample_format_t = pa_sample_format_t::S24be;
61pub const PA_SAMPLE_S24_32LE:  pa_sample_format_t = pa_sample_format_t::S24_32le;
62pub const PA_SAMPLE_S24_32BE:  pa_sample_format_t = pa_sample_format_t::S24_32be;
63pub const PA_SAMPLE_INVALID:   pa_sample_format_t = pa_sample_format_t::Invalid;
64
65impl Default for pa_sample_format_t {
66    fn default() -> Self {
67        pa_sample_format_t::Invalid
68    }
69}
70
71pub use self::ei_formats::*;
72
73/// Endian-independent format identifiers.
74#[cfg(target_endian = "big")]
75mod ei_formats {
76    use super::pa_sample_format_t;
77
78    pub const PA_SAMPLE_S16NE:     pa_sample_format_t = pa_sample_format_t::S16be;
79    pub const PA_SAMPLE_FLOAT32NE: pa_sample_format_t = pa_sample_format_t::F32be;
80    pub const PA_SAMPLE_S32NE:     pa_sample_format_t = pa_sample_format_t::S32be;
81    pub const PA_SAMPLE_S24NE:     pa_sample_format_t = pa_sample_format_t::S24be;
82    pub const PA_SAMPLE_S24_32NE:  pa_sample_format_t = pa_sample_format_t::S24_32be;
83
84    pub const PA_SAMPLE_S16RE:     pa_sample_format_t = pa_sample_format_t::S16le;
85    pub const PA_SAMPLE_FLOAT32RE: pa_sample_format_t = pa_sample_format_t::F32le;
86    pub const PA_SAMPLE_S32RE:     pa_sample_format_t = pa_sample_format_t::S32le;
87    pub const PA_SAMPLE_S24RE:     pa_sample_format_t = pa_sample_format_t::S24le;
88    pub const PA_SAMPLE_S24_32RE:  pa_sample_format_t = pa_sample_format_t::S24_32le;
89}
90
91/// Endian-independent format identifiers.
92#[cfg(target_endian = "little")]
93mod ei_formats {
94    use super::pa_sample_format_t;
95
96    pub const PA_SAMPLE_S16NE:     pa_sample_format_t = pa_sample_format_t::S16le;
97    pub const PA_SAMPLE_FLOAT32NE: pa_sample_format_t = pa_sample_format_t::F32le;
98    pub const PA_SAMPLE_S32NE:     pa_sample_format_t = pa_sample_format_t::S32le;
99    pub const PA_SAMPLE_S24NE:     pa_sample_format_t = pa_sample_format_t::S24le;
100    pub const PA_SAMPLE_S24_32NE:  pa_sample_format_t = pa_sample_format_t::S24_32le;
101
102    pub const PA_SAMPLE_S16RE:     pa_sample_format_t = pa_sample_format_t::S16be;
103    pub const PA_SAMPLE_FLOAT32RE: pa_sample_format_t = pa_sample_format_t::F32be;
104    pub const PA_SAMPLE_S32RE:     pa_sample_format_t = pa_sample_format_t::S32be;
105    pub const PA_SAMPLE_S24RE:     pa_sample_format_t = pa_sample_format_t::S24be;
106    pub const PA_SAMPLE_S24_32RE:  pa_sample_format_t = pa_sample_format_t::S24_32be;
107}
108
109/// A shortcut for [`PA_SAMPLE_FLOAT32NE`].
110pub const PA_SAMPLE_FLOAT32: pa_sample_format_t = PA_SAMPLE_FLOAT32NE;
111
112/// A sample format and attribute specification.
113#[repr(C)]
114#[derive(Debug, Copy, Clone, PartialEq, Eq)]
115pub struct pa_sample_spec {
116    /// The sample format.
117    pub format: pa_sample_format_t,
118
119    /// The sample rate. (e.g. 44100).
120    pub rate: u32,
121
122    /// Audio channels. (1 for mono, 2 for stereo, ...).
123    pub channels: u8,
124}
125
126/// Type for usec specifications (unsigned). Always 64 bit.
127pub type pa_usec_t = u64;
128
129/// The maximum length of strings returned by [`pa_sample_spec_snprint()`].
130///
131/// Please note that this value can change with any release without warning and without being
132/// considered API or ABI breakage. You should not use this definition anywhere where it might
133/// become part of an ABI.
134pub const PA_SAMPLE_SPEC_SNPRINT_MAX: usize = 32;
135
136/// The maximum length of strings returned by [`pa_bytes_snprint()`].
137///
138/// Please note that this value can change with any release without warning and without being
139/// considered API or ABI breakage. You should not use this definition anywhere where it might
140/// become part of an ABI.
141pub const PA_BYTES_SNPRINT_MAX: usize = 11;
142
143#[rustfmt::skip]
144#[link(name = "pulse")]
145extern "C" {
146    pub fn pa_bytes_per_second(spec: *const pa_sample_spec) -> usize;
147    pub fn pa_frame_size(spec: *const pa_sample_spec) -> usize;
148    pub fn pa_sample_size(spec: *const pa_sample_spec) -> usize;
149    pub fn pa_sample_size_of_format(f: pa_sample_format_t) -> usize;
150    pub fn pa_bytes_to_usec(length: u64, spec: *const pa_sample_spec) -> pa_usec_t;
151    pub fn pa_usec_to_bytes(t: pa_usec_t, spec: *const pa_sample_spec) -> usize;
152    pub fn pa_sample_spec_init(spec: *mut pa_sample_spec) -> *mut pa_sample_spec;
153    pub fn pa_sample_format_valid(format: u32) -> i32;
154    pub fn pa_sample_rate_valid(rate: u32) -> i32;
155    pub fn pa_channels_valid(channels: u8) -> i32;
156    pub fn pa_sample_spec_valid(spec: *const pa_sample_spec) -> i32;
157    pub fn pa_sample_spec_equal(a: *const pa_sample_spec, b: *const pa_sample_spec) -> i32;
158    pub fn pa_sample_format_to_string(f: pa_sample_format_t) -> *const c_char;
159    pub fn pa_parse_sample_format(format: *const c_char) -> pa_sample_format_t;
160
161    pub fn pa_sample_spec_snprint(s: *mut c_char, l: usize, spec: *const pa_sample_spec) -> *mut c_char;
162
163    pub fn pa_bytes_snprint(s: *mut c_char, l: usize, v: u32) -> *mut c_char;
164    pub fn pa_sample_format_is_le(f: pa_sample_format_t) -> i32;
165    pub fn pa_sample_format_is_be(f: pa_sample_format_t) -> i32;
166}