Skip to content

Commit

Permalink
Fix issues with overloading FileIO.save and load. (#150)
Browse files Browse the repository at this point in the history
* be explicit about FileIO

* rename to fileio_load

* remove unecessary docstrings

* bump project version

* remove code coverage
  • Loading branch information
Datseris authored Oct 20, 2021
1 parent c827fdd commit ec410a5
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CompatHelper.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
julia-version: [1.2.0]
julia-version: [1]
julia-arch: [x86]
os: [ubuntu-latest]
steps:
Expand Down
3 changes: 0 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,3 @@ jobs:
- uses: julia-actions/julia-buildpkg@latest
- uses: julia-actions/julia-runtest@latest
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v1
with:
file: lcov.info
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "MIDI"
uuid = "f57c4921-e30c-5f49-b073-3f2f2ada663e"
repo = "https://github.com/JuliaMusic/MIDI.jl.git"
version = "2.0.2"
version = "2.0.3"

[deps]
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# MIDI.jl

| **Documentation** | **Travis** | **Citation** |
| **Documentation** | **Tests** | **Citation** |
|:--------:|:--------:|:------:|
|[![](https://img.shields.io/badge/docs-online-blue.svg)](https://juliamusic.github.io/JuliaMusic_documentation.jl/latest/)| [![Build Status](https://travis-ci.org/JuliaMusic/MIDI.jl.svg?branch=master)](https://travis-ci.org/JuliaMusic/MIDI.jl) | [![status](http://joss.theoj.org/papers/e0cfc67982f857ed96d906ff2266aa15/status.svg)](http://joss.theoj.org/papers/e0cfc67982f857ed96d906ff2266aa15)
|[![](https://img.shields.io/badge/docs-online-blue.svg)](https://juliamusic.github.io/JuliaMusic_documentation.jl/latest/)| [![CI](https://github.com/juliamusic/MIDI.jl/workflows/CI/badge.svg)](https://github.com/JuliaMusic/MIDI.jl/actions) | [![status](http://joss.theoj.org/papers/e0cfc67982f857ed96d906ff2266aa15/status.svg)](http://joss.theoj.org/papers/e0cfc67982f857ed96d906ff2266aa15)

---

Expand Down
1 change: 0 additions & 1 deletion src/MIDI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ A Julia library for reading and writing MIDI files.
"""
module MIDI

using FileIO
using Base.Unicode

include("constants.jl")
Expand Down
39 changes: 14 additions & 25 deletions src/io.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
"""
load(filename::File{format"MIDI"})
Read a file into a `MIDIFile` data type.
# This file extends the FileIO.jl interface for filenames ending with ".mid".
# See https://juliaio.github.io/FileIO.jl/stable/implementing/#Implementing-loaders/savers

!!! note
This function must not be called explicitly. [`FileIO.load`](https://juliaio.github.io/FileIO.jl/stable/) must be called instead.
"""
function load(f::File{format"MIDI"})
using FileIO
export load, save

function fileio_load(f::File{format"MIDI"})
open(f) do s
skipmagic(s)
midifile = load(s)
end
end

function load(s::Stream{format"MIDI"})
function fileio_load(s::Stream{format"MIDI"})
midifile = MIDIFile()

# Skip the next four bytes - this is the header size, and it's always equal to 6.
Expand All @@ -27,41 +26,31 @@ function load(s::Stream{format"MIDI"})
midifile.tpq = ntoh(read(s, Int16))
midifile.tracks = [readtrack(s.io) for x in 1:numberoftracks]

midifile
return midifile
end

"""
save(filename::File{format"MIDI"}, data::MIDIFile)
Write a `MIDIFile` as a ".mid" file to the given filename.
save(filename::File{format"MIDI"}, notes::Notes)
Create a `MIDIFile` directly from `notes`, using format 1.
!!! note
This function must not be called explicitly. [`FileIO.save`](https://juliaio.github.io/FileIO.jl/stable/) must be called instead.
"""
function save(f::File{format"MIDI"}, data::MIDIFile)
function fileio_save(f::File{format"MIDI"}, data::MIDIFile)
open(f, "w") do s
save(s, data)
fileio_save(s, data)
end
end

function save(s::Stream{format"MIDI"}, data::MIDIFile)
function fileio_save(s::Stream{format"MIDI"}, data::MIDIFile)
write(s, magic(format"MIDI"))

write(s, hton(convert(UInt32, 6))) # Header length
write(s, hton(data.format))
write(s, hton(convert(UInt16, length(data.tracks))))
write(s, hton(data.tpq))

map(track->writetrack(s.io, track), data.tracks)
map(track -> writetrack(s.io, track), data.tracks)
return data
end

function save(s::Union{File{format"MIDI"}, Stream{format"MIDI"}}, notes::Notes)
function fileio_save(s::Union{File{format"MIDI"}, Stream{format"MIDI"}}, notes::Notes)
track = MIDITrack()
addnotes!(track, notes)
midi = MIDIFile(1, notes.tpq, [track])
save(s, midi)
fileio_save(s, midi)
return midi
end

0 comments on commit ec410a5

Please sign in to comment.