@@ -3,7 +3,7 @@ use std::thread;
33
44use bytes:: { BufMut , Bytes , BytesMut } ;
55use cpal:: traits:: { DeviceTrait , HostTrait , StreamTrait } ;
6- use cpal:: Sample ;
6+ use cpal:: { Sample , SampleFormat } ;
77use crossbeam:: channel:: RecvError ;
88use deepgram:: common:: options:: Encoding ;
99use futures:: channel:: mpsc:: { self , Receiver as FuturesReceiver } ;
@@ -12,6 +12,25 @@ use futures::SinkExt;
1212
1313use deepgram:: { Deepgram , DeepgramError } ;
1414
15+ macro_rules! create_stream {
16+ ( $device: ident, $config: expr, $sync_tx: ident, $sample_type: ty) => {
17+ $device
18+ . build_input_stream(
19+ & $config. into( ) ,
20+ move |data: & [ $sample_type] , _: & _| {
21+ let mut bytes = BytesMut :: with_capacity( data. len( ) * 2 ) ;
22+ for sample in data {
23+ bytes. put_i16_le( sample. to_sample( ) ) ;
24+ }
25+ $sync_tx. send( bytes. freeze( ) ) . unwrap( ) ;
26+ } ,
27+ |_| panic!( ) ,
28+ None ,
29+ )
30+ . unwrap( )
31+ } ;
32+ }
33+
1534fn microphone_as_stream ( ) -> FuturesReceiver < Result < Bytes , RecvError > > {
1635 let ( sync_tx, sync_rx) = crossbeam:: channel:: unbounded ( ) ;
1736 let ( mut async_tx, async_rx) = mpsc:: channel ( 1 ) ;
@@ -30,45 +49,12 @@ fn microphone_as_stream() -> FuturesReceiver<Result<Bytes, RecvError>> {
3049 // dbg!(&config);
3150
3251 let stream = match config. sample_format ( ) {
33- cpal:: SampleFormat :: F32 => device
34- . build_input_stream (
35- & config. into ( ) ,
36- move |data : & [ f32 ] , _: & _ | {
37- let mut bytes = BytesMut :: with_capacity ( data. len ( ) * 2 ) ;
38- for sample in data {
39- bytes. put_i16_le ( sample. to_i16 ( ) ) ;
40- }
41- sync_tx. send ( bytes. freeze ( ) ) . unwrap ( ) ;
42- } ,
43- |_| panic ! ( ) ,
44- )
45- . unwrap ( ) ,
46- cpal:: SampleFormat :: I16 => device
47- . build_input_stream (
48- & config. into ( ) ,
49- move |data : & [ i16 ] , _: & _ | {
50- let mut bytes = BytesMut :: with_capacity ( data. len ( ) * 2 ) ;
51- for sample in data {
52- bytes. put_i16_le ( * sample) ;
53- }
54- sync_tx. send ( bytes. freeze ( ) ) . unwrap ( ) ;
55- } ,
56- |_| panic ! ( ) ,
57- )
58- . unwrap ( ) ,
59- cpal:: SampleFormat :: U16 => device
60- . build_input_stream (
61- & config. into ( ) ,
62- move |data : & [ u16 ] , _: & _ | {
63- let mut bytes = BytesMut :: with_capacity ( data. len ( ) * 2 ) ;
64- for sample in data {
65- bytes. put_i16_le ( sample. to_i16 ( ) ) ;
66- }
67- sync_tx. send ( bytes. freeze ( ) ) . unwrap ( ) ;
68- } ,
69- |_| panic ! ( ) ,
70- )
71- . unwrap ( ) ,
52+ SampleFormat :: F32 => create_stream ! ( device, config, sync_tx, f32 ) ,
53+ SampleFormat :: I16 => create_stream ! ( device, config, sync_tx, i16 ) ,
54+ SampleFormat :: U16 => create_stream ! ( device, config, sync_tx, u16 ) ,
55+ sample_format => {
56+ panic ! ( "Unsupported sample format: {sample_format:?}" ) ;
57+ }
7258 } ;
7359
7460 stream. play ( ) . unwrap ( ) ;
@@ -109,7 +95,7 @@ async fn main() -> Result<(), DeepgramError> {
10995
11096 println ! ( "Deepgram Request ID: {}" , results. request_id( ) ) ;
11197 while let Some ( result) = results. next ( ) . await {
112- println ! ( "got: {:?}" , result ) ;
98+ println ! ( "got: {result :?}" ) ;
11399 }
114100
115101 Ok ( ( ) )
0 commit comments