adds do syntax support
This commit is contained in:
parent
7d1be74eae
commit
8d42b94a6a
2 changed files with 28 additions and 3 deletions
17
README.md
17
README.md
|
@ -53,13 +53,26 @@ PortAudio.jl also provides convenience wrappers around the `PortAudioStream` typ
|
|||
|
||||
```julia
|
||||
stream = PortAudioStream(2, 2)
|
||||
try
|
||||
# cancel with Ctrl-C
|
||||
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
|
||||
```julia
|
||||
stream = PortAudioStream("Built-in Microph", "Built-in Output")
|
||||
PortAudioStream("Built-in Microph", "Built-in Output") do stream
|
||||
write(stream, stream)
|
||||
end
|
||||
```
|
||||
|
||||
### 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)
|
||||
```
|
||||
|
||||
|
|
|
@ -210,6 +210,16 @@ function PortAudioStream(inchans=2, outchans=2; kwargs...)
|
|||
PortAudioStream(indevice, outdevice, inchans, outchans; kwargs...)
|
||||
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 active_streams = Set{PortAudioStream}()
|
||||
|
||||
|
|
Loading…
Reference in a new issue