Skip to content

Commit e7d7ac1

Browse files
Add note and notes constructor (#163)
* add note constructor. * use string to initialize Notes
1 parent 74b4e78 commit e7d7ac1

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
7+
# v2.5.0
8+
* Implement `Note(pitch_name::String; position = 0, velocity = 100, duration = 960, channel = 0)`
9+
* Implement `Notes(notes_string::String, tpq::Int = 960)`
710
# v2.4.0
811
* Implement `Base.empty!(::Notes)` and `Base.isempty(::Notes)`.
912
# v2.3.0

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "MIDI"
22
uuid = "f57c4921-e30c-5f49-b073-3f2f2ada663e"
33
repo = "https://github.com/JuliaMusic/MIDI.jl.git"
4-
version = "2.4.0"
4+
version = "2.5.0"
55

66
[deps]
77
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"

src/note.jl

+11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ If the `channel` of the note is `0` (default), it is not shown.
1111
You can also create a `Note` with the following keyword constructor:
1212
```julia
1313
Note(pitch, position; velocity = 100, duration = 960, channel = 0)
14+
Note(pitch_name::String; position = 0, velocity = 100, duration = 960, channel = 0)
1415
```
1516
1617
## Fields:
@@ -41,6 +42,8 @@ mutable struct Note <: AbstractNote
4142
end
4243
Note(pitch, position; velocity = 100, duration = 960, channel = 0) =
4344
Note(pitch, velocity, position, duration, channel)
45+
Note(pitch_name::String; position = 0, velocity = 100, duration = 960, channel = 0) =
46+
Note(name_to_pitch(pitch_name), velocity, position, duration, channel)
4447

4548
@inline Note(n::Note) = n
4649

@@ -69,9 +72,16 @@ N(n.pitch, n.velocity, n.position, n.duration, n.channel)
6972

7073
"""
7174
Notes(note_vector, tpq = 960) -> Notes
75+
Notes(notes_string::String, tpq::Int = 960) -> Notes
7276
A data structure describing a collection of music notes, bundled with the ticks
7377
per quarter note (so that the notes can be attributed rhythmic value).
7478
79+
Notes can be initialized by string, the name of notes are separated by spaces.
80+
81+
```julia
82+
Notes("C2 F3 D#6")
83+
```
84+
7585
`Notes` can be iterated and accessed as the given `note_vector`.
7686
This eliminates the need for custom iteration or search functions.
7787
For example, to get the note of maximum pitch you can do:
@@ -94,6 +104,7 @@ function Notes(notes::Vector{N}, tpq::Int = 960) where {N <: AbstractNote}
94104
end
95105

96106
Notes(; tpq = 960) = Notes{Note}(Vector{Note}[], tpq)
107+
Notes(notes_string::String, tpq::Int = 960) = Notes([Note(String(s)) for s in split(notes_string," ")], tpq)
97108

98109
# Iterator Interface for notes:
99110
Base.iterate(n::Notes, i = 1) = iterate(n.notes, i)

test/note.jl

+10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ cd(@__DIR__)
1010
@test typeof(notes[1:3]) == Notes{Note}
1111
@test typeof(notes[1:3]) <: Notes
1212
@test notes[1:3].notes == notes.notes[1:3]
13+
14+
notes2 = Notes("C2 F3 D#6")
15+
@test notes2[1].pitch == name_to_pitch("C2")
16+
@test notes2[2].pitch == name_to_pitch("F3")
17+
@test notes2[3].pitch == name_to_pitch("D#6")
18+
end
19+
20+
@testset "Note" begin
21+
c4 = Note("C4")
22+
@test c4.pitch == name_to_pitch("C4")
1323
end
1424

1525
@testset "pitch names" begin

0 commit comments

Comments
 (0)