releases read/write busy flag on an exception

This commit is contained in:
Spencer Russell 2016-03-20 00:53:08 -04:00
parent 8ee46f5125
commit d193a9c83d
2 changed files with 51 additions and 33 deletions

View file

@ -1,7 +1,18 @@
PortAudio.jl
==========
============
[![Build Status](https://travis-ci.org/JuliaAudio/PortAudio.jl.svg?branch=master)](https://travis-ci.org/JuliaAudio/PortAudio.jl)
[![codecov.io] (http://codecov.io/github/JuliaAudio/PortAudio.jl/coverage.svg?branch=master)] (http://codecov.io/github/JuliaAudio/PortAudio.jl?branch=master)
PortAudio.jl is a wrapper for [libportaudio](http://www.portaudio.com/), which gives cross-platform access to audio devices. It is compatible with the types defined in [SampleTypes.jl](https://github.com/JuliaAudio/SampleTypes.jl), so it provides `PASampleSink` and `PASampleSource` types, which can be read from and written to.
PortAudio.jl is a wrapper for [libportaudio](http://www.portaudio.com/), which gives cross-platform access to audio devices. It is compatible with the types defined in [SampleTypes.jl](https://github.com/JuliaAudio/SampleTypes.jl), so it provides `PortAudioSink` and `PortAudioSource` types, which can be read from and written to.
## Examples
### Set up an audio pass-through from microphone to speaker
```julia
src = PortAudioSource()
sink = PortAudioSink()
write(sink, source)
end
```

View file

@ -93,9 +93,10 @@ function SampleTypes.unsafe_write(sink::PortAudioSink, buf::SampleBuf)
shift!(sink.waiters)
end
sink.busy = true
total = nframes(buf)
written = 0
try
sink.busy = true
while written < total
n = min(size(sink.pabuf, 2), total-written, Pa_GetStreamWriteAvailable(sink.stream))
@ -107,11 +108,14 @@ function SampleTypes.unsafe_write(sink::PortAudioSink, buf::SampleBuf)
written += n
sleep(POLL_SECONDS)
end
finally
# make sure we release the busy flag even if the user ctrl-C'ed out
sink.busy = false
if length(sink.waiters) > 0
# let the next task in line go
notify(sink.waiters[1])
end
end
written
end
@ -124,11 +128,12 @@ function SampleTypes.unsafe_read!(source::PortAudioSource, buf::SampleBuf)
shift!(source.waiters)
end
source.busy = true
total = nframes(buf)
read = 0
try
source.busy = true
while read < total
n = min(size(source.pabuf, 2), total-read, Pa_GetStreamReadAvailable(source.stream))
Pa_ReadStream(source.stream, source.pabuf, n, false)
@ -140,11 +145,13 @@ function SampleTypes.unsafe_read!(source::PortAudioSource, buf::SampleBuf)
sleep(POLL_SECONDS)
end
finally
source.busy = false
if length(source.waiters) > 0
# let the next task in line go
notify(source.waiters[1])
end
end
read
end