summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 6e247c9d053451c209f3852a8dab11b16925734e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
use cpal::traits::HostTrait;
use cpal::traits::DeviceTrait;
use cpal::traits::StreamTrait;

use eframe;
use eframe::egui;
use egui::plot::*;

use std::io::Write;

use std::time::{Duration, Instant};
use rustfft::num_complex::Complex32;

use fftw::array::AlignedVec;
use fftw::plan::*;
use fftw::types::*;
use std::f64::consts::PI;

use std::time;
use std::thread;
use std::sync::mpsc;

use std::fs::File;
use std::path::Path;


struct App {
    val: bool,
    it: u32,
}

impl Default for App {
    fn default() -> Self {
        Self {
            val: false,
            it: 0,
        }
    }
}

impl eframe::App for App {
    fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
        egui::SidePanel::left("side_panel").show(ctx, |ui| {
            let sin: PlotPoints = (0..1000).map(|i| {
                let x = i as f64 * 0.01;
                [x, x.sin() * (self.it as f64 * 0.01).cos()]
            }).collect();
            
            
            
            if self.val {
                ui.ctx().request_repaint();
                self.it += 1;
            }
        
            let line = Line::new(sin);
        
            Plot::new("plot").view_aspect(2.0).center_y_axis(true).show(ui, |plui| plui.line(line));
        
            ui.checkbox(&mut self.val, "Animate");
            println!("{}", &self.val);
            
            // std::thread::sleep(std::time::Duration::from_millis(1))
        });
        
        egui::TopBottomPanel::top("top").show(ctx, |_ui| {
            _ui.label("lol");
        
        });
    }
}



struct SamplePacket {
    sample_rate: u32,
    data: Box<[Complex32]>
}

impl SamplePacket {
    fn new(sample_rate: u32, data: Box<[Complex32]>) -> Self {
        Self {
            sample_rate: sample_rate,
            data: data
        }
    }
}

trait Process {
    fn process(&mut self, src: SamplePacket) -> SamplePacket;
}

struct PipelineBuilder {
    input: mpsc::SyncSender<SamplePacket>,
    output: mpsc::Receiver<SamplePacket>
}

impl PipelineBuilder {
    fn new() -> Self {
        let (input, output) = mpsc::sync_channel(1);
    
        Self {
            input: input,
            output: output
        }
    }

    fn pipe<T>(mut self, mut proc: T) -> Self
    where
    T: Process + Send + 'static
    {
        let (proc_out, output) = mpsc::sync_channel(1);
        
        let proc_in = std::mem::replace(&mut self.output, output);
    
        thread::spawn(move || {
            loop {
                let src = match proc_in.recv() {
                    Ok(s) => s,
                    Err(_) => break,
                };
                
                let dst = proc.process(src);
                
                if proc_out.send(dst).is_err() {
                    break;
                }
            }
        });
        
        self
    }
    
    fn finish(self) -> (mpsc::SyncSender<SamplePacket>, mpsc::Receiver<SamplePacket>) {
        (self.input, self.output)
    }
}

struct Squarer;
impl Process for Squarer {
    fn process(&mut self, mut src: SamplePacket) -> SamplePacket {
        for sample in src.data.iter_mut() {
            sample.re = (sample.re * 10.0) as i32 as f32 / 10.0;
            sample.im = (sample.im * 10.0) as i32 as f32 / 10.0;
        }
        
        src
    }
}

struct BandFilter {
    wrap_around: Box<[f32]>,
    window_filter: Box<[f32]>,
    test: usize,
    agc: f32,
    phase: f32
}

impl BandFilter {
    fn new(window_size: usize) -> Self {
        let mut window_filter = vec![0.0f32; window_size].into_boxed_slice();
        
    
        Self {
            wrap_around: vec![0.0f32; window_size - 1].into_boxed_slice(),
            window_filter: window_filter,
            test: 0,
            agc: 1.0,
            phase: 0.0
        }
    }
}

impl Process for BandFilter {
    fn process(&mut self, mut src: SamplePacket) -> SamplePacket {
        if src.data.len() != 250000 {
            println!("smh {}", src.data.len());
            return src;
        }
        
        use rustfft::{Fft, FftDirection, algorithm::Radix4};
        use rustfft::num_complex::Complex32;
        
        let mut fft_planner = rustfft::FftPlannerAvx::new().unwrap();
        
        let len = 250000;
        let fft = fft_planner.plan_fft_forward(len);
        let ifft = fft_planner.plan_fft_inverse(len);
        
        src.data.iter_mut().enumerate().for_each(|(i, c)| {
            let phase = i as f32 / len as f32 * 2.0 * 3.14159265359 * 0.0;
            
            *c *= Complex32 { re: phase.cos(), im: phase.sin() };
        });
    
    
        fft.process(&mut src.data);
        
        src.data[100000..len-100000].iter_mut().for_each(|c| *c = Complex32::default());
        
        ifft.process(&mut src.data);
        
        src.data.iter_mut().enumerate().for_each(|(i, c)| {
            *c /= len as f32;
    
            //let (_, phase) = c.to_polar();
            let (_, phase) = c.to_polar();
            
            let mut diff = phase - self.phase;
           
            if diff > 3.141592 {
                diff -= 2.0 * 3.141592;
            } else if diff <= -3.141592 {
                diff += 2.0 * 3.141592;
            }
            
            let deriv = diff * 250000.0 / 1000000.0;
            self.phase = phase;           

            c.re = deriv;
            c.im = 0.0;
            
            let bytes = unsafe { std::mem::transmute::<Complex32, [u8;8]>(*c) };
            
            // std::io::stdout().write(&bytes);
        });
    
        src
    }
}

struct Resampler {
    output_rate: u32,
}

impl Resampler {
    fn new(output_rate: u32) -> Self {
        Self {
            output_rate: output_rate,
        }
    }
}

impl Process for Resampler {
    fn process(&mut self, src: SamplePacket) -> SamplePacket {
        if src.sample_rate == self.output_rate {
            return src;
        }
    
        let skip = src.sample_rate as f32 / self.output_rate as f32;
        let length = (src.data.len() as f32 / skip) as usize;
    
        let mut dst = vec![Complex32::default();length].into_boxed_slice();
        
        //dst.copy_from_slice(&src.data);
        let mut idx = 0.0f32;
        let mut isrc = idx as usize;
        let mut idst = 0;
        while idst < dst.len() && isrc < src.data.len() {
            dst[idst] = src.data[isrc];
        
            idx += skip;
            isrc = idx as usize;
            idst += 1;
        }
        
        SamplePacket::new(self.output_rate, dst)
    }
}

struct AudioSink {
    recv: mpsc::Receiver<SamplePacket>,
    buffer: Option<SamplePacket>,
    idx: usize
}

impl AudioSink {
    fn new(recv: mpsc::Receiver<SamplePacket>) -> Self {
        Self {
            recv: recv,
            buffer: None,
            idx: 0,
        }
    }
    
    fn pour(&mut self, mut dst: &mut [f32]) {
        loop {
            match self.buffer.as_ref() {
                Some(src) => {
                    let src = &src.data;
                    let src_new = &src[self.idx..];
                    let count = std::cmp::min(src_new.len(), dst.len());
                    let src_new = &src_new[..count];
                    let dst_new = &mut dst[..count];
                    
                    dst_new.iter_mut().zip(src_new.iter())
                        .for_each(|(d, s)| *d = s.re);
                    
                    self.idx += count;
                    
                    if self.idx >= src.len() {
                        self.buffer = None;
                        self.idx = 0;
                    }
                    
                    if dst.len() > count {
                        dst = &mut dst[count..];
                        continue;
                    }
                    
                    return;
                },
                None => {
                    self.buffer = Some(self.recv.recv().unwrap());
                }
            };
        }
    }
}

fn main() {
    eframe::run_native(
        "λ",
        eframe::NativeOptions::default(),
        Box::new(|_cc| Box::new(App::default()))
    );

    let host = cpal::default_host();
    let dev = host.default_output_device().expect("no audio device found");
    
    let cfg: cpal::StreamConfig = dev.supported_output_configs()
        .expect("couldn't find configs")
        .next()
        .expect("bogus device")
        .with_max_sample_rate().into();
    let sample_rate = cfg.sample_rate.0 * cfg.channels as u32;
    let denom = cfg.sample_rate.0 as f32 * cfg.channels as f32;

    //println!("sample_rate{}", sample_rate);
    
    let mut inp_file = File::open(Path::new("baseband_100100000Hz_19-35-33_03-11-2022.wav")).unwrap();
    let (header, data) = wav::read(&mut inp_file).unwrap();
    let data = match data {
        wav::BitDepth::ThirtyTwoFloat(d) => d,
        wav::BitDepth::TwentyFour(s) => s.into_iter().map(|m: i32| m as f32 / (1<<23) as f32).collect::<Vec<f32>>(),
        wav::BitDepth::Sixteen(s) => s.into_iter().map(|m: i16| m as f32 / i16::MAX as f32).collect::<Vec<f32>>(),
        _ => panic!("unsupported bit depth")
    };
    
    
    let (send, recv) = PipelineBuilder::new()
        .pipe(BandFilter::new(1024))
        .pipe(Resampler::new(sample_rate))
        .finish();

    let mut snk = AudioSink::new(recv);

    let mut snum: f32 = 0.0;
    let stream = dev.build_output_stream(
        &cfg,
        move |data: &mut [f32], _: &cpal::OutputCallbackInfo| {
            snk.pour(data);
        },
        move |err| {
            println!("err: {}", err);
        }
    ).expect("could not build stream");

    stream.play().unwrap();

    let mut snum: usize = 0;
        

    //println!("{}", header.channel_count);
    
    let sampling_rate = header.sampling_rate as usize;
    let channel_count = header.channel_count as usize;
    
    let mut i = 0;
    while i + sampling_rate*channel_count < data.len() {
        let mut samples = vec![Complex32::default();sampling_rate].into_boxed_slice();
        
        samples.iter_mut().zip(data[i..i + sampling_rate*channel_count].chunks(2))
            .for_each(|(a, b)| *a = Complex32 { re: b[0], im: b[1] });
        
        send.send(SamplePacket::new(header.sampling_rate, samples)).unwrap();
        i += sampling_rate*channel_count;
    }
}