diff --git a/README.md b/README.md index ef96132..879f46a 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ # MusicMakie [![Build Status](https://github.com/dpsanders/MusicMakie.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/dpsanders/MusicMakie.jl/actions/workflows/CI.yml?query=branch%3Amain) + diff --git a/examples/scale.jl b/examples/scale.jl index 4c9a0b5..5c00357 100644 --- a/examples/scale.jl +++ b/examples/scale.jl @@ -1,38 +1,43 @@ using Revise, MusicTheory, MusicTheory.PitchNames using CairoMakie, MusicMakie -# scale = Scale(C[4], major_scale) -scale = Scale(E[3], major_scale) +function draw_scale() -pitches = Base.Iterators.take(scale, 3*8-1) |> collect + # scale = Scale(C[4], major_scale) + scale = Scale(E[3], major_scale) + pitches = Base.Iterators.take(scale, 2*8-1) |> collect + notes = pitches ./ 1 -fig, ax = make_canvas() + fig, ax = make_canvas() -s = Stave(0, 30, 0, 0.5) + s = Stave(0, 30, 0, 0.5) -sc = StaveWithClef(s, treble_clef) + sc = StaveWithClef(s, treble_clef) -draw!(ax, sc) + draw!(ax, sc) + draw!(ax, sc, notes, x0=3, w = 1.1) + @show notes -draw!(ax, sc, pitches, x0=3, w = 1.1) + fig +end -fig +draw_scale() -function scale_notes(p::Pitch) - scale = Scale(p, natural_minor_scale) +# function scale_notes(p::Pitch) +# scale = Scale(p, natural_minor_scale) - pitches = Base.Iterators.take(scale, 3*8-1) |> collect +# pitches = Base.Iterators.take(scale, 3*8-1) |> collect - return pitches -end +# return pitches +# end + +# scale_notes(C[4]) -scale_notes(C[4]) -fig diff --git a/src/notes.jl b/src/notes.jl index 1ad0470..a1c79e1 100644 --- a/src/notes.jl +++ b/src/notes.jl @@ -7,22 +7,26 @@ function draw_circle!(ax, cx, cy, r) poly!(ax, xs, ys, color=:blue) end -function draw_ellipse!(ax, cx, cy, a, b; color=RGBAf(0, 0, 1, 0.5)) +function draw_ellipse!(ax, cx, cy, a, b; color=RGBAf(0, 0, 1, 0.5), filled=true) θs = 0:0.1:2π xs = cx .+ a .* cos.(θs) ys = cy .+ b .* sin.(θs) - poly!(ax, Point2.(xs, ys), color=color, alpha=0.7) + if filled + poly!(ax, Point2.(xs, ys), color=color, alpha=0.7) + else + lines!(ax, xs, ys, color=color, alpha=0.7, linewidth=5) + end end -function draw_note_head!(ax, s::Stave, x, pos; color=RGBAf(0, 0, 1, 0.5)) +function draw_note_head!(ax, s::Stave, x, pos; color=RGBAf(0, 0, 1, 0.5), filled=true) y = height(s, pos) h = 0.5 * s.h w = 1.2 * h - draw_ellipse!(ax, x, y, w, h, color=color) + draw_ellipse!(ax, x, y, w, h; filled=filled, color=color) end function draw_stem_up!(ax, s::Stave, x, pos; color=RGBAf(0, 0, 1, 0.5)) @@ -68,18 +72,37 @@ end function draw!(ax, s::StaveWithClef, p::Pitch, x; color=RGBAf(0, 0, 1, 0.5)) + draw!(ax, s, Note(p, 1//4), x, color=color) +end + + +function draw!(ax, s::StaveWithClef, n::Note, x; color=RGBAf(0, 0, 1, 0.5)) + p = n.pitch pos = map_to_stave(p, s.clef) + if accidental(p) != ♮ + draw_text!(ax, s.stave, x, pos, string(accidental(p))) + x += 1 + end + draw_leger_lines!(ax, s.stave, x, pos, color=color) - draw_note_head!(ax, s.stave, x, pos, color=color) - if pos > 0 - draw_stem_down!(ax, s.stave, x, pos, color=color) - else - draw_stem_up!(ax, s.stave, x, pos, color=color) - end + duration = n.duration + + filled = (duration == 1 // 4) + draw_note_head!(ax, s.stave, x, pos, color=color, filled = filled) + draw_stem = (duration == 1 // 2) || (duration == 1 // 4) + if draw_stem + if pos > 0 + draw_stem_down!(ax, s.stave, x, pos, color=color) + else + draw_stem_up!(ax, s.stave, x, pos, color=color) + end + end + + return x end @@ -89,14 +112,14 @@ end x0 is the left-most position """ -function draw!(ax, s::StaveWithClef, pitches::Vector{Pitch}; +function draw!(ax, s::StaveWithClef, notes::Vector{<:Union{Pitch, Note}}; x0 = 1.0, w = 1, color = RGBAf(0, 0, 1, 0.5)) x = x0 - for p in pitches - draw!(ax, s, p, x, color=color) + for p in notes + x = draw!(ax, s, p, x, color=color) x += w end -end \ No newline at end of file +end