Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# SPDX-License-Identifier: CECILL-2.1
#=====================================================================#
# JSON serialization of a manifest item
function Base.show(io::IO, m::MIME"application/json", s::FileAudioSource)
compact = get(io, :compact, false)
indent = get(io, :indent, 0)
printfn = compact ? print : println
printfn(io, "{")
printfn(io, repeat(" ", indent + (compact ? 0 : 2)), "\"type\": \"path\", ")
printfn(io, repeat(" ", indent + (compact ? 0 : 2)), "\"data\": \"", s.path, "\"")
print(io, repeat(" ", indent), "}")
end
function Base.show(io::IO, m::MIME"application/json", s::URLAudioSource)
compact = get(io, :compact, false)
indent = get(io, :indent, 0)
printfn = compact ? print : println
printfn(io, repeat(" ", indent), "{")
printfn(io, repeat(" ", indent + (compact ? 0 : 2)), "\"type\": \"url\", ")
printfn(io, repeat(" ", indent + (compact ? 0 : 2)), "\"data\": \"", s.url, "\"")
print(io, repeat(" ", indent), "}")
end
function Base.show(io::IO, m::MIME"application/json", s::CmdAudioSource)
compact = get(io, :compact, false)
indent = get(io, :indent, 0)
printfn = compact ? print : println
printfn(io, "{")
printfn(io, repeat(" ", indent + (compact ? 0 : 2)), "\"type\": \"cmd\", ")
strcmd = replace("$(s.cmd)", "`" => "")
printfn(io, repeat(" ", indent + (compact ? 0 : 2)), "\"data\": \"$(strcmd)\"")
print(io, repeat(" ", indent), "}")
end
function Base.show(io::IO, m::MIME"application/json", r::Recording)
compact = get(io, :compact, false)
indent = compact ? 0 : 2
printfn = compact ? print : println
printfn(io, "{")
printfn(io, repeat(" ", indent), "\"id\": \"", r.id, "\", ")
print(io, repeat(" ", indent), "\"src\": ")
show(IOContext(io, :indent => compact ? 0 : 2), m, r.source)
printfn(io, ", ")
print(io, repeat(" ", indent), "\"channels\": [")
for (i, c) in enumerate(r.channels)
print(io, c)
i < length(r.channels) && print(io, ",")
end
printfn(io, "], ")
printfn(io, repeat(" ", indent), "\"samplerate\": ", r.samplerate)
print(io, "}")
end
function Base.show(io::IO, m::MIME"application/json", s::Supervision)
compact = get(io, :compact, false)
indent = compact ? 0 : 2
printfn = compact ? print : println
printfn(io, "{")
printfn(io, repeat(" ", indent), "\"id\": \"", s.id, "\", ")
printfn(io, repeat(" ", indent), "\"recording_id\": \"", s.recording_id, "\", ")
printfn(io, repeat(" ", indent), "\"start\": ", s.start, ", ")
printfn(io, repeat(" ", indent), "\"duration\": ", s.duration, ", ")
printfn(io, repeat(" ", indent), "\"channels\": ", s.channels |> json, ", ")
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
printfn(io, repeat(" ", indent), "\"data\": ", s.data |> json)
print(io, "}")
end
function JSON.json(r::Union{Recording, Supervision}; compact = true)
out = IOBuffer()
show(IOContext(out, :compact => compact), MIME("application/json"), r)
String(take!(out))
end
#=====================================================================#
# Converting a dictionary to a manifest item.
function AudioSource(d::Dict)
if d["type"] == "path"
T = FileAudioSource
elseif d["type"] == "url"
T = URLAudioSource
elseif d["type"] == "cmd"
T = CmdAudioSource
else
throw(ArgumentError("invalid type: $(d["type"])"))
end
T(d["data"])
end
Recording(d::Dict) = Recording(
d["id"],
AudioSource(d["src"]),
convert(Vector{Int}, d["channels"]),
d["samplerate"]
)
Supervision(d::Dict) = Supervision(
d["id"],
d["recording_id"],
d["start"],
d["duration"],
d["data"]
)
#=====================================================================#
# Writing / reading manifest from file.
function writemanifest(io::IO, manifest::Dict)
writefn = x -> println(io, x)
for item in values(manifest)
item |> json |> writefn
end
end
function readmanifest(io::IO, T)
manifest = Dict()
for line in eachline(io)
item = JSON.parse(line) |> T
manifest[item.id] = item
end
manifest
end
# Some utilities
manifestname(::Type{<:Recording}, name) = "recordings-$name.jsonl"
manifestname(::Type{<:Supervision}, name) = "supervisions-$name.jsonl"
"""
load(Supervision, path)
load(Recording, path)
Load Recording/Supervision manifest from `path`.
"""
load(T::Type{<:Union{Recording,Supervision}}, manifestpath::AbstractString) =
open(f -> readmanifest(f, T), manifestpath, "r")
load(T::Type{<:Union{Recording, Supervision}}, manifestroot::AbstractString, subset) =
load(T, joinpath(manifestroot, manifestname(T, subset)))