now just using floating-point samplerate
This commit is contained in:
parent
eaa5aa96bb
commit
15dcee6245
2 changed files with 19 additions and 19 deletions
|
@ -89,9 +89,8 @@ immutable CallbackInfo{T}
|
|||
synced::Bool
|
||||
end
|
||||
|
||||
# paramaterized on the sample type and sampling rate type
|
||||
type PortAudioStream{T, U}
|
||||
samplerate::U
|
||||
type PortAudioStream{T}
|
||||
samplerate::Float64
|
||||
blocksize::Int
|
||||
stream::PaStream
|
||||
sink # untyped because of circular type definition
|
||||
|
@ -112,8 +111,8 @@ type PortAudioStream{T, U}
|
|||
Ref(Pa_StreamParameters(outdev.idx, outchans, type_to_fmt[T], 0.0, C_NULL))
|
||||
this = new(sr, blocksize, C_NULL)
|
||||
finalizer(this, close)
|
||||
this.sink = PortAudioSink{T, U}(outdev.name, this, outchans, blocksize*2)
|
||||
this.source = PortAudioSource{T, U}(indev.name, this, inchans, blocksize*2)
|
||||
this.sink = PortAudioSink{T}(outdev.name, this, outchans, blocksize*2)
|
||||
this.source = PortAudioSource{T}(indev.name, this, inchans, blocksize*2)
|
||||
if synced && inchans > 0 && outchans > 0
|
||||
# we've got a synchronized duplex stream. initialize with the output buffer full
|
||||
write(this.sink, SampleBuf(zeros(T, blocksize*2, outchans), sr))
|
||||
|
@ -136,8 +135,8 @@ end
|
|||
function PortAudioStream(indev::PortAudioDevice, outdev::PortAudioDevice,
|
||||
inchans=2, outchans=2; eltype=Float32, samplerate=-1, blocksize=DEFAULT_BLOCKSIZE, synced=false)
|
||||
if samplerate == -1
|
||||
sampleratein = rationalize(indev.defaultsamplerate) * Hz;
|
||||
samplerateout = rationalize(outdev.defaultsamplerate) * Hz;
|
||||
sampleratein = indev.defaultsamplerate
|
||||
samplerateout = outdev.defaultsamplerate
|
||||
if inchans > 0 && outchans > 0 && sampleratein != samplerateout
|
||||
error("""
|
||||
Can't open duplex stream with mismatched samplerates (in: $sampleratein, out: $samplerateout).
|
||||
|
@ -149,7 +148,7 @@ function PortAudioStream(indev::PortAudioDevice, outdev::PortAudioDevice,
|
|||
samplerate = samplerateout
|
||||
end
|
||||
end
|
||||
PortAudioStream{eltype, typeof(samplerate)}(indev, outdev, inchans, outchans, samplerate, blocksize, synced)
|
||||
PortAudioStream{eltype}(indev, outdev, inchans, outchans, samplerate, blocksize, synced)
|
||||
end
|
||||
|
||||
# handle device names given as streams
|
||||
|
@ -207,7 +206,7 @@ end
|
|||
Base.isopen(stream::PortAudioStream) = stream.stream != C_NULL
|
||||
|
||||
SampledSignals.samplerate(stream::PortAudioStream) = stream.samplerate
|
||||
Base.eltype{T, U}(stream::PortAudioStream{T, U}) = T
|
||||
Base.eltype{T}(stream::PortAudioStream{T}) = T
|
||||
|
||||
Base.read(stream::PortAudioStream, args...) = read(stream.source, args...)
|
||||
Base.read!(stream::PortAudioStream, args...) = read!(stream.source, args...)
|
||||
|
@ -217,7 +216,7 @@ Base.flush(stream::PortAudioStream) = flush(stream.sink)
|
|||
|
||||
function Base.show(io::IO, stream::PortAudioStream)
|
||||
println(io, typeof(stream))
|
||||
println(io, " Samplerate: ", samplerate(stream))
|
||||
println(io, " Samplerate: ", samplerate(stream), "Hz")
|
||||
print(io, " Buffer Size: ", stream.blocksize, " frames")
|
||||
if nchannels(stream.sink) > 0
|
||||
print(io, "\n ", nchannels(stream.sink), " channel sink: \"", stream.sink.name, "\"")
|
||||
|
@ -230,9 +229,9 @@ end
|
|||
# Define our source and sink types
|
||||
for (TypeName, Super) in ((:PortAudioSink, :SampleSink),
|
||||
(:PortAudioSource, :SampleSource))
|
||||
@eval type $TypeName{T, U} <: $Super
|
||||
@eval type $TypeName{T} <: $Super
|
||||
name::UTF8String
|
||||
stream::PortAudioStream{T, U}
|
||||
stream::PortAudioStream{T}
|
||||
chunkbuf::Array{T, 2}
|
||||
ringbuf::LockFreeRingBuffer{T}
|
||||
nchannels::Int
|
||||
|
@ -250,7 +249,7 @@ end
|
|||
SampledSignals.nchannels(s::Union{PortAudioSink, PortAudioSource}) = s.nchannels
|
||||
SampledSignals.samplerate(s::Union{PortAudioSink, PortAudioSource}) = samplerate(s.stream)
|
||||
SampledSignals.blocksize(s::Union{PortAudioSink, PortAudioSource}) = s.stream.blocksize
|
||||
Base.eltype{T, U}(::Union{PortAudioSink{T, U}, PortAudioSource{T, U}}) = T
|
||||
Base.eltype{T}(::Union{PortAudioSink{T}, PortAudioSource{T}}) = T
|
||||
Base.close(s::Union{PortAudioSink, PortAudioSource}) = close(s.ringbuf)
|
||||
|
||||
function Base.show{T <: Union{PortAudioSink, PortAudioSource}}(io::IO, stream::T)
|
||||
|
|
|
@ -8,6 +8,7 @@ end
|
|||
using PortAudio
|
||||
using SampledSignals
|
||||
using RingBuffers
|
||||
using SampledSignals
|
||||
|
||||
function test_callback(inchans, outchans)
|
||||
nframes = Culong(8)
|
||||
|
@ -117,7 +118,7 @@ end
|
|||
stream = PortAudioStream(2, 0)
|
||||
buf = read(stream, 5s)
|
||||
close(stream)
|
||||
@test size(buf) == (round(Int, 5s * samplerate(stream)), nchannels(stream.source))
|
||||
@test size(buf) == (round(Int, 5 * samplerate(stream)), nchannels(stream.source))
|
||||
println("Playing back recording...")
|
||||
stream = PortAudioStream(0, 2)
|
||||
write(stream, buf)
|
||||
|
@ -133,21 +134,21 @@ end
|
|||
end
|
||||
@testset "Samplerate-converting writing" begin
|
||||
stream = PortAudioStream()
|
||||
write(stream, SinSource(eltype(stream), samplerate(stream)*0.8, [220Hz, 330Hz]), 3s)
|
||||
write(stream, SinSource(eltype(stream), samplerate(stream)*1.2, [220Hz, 330Hz]), 3s)
|
||||
write(stream, SinSource(eltype(stream), samplerate(stream)*0.8, [220, 330]), 3s)
|
||||
write(stream, SinSource(eltype(stream), samplerate(stream)*1.2, [220, 330]), 3s)
|
||||
flush(stream)
|
||||
close(stream)
|
||||
end
|
||||
@testset "Open Device by name" begin
|
||||
stream = PortAudioStream("Built-in Microph", "Built-in Output")
|
||||
buf = read(stream, 0.001s)
|
||||
@test size(buf) == (round(Int, 0.001s * samplerate(stream)), nchannels(stream.source))
|
||||
@test size(buf) == (round(Int, 0.001 * samplerate(stream)), nchannels(stream.source))
|
||||
write(stream, buf)
|
||||
io = IOBuffer()
|
||||
show(io, stream)
|
||||
@test takebuf_string(io) == """
|
||||
PortAudio.PortAudioStream{Float32,SIUnits.SIQuantity{Rational{Int64},0,0,-1,0,0,0,0,0,0}}
|
||||
Samplerate: 48000//1 s⁻¹
|
||||
PortAudio.PortAudioStream{Float32}
|
||||
Samplerate: 48000.0Hz
|
||||
Buffer Size: 4096 frames
|
||||
2 channel sink: "Built-in Output"
|
||||
2 channel source: "Built-in Microph\""""
|
||||
|
|
Loading…
Reference in a new issue