Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use cpal;
- use cpal::EventLoop;
- use std::thread;
- use std::time::Duration;
- use std::sync::mpsc;
- mod osc;
- fn main() {
- let event_loop = EventLoop::new();
- let device = match cpal::default_output_device() {
- Some(dev) => {
- println!("Sound Device Found");
- dev
- },
- None => panic!(),
- };
- println!("Using device: {}", device.name());
- let mut supported_formats_range = device.supported_output_formats()
- .expect("error while querying formats");
- let format = supported_formats_range.next()
- .expect("no supported format?!")
- .with_max_sample_rate();
- let sample_rate = format.sample_rate.0 as f32;
- println!("Sample Rate: {}", sample_rate);
- let stream_id = event_loop.build_output_stream(&device, &format).unwrap();
- event_loop.play_stream(stream_id);
- let (tx, rx) = mpsc::channel();
- let audio_thread = thread::spawn(move|| {
- tx.send(String::from("hello")).unwrap();
- let mut osc_pitch = osc::OSC {
- sample: 0.0,
- sample_clock: 0.0,
- freq: 60.0,
- sample_rate: sample_rate,
- gain: 1.0,
- offset: 0.0,
- scale: 1.0,
- };
- let mut lfo = osc::OSC {
- sample: 0.0,
- sample_clock: 0.0,
- freq: 0.1,
- sample_rate: sample_rate,
- gain: 100.0,
- offset: 90.0,
- scale: 0.5,
- };
- let mut next_value = || {
- osc_pitch.freq = lfo.get_next_sample();
- osc_pitch.get_next_sample()
- };
- event_loop.run(move |_, data| {
- match data {
- cpal::StreamData::Output { buffer: cpal::UnknownTypeOutputBuffer::F32(mut buffer) } => {
- for sample in buffer.chunks_mut(format.channels as usize) {
- let value = next_value();
- for out in sample.iter_mut() {
- *out = value;
- }
- }
- },
- _ => (),
- }
- });
- });
- loop {
- let got = rx.recv().unwrap();
- println!("Got: {}", got);
- }
- audio_thread.join().unwrap();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement