Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bramtayl committed Jul 24, 2022
1 parent 33ce48b commit 0661610
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/Chord.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function Chord(
modulation = Modulation(),
words = "",
notes = Note[],
note_cursor = 0
note_cursor = 0,
)
Chord(modulation, words, notes, make_list_model(notes, instruments), note_cursor)
end
Expand Down
1 change: 0 additions & 1 deletion src/Instrument.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ end

export default_wave


const INSTRUMENT_ARGUMENT_TYPES =
(AudioSchedule, typeof(0.0s), typeof(1.0s), Float64, typeof(440.0Hz))

Expand Down
47 changes: 25 additions & 22 deletions src/Justly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,21 @@ include("Note.jl")
include("Chord.jl")
include("Song.jl")

function add_press!(audio_schedule, song, press_type, @nospecialize press_arguments)
if press_type == "play notes"
add_notes!(audio_schedule, song, press_arguments...)
elseif press_type == "play chords"
add_chords!(audio_schedule, song, press_arguments...)
else
throw(ArgumentError("Press type \"$press_type\" not recognized"))
end
end

function consume!(presses, stream, song, audio_schedule)
precompiling_observable = song.precompiling_observable
for (press_type, press_arguments) in presses
audio_schedule.is_on[] = true
if press_type == "play notes"
play_notes!(audio_schedule, song, press_arguments...)
elseif press_type == "play chords"
play_chords!(audio_schedule, song, press_arguments...)
else
throw(ArgumentError("Press type $press_type not recognized"))
end
add_press!(audio_schedule, song, press_type, press_arguments)
precompiling_observable[] = true
compile(stream, audio_schedule)
precompiling_observable[] = false
Expand All @@ -79,11 +83,7 @@ For more information, see the `README`.
```julia
julia> using Justly
julia> edit_justly(joinpath(pkgdir(Justly), "examples", "simple.yml"); test = true)
julia> edit_justly(joinpath(pkgdir(Justly), "not_a_folder", "simple.yml"))
ERROR: ArgumentError: Folder doesn't exist!
[...]
julia> edit_justly(joinpath(pkgdir(Justly), "examples", "simple.yml"))
```
"""
function edit_justly(song_file, instruments = DEFAULT_INSTRUMENTS; test = false)
Expand All @@ -109,9 +109,12 @@ function edit_justly(song_file, instruments = DEFAULT_INSTRUMENTS; test = false)
empty!(audio_schedule)
consume_task = @spawn consume!($presses, $stream, $song, $audio_schedule)
try
qmlfunction("press", let presses = presses
(action_type, arguments...) -> put!(presses, (action_type, arguments))
end)
qmlfunction(
"press",
let presses = presses
(action_type, arguments...) -> put!(presses, (action_type, arguments))
end,
)
qmlfunction("release", let is_on = audio_schedule.is_on
() -> is_on[] = false
end)
Expand All @@ -125,18 +128,18 @@ function edit_justly(song_file, instruments = DEFAULT_INSTRUMENTS; test = false)
"volume" => song.volume_observable,
"frequency" => song.frequency_observable,
"tempo" => song.tempo_observable,
"precompiling" => song.precompiling_observable
"precompiling" => song.precompiling_observable,
),
)
exec()
if test
# precompile before each play
# play the first note of the first chord
put!(presses, ("play notes", (1, 1, 1)))
# play the first chord
put!(presses, ("play chords", (1, 1)))
# play starting with the first chord
put!(presses, ("play chords", (1,)))
# play the second note of the second chord
put!(presses, ("play notes", (2, 2, 2)))
# play the second chord
put!(presses, ("play chords", (2, 2)))
# play starting with the second chord
put!(presses, ("play chords", (2,)))
end
catch an_error
showerror(stdout, an_error, catch_backtrace())
Expand Down
6 changes: 1 addition & 5 deletions src/Note.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ mutable struct Note
volume::Float64
end

function Note(instrument;
interval = Interval(),
beats = 1,
volume = 1.0
)
function Note(instrument; interval = Interval(), beats = 1, volume = 1.0)
Note(instrument, interval, beats, volume)
end

Expand Down
27 changes: 17 additions & 10 deletions src/Song.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function Song(
volume = 0.2,
frequency = 200.0,
tempo = 200.0,
precompiling = false
precompiling = false,
)
Song(
instruments,
Expand Down Expand Up @@ -102,7 +102,7 @@ function PlayState(song::Song, time)
song.volume_observable[],
(song.frequency_observable[])Hz,
(60 / song.tempo_observable[])s,
time
time,
)
end

Expand Down Expand Up @@ -142,7 +142,13 @@ end

precompile(push!, (AudioSchedule, Song, typeof(0.0s)))

@noinline function play_notes!(audio_schedule, song, chord_index, first_note_index, last_note_index)
@noinline function add_notes!(
audio_schedule,
song,
chord_index,
first_note_index,
last_note_index,
)
chords = song.chords
play_state = PlayState(song, 0.0s)
for chord in chords[1:(chord_index - 1)]
Expand All @@ -154,9 +160,14 @@ precompile(push!, (AudioSchedule, Song, typeof(0.0s)))
nothing
end

precompile(play_notes!, (AudioSchedule, Song, Int, Int, Int))
precompile(add_notes!, (AudioSchedule, Song, Int, Int, Int))

@noinline function play_chords!(audio_schedule, song, first_chord_index, last_chord_index = length(song.chords))
@noinline function add_chords!(
audio_schedule,
song,
first_chord_index,
last_chord_index = length(song.chords),
)
play_state = PlayState(song, 0.0s)
chords = song.chords
for chord in chords[1:(first_chord_index - 1)]
Expand All @@ -169,8 +180,4 @@ precompile(play_notes!, (AudioSchedule, Song, Int, Int, Int))
nothing
end

precompile(play_chords!, (AudioSchedule, Song, Int, Int))




precompile(add_chords!, (AudioSchedule, Song, Int, Int))
33 changes: 26 additions & 7 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
using Justly
using Justly: add_press!, DEFAULT_INSTRUMENTS, get_instrument, sustain!, pulse!
using Documenter: doctest
using Test: @test, @test_throws, @testset
using AudioSchedules: AudioSchedule, duration
using AudioSchedules: AudioSchedule, duration, Hz, s
using Unitful: s

cd(joinpath(pkgdir(Justly))) do
audio_schedule = AudioSchedule()
song = cd(joinpath(pkgdir(Justly))) do
@test_throws ArgumentError("Folder doesn't exist!") edit_justly("not_a_folder/simple.yml")
edit_justly("examples/simple.yml"; test = true)
song = read_justly("examples/simple.yml")
@test length(song.chords[1].notes) == 3
audio_schedule = AudioSchedule()
push!(audio_schedule, song, 0.0s)
push!(audio_schedule, song, 1.0s)
@test duration(audio_schedule) 2.27s
end
@test length(song.chords[1].notes) == 3
push!(audio_schedule, song, 0.0s)
push!(audio_schedule, song, 1.0s)
@test duration(audio_schedule) 2.27s
empty!(audio_schedule)
@test_throws ArgumentError("Instrument \"not an instrument\" not found!") get_instrument(
DEFAULT_INSTRUMENTS,
"not an instrument",
)
pulse!(audio_schedule, 0s, 0.01s, 1, 440Hz)
@test length(collect(audio_schedule)) == 2
empty!(audio_schedule)
sustain!(audio_schedule, 0s, 0.01s, 1, 440Hz)
@test length(collect(audio_schedule)) == 2
empty!(audio_schedule)
@test_throws ArgumentError("Press type \"not a press\" not recognized") add_press!(
audio_schedule,
song,
"not a press",
(),
)

if v"1.7" <= VERSION < v"1.8"
doctest(Justly)
Expand Down

2 comments on commit 0661610

@bramtayl
Copy link
Owner Author

Choose a reason for hiding this comment

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

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

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 v0.3.0 -m "<description of version>" 066161003da3977b1f23590f1a4978800882865f
git push origin v0.3.0

Please sign in to comment.