Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. use cpal;
  2. use cpal::EventLoop;
  3. use std::thread;
  4. use std::time::Duration;
  5. use std::sync::mpsc;
  6. mod osc;
  7.  
  8. fn main() {
  9. let event_loop = EventLoop::new();
  10.  
  11. let device = match cpal::default_output_device() {
  12. Some(dev) => {
  13. println!("Sound Device Found");
  14. dev
  15. },
  16. None => panic!(),
  17. };
  18.  
  19. println!("Using device: {}", device.name());
  20.  
  21. let mut supported_formats_range = device.supported_output_formats()
  22. .expect("error while querying formats");
  23.  
  24. let format = supported_formats_range.next()
  25. .expect("no supported format?!")
  26. .with_max_sample_rate();
  27.  
  28. let sample_rate = format.sample_rate.0 as f32;
  29. println!("Sample Rate: {}", sample_rate);
  30.  
  31. let stream_id = event_loop.build_output_stream(&device, &format).unwrap();
  32. event_loop.play_stream(stream_id);
  33.  
  34. let (tx, rx) = mpsc::channel();
  35.  
  36. let audio_thread = thread::spawn(move|| {
  37.  
  38.  
  39. tx.send(String::from("hello")).unwrap();
  40.  
  41. let mut osc_pitch = osc::OSC {
  42. sample: 0.0,
  43. sample_clock: 0.0,
  44. freq: 60.0,
  45. sample_rate: sample_rate,
  46. gain: 1.0,
  47. offset: 0.0,
  48. scale: 1.0,
  49. };
  50.  
  51. let mut lfo = osc::OSC {
  52. sample: 0.0,
  53. sample_clock: 0.0,
  54. freq: 0.1,
  55. sample_rate: sample_rate,
  56. gain: 100.0,
  57. offset: 90.0,
  58. scale: 0.5,
  59. };
  60.  
  61. let mut next_value = || {
  62. osc_pitch.freq = lfo.get_next_sample();
  63. osc_pitch.get_next_sample()
  64. };
  65.  
  66. event_loop.run(move |_, data| {
  67. match data {
  68. cpal::StreamData::Output { buffer: cpal::UnknownTypeOutputBuffer::F32(mut buffer) } => {
  69. for sample in buffer.chunks_mut(format.channels as usize) {
  70. let value = next_value();
  71. for out in sample.iter_mut() {
  72. *out = value;
  73. }
  74. }
  75. },
  76. _ => (),
  77. }
  78. });
  79. });
  80.  
  81.  
  82. loop {
  83. let got = rx.recv().unwrap();
  84. println!("Got: {}", got);
  85. }
  86.  
  87. audio_thread.join().unwrap();
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement