Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. use cpal::traits::{DeviceTrait, EventLoopTrait, HostTrait};
  2. use sample::{ring_buffer, interpolate, signal, Sample, Signal};
  3.  
  4. fn main() -> Result<(), failure::Error> {
  5. let host = cpal::default_host();
  6. let event_loop = host.event_loop();
  7.  
  8. // Default devices.
  9. let input_device = host.default_input_device().expect("failed to get default input device");
  10. let output_device = host.default_output_device().expect("failed to get default output device");
  11. println!("Using default input device: \"{}\"", input_device.name()?);
  12. println!("Using default output device: \"{}\"", output_device.name()?);
  13.  
  14. // We'll try and use the same format between streams to keep it simple
  15. let mut input_format = input_device.default_input_format()?;
  16. input_format.data_type = cpal::SampleFormat::I16;
  17. input_format.channels = 1;
  18.  
  19. let mut output_format = output_device.default_output_format()?;
  20. output_format.data_type = cpal::SampleFormat::I16;
  21. output_format.channels = 1;
  22.  
  23. // Build streams.
  24. println!("Attempting to build both streams with `{:?}`|`{:?}`.", input_format, output_format);
  25. let input_stream_id = event_loop.build_input_stream(&input_device, &input_format)?;
  26. let output_stream_id = event_loop.build_output_stream(&output_device, &output_format)?;
  27. println!("Successfully built streams.");
  28.  
  29. // The channel to share samples.
  30. let (tx, rx) = std::sync::mpsc::sync_channel(48000);
  31.  
  32. // Play the streams.
  33. println!("Starting the input and output streams.");
  34. event_loop.play_stream(input_stream_id.clone())?;
  35. event_loop.play_stream(output_stream_id.clone())?;
  36.  
  37. let ring_buffer = ring_buffer::Fixed::from([[0.0]; 100]);
  38. let sinc = interpolate::Sinc::new(ring_buffer);
  39. let signal = signal::from_iter(rx.try_iter());
  40. let mut new_signal = signal.from_hz_to_hz(sinc, input_format.sample_rate.0 as f64, output_format.sample_rate.0 as f64);
  41.  
  42. event_loop.run(move |id, result| {
  43. let data = match result {
  44. Ok(data) => data,
  45. Err(err) => {
  46. eprintln!("an error occurred on stream {:?}: {}", id, err);
  47. return;
  48. }
  49. };
  50.  
  51. match data {
  52. cpal::StreamData::Input { buffer: cpal::UnknownTypeInputBuffer::I16(buffer) } => {
  53. assert_eq!(id, input_stream_id);
  54. let mut output_fell_behind = false;
  55. for &sample in buffer.iter() {
  56. let frame = [sample.to_sample::<f64>()];
  57. if tx.try_send(frame).is_err() {
  58. output_fell_behind = true;
  59. }
  60. }
  61. if output_fell_behind {
  62. eprintln!("output stream fell behind: try increasing latency");
  63. }
  64. },
  65. cpal::StreamData::Output { buffer: cpal::UnknownTypeOutputBuffer::I16(mut buffer) } => {
  66. assert_eq!(id, output_stream_id);
  67. let mut input_fell_behind = None;
  68. for sample in buffer.iter_mut() {
  69. *sample = match signal.is_exhausted() {
  70. false => new_signal.next()[0].to_sample::<i16>(),
  71. true => {
  72. input_fell_behind = Some("whoops");
  73. new_signal.next()[0].to_sample::<i16>()
  74. },
  75. };
  76. }
  77. if let Some(err) = input_fell_behind {
  78. eprintln!("input stream fell behind: {}: try increasing latency", err);
  79. }
  80. },
  81. _ => panic!("we're expecting i16 data"),
  82. }
  83. })
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement