PulseTTY 1.1.0

A terminal-based music visualiser (system audio, microphone, or file), featuring multiple render modes.
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
use std::fs::File;
use symphonia::core::audio::SampleBuffer;
use symphonia::core::codecs::{DecoderOptions, CODEC_TYPE_NULL};
use symphonia::core::errors::Error;
use symphonia::core::formats::FormatOptions;
use symphonia::core::io::{MediaSourceStream, MediaSourceStreamOptions};
use symphonia::core::meta::MetadataOptions;
use symphonia::core::probe::Hint;
use wasapi::{self, AudioCaptureClient, WaveFormat};
use crate::FFT_SIZE;
use std::path::Path;

pub enum AudioSource {
    System {
        format: WaveFormat,
        capture_client: AudioCaptureClient,
        readpos: usize,
    },
    Microphone {
        format: WaveFormat,
        capture_client: AudioCaptureClient,
        readpos: usize,
    },
    File {
        format: Box<dyn symphonia::core::formats::FormatReader>,
        sample_buf: Option<SampleBuffer<f32>>,
        decoder: Box<dyn symphonia::core::codecs::Decoder>,
        track_id: u32,
    },
}

pub struct AudioState {
    pub source: AudioSource,
    pub sample_rate: f32,
    pub buffer: Vec<f32>,
}

impl AudioState {
    pub fn from_file(path: &str) -> Self {
        let file = Box::new(File::open(path).expect("Failed to open file."));
        let mss = MediaSourceStream::new(file, MediaSourceStreamOptions::default());

        let mut hint = Hint::new();
        if let Some(extension) = Path::new(path).extension().and_then(|e| e.to_str()) {
            hint.with_extension(extension);
        }
        let format_opts = FormatOptions::default();
        let metadata_opts = MetadataOptions::default();
        let decoder_opts = DecoderOptions::default();

        let probed = symphonia::default::get_probe()
            .format(&hint, mss, &format_opts, &metadata_opts)
            .expect("no support for format");

        let format = probed.format;
        let track = format
            .tracks()
            .iter()
            .find(|t| t.codec_params.codec != CODEC_TYPE_NULL)
            .expect("no supported audio tracks");

        let decoder = symphonia::default::get_codecs()
            .make(&track.codec_params, &decoder_opts)
            .expect("unsupported codec");

        let track_id = track.id;

        Self {
            source: AudioSource::File { format, sample_buf: None, decoder, track_id },
            sample_rate: 44100f32,
            buffer: Vec::new(),
        }
    }

    pub fn from_system(device_selector: Option<&str>) -> Self {
        wasapi::initialize_mta().unwrap();
        let enumerator = wasapi::DeviceEnumerator::new().unwrap();

        let device = match device_selector {
            Some(sel) => pick_render_device(Some(sel)),
            None => enumerator.get_default_device(&wasapi::Direction::Render).unwrap(),
        };

        let mut audio_client = device.get_iaudioclient().unwrap();
        let format = audio_client.get_mixformat().unwrap();
        audio_client.initialize_client(
            &format,
            &wasapi::Direction::Capture,
            &wasapi::StreamMode::PollingShared {autoconvert: true, buffer_duration_hns: 100000},
        ).unwrap();
        let capture_client = audio_client.get_audiocaptureclient().unwrap();
        audio_client.start_stream().unwrap();


        Self {
            sample_rate: format.get_samplespersec() as f32,
            buffer: Vec::new(),
            source: AudioSource::System { capture_client, format, readpos: 0usize },
        }
    }

    pub fn from_microphone(device_selector: Option<&str>) -> Self {
        wasapi::initialize_mta().unwrap();
        let enumerator = wasapi::DeviceEnumerator::new().unwrap();

        let device = match device_selector {
            Some(sel) => pick_capture_device(Some(sel)),
            None => enumerator.get_default_device(&wasapi::Direction::Capture).unwrap(),
        };

        let mut audio_client = device.get_iaudioclient().unwrap();
        let format = audio_client.get_mixformat().unwrap();
        audio_client.initialize_client(
            &format,
            &wasapi::Direction::Capture,
            &wasapi::StreamMode::PollingShared {autoconvert: true, buffer_duration_hns: 100000},
        ).unwrap();
        let capture_client = audio_client.get_audiocaptureclient().unwrap();
        audio_client.start_stream().unwrap();


        Self {
            sample_rate: format.get_samplespersec() as f32,
            buffer: Vec::new(),
            source: AudioSource::Microphone { capture_client, format, readpos: 0usize },
        }
    }

    pub fn next_sample(&mut self) -> Result<bool, Error> {
        match &mut self.source {
            AudioSource::File {
                format,
                decoder,
                track_id,
                sample_buf,
            } => {
                let packet = match format.next_packet() {
                    Ok(packet) => packet,
                    Err(Error::ResetRequired) => {
                        return Err(Error::ResetRequired);
                    }
                    Err(Error::IoError(err)) if err.kind() == std::io::ErrorKind::UnexpectedEof => {
                        return Ok(false);
                    }
                    Err(err) => {
                        eprintln!("Error reading packet: {}", err);
                        return Err(err.into());
                    }
                };

                if packet.track_id() != *track_id {
                    return Ok(true);
                }

                while !format.metadata().is_latest() {
                    format.metadata().pop();
                }

                match decoder.decode(&packet) {
                    Ok(decoded) => {
                        if sample_buf.is_none() {
                            self.sample_rate = decoded.spec().rate as f32;
                            let spec = *decoded.spec();
                            let duration = decoded.capacity() as u64;
                            *sample_buf = Some(SampleBuffer::<f32>::new(duration, spec));
                        }

                        if let Some(buf) = sample_buf {
                            buf.copy_interleaved_ref(decoded);

                            let samples = buf.samples();
                            for frame in samples.chunks(2) {
                                // let mono = frame[0];

                                // proper mono averaging
                                let mono: f32 = if frame.len() == 2 {
                                    (frame[0] + frame[1]) * 0.5
                                } else {
                                    frame[0]
                                };
                                self.buffer.push(mono);
                            }
                            if self.buffer.len() > FFT_SIZE * 4 {
                                self.buffer.drain(0..self.buffer.len() - FFT_SIZE * 2);
                            }
                        }
                    }
                    Err(Error::IoError(_)) => {
                        {};
                    }
                    Err(Error::DecodeError(_)) => {
                        {};
                    }
                    Err(err) => {
                        eprintln!("Unrecoverable decode error: {}", err);
                        return Err(err.into());
                    }
                }
                Ok(true)
            },

            AudioSource::System {
                capture_client,
                format,
                readpos
            } => {
                let mut got_wasapi_samples= false;
                self.sample_rate = format.get_samplespersec() as f32;

                while let Some(packet_size) = capture_client.get_next_packet_size().unwrap() {
                    if packet_size == 0 {
                        break;
                    }

                    let bytes_per_frame = format.get_nchannels() as usize * (format.get_validbitspersample() as usize / 8);
                    let mut buf = vec![0u8; (packet_size as usize) * bytes_per_frame];

                    let (frames_read, _) = capture_client.read_from_device(&mut buf).unwrap();
                    let bytes_read = frames_read as usize * bytes_per_frame;
                    let raw_bytes = &buf[..bytes_read];

                    let samples: Vec<f32> = match format.get_subformat().unwrap() {
                        wasapi::SampleType::Float => unsafe {
                            std::slice::from_raw_parts(raw_bytes.as_ptr() as *const f32, bytes_read / 4).to_vec()
                        },
                        wasapi::SampleType::Int => unsafe {
                            std::slice::from_raw_parts(raw_bytes.as_ptr() as *const i16, bytes_read / 2)
                                .iter()
                                .map(|&v| v as f32 / i16::MAX as f32)
                                .collect()
                        },
                    };

                    let mono: Vec<f32> = if format.get_nchannels() == 2 {
                        (&samples).chunks(2).map(|c| (c[0] + c[1]) * 0.5).collect()
                    } else {
                        samples
                    };

                    self.buffer.extend(mono);
                    got_wasapi_samples = true;
                }

                if !got_wasapi_samples {
                    let silence_length = (format.get_samplespersec() as f32 * 0.075) as usize;
                    self.buffer.extend(std::iter::repeat(0f32).take(silence_length));
                }

                if self.buffer.len() > FFT_SIZE * 2 {
                    self.buffer.drain(0..*readpos);
                    *readpos = 0;
                }

                Ok(true)
            },

            AudioSource::Microphone {
                capture_client,
                format,
                readpos
            } => {
                let mut got_wasapi_samples= false;
                self.sample_rate = format.get_samplespersec() as f32;

                while let Some(packet_size) = capture_client.get_next_packet_size().unwrap() {
                    if packet_size == 0 {
                        break;
                    }

                    let bytes_per_frame = format.get_nchannels() as usize * (format.get_validbitspersample() as usize / 8);
                    let mut buf = vec![0u8; (packet_size as usize) * bytes_per_frame];

                    let (frames_read, _) = capture_client.read_from_device(&mut buf).unwrap();
                    let bytes_read = frames_read as usize * bytes_per_frame;
                    let raw_bytes = &buf[..bytes_read];

                    let samples: Vec<f32> = match format.get_subformat().unwrap() {
                        wasapi::SampleType::Float => unsafe {
                            std::slice::from_raw_parts(raw_bytes.as_ptr() as *const f32, bytes_read / 4).to_vec()
                        },
                        wasapi::SampleType::Int => unsafe {
                            std::slice::from_raw_parts(raw_bytes.as_ptr() as *const i16, bytes_read / 2)
                                .iter()
                                .map(|&v| v as f32 / i16::MAX as f32)
                                .collect()
                        },
                    };

                    let mono: Vec<f32> = if format.get_nchannels() == 2 {
                        (&samples).chunks(2).map(|c| (c[0] + c[1]) * 0.5).collect()
                    } else {
                        samples
                    };

                    self.buffer.extend(mono);
                    got_wasapi_samples = true;
                }

                if !got_wasapi_samples {
                    let silence_length = (format.get_samplespersec() as f32 * 0.075) as usize;
                    self.buffer.extend(std::iter::repeat(0f32).take(silence_length));
                }

                if self.buffer.len() > FFT_SIZE * 2 {
                    self.buffer.drain(0..*readpos);
                    *readpos = 0;
                }

                Ok(true)
            },
        }
    }
}

