Skip to content

Commit

Permalink
Merge pull request #57 from michiboo/master
Browse files Browse the repository at this point in the history
add humanize function to generate pink noise
  • Loading branch information
Datseris authored Sep 25, 2020
2 parents 81ad527 + 99be82e commit 8d734fc
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ os:
- osx

julia:
- 1.2
- 1
# - nightly # commented to save energy and time

before_script:
Expand Down
7 changes: 5 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
name = "MusicManipulations"
uuid = "274955c0-c284-5bf7-b122-5ecd51c559de"
repo = "https://github.com/JuliaMusic/MusicManipulations.jl.git"
version = "1.5.0"
version = "1.6.0"

[deps]
ARFIMA = "9d0fb3db-ba49-4108-bc86-650b3813b6d5"
MIDI = "f57c4921-e30c-5f49-b073-3f2f2ada663e"
MotifSequenceGenerator = "7d228ccb-0fc9-53c8-a429-8c9b5b572bc3"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"

[compat]
julia = "1.1"
ARFIMA = "0.3.0"
MIDI = "1.4.1"
MotifSequenceGenerator = "1.0"
julia = "1.1"
Reexport = "0.2, 0.3, 1"
StatsBase = "0.32, 0.33"

Expand Down
2 changes: 2 additions & 0 deletions src/MusicManipulations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ include("general.jl")
include("midifiles.jl")
include("quantize.jl")
include("scale.jl")
include("humanize.jl")

include("data_handling/data_extraction.jl")
include("data_handling/timeseries.jl")

Expand Down
37 changes: 37 additions & 0 deletions src/humanize.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export humanize!, humanize

using ARFIMA, Statistics
const φ0 = ARFIMA.SVector(-0.5, -1.5)

"""
humanize!(notes, property, σ, noise = :ARFIMA; kwargs...)
Humanize given `notes` by adding noise of standard deviation `σ` to their `property`,
typically either `:position` or `:velocity`. Research[^Datseris2019] suggests that `σ`
should be around 40 (for `:position`) but that depends on the BPM, and around 10 for `:velocity`.
The `noise` argument decides the type of noise:
* `:ARFIMA` uses ARFIMA.jl and attempts to generate a power-law correlated (pink) noise.
Keywords `d = 0.25, φ = SVector(-0.5, -1.5)` are propagated to function `arfima`.
* `:white` plain ol' white noise.
Use `humanize` for a non-modifying version.
[^Datseris2019]: Datseris, G., et al. [Microtiming Deviations and Swing Feel in Jazz. Sci Rep 9, 19824 (2019)](https://doi.org/10.1038/s41598-019-55981-3)
"""
function humanize!(n::Notes, property, σ, noise = :ARFIMA; d=0.25, φ=φ0)
N = length(n)
if noise == :ARFIMA
ξ = arfima(N, 1.0, d, φ)
elseif noise == :white
ξ = randn(N)
else
error("Unrecognized noise type")
end
ξ .*= σ/std(ξ)
for j in 1:N
setproperty!(n[j], property, getproperty(n[j], property) + round(Int, ξ[j]))
end
return n
end

humanize(notes, args...; kwargs...) = humanize!(copy(notes), args...; kwargs...)
13 changes: 13 additions & 0 deletions test/humanizer_tests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using MusicManipulations, Test, Statistics

@testset "humanize" begin

#test of humanize functionality
notes = randomnotes(100)
for n in notes; n.velocity = 100; end
hnotes = humanize(notes, :velocity, 10)
@test any(i -> notes[i].velocity hnotes[i].velocity, 1:length(notes))
σ = std(velocities(hnotes))
@test 9.5 σ 10.5

end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ include("quantizer_tests.jl")
include("timeseries.jl")
include("data_extraction.jl")
include("scale_tests.jl")
include("humanizer_tests.jl")

2 comments on commit 8d734fc

@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/22000

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 the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.6.0 -m "<description of version>" 8d734fc75776a62545b314f866ca0dd46d291837
git push origin v1.6.0

Please sign in to comment.