Skip to content

Commit

Permalink
Distinguish between 1/4, 1/2 and 1 duration. Add accidentals
Browse files Browse the repository at this point in the history
  • Loading branch information
dpsanders committed Jan 22, 2024
1 parent fbbb727 commit 4c041ba
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 30 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)

37 changes: 21 additions & 16 deletions examples/scale.jl
Original file line number Diff line number Diff line change
@@ -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



51 changes: 37 additions & 14 deletions src/notes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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


Expand All @@ -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
end

0 comments on commit 4c041ba

Please sign in to comment.