11use crate :: amp:: Amp ;
2- use hound:: WavWriter ;
2+ use crate :: recorder:: { AudioBlock , BLOCK_FRAMES } ;
3+ use crossbeam:: channel:: Sender ;
34use jack:: { AudioIn , AudioOut , Client , Control , Port , ProcessScope } ;
45use rubato:: {
56 Resampler , SincFixedIn , SincInterpolationParameters , SincInterpolationType , WindowFunction ,
67} ;
7- use std:: fs:: File ;
8- use std:: io:: BufWriter ;
9- use std:: sync:: { Arc , Mutex } ;
10-
11- pub type RecordingWriter = Option < Arc < Mutex < Option < WavWriter < BufWriter < File > > > > > > ;
128
139pub struct Processor {
1410 amp : Amp ,
15- writer : RecordingWriter ,
11+ tx : Option < Sender < AudioBlock > > ,
1612 in_port : Port < AudioIn > ,
1713 out_l : Port < AudioOut > ,
1814 out_r : Port < AudioOut > ,
@@ -21,7 +17,7 @@ pub struct Processor {
2117}
2218
2319impl Processor {
24- pub fn new ( client : & Client , amp : Amp , recording : bool ) -> ( Self , RecordingWriter ) {
20+ pub fn new ( client : & Client , amp : Amp , tx : Option < Sender < AudioBlock > > ) -> Self {
2521 let in_port = client. register_port ( "in" , AudioIn ) . unwrap ( ) ;
2622 let out_l = client. register_port ( "out_l" , AudioOut ) . unwrap ( ) ;
2723 let out_r = client. register_port ( "out_r" , AudioOut ) . unwrap ( ) ;
@@ -30,26 +26,6 @@ impl Processor {
3026 let _ = client. connect_ports_by_name ( "rustortion:out_l" , "system:playback_1" ) ;
3127 let _ = client. connect_ports_by_name ( "rustortion:out_r" , "system:playback_2" ) ;
3228
33- let sample_rate = client. sample_rate ( ) as f32 ;
34-
35- let writer = if recording {
36- let spec = hound:: WavSpec {
37- channels : 2 ,
38- sample_rate : sample_rate as u32 ,
39- bits_per_sample : 16 ,
40- sample_format : hound:: SampleFormat :: Int ,
41- } ;
42- let filename = format ! (
43- "./recordings/recording_{}.wav" ,
44- chrono:: Local :: now( ) . format( "%Y%m%d_%H%M%S" )
45- ) ;
46- println ! ( "Recording to: {}" , filename) ;
47- let writer = hound:: WavWriter :: create ( filename, spec) . unwrap ( ) ;
48- Some ( Arc :: new ( Mutex :: new ( Some ( writer) ) ) )
49- } else {
50- None
51- } ;
52-
5329 let channels = 1 ;
5430 let oversample_factor: f32 = 2.0 ;
5531
@@ -88,26 +64,23 @@ impl Processor {
8864 )
8965 . unwrap ( ) ;
9066
91- (
92- Self {
93- amp,
94- writer : writer. clone ( ) ,
95- in_port,
96- out_l,
97- out_r,
98- upsampler,
99- downsampler,
100- } ,
101- writer,
102- )
67+ Self {
68+ amp,
69+ tx,
70+ in_port,
71+ out_l,
72+ out_r,
73+ upsampler,
74+ downsampler,
75+ }
10376 }
10477
10578 pub fn into_process_handler (
10679 self ,
10780 ) -> impl FnMut ( & Client , & ProcessScope ) -> Control + Send + ' static {
10881 let Processor {
10982 mut amp,
110- writer ,
83+ tx ,
11184 in_port,
11285 mut out_l,
11386 mut out_r,
@@ -167,15 +140,13 @@ impl Processor {
167140 out_buf_r[ i] = out_sample;
168141 }
169142
170- if let Some ( mut writer_mutex) = writer. as_ref ( ) . map ( |w| w. lock ( ) . unwrap ( ) ) {
171- if let Some ( ref mut writer) = * writer_mutex {
172- for & s in final_samples. iter ( ) . take ( frames_to_copy) {
173- let sample_i16 =
174- ( s * i16:: MAX as f32 ) . clamp ( i16:: MIN as f32 , i16:: MAX as f32 ) as i16 ;
175- writer. write_sample ( sample_i16) . unwrap ( ) ; // left
176- writer. write_sample ( sample_i16) . unwrap ( ) ; // right
177- }
143+ if let Some ( ref tx) = tx {
144+ let mut block = AudioBlock :: with_capacity ( BLOCK_FRAMES * 2 ) ;
145+ for & s in final_samples. iter ( ) . take ( BLOCK_FRAMES ) {
146+ let v = ( s * i16:: MAX as f32 ) . clamp ( i16:: MIN as f32 , i16:: MAX as f32 ) as i16 ;
147+ block. extend_from_slice ( & [ v, v] ) ;
178148 }
149+ let _ = tx. try_send ( block) ; // never blocks
179150 }
180151
181152 Control :: Continue
0 commit comments