adds do syntax support

This commit is contained in:
Spencer Russell 2018-12-05 11:17:36 -05:00
parent 7d1be74eae
commit 8d42b94a6a
2 changed files with 28 additions and 3 deletions

View file

@ -53,13 +53,26 @@ PortAudio.jl also provides convenience wrappers around the `PortAudioStream` typ
```julia ```julia
stream = PortAudioStream(2, 2) stream = PortAudioStream(2, 2)
try
# cancel with Ctrl-C
write(stream, stream) write(stream, stream)
finally
close(stream)
end
```
### Use `do` syntax to auto-close the stream
```julia
PortAudioStream(2, 2) do stream
write(stream, stream)
end
``` ```
### Open your built-in microphone and speaker by name ### Open your built-in microphone and speaker by name
```julia ```julia
stream = PortAudioStream("Built-in Microph", "Built-in Output") PortAudioStream("Built-in Microph", "Built-in Output") do stream
write(stream, stream) write(stream, stream)
end
``` ```
### Record 10 seconds of audio and save to an ogg file ### Record 10 seconds of audio and save to an ogg file
@ -79,6 +92,8 @@ julia> buf = read(stream, 10s)
▁▄▂▃▅▃▂▄▃▂▂▁▁▂▂▁▁▄▃▁▁▄▂▁▁▁▄▃▁▁▃▃▁▁▁▁▁▁▁▁▄▄▄▄▄▂▂▂▁▃▃▁▃▄▂▁▁▁▁▃▃▂▁▁▁▁▁▁▃▃▂▂▁▃▃▃▁▁▁▁ ▁▄▂▃▅▃▂▄▃▂▂▁▁▂▂▁▁▄▃▁▁▄▂▁▁▁▄▃▁▁▃▃▁▁▁▁▁▁▁▁▄▄▄▄▄▂▂▂▁▃▃▁▃▄▂▁▁▁▁▃▃▂▁▁▁▁▁▁▃▃▂▂▁▃▃▃▁▁▁▁
▁▄▂▃▅▃▂▄▃▂▂▁▁▂▂▁▁▄▃▁▁▄▂▁▁▁▄▃▁▁▃▃▁▁▁▁▁▁▁▁▄▄▄▄▄▂▂▂▁▃▃▁▃▄▂▁▁▁▁▃▃▂▁▁▁▁▁▁▃▃▂▂▁▃▃▃▁▁▁▁ ▁▄▂▃▅▃▂▄▃▂▂▁▁▂▂▁▁▄▃▁▁▄▂▁▁▁▄▃▁▁▃▃▁▁▁▁▁▁▁▁▄▄▄▄▄▂▂▂▁▃▃▁▃▄▂▁▁▁▁▃▃▂▁▁▁▁▁▁▃▃▂▂▁▃▃▃▁▁▁▁
julia> close(stream)
julia> save(joinpath(homedir(), "Desktop", "myvoice.ogg"), buf) julia> save(joinpath(homedir(), "Desktop", "myvoice.ogg"), buf)
``` ```

View file

@ -210,6 +210,16 @@ function PortAudioStream(inchans=2, outchans=2; kwargs...)
PortAudioStream(indevice, outdevice, inchans, outchans; kwargs...) PortAudioStream(indevice, outdevice, inchans, outchans; kwargs...)
end end
# handle do-syntax
function PortAudioStream(fn::Function, args...; kwargs...)
str = PortAudioStream(args...; kwargs...)
try
fn(str)
finally
close(str)
end
end
const pa_inited = Ref(false) const pa_inited = Ref(false)
const active_streams = Set{PortAudioStream}() const active_streams = Set{PortAudioStream}()