]>
code.octet-stream.net Git - m17rt/blob - m17app/src/rtlsdr.rs
33f8070621ae1eb08e05eebdc6cf0f496383b08b
3 process
::{Child
, Command
, Stdio
},
5 mpsc
::{sync_channel
, Receiver
, SyncSender
},
8 time
::{Duration
, Instant
},
12 traits
::{DeviceTrait
, HostTrait
, StreamTrait
},
13 SampleFormat
, SampleRate
, Stream
,
18 soundmodem
::{InputSource
, OutputBuffer
, OutputSink
, SoundmodemEvent
},
24 rtlfm
: Mutex
<Option
<Child
>>,
28 pub fn new(device_index
: usize, frequency_mhz
: f32) -> Result
<Self, M17Error
> {
32 rtlfm
: Mutex
::new(None
),
37 impl InputSource
for RtlSdr
{
38 fn start(&self, tx
: SyncSender
<SoundmodemEvent
>) {
39 // TODO: error handling
40 let mut cmd
= Command
::new("rtl_fm")
45 &format
!("{:.6}M", self.freq
uency
_mhz
),
47 &self.device_index
.to_string(),
51 .stdout(Stdio
::piped())
54 let mut stdout
= cmd
.stdout
.take().unwrap
();
55 let mut buf
= [0u8; 1024];
56 let mut leftover
: Option
<u8> = None
;
57 std
::thread
::spawn(move || {
58 while let Ok(n
) = stdout
.read(&mut buf
) {
59 let mut start_idx
= 0;
60 let mut samples
= vec
![];
61 if let Some(left
) = leftover
{
63 samples
.push(i16::from_le_bytes([left
, buf
[0]]));
68 for sample
in buf
[start_idx
..n
].chunks(2) {
69 if sample
.len() == 2 {
70 samples
.push(i16::from_le_bytes([sample
[0], sample
[1]]))
72 leftover
= Some(sample
[0]);
76 .send(SoundmodemEvent
::BasebandInput(samples
.into
()))
86 if let Some(mut process
) = self.rtlfm
.lock().unwrap
().take() {
87 let _
= process
.kill();