pub fn list_render_devices() {
    wasapi::initialize_mta().unwrap();
    let enumerator = wasapi::DeviceEnumerator::new().unwrap();

    let collection = enumerator.get_device_collection(&wasapi::Direction::Render).unwrap();
    let devices_count = collection.get_nbr_devices().unwrap_or(0);

    if devices_count == 0 {
        println!("No Devices Found!");
        return;
    }

    for i in 0..devices_count {
        if let Ok(device) = collection.get_device_at_index(i) {
            let name = device.get_friendlyname().unwrap_or_else(|_| "<unknown>".to_string());
            println!("{i}: {name}");
        }
    }
}

fn pick_render_device(selector: Option<&str>) -> wasapi::Device {
    wasapi::initialize_mta().unwrap();
    let enumerator = wasapi::DeviceEnumerator::new().unwrap();

    let Some(sel) = selector else {
        return enumerator.get_default_device(&wasapi::Direction::Render).unwrap();
    };

    let device_collection = enumerator.get_device_collection(&wasapi::Direction::Render).unwrap();

    if let Ok(index) = sel.parse::<usize>() {
        if let Ok(device) = device_collection.get_device_at_index(index as u32) {
            return device;
        }
        eprintln!("Invalid device index: {idx}. Run `pulsetty --list-devices` to get a list of available devices.", idx=index);
        panic!("Invalid --device index");
    }

    let search_str = sel.to_lowercase();
    let devices_count = device_collection.get_nbr_devices().unwrap_or(0);

    for i in 0..devices_count {
        if let Ok(device) = device_collection.get_device_at_index(i) {
            let name = device.get_friendlyname().unwrap_or_else(|_| "<unknown>".to_string());
            if name.as_str().to_lowercase().as_str().contains(&search_str) {
                return device;
            }
        }
    }

    eprintln!("No device matched '{selstr}'. Run `pulsetty --list-devices` to get a list of available devices.", selstr=sel);
    panic!("No matching --device")
}

pub fn list_capture_devices() {
    wasapi::initialize_mta().unwrap();
    let enumerator = wasapi::DeviceEnumerator::new().unwrap();

    let collection = enumerator.get_device_collection(&wasapi::Direction::Capture).unwrap();
    let devices_count = collection.get_nbr_devices().unwrap_or(0);

    if devices_count == 0 {
        println!("No Capture Devices Found!");
        return;
    }

    for i in 0..devices_count {
        if let Ok(device) = collection.get_device_at_index(i) {
            let name = device.get_friendlyname().unwrap_or_else(|_| "<unknown>".to_string());
            println!("{i}: {name}");
        }
    }
}

fn pick_capture_device(selector: Option<&str>) -> wasapi::Device {
    wasapi::initialize_mta().unwrap();
    let enumerator = wasapi::DeviceEnumerator::new().unwrap();

    let Some(sel) = selector else {
        return enumerator.get_default_device(&wasapi::Direction::Capture).unwrap();
    };

    let device_collection = enumerator.get_device_collection(&wasapi::Direction::Capture).unwrap();

    if let Ok(index) = sel.parse::<usize>() {
        if let Ok(device) = device_collection.get_device_at_index(index as u32) {
            return device;
        }
        eprintln!("Invalid device index: {idx}. Run `pulsetty --list-mics` to get a list of available microphones.", idx=index);
        panic!("Invalid --mic index");
    }

    let search_str = sel.to_lowercase();
    let devices_count = device_collection.get_nbr_devices().unwrap_or(0);

    for i in 0..devices_count {
        if let Ok(device) = device_collection.get_device_at_index(i) {
            let name = device.get_friendlyname().unwrap_or_else(|_| "<unknown>".to_string());
            if name.as_str().to_lowercase().as_str().contains(&search_str) {
                return device;
            }
        }
    }

    eprintln!("No device matched '{selstr}'. Run `pulsetty --list-mics` to get a list of available microphones.", selstr=sel);
    panic!("No matching --mic")
}