-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add NamedNote * remove deprecated code * use eachindex to remove vscode infos * add NamedNotes * change version to 1.7 * test change * remove pitch for NamedNote. Add equality compare * document change * add tests * re-run CI test * add changelog
- Loading branch information
1 parent
807728e
commit fefe123
Showing
16 changed files
with
145 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
using MIDI: name_to_pitch, pitch_to_name, Note | ||
export NamedNote,NamedNotes | ||
|
||
""" | ||
NamedNote(name, velocity, position, duration, channel = 0) <: AbstractNote | ||
Mutable data structure describing a music note with certain pitch name. A bundle of many notes results | ||
in the [`Notes`](@ref) struct, which is the output of the [`getnotes`](@ref) | ||
function. | ||
If the `channel` of the note is `0` (default), it is not shown. | ||
You can also create a `NamedNote` with the following keyword constructor: | ||
```julia | ||
NamedNote(pitch_name::String; position = 0, velocity = 100, duration = 960, channel = 0) | ||
NamedNote(n::Note; pitch_name::String = "") | ||
``` | ||
Initialize `Notes{NamedNote}` from a pitch name string, use spaces to separate pitch name. | ||
``` julia | ||
NamedNotes(notes_string::String; tpq::Int = 960) | ||
# NamedNotes("C#6 Db5 E4") | ||
``` | ||
Converte `NamedNote` to `Note`: | ||
``` julia | ||
Note(n::NamedNote) | ||
``` | ||
Warning: We use attribute `name_to_pitch(name)`,`duration`,`position`,`channel`,`velocity` to compare `NamedNote`. So: | ||
``` julia | ||
NamedNote("Db5") == NamedNote("C#5") | ||
# true | ||
``` | ||
## Fields: | ||
* `name::String` : Pitch name, Uppercase. | ||
* `velocity::UInt8` : Dynamic intensity. Cannot be higher than 127 (0x7F). | ||
* `position::UInt` : Position in absolute time (since beginning of track), in ticks. | ||
* `duration::UInt` : Duration in ticks. | ||
* `channel::UInt8 = 0` : Channel of the track that the note is played on. | ||
Cannot be higher than 127 (0x7F). | ||
""" | ||
mutable struct NamedNote <: AbstractNote | ||
name::String | ||
velocity::UInt8 | ||
position::UInt | ||
duration::UInt | ||
channel::UInt8 | ||
|
||
NamedNote(name, velocity, position, duration, channel = 0) = | ||
if channel > 0x7F | ||
error( "Channel must be less than 128" ) | ||
elseif velocity > 0x7F | ||
error( "Velocity must be less than 128") | ||
# check if match the regex | ||
elseif !occursin(r"^[A-G][#b♯♭]?\d?$",name) | ||
error("Invalid note pitch name") | ||
else | ||
if !isnumeric(name[end]) | ||
name *= "5" | ||
end | ||
|
||
# store flat use "♭", store sharp use "♯" | ||
if name[prevind(name,end,1)] == 'b' | ||
name = replace(name,"b"=>"♭") | ||
elseif name[prevind(name,end,1)] == '#' | ||
name = replace(name,"#"=>"♯") | ||
end | ||
|
||
new(name, velocity, position, duration, channel) | ||
end | ||
end | ||
|
||
NamedNote(pitch_name::String; position = 0, velocity = 100, duration = 960, channel = 0) = | ||
NamedNote(pitch_name, velocity, position, duration, channel) | ||
|
||
NamedNote(n::Note; pitch_name::String = "") = | ||
length(pitch_name) == 0 ? NamedNote(pitch_to_name(n.pitch), n.position, n.velocity, n.duration, n.channel) : NamedNote(name, n.position, n.velocity, n.duration, n.channel) | ||
|
||
NamedNotes(notes_string::String; tpq::Int = 960) = Notes([NamedNote(String(s)) for s in split(notes_string," ")], tpq) | ||
|
||
Note(n::NamedNote) = Note(name_to_pitch(n.name), n.position, n.velocity, n.duration, n.channel) | ||
|
||
function Base.show(io::IO, note::NamedNote) | ||
nn = rpad(note.name, 3) | ||
chpr = note.channel == 0 ? "" : " | channel $(note.channel)" | ||
velprint = rpad("vel = $(Int(note.velocity))", 9) | ||
print(io, "NamedNote $nn | $velprint | "* | ||
"pos = $(Int(note.position)), "* | ||
"dur = $(Int(note.duration))"*chpr) | ||
end | ||
|
||
import Base.== | ||
==(n1::NamedNote, n2::NamedNote) = | ||
name_to_pitch(n1.name) == name_to_pitch(n2.name) && | ||
n1.duration == n2.duration && | ||
n1.position == n2.position && | ||
n1.channel == n2.channel && | ||
n1.velocity == n2.velocity |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
@testset "Random Notes Sequence" | ||
@testset "Random Notes Sequence" begin | ||
|
||
# Dummy sequence | ||
tpq = 960 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using Test, MusicManipulations | ||
@testset "NamedNote" begin | ||
nn1 = NamedNote("C") | ||
@test nn1.name == "C5" | ||
nn2 = NamedNote("C#6") | ||
@test nn2.name == "C♯6" | ||
|
||
@test NamedNote("C♯4").name == "C♯4" | ||
|
||
@test NamedNote("Db5") == NamedNote("C#5") | ||
|
||
nns = NamedNotes("C#6 Db5 E4") | ||
@test nns[1].name == "C♯6" | ||
@test nns[2].name == "D♭5" | ||
@test nns[3].name == "E4" | ||
|
||
@test NamedNote(Note("C#4")) == NamedNote(Note("Db4")) | ||
|
||
@test Note(NamedNote("C#4")) == Note(NamedNote("Db4")) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
using Test | ||
using MusicManipulations: scale_identification | ||
|
||
tester = getnotes(readMIDIFile(joinpath(@__DIR__, "recording_uwe_2.mid")),1) | ||
tester = getnotes(load(joinpath(@__DIR__, "recording_uwe_2.mid")),1) | ||
@test scale_identification(tester) == "A♯ Major/G minor" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters