From c67c5e976a406ac53f9a220d5c5f7479c38ca858 Mon Sep 17 00:00:00 2001 From: Datseris Date: Mon, 17 Feb 2020 22:33:15 +0100 Subject: [PATCH] more convenient syntax for drumnotes Closes #125 --- CHANGELOG.md | 2 ++ Project.toml | 3 +-- src/constants.jl | 31 +++++++++++++++++++++++++++++++ src/note.jl | 17 ++++++++++++++--- 4 files changed, 48 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 33bcd55..ca8e807 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +# v1.8.0 +* `DrumNote` shorthand constructor, keyword constructor for `Note` and added `DRUMKEY` dictionary in constants.jl (originally in MusicVisualizations). # v1.7.0 * Notes constructor allows values of `tpq` greater than `960`. # v1.6.0 diff --git a/Project.toml b/Project.toml index 056a8c5..26bc650 100644 --- a/Project.toml +++ b/Project.toml @@ -1,12 +1,11 @@ name = "MIDI" uuid = "f57c4921-e30c-5f49-b073-3f2f2ada663e" repo = "https://github.com/JuliaMusic/MIDI.jl.git" -version = "1.7.1" +version = "1.8.0" [compat] julia = "1" - [extras] Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/src/constants.jl b/src/constants.jl index 08d4856..c878392 100644 --- a/src/constants.jl +++ b/src/constants.jl @@ -172,3 +172,34 @@ const GM = Dict( "Applause" => UInt8(127), "Gunshot" => UInt8(128) ) + +""" + DRUMKEY + +A dictionary that given the drum instrument as a string it returns the +MIDI pitch that MuseScore uses. See +https://musescore.org/sites/musescore.org/files/General%20MIDI%20Standard%20Percussion%20Set%20Key%20Map.pdf +for a more complete list. +""" +const DRUMKEY = begin + a = + Dict( + "Acoustic Bass Drum" => "B1", + "Acoustic Snare" => "D2", + "Side Stick" => "C#2", + "Closed Hi-Hat" => "F#2", + "Open Hi-Hat" => "A#2", + "Pedal Hi-Hat" => "G#2", + "Ride Cymbal" => "D#3", + "Ride Bell" => "F3", + "Low-Mid Tom" => "B2", + "Cowbell" => "G#3", + "Tambourine" => "F#3", + "High Floor Tom" => "G2", + "Low Floor Tom" => "F2", + "Crash Cymbal 1" => "C#3", + "Crash Cymbal 2" => "A3" + ) +end + +export DRUMKEY diff --git a/src/note.jl b/src/note.jl index 9336ec5..260914d 100644 --- a/src/note.jl +++ b/src/note.jl @@ -1,4 +1,4 @@ -export Note, Notes, AbstractNote +export Note, Notes, AbstractNote, DrumNote export pitch_to_name, name_to_pitch abstract type AbstractNote end @@ -47,9 +47,20 @@ Note(pitch, velocity, position, duration, channel) @inline Note(n::Note) = n -import Base.== +""" + DrumNote(pitch, position, duration = 960; velocity = 100) +Shorthand constructor for a [`Note`](@ref) that is always on channel `9`. +It is possible to specify pitch as a `String` (e.g. "Acoustic Snare"), which +will be converted to actual pitch using [`DRUMKEY`](@ref). +""" +DrumNote(pitch, position, duration = 960; velocity = 100, gmmap = DRUMKEY) = +Note(_drumpitch(pitch, gmmap), velocity, position, duration, 9) -==(n1::Note, n2::Note) = +_drumpitch(pitch::Real, gmmap) = pitch +_drumpitch(pitch::String, gmmap) = name_to_pitch(gmmap[pitch]) + +import Base.== +==(n1::AbstractNote, n2::AbstractNote) = n1.pitch == n2.pitch && n1.duration == n2.duration && n1.position == n2.position &&