Skip to content

Commit

Permalink
WIP output stream
Browse files Browse the repository at this point in the history
  • Loading branch information
geom3trik committed Jul 28, 2024
1 parent b3a6671 commit e78d53c
Show file tree
Hide file tree
Showing 13 changed files with 476 additions and 79 deletions.
88 changes: 88 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion examples/enumerate_alsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
fn main() {
println!("ALSA driver is not available on this platform");
}

1 change: 0 additions & 1 deletion examples/enumerate_wasapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
fn main() {
println!("WASAPI driver is not available on this platform");
}

21 changes: 13 additions & 8 deletions examples/sine_wave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ fn main() -> Result<()> {
};
assert!(device.is_config_supported(&config));
println!("Using device {}", device.name());
let stream = device.create_output_stream(
config,
SineWave {
frequency: 440.,
phase: 0.,
},
).unwrap();
let stream = device
.create_output_stream(
config,
SineWave {
frequency: 440.,
phase: 0.,
},
)
.unwrap();
println!("Press Enter to stop");
std::io::stdin().read_line(&mut String::new())?;
stream.eject().unwrap();
Expand All @@ -33,7 +35,10 @@ struct SineWave {

impl AudioOutputCallback for SineWave {
fn on_output_data(&mut self, context: AudioCallbackContext, mut output: AudioOutput<f32>) {
eprintln!("Callback called, timestamp: {:2.3} s", context.timestamp.as_seconds());
eprintln!(
"Callback called, timestamp: {:2.3} s",
context.timestamp.as_seconds()
);
let sr = context.timestamp.samplerate as f32;
for i in 0..output.buffer.num_samples() {
output.buffer.set_mono(i, self.next_sample(sr));
Expand Down
7 changes: 5 additions & 2 deletions examples/util/enumerate.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use interflow::{AudioDevice, AudioDriver, DeviceType};
use std::error::Error;

pub fn enumerate_devices<Driver: AudioDriver>(driver: Driver) -> Result<(), Box<dyn Error>> where <Driver as AudioDriver>::Error: 'static {
pub fn enumerate_devices<Driver: AudioDriver>(driver: Driver) -> Result<(), Box<dyn Error>>
where
<Driver as AudioDriver>::Error: 'static,
{
eprintln!("Driver name : {}", Driver::DISPLAY_NAME);
eprintln!("Driver version: {}", driver.version()?);
eprintln!("Default device");
Expand All @@ -19,4 +22,4 @@ pub fn enumerate_devices<Driver: AudioDriver>(driver: Driver) -> Result<(), Box<
eprintln!("\t{} ({:?})", device.name(), device.device_type());
}
Ok(())
}
}
2 changes: 1 addition & 1 deletion examples/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod enumerate;
pub mod enumerate;
11 changes: 4 additions & 7 deletions src/audio_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl<S: DataOwned> Default for AudioBufferBase<S> {
}
}

impl<S: Data, S2: Data<Elem=S::Elem>> PartialEq<AudioBufferBase<S2>> for AudioBufferBase<S>
impl<S: Data, S2: Data<Elem = S::Elem>> PartialEq<AudioBufferBase<S2>> for AudioBufferBase<S>
where
S::Elem: PartialEq<S::Elem>,
{
Expand Down Expand Up @@ -178,7 +178,7 @@ impl<S: DataMut> AudioBufferBase<S> {
pub fn channels_mut(&mut self) -> impl '_ + Iterator<Item = ArrayViewMut1<S::Elem>> {
self.storage.rows_mut().into_iter()
}

pub fn as_interleaved_mut(&mut self) -> ArrayViewMut2<S::Elem> {
self.storage.view_mut().reversed_axes()
}
Expand Down Expand Up @@ -374,11 +374,8 @@ impl<'a, T: Sample> AudioMut<'a, T> {
}
}

pub fn mix(
&mut self,
other: AudioRef<T>,
other_amplitude: T::Float,
) where
pub fn mix(&mut self, other: AudioRef<T>, other_amplitude: T::Float)
where
T: AddAssign<T>,
{
for (mut ch_a, ch_b) in self.channels_mut().zip(other.channels()) {
Expand Down
12 changes: 6 additions & 6 deletions src/backends/alsa.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
use core::fmt;
use std::{borrow::Cow, convert::Infallible, ffi::CStr};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread::JoinHandle;
use std::time::Duration;
use std::{borrow::Cow, convert::Infallible, ffi::CStr};

use alsa::{device_name::HintIter, pcm, PCM};
use thiserror::Error;

use crate::audio_buffer::{AudioMut, AudioRef};
use crate::channel_map::{Bitset, ChannelMap32};
use crate::timestamp::Timestamp;
use crate::{
AudioCallbackContext, AudioDevice, AudioDriver, AudioInput, AudioInputCallback,
AudioInputDevice, AudioOutput, AudioOutputCallback, AudioOutputDevice, AudioStreamHandle,
Channel, DeviceType, StreamConfig,
};
use crate::audio_buffer::{AudioMut, AudioRef};
use crate::channel_map::{Bitset, ChannelMap32};
use crate::timestamp::Timestamp;

#[derive(Debug, Error)]
#[error("ALSA error: ")]
Expand Down Expand Up @@ -236,7 +236,7 @@ impl<Callback: 'static + Send + AudioInputCallback> AlsaStream<Callback> {
let input = AudioInput { buffer, timestamp };
callback.on_input_data(context, input);
timestamp += frames as u64;

match device.pcm.state() {
pcm::State::Suspended => {
if hwp.can_resume() {
Expand Down
17 changes: 12 additions & 5 deletions src/backends/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ use crate::{
#[cfg(os_alsa)]
pub mod alsa;

#[cfg(os_wasapi)]
pub mod wasapi;

pub fn default_driver() -> impl AudioDriver {
#[cfg(os_alsa)]
alsa::AlsaDriver
return alsa::AlsaDriver;
#[cfg(os_wasapi)]
return wasapi::WasapiDriver;
}

pub fn default_input_device_from<Driver: AudioDriver>(driver: &Driver) -> Driver::Device
Expand All @@ -26,7 +31,9 @@ where

pub fn default_input_device() -> impl AudioInputDevice {
#[cfg(os_alsa)]
default_input_device_from(&alsa::AlsaDriver)
return default_input_device_from(&alsa::AlsaDriver);
#[cfg(os_wasapi)]
return default_input_device_from(&wasapi::WasapiDriver);
}

pub fn default_output_device_from<Driver: AudioDriver>(driver: &Driver) -> Driver::Device
Expand All @@ -42,7 +49,7 @@ where

pub fn default_output_device() -> impl AudioOutputDevice {
#[cfg(os_alsa)]
default_output_device_from(&alsa::AlsaDriver)
return default_output_device_from(&alsa::AlsaDriver);
#[cfg(os_wasapi)]
return default_output_device_from(&wasapi::WasapiDriver);
}
#[cfg(os_wasapi)]
pub mod wasapi;
Loading

0 comments on commit e78d53c

Please sign in to comment.