Skip to content

Commit

Permalink
Merge pull request #14 from webern/getters
Browse files Browse the repository at this point in the history
implement getters for most things
  • Loading branch information
webern authored Dec 13, 2022
2 parents aa9f8b4 + 7a0a9dc commit 17d7965
Show file tree
Hide file tree
Showing 16 changed files with 123 additions and 15 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [v0.0.3] 2022-12-13
## Changed
- Add some getters to access private fields. [#14]

[#14]: https://github.com/webern/midi_file/issues/14

## [v0.0.2] 2021-03-07
## Changed
- Fixed a bug where running status bytes were not parsed (thank you @zigazeljko). [#6]
Expand All @@ -24,7 +30,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Everything: you can create simple MIDI files with this library.

<!-- version diff links -->
[Unreleased]: https://github.com/webern/midi_file/compare/v0.0.2...HEAD
[Unreleased]: https://github.com/webern/midi_file/compare/v0.0.3...HEAD
[v0.0.3]: https://github.com/webern/midi_file/compare/v0.0.2...v0.0.3
[v0.0.2]: https://github.com/webern/midi_file/compare/v0.0.1...v0.0.2
[v0.0.1]: https://github.com/webern/midi_file/compare/v0.0.0...v0.0.1
[v0.0.0]: https://github.com/webern/midi_file/releases/tag/v0.0.0
4 changes: 3 additions & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "midi_file"
version = "0.0.2"
version = "0.0.3"
authors = ["Matthew James Briggs <[email protected]>"]
edition = "2018"
exclude = [
Expand Down
4 changes: 2 additions & 2 deletions src/core/clocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl Clocks {
}

// Get the `u8` value represented by the enum.
pub(crate) fn to_u8(&self) -> u8 {
pub(crate) fn to_u8(self) -> u8 {
match self {
Clocks::DottedWhole => 142,
Clocks::Whole => 96,
Expand All @@ -75,7 +75,7 @@ impl Clocks {
Clocks::Eighth => 12,
Clocks::DottedSixteenth => 9,
Clocks::Sixteenth => 6,
Clocks::Other(v) => *v,
Clocks::Other(v) => v,
}
}

Expand Down
44 changes: 44 additions & 0 deletions src/core/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ pub struct NoteMessage {
}

impl NoteMessage {
/// Getter for the `channel` field.
pub fn channel(&self) -> Channel {
self.channel
}

/// Getter for the `note_number` field.
pub fn note_number(&self) -> NoteNumber {
self.note_number
}

/// Getter for the `velocity` field.
pub fn velocity(&self) -> Velocity {
self.velocity
}

fn parse<R: Read>(iter: &mut ByteIter<R>, channel: Channel) -> LibResult<Self> {
Ok(NoteMessage {
channel,
Expand Down Expand Up @@ -121,12 +136,36 @@ impl Default for ModeMessage {
}
}

impl LocalControlValue {
/// A getter for the `channel` field.
pub fn channel(&self) -> Channel {
self.channel
}

/// A getter for the `on_off` field.
pub fn on_off(&self) -> OnOff {
self.on_off
}
}

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct MonoModeOnValue {
channel: Channel,
mono_mode_channels: MonoModeChannels,
}

impl MonoModeOnValue {
/// A getter for the `channel` field.
pub fn channel(&self) -> Channel {
self.channel
}

/// A getter for the `mono_mode_channels` field.
pub fn mono_mode_channels(&self) -> MonoModeChannels {
self.mono_mode_channels
}
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[allow(dead_code)]
pub enum SystemCommonMessage {
Expand Down Expand Up @@ -745,12 +784,17 @@ pub struct ControlChangeValue {
}

impl ControlChangeValue {
/// A getter for the `channel` field.
pub fn channel(&self) -> Channel {
self.channel
}

/// A getter for the `control` field.
pub fn control(&self) -> Control {
self.control
}

/// A getter for the `value` field.
pub fn value(&self) -> ControlValue {
self.value
}
Expand Down
4 changes: 3 additions & 1 deletion src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ pub(crate) mod vlq;
pub use clocks::Clocks;
pub use duration_name::DurationName;
pub use general_midi::GeneralMidi;
pub use message::{Control, Message, NoteMessage, ProgramChangeValue};
pub use message::{
Control, LocalControlValue, Message, MonoModeOnValue, NoteMessage, ProgramChangeValue,
};
pub use numbers::{
Channel, ControlValue, MonoModeChannels, NoteNumber, PortValue, Program, Velocity,
};
Expand Down
2 changes: 1 addition & 1 deletion src/core/vlq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl Vlq {
Self { inner: value }
}

pub(crate) fn to_bytes(&self) -> Vec<u8> {
pub(crate) fn to_bytes(self) -> Vec<u8> {
encode_u32(self.inner)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ macro_rules! noimpl {
site: site!(),
feature: $name.to_string(),
}
.fail();
.fail()
};
}

Expand Down
12 changes: 12 additions & 0 deletions src/file/division.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,15 @@ impl Default for SmpteRate {
}
}
}

impl SmpteRate {
/// A getter for the `frame_rate` field.
pub fn frame_rate(&self) -> FrameRate {
self.frame_rate
}

/// A getter for the `resolution` field.
pub fn resolution(&self) -> u8 {
self.resolution
}
}
2 changes: 2 additions & 0 deletions src/file/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,12 @@ impl TrackEvent {
Self { delta_time, event }
}

/// A getter for the `delta_time` field.
pub fn delta_time(&self) -> u32 {
self.delta_time
}

/// A getter for the `event` field.
pub fn event(&self) -> &Event {
&self.event
}
Expand Down
3 changes: 3 additions & 0 deletions src/file/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ pub struct Header {
}

impl Header {
/// Create a new `Header` object.
pub fn new(format: Format, division: Division) -> Self {
Self { format, division }
}

/// A getter for the `format` field.
pub fn format(&self) -> &Format {
&self.format
}

/// A getter for the `division` field.
pub fn division(&self) -> &Division {
&self.division
}
Expand Down
24 changes: 23 additions & 1 deletion src/file/meta_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ fn write_text<W: Write>(w: &mut Scribe<W>, text_type: u8, text: &Text) -> LibRes
let size_u32 = u32::try_from(bytes.len()).context(error::StringTooLong { site: site!() })?;
let size = Vlq::new(size_u32).to_bytes();
w.write_all(&size).context(wr!())?;
w.write_all(&bytes).context(wr!())?;
w.write_all(bytes).context(wr!())?;
Ok(())
}

Expand All @@ -287,6 +287,9 @@ pub struct SmpteOffsetValue {
}

impl SmpteOffsetValue {
// TODO - create a `new` function.
// TODO - create getters.

pub(crate) fn parse<R: Read>(iter: &mut ByteIter<R>) -> LibResult<Self> {
// after 0x54 we should see 0x05
iter.read_expect(LEN_META_SMTPE_OFFSET).context(io!())?;
Expand Down Expand Up @@ -365,6 +368,7 @@ pub struct TimeSignatureValue {
}

impl TimeSignatureValue {
/// Create a new `TimeSignatureValue` object.
pub fn new(numerator: u8, denominator: DurationName, click: Clocks) -> Result<Self> {
ensure!(numerator > 0, error::Other { site: site!() });
Ok(Self {
Expand All @@ -375,14 +379,17 @@ impl TimeSignatureValue {
})
}

/// A getter for the `numerator` field.
pub fn numerator(&self) -> u8 {
self.numerator
}

/// A getter for the `denominator` field.
pub fn denominator(&self) -> DurationName {
self.denominator
}

/// A getter for the `click` field.
pub fn click(&self) -> Clocks {
self.click
}
Expand Down Expand Up @@ -439,6 +446,21 @@ pub struct KeySignatureValue {
}

impl KeySignatureValue {
/// Create a new `KeySignatureValue` object.
pub fn new(accidentals: KeyAccidentals, mode: KeyMode) -> Self {
Self { accidentals, mode }
}

/// A getter for the `accidentals` field.
pub fn accidentals(&self) -> KeyAccidentals {
self.accidentals
}

/// A getter for the `mode` field.
pub fn mode(&self) -> KeyMode {
self.mode
}

pub(crate) fn parse<R: Read>(iter: &mut ByteIter<R>) -> LibResult<Self> {
iter.read_expect(LEN_META_KEY_SIG).context(io!())?;
let raw_accidentals_byte = iter.read_or_die().context(io!())?;
Expand Down
4 changes: 4 additions & 0 deletions src/file/sysex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::error::LibResult;
use crate::scribe::Scribe;
use std::io::{Read, Write};

// TODO - implement sysex messages
/// Caution: Sysex messages are [not implemented](https://github.com/webern/midi_file/issues/7) and
/// will error.
#[derive(Clone, Debug, Default, Eq, Ord, PartialEq, PartialOrd, Hash)]
Expand All @@ -12,6 +13,9 @@ pub struct SysexEvent {
}

impl SysexEvent {
// TODO - implement a `new` function.
// TODO - implement getter functions.

pub(crate) fn parse<R: Read>(_first_byte: u8, _r: &mut ByteIter<R>) -> LibResult<Self> {
noimpl!("SysexEvent::parse")
}
Expand Down
2 changes: 2 additions & 0 deletions src/file/track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ impl Track {
self.events.len()
}

// TODO - maybe implement Iterator and IntoIterator on this type instead of doing this.
/// Iterator over the events in the track.
pub fn events(&self) -> impl Iterator<Item = &TrackEvent> {
self.events.iter()
}
Expand Down
18 changes: 13 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ impl MidiFile {
Self::new_with_settings(Settings::new())
}

/// A getter for the `header` field.
pub fn header(&self) -> &Header {
&self.header
}

/// A getter for the `running_status` field.
pub fn running_status(&self) -> bool {
self.running_status
}

/// Create a new `MidiFile` with customizable [`Settings`].
pub fn new_with_settings(settings: Settings) -> Self {
Self {
Expand Down Expand Up @@ -160,7 +170,7 @@ impl MidiFile {
/// Save a `MidiFile` to a file path.
pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<()> {
let path = path.as_ref();
let file = File::create(&path).context(error::Create {
let file = File::create(path).context(error::Create {
site: site!(),
path,
})?;
Expand All @@ -174,18 +184,16 @@ impl MidiFile {
self.write(&mut scribe)
}

pub fn header(&self) -> &Header {
&self.header
}

pub fn tracks_len(&self) -> u32 {
u32::try_from(self.tracks.len()).unwrap_or(u32::MAX)
}

/// An iterator over the tracks in the file.
pub fn tracks(&self) -> impl Iterator<Item = &Track> {
self.tracks.iter()
}

/// Get a reference to the track at `index` if it exists.
pub fn track(&self, index: u32) -> Option<&Track> {
let i = match usize::try_from(index) {
Ok(ok) => ok,
Expand Down
2 changes: 1 addition & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ macro_rules! clamp {
}

#[test]
#[allow(clippy::blacklisted_name)]
#[allow(clippy::disallowed_names)]
fn clamp_test() {
clamp!(Foo, u8, 1, 16, 1, pub);
let foo: Foo = 0u8.into();
Expand Down

0 comments on commit 17d7965

Please sign in to comment.