Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ fn midi_to_bytes(message: wmidi::MidiMessage<'_>) -> Vec<u8> {
### Unreleased

* Use `error` from `core`.
* Added `MidiMessage::set_channel()` method.

### 4.0.0

Expand Down
25 changes: 25 additions & 0 deletions src/midi_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,20 @@ impl<'a> MidiMessage<'a> {
}
}

/// Set the channel of the MIDI message, if applicable for the message type.
pub fn set_channel(&mut self, channel: Channel) {
match self {
MidiMessage::NoteOff(c, ..) => *c = channel,
MidiMessage::NoteOn(c, ..) => *c = channel,
MidiMessage::PolyphonicKeyPressure(c, ..) => *c = channel,
MidiMessage::ControlChange(c, ..) => *c = channel,
MidiMessage::ProgramChange(c, ..) => *c = channel,
MidiMessage::ChannelPressure(c, ..) => *c = channel,
MidiMessage::PitchBendChange(c, ..) => *c = channel,
_ => {}
}
}

#[inline(always)]
fn new_sysex(bytes: &'a [u8]) -> Result<Self, Error> {
debug_assert!(bytes[0] == 0xF0);
Expand Down Expand Up @@ -698,4 +712,15 @@ mod test {
);
assert_eq!(MidiMessage::Start.channel(), None);
}

#[test]
fn set_channel() {
let mut msg = MidiMessage::ControlChange(
Channel::Ch8,
ControlFunction::DAMPER_PEDAL,
U7::try_from(55).unwrap(),
);
msg.set_channel(Channel::Ch9);
assert_eq!(msg.channel(), Some(Channel::Ch9));
}
}
Loading