Skip to content

Commit

Permalink
set input device
Browse files Browse the repository at this point in the history
  • Loading branch information
tomara-x committed Jun 8, 2024
1 parent 5b345e6 commit bd8c1fd
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 7 deletions.
40 changes: 37 additions & 3 deletions src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ pub fn default_in_device(mut commands: Commands) {
let host = cpal::default_host();
if let Some(device) = host.default_input_device() {
let config = device.default_input_config().unwrap();
info!("{:?}", config);
let stream = match config.sample_format() {
cpal::SampleFormat::F32 => run_in::<f32>(&device, &config.into(), ls, rs),
cpal::SampleFormat::I16 => run_in::<i16>(&device, &config.into(), ls, rs),
Expand All @@ -148,6 +147,41 @@ pub fn default_in_device(mut commands: Commands) {
}
}

pub fn set_in_device(
mut commands: Commands,
mut in_device_event: EventReader<InDeviceCommand>,
) {
for e in in_device_event.read() {
let (ls, lr) = bounded(64);
let (rs, rr) = bounded(64);
commands.insert_resource(InputReceivers(lr, rr));
let (h, d) = e.0;
if let Some(host_id) = cpal::platform::ALL_HOSTS.get(h) {
if let Ok(host) = cpal::platform::host_from_id(*host_id) {
if let Ok(mut devices) = host.input_devices() {
if let Some(device) = devices.nth(d) {
let config = device.default_input_config().unwrap();
let stream = match config.sample_format() {
cpal::SampleFormat::F32 => run_in::<f32>(&device, &config.into(), ls, rs),
cpal::SampleFormat::I16 => run_in::<i16>(&device, &config.into(), ls, rs),
cpal::SampleFormat::U16 => run_in::<u16>(&device, &config.into(), ls, rs),
format => {
error!("unsupported sample format: {}", format);
None
},
};
if let Some(stream) = stream {
commands.insert_resource(InStream(stream.into_inner()));
} else {
error!("couldn't build stream");
}
}
}
}
}
}
}

fn run_in<T>(
device: &cpal::Device,
config: &cpal::StreamConfig,
Expand Down Expand Up @@ -178,7 +212,7 @@ where
T: SizedSample, f32: FromSample<T>
{
for frame in input.chunks(2) {
ls.send(frame[0].to_sample::<f32>()).unwrap();
rs.send(frame[1].to_sample::<f32>()).unwrap();
let _ = ls.send(frame[0].to_sample::<f32>());
let _ = rs.send(frame[1].to_sample::<f32>());
}
}
9 changes: 7 additions & 2 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub struct Access<'w, 's> {
visible: Query<'w, 's, &'static VisibleEntities>,
text_size: Res<'w, TextSize>,
out_device_event: EventWriter<'w, OutDeviceCommand>,
in_device_event: EventWriter<'w, InDeviceCommand>,
node_limit: ResMut<'w, NodeLimit>,
}

Expand Down Expand Up @@ -188,14 +189,18 @@ pub fn command_parser(
Some(":q") => {
access.exit_event.send_default();
}
Some(":od") => {
Some(":od") | Some(":id") => {
let h = command.next();
let d = command.next();
if let (Some(h), Some(d)) = (h, d) {
let h = h.parse::<usize>();
let d = d.parse::<usize>();
if let (Ok(h), Ok(d)) = (h, d) {
access.out_device_event.send(OutDeviceCommand((h, d)));
if c0 == Some(":od") {
access.out_device_event.send(OutDeviceCommand((h, d)));
} else {
access.in_device_event.send(InDeviceCommand((h, d)));
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,6 @@ pub struct ConnectCommand(pub Entity);

#[derive(Event)]
pub struct OutDeviceCommand(pub (usize, usize));

#[derive(Event)]
pub struct InDeviceCommand(pub (usize, usize));
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ fn main() {
.add_systems(Startup, default_out_device)
.add_systems(Update, set_out_device)
.add_systems(Startup, default_in_device)
.add_systems(Update, set_in_device)

.add_systems(Update, toggle_pan)
.init_state::<Mode>()
Expand Down Expand Up @@ -129,6 +130,7 @@ fn main() {
.add_event::<DacChange>()
.add_event::<ConnectCommand>()
.add_event::<OutDeviceCommand>()
.add_event::<InDeviceCommand>()
// connections
.add_systems(Update, connect.run_if(in_state(Mode::Connect)))
.add_systems(Update, connect_targets)
Expand Down
4 changes: 2 additions & 2 deletions src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,8 +510,8 @@ impl AudioNode for InputNode {
&mut self,
_input: &Frame<Self::Sample, Self::Inputs>,
) -> Frame<Self::Sample, Self::Outputs> {
let l = self.lr.recv().unwrap();
let r = self.rr.recv().unwrap();
let l = self.lr.recv().unwrap_or(0.);
let r = self.rr.recv().unwrap_or(0.);
[l, r].into()
}

Expand Down

0 comments on commit bd8c1fd

Please sign in to comment.