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
# 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, ::MIME"text/html", r::Recording)
print(io, "<audio controls ")
print(io, "src=\"data:audio/wav;base64,")
x, s = load(r)
iob64_encode = Base64EncodePipe(io)
wavwrite(x, iob64_encode, Fs = s, nbits = 8, compression = WAV.WAVE_FORMAT_PCM)
close(iob64_encode)
println(io, "\" />")
end
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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), "\"channel\": ", s.channel, ", ")
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["channel"],
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
manifestname(T::Type{<:Recording}, subset) = "recording-manifest-$(subset).jsonl"
manifestname(T::Type{<:Supervision}, subset) = "supervision-manifest-$(subset).jsonl"
load(T::Type{<:Union{Recording,Supervision}}, path::AbstractString) =
open(f -> readmanifest(f, T), path, "r")
load(corpus::SpeechCorpus, dir, T, subset) =
load(T, joinpath(path(corpus, dir), manifestname(T, subset)))
load(corpus::SpeechCorpus, T, subset) =
load(corpus, corporadir, T, subset)