Skip to content

Commit

Permalink
add repeat function
Browse files Browse the repository at this point in the history
  • Loading branch information
Datseris committed Jan 19, 2020
1 parent bf98275 commit 8033a30
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
All notable changes to this project will be documented in this file.

This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
# 1.4.0
* New function `repeat`.
* `combine` now works with `Vector{Vector{<:Note}}` as well.

# 1.3.0
* `translate, transpose, louden` now also work on single notes.

Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "MusicManipulations"
uuid = "274955c0-c284-5bf7-b122-5ecd51c559de"
repo = "https://github.com/JuliaMusic/MusicManipulations.jl.git"
version = "1.3.0"
version = "1.4.0"

[deps]
MIDI = "f57c4921-e30c-5f49-b073-3f2f2ada663e"
Expand Down
41 changes: 35 additions & 6 deletions src/general.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ N(n.pitch, n.velocity+ticks, n.position, n.duration, n.channel)

"""
timesort!(notes::Notes)
Sort the `notes` by their temporal position.
Non-mutating version also exists.
In-place sort the `notes` by their temporal position.
Use `timesort` for a non-mutating version.
"""
function timesort!(notes::Notes)
sort!(notes.notes, by = x -> x.position)
Expand All @@ -90,20 +90,31 @@ Combine the given container (either `Array{Notes}` or `Dict{Any, Notes}`) into
a single `Notes` instance. In the process, sort the notes by position in the
final container.
"""
function combine(notearray::AbstractArray{<:Notes})
function combine(notearray::AbstractArray{<:Notes}, tsort = true)
notes = copy(notearray[1])
for i in 2:length(notearray)
append!(notes, notearray[i])
end
timesort!(notes)
tsort && timesort!(notes)
return notes
end

function combine(notedict::Dict{<:Any, Notes{N}}) where {N}
function combine(notearray::AbstractArray{<:AbstractArray{<:AbstractNote}}, tsort=true)
notes = copy(notearray[1])
for i in 2:length(notearray)
append!(notes, notearray[i])
end
tsort && timesort!(notes)
return notes
end

function combine(notedict::Dict{<:Any, Notes{N}}, tsort = true) where {N}
n = Notes(N[], first(values(notedict)).tpq)
for (k, v) in notedict
append!(n, v)
end
timesort!(n)
tsort && timesort!(n)
return n
end


Expand All @@ -126,3 +137,21 @@ function relpos(notes::Notes, grid)
end
return rpos
end

"""
repeat(notes, i = 1)
Repeat the `notes` `i` times, by successively adding duplicates of `notes`
shifted by the total duration of `notes`. Return a single `Notes` container
for convenience.
The function assumes that notes are `timesort`ed.
"""
Base.repeat(n::Notes, i::Int = 1) = Notes(repeat(n.notes, i), n.tpq)
function Base.repeat(n::Vector{<:AbstractNote}, i::Int = 1)
maxdur = maximum(a.position + a.duration for a in n)
r = [copy(n)]
for j in 1:i
push!(r, translate(r[end], maxdur))
end
return combine(r, false)
end

2 comments on commit 8033a30

@Datseris
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/8156

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v1.4.0 -m "<description of version>" 8033a3028ed04d41d3671d7fcac71537ee2706cd
git push origin v1.4.0

Please sign in to comment.