56
56
# process the chunk
57
57
end
58
58
finally
59
- close (stream )
59
+ close (audio )
60
60
end
61
61
```
62
62
63
63
or use ` do ` syntax to auto-close the stream:
64
64
65
65
``` jl
66
66
using FileIO
67
- do loadstreaming (" bigfile.wav" ) audio
67
+ loadstreaming (" bigfile.wav" ) do audio
68
68
while ! eof (audio)
69
69
chunk = read (audio, 4096 ) # read 4096 frames
70
70
# process the chunk
@@ -185,7 +185,7 @@ closing any streams you opened in order to read or write the file. If you are
185
185
given a ` Stream ` , your ` close ` method should only do the clean up for your
186
186
reader or writer type, not close the stream.
187
187
188
- ``` julia
188
+ ``` jl
189
189
struct WAVReader
190
190
io:: IO
191
191
ownstream:: Bool
@@ -197,28 +197,26 @@ end
197
197
198
198
function close (reader:: WAVReader )
199
199
# do whatever cleanup the reader needs
200
- if reader. ownstream
201
- close (reader. io)
202
- end
200
+ reader. ownstream && close (reader. io)
203
201
end
204
- loadstreaming (f:: File{format"WAV"} ) = WAVReader (open (f), ownstream = true )
205
- loadstreaming (s:: Stream{format"WAV"} ) = WAVReader (s, ownstream = false )
202
+ loadstreaming (f:: File{format"WAV"} ) = WAVReader (open (f), true )
203
+ loadstreaming (s:: Stream{format"WAV"} ) = WAVReader (s, false )
206
204
# FileIO has fallback functions that make these work using `do` syntax as well.
207
205
```
208
206
209
207
If you choose to implement ` loadstreaming ` and ` savestreaming ` in your package,
210
208
you can easily add ` save ` and ` load ` methods in the form of:
211
209
212
- ``` julia
210
+ ``` jl
213
211
function save (q:: Formatted{format"WAV"} , data, args... ; kwargs... )
214
- savestreaming (args... ; kwargs... ) do stream
212
+ savestreaming (q, args... ; kwargs... ) do stream
215
213
write (stream, data)
216
214
end
217
215
end
218
216
219
217
function load (q:: Formatted{format"WAV"} , args... ; kwargs... )
220
- savestreaming (args... ; kwargs... ) do stream
221
- readall (stream)
218
+ savestreaming (q, args... ; kwargs... ) do stream
219
+ read (stream)
222
220
end
223
221
end
224
222
```
0 commit comments