summaryrefslogtreecommitdiff
path: root/src/buf.rs
blob: fb4a6ddb1b5cd82e7d207037d2dd319847fc38d1 (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
use crate::proc::{Procer, ProcerData, Update};
use crate::notes::{Note, NoteValue, ProcerNote};
use crate::outputers::{Outputer, Outputers, SimpleOutputer};
use crate::rfft::RFftProcer;
use realfft::FftNum;
use rustfft::num_traits::float::Float;
use alsa::{Direction, ValueOr};
use alsa::pcm::{PCM, HwParams, Format, Access, State};
use derive_more::Display;
use std::fmt::Debug;
use std::io::Write;
//use std::collections::VecDeque;

pub enum Procers<I: Default + Clone + FftNum + Float, const US: usize, const BS: usize> {
    Rfft(RFftProcer<I, US, BS>),
    // XXX: if i add a fir filter that doesn't need a buffer i think
    // i can just spec it as Fir12Tet(Fir12TetProcer<I, US>) or similar
    // here
}

#[derive(Display)]
#[display(fmt="StaticBuffer(rate={}, psize={}, channels={}, opsize={}, adev=(name={}), outdev=(name={:?}), cperiods={}, operiods={})", rate, period_size, channels, out_period_size, adev_name, outdev_name, capture_periods, output_periods)]
pub struct StaticBuffer<'nl, I: Default + Debug + Clone + FftNum + Float + NoteValue, const US: usize, const BS: usize> {
    pub rate: u32,
    pub period_size: usize,
    pub channels: u32,
    pub out_period_size: usize,
    pub procers: Vec<(Procers<I, US, BS>, ProcerData<'nl, I>)>,
    pub adev_name: String,
    pub adev: PCM,
    pub outdev_name: Option<String>,
    pub outdev: Option<PCM>,
    pub outputer: Outputers,
    pub capture_periods: u32,
    pub output_periods: u32,
}

impl<'nl, I: Default + Debug + Clone + FftNum + Float + From<i16> + NoteValue, const US: usize, const BS: usize> StaticBuffer<'nl, I, US, BS> {
    pub fn new(rate: u32, channels: u32, procers: Vec<(Procers<I, US, BS>, ProcerData<'nl, I>)>, adev_name: String, outdev_name: Option<String>, outputer: Outputers) -> Self {
        let mut adev = PCM::new(&adev_name, Direction::Capture, true).unwrap();
        let mut capture_periods = 0;
        {
            let hwp = HwParams::any(&adev).unwrap();
            hwp.set_channels(channels).unwrap();
            //hwp.set_rate_resample(false);
            hwp.set_rate(rate, ValueOr::Nearest).unwrap();
            hwp.set_format(Format::s16()).unwrap();
            hwp.set_access(Access::MMapInterleaved).unwrap();
            hwp.set_period_size(US as i64, ValueOr::Nearest).unwrap();
            hwp.set_periods(8, ValueOr::Nearest).unwrap();
            // TODO: directly using the ALSA ring buffer and copying directly from it instead of
            // going through the Update/VecDeque might be interesting in the future, i think it'd
            // require changing the const sizes to runtime determined sizes though, so future
            // changes to try~
            adev.hw_params(&hwp).unwrap();
            capture_periods = hwp.get_periods().unwrap();
            println!("capture periods: {}", capture_periods);
        }
        let (outdev, out_period_size, output_periods) = match &outdev_name {
            None => (None, 0, 0),
            Some(od_name) => {
                let mut outdev = PCM::new(&od_name, Direction::Playback, true).unwrap();
                let mut output_periods = 0;
                let mut out_period_size = 0;
                {
                    let hwp = HwParams::any(&outdev).unwrap();
                    hwp.set_channels(channels).unwrap();
                    hwp.set_rate(rate, ValueOr::Nearest).unwrap();
                    hwp.set_format(Format::s16()).unwrap();
                    hwp.set_access(Access::RWInterleaved).unwrap();
                    //hwp.set_period_size(US as i64, ValueOr::Nearest).unwrap();
                    //hwp.set_periods(8, ValueOr::Nearest).unwrap();
                    outdev.hw_params(&hwp).unwrap();
                    output_periods = hwp.get_periods().unwrap();
                    out_period_size = hwp.get_period_size().unwrap() as usize;
                    println!("output periods: {}", output_periods);
                }
                (Some(outdev), out_period_size, output_periods)
            }
        };
        return Self {
            rate, procers, adev_name, adev, outdev_name, outdev,
            period_size: US, out_period_size: out_period_size,
            channels: channels,
            outputer,
            capture_periods, output_periods,
        }
    }

    pub fn capture_loop(&mut self) {
        let empty: Vec<i16> = vec![0i16; US * self.channels as usize];
        //let mut line_str: String = format!("\r{:80}", "");
        let in_io = self.adev.io_i16().unwrap();
        let out_io_opt = {
            match &self.outdev {
            None => (None),
            Some(outdev) => {
                //self.adev.link(&outdev).unwrap();
                let out_io = outdev.io_i16().unwrap();
                // queue up an empty period in the outdev so we can sync between in and out devices
                // with a 4 period latency
                for _i in 0..std::cmp::min(4, self.output_periods/2) {
                    out_io.writei(&empty).unwrap();
                }
                outdev.start();//.unwrap();
                println!("output available: {}", outdev.avail().unwrap());
                Some(out_io)
            }
            }
        };
        self.adev.start();//.unwrap();
        //let vt: I = I::from(112);
        let conv_scale: I = <I as From<i16>>::from(i16::MAX);
        println!("input available: {}", self.adev.avail_update().unwrap());
        let mut latentframes = 0;
        let mut unstarted = false;
        loop {
            /*println!("output available: {}", self.outdev.avail().unwrap());
            println!("input available: {}", self.adev.avail_update().unwrap());*/
            self.adev.wait(None).unwrap();
            //println!("a");
            // should be called right before mmap_begin according to:
            // https://www.alsa-project.org/alsa-doc/alsa-lib/group___p_c_m___direct.html#ga6d4acf42de554d4d1177fb035d484ea4
            let avail_frames = self.adev.avail_update().unwrap();
            in_io.mmap(US, |in_slice| {
                let size = in_slice.len();
                //println!("slice length: {}", size);
                if size == 0 {
                    println!("warning: passed a 0-sized buffer from mmap\n");
                    return 0;
                }
                let stride = self.channels as usize;
                if size != (US * stride) {
                    println!("warning: passed a {} sized buffer from mmap\n\n", size);
                    return 0;
                }
                assert_eq!(size, US * stride);
                let (written, oa) =  match &out_io_opt {
                    Some(out_io) => {
                        let wri = out_io.writei(&in_slice).unwrap();
                        assert_eq!(wri, (size/stride));
                        let oaa = self.outdev.as_ref().unwrap().avail().unwrap();
                        (wri, oaa)
                    }
                    None => (size/stride, 0i64),
                };
                let ia = self.adev.avail_update().unwrap();
                let total_outbuf: i64 = oa - ia;
                /*print!("\rtotal: {}, out: {}, in: {}", total_outbuf, oa, ia);
                std::io::stdout().flush().unwrap();*/
                //return written;
                //let constrained_slice = in_slice[0..US*stride];
                let mut update: Update<I, US> = [I::default(); US];
                // copy over the left channel data (even indexes)
                // TODO/XXX: see if this uses SIMD or needs optimizing :o
                // using iter and map/function that does strides would prob also work well :o
                // gotta scale this from i16::MAX to float, alsa
                // seems to divide by i16::MAX, not sure if there's a better way to do this
                for i in 0..US {
                    update[i] = <I as From<i16>>::from(in_slice[i*stride]) / conv_scale;
                };
                // hmm, i only need to use 1 procer here for displaying, not sure what to do with
                // the rest :o
                //for (procer, pdata) in self.procers { }
                let (ref mut procer, ref mut pdata) = self.procers.first_mut().unwrap();
                let mut processed = false;
                {
                    let mut pnotes = pdata.pnotes.0.as_mut_slice();
                    //let processed = true;
                    processed = procer.process_data(&update, pnotes);
                }
                if processed {
                    self.outputer.handle_pnotes(&mut pdata.pnotes);
                    //let mut position = 1;
                    // TODO: maybe copy, inplace probably works for now though
                    /*pnotes.sort_unstable_by(|pn1, pn2| (-pn1.2).partial_cmp(&-pn2.2).unwrap());
                    print!("\r{:-80}", pdata.pnotes);
                    std::io::stdout().flush();*/
                }
                //let mut position = 0;
                //let mut remaining = in_slice.len();
                //let mut remain_frames = remaining/(self.channels as usize);
                //let mut handled = false;
                //self.outdev.wait(None).unwrap();
                //println!("wrote {} frames", written);
                /*while handled == false {
                    self.outdev.wait(None).unwrap();
                    out_io.mmap(remain_frames, |out_slice| {
                        let outlen = out_slice.len();
                        println!("out slice len: {}", out_slice.len());
                        if outlen == 0 {
                            return 0;
                        }
                        out_slice.clone_from_slice(&in_slice);
                        handled = true;
                        return out_slice.len()/(self.channels as usize);
                    }).unwrap();
                }*/
                /*if latentframes > 0 {
                    latentframes -= 1;
                } else if unstarted {
                    unstarted = false;
                    println!("starting outdev");
                    self.outdev.start().unwrap();
                }*/
                return in_slice.len()/(self.channels as usize);
            }).unwrap();
        }
    }
    //fn new(
}

/*impl StaticBuffer<'nl, B> {
}*/

//impl<'nl, B> StaticBuffer<'nl, A> {
    //pub fn fill_initial(&self, 
    //pub fn new(rate: usize, period_size: usize, out_period_size: usize, procers: Vec<(Procers<f64>, ProcerData<'nl>)>, 
//}


impl<I: Default + Clone + FftNum + Float + NoteValue, const US: usize, const BS: usize> Procer<I, US> for Procers<I, US, BS> {
    fn get_size(&self) -> usize {
        match self {
            Procers::Rfft(rfp) => rfp.get_size(),
        }
    }
    fn get_frequency(&self) -> usize {
        match self {
            Procers::Rfft(rfp) => rfp.get_frequency(),
        }
    }
    fn make_pnotes<'nl>(&self, notes: &'nl [Note]) -> Vec<ProcerNote<'nl, I>> {
        match self {
            Procers::Rfft(rfp) => rfp.make_pnotes(notes),
        }
    }
    fn process_data(&mut self, input: &Update<I, US>, notes: &mut [ProcerNote<I>]) -> bool {
        match self {
            Procers::Rfft(rfp) => rfp.process_data(input, notes),
        }
    }
}