Skip to content

Commit 7403c6f

Browse files
committed
Add trigger_example from issue #135
1 parent 0cd1401 commit 7403c6f

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

examples/trigger_hstamp.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use alsa::{
2+
pcm::{Access, Format, HwParams},
3+
Direction, ValueOr, PCM,
4+
};
5+
6+
use anyhow::Result; // Sorry for the extra dep :D
7+
8+
fn main() -> Result<()> {
9+
// Also tried specifying the hw card name, but it doesn't matter
10+
let pcm = PCM::new("default", Direction::Capture, false)?;
11+
12+
{
13+
let hwp = HwParams::any(&pcm)?;
14+
hwp.set_channels(2)?;
15+
hwp.set_rate(16000, ValueOr::Nearest)?;
16+
hwp.set_format(Format::s32())?;
17+
hwp.set_access(Access::RWInterleaved)?;
18+
pcm.hw_params(&hwp)?;
19+
}
20+
21+
{
22+
let swp = pcm.sw_params_current()?;
23+
swp.set_tstamp_mode(true)?;
24+
swp.set_tstamp_type(alsa::pcm::TstampType::Monotonic)?; // Tried with every kind of timestamp type, it doesn't matter
25+
pcm.sw_params(&swp)?;
26+
}
27+
28+
let mut buffer = [0i32; 8000];
29+
30+
pcm.start()?;
31+
32+
let pcm_io = pcm.io_i32()?;
33+
34+
loop {
35+
// Debug: check if timestamps are enabled (it returns true as expected)
36+
let params = pcm.sw_params_current()?;
37+
println!("timestamps enabled: {}", params.get_tstamp_mode()?);
38+
39+
let read_bytes = pcm_io.readi(&mut buffer)?;
40+
assert!(read_bytes > 0);
41+
42+
// I've also tried to construct the status object just once outside the loop but nothing changes
43+
let status = pcm.status()?;
44+
45+
dbg!(&status);
46+
47+
// The following "htstamp" functions all wrongly return a timespec struct with 0 seconds and 0 nanoseconds
48+
// when using Rust >=1.88 (even tried on Nightly to check if it worked on there)
49+
let audio_htstamp = status.get_audio_htstamp();
50+
println!("{} {}", audio_htstamp.tv_sec, audio_htstamp.tv_nsec);
51+
52+
let htstamp = status.get_htstamp();
53+
println!("{} {}", htstamp.tv_sec, htstamp.tv_nsec);
54+
55+
let trigger_htstamp = status.get_trigger_htstamp();
56+
println!("{} {}", trigger_htstamp.tv_sec, trigger_htstamp.tv_nsec);
57+
}
58+
}

0 commit comments

Comments
 (0)