Skip to content

Commit

Permalink
Fix issues with MIDI port numbers (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertBendun authored Oct 25, 2024
1 parent 220b01d commit ecf8414
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Connection to MIDI port 0 on Windows doesn't result in crash anymore
- When creating new MIDI source use minimal port number by default

## [0.3.0] - 2024-10-22

### Added
Expand Down
20 changes: 15 additions & 5 deletions src/audio_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use std::{
time::Duration,
};

use anyhow::anyhow;
use midir::{MidiOutput, MidiOutputConnection};
use midly::live::LiveEvent;
use rusty_link::SessionState;
Expand Down Expand Up @@ -254,24 +255,33 @@ async fn audio_engine_main_midi(

{
let output: &mut MidiOutputConnection = {
if midi_source.associated_port == 0 {
let port_number = midi_source
.associated_port
.max(crate::handlers::MIN_PORT_NUMBER);
if port_number == 0 {
#[cfg(windows)]
unreachable!();

#[cfg(unix)]
&mut virtual_output
} else {
let out = MidiOutput::new("harmonia")?;

let midi_port = &out.ports()[midi_source.associated_port - 1];
let ports = out.ports();
let Some(midi_port) = ports.get(port_number - 1) else {
return Err(anyhow!(
"failed to connect to unknown midi port number {} (max {})",
port_number,
ports.len()
));
};
info!(
"outputing to output port #{} named: {}",
midi_source.associated_port,
out.port_name(midi_port).unwrap(),
out.port_name(&midi_port).unwrap(),
);

conn_storage = Some(
out.connect(midi_port, /* TODO: Better name */ "harmonia-play")
out.connect(&midi_port, /* TODO: Better name */ "harmonia-play")
.map_err(|err| {
anyhow::Error::msg(format!("failed to connect to midi port: {err}"))
})?,
Expand Down
8 changes: 3 additions & 5 deletions src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,9 @@ pub async fn index(app_state: State<Arc<AppState>>) -> Markup {
let cache = cache_path();
let c: &std::path::Path = &cache;

// TODO: On Windows don't use "~" as HOME since it may not work,
// use environment variable instead
dirs::home_dir()
.and_then(|home| c.strip_prefix(home).ok())
.map(|p| PathBuf::from("~").join(p))
.map(|p| PathBuf::from(if cfg!(unix) { "~" } else { "%HOMEPATH%" }).join(p))
.unwrap_or(cache)
.to_str()
.unwrap()
Expand Down Expand Up @@ -485,7 +483,7 @@ pub struct SetPort {
///
/// On windows it is 1 since we don't have virtual ports.
/// On unix'es it's 0 since we have virtual ports.
const MIN_PORT_NUMBER: usize = if cfg!(unix) { 0 } else { 1 };
pub const MIN_PORT_NUMBER: usize = if cfg!(unix) { 0 } else { 1 };

/// Set port for MIDI block
pub async fn set_port_for_midi(
Expand Down Expand Up @@ -636,7 +634,7 @@ pub async fn add_new_midi_source_block(
let midi_source = block::MidiSource {
bytes: data,
file_name: file_name.clone(),
associated_port: 0,
associated_port: MIN_PORT_NUMBER,
};

let block = block::Block {
Expand Down

0 comments on commit ecf8414

Please sign in to comment.