updates for SampledSignals 0.2.0 API

This commit is contained in:
Spencer Russell 2016-09-04 13:05:42 -04:00
parent 7012c445f8
commit 4fe967465c
2 changed files with 9 additions and 13 deletions

View file

@ -264,20 +264,18 @@ function Base.flush(sink::PortAudioSink)
end end
end end
function SampledSignals.unsafe_write(sink::PortAudioSink, buf::SampleBuf) function SampledSignals.unsafe_write(sink::PortAudioSink, buf::Array, frameoffset, framecount)
total = nframes(buf)
nwritten = 0 nwritten = 0
while nwritten < total while nwritten < framecount
while nwritable(sink.ringbuf) == 0 while nwritable(sink.ringbuf) == 0
wait(sink.ringbuf) wait(sink.ringbuf)
end end
# in 0.4 transpose! throws an error if the range is a UInt # in 0.4 transpose! throws an error if the range is a UInt
writable = div(nwritable(sink.ringbuf), nchannels(sink)) writable = div(nwritable(sink.ringbuf), nchannels(sink))
towrite = Int(min(writable, CHUNKSIZE, total-nwritten)) towrite = Int(min(writable, CHUNKSIZE, framecount-nwritten))
# make a buffer of interleaved samples # make a buffer of interleaved samples
# TODO: don't directly access buf.data
transpose!(view(sink.chunkbuf, :, 1:towrite), transpose!(view(sink.chunkbuf, :, 1:towrite),
view(buf.data, (1:towrite)+nwritten, :)) view(buf, (1:towrite)+nwritten+frameoffset, :))
write(sink.ringbuf, sink.chunkbuf, towrite*nchannels(sink)) write(sink.ringbuf, sink.chunkbuf, towrite*nchannels(sink))
nwritten += towrite nwritten += towrite
@ -286,20 +284,18 @@ function SampledSignals.unsafe_write(sink::PortAudioSink, buf::SampleBuf)
nwritten nwritten
end end
function SampledSignals.unsafe_read!(source::PortAudioSource, buf::SampleBuf) function SampledSignals.unsafe_read!(source::PortAudioSource, buf::Array, frameoffset, framecount)
total = nframes(buf)
nread = 0 nread = 0
while nread < total while nread < framecount
while nreadable(source.ringbuf) == 0 while nreadable(source.ringbuf) == 0
wait(source.ringbuf) wait(source.ringbuf)
end end
# in 0.4 transpose! throws an error if the range is a UInt # in 0.4 transpose! throws an error if the range is a UInt
readable = div(nreadable(source.ringbuf), nchannels(source)) readable = div(nreadable(source.ringbuf), nchannels(source))
toread = Int(min(readable, CHUNKSIZE, total-nread)) toread = Int(min(readable, CHUNKSIZE, framecount-nread))
read!(source.ringbuf, source.chunkbuf, toread*nchannels(source)) read!(source.ringbuf, source.chunkbuf, toread*nchannels(source))
# de-interleave the samples # de-interleave the samples
# TODO: don't directly access buf.data transpose!(view(buf, (1:toread)+nread+frameoffset, :),
transpose!(view(buf.data, (1:toread)+nread, :),
view(source.chunkbuf, :, 1:toread)) view(source.chunkbuf, :, 1:toread))
nread += toread nread += toread

View file

@ -119,7 +119,7 @@ end
close(stream) close(stream)
@test size(buf) == (round(Int, 5s * samplerate(stream)), nchannels(stream.source)) @test size(buf) == (round(Int, 5s * samplerate(stream)), nchannels(stream.source))
println("Playing back recording...") println("Playing back recording...")
stream = PortAudioStream() stream = PortAudioStream(0, 2)
write(stream, buf) write(stream, buf)
println("flushing...") println("flushing...")
flush(stream) flush(stream)