PortAudio.jl/src/AudioIO.jl

103 lines
2.3 KiB
Julia
Raw Normal View History

module AudioIO
2013-12-11 20:18:36 -05:00
# export the basic API
export play, stop
2013-12-21 20:39:47 -05:00
# default stream used when none is given
_stream = nothing
2013-12-21 20:39:47 -05:00
################## Types ####################
typealias AudioSample Float32
# A frame of audio, possibly multi-channel
typealias AudioBuf Array{AudioSample}
# A node in the render tree
abstract AudioNode
# A stream of audio (for instance that writes to hardware)
# All AudioStream subtypes should have a mixer and info field
abstract AudioStream
# Info about the hardware device
type DeviceInfo
sample_rate::Integer
buf_size::Integer
end
include("nodes.jl")
include("portaudio.jl")
2014-01-06 11:24:53 -05:00
include("sndfile.jl")
2013-12-13 02:17:21 -05:00
############ Exported Functions #############
# Play an AudioNode by adding it as an input to the root mixer node
function play(node::AudioNode, stream::AudioStream)
2014-01-13 19:19:56 -05:00
activate(node)
add_input(stream.mixer, node)
return node
end
# If the stream is not given, use the default global PortAudio stream
function play(node::AudioNode)
global _stream
if _stream == nothing
_stream = PortAudioStream()
end
play(node, _stream)
end
# Allow users to play a raw array by wrapping it in an ArrayPlayer
function play(arr::AudioBuf, args...)
player = ArrayPlayer(arr)
play(player, args...)
2013-12-21 20:39:47 -05:00
end
# If the array is the wrong floating type, convert it
function play{T <: FloatingPoint}(arr::Array{T}, args...)
arr = convert(AudioBuf, arr)
play(arr, args...)
end
# If the array is an integer type, scale to [-1, 1] floating point
# integer audio can be slightly (by 1) more negative than positive,
# so we just scale so that +/- typemax(T) becomes +/- 1
function play{T <: Signed}(arr::Array{T}, args...)
arr = arr / typemax(T)
play(arr, args...)
end
function play{T <: Unsigned}(arr::Array{T}, args...)
zero = (typemax(T) + 1) / 2
range = floor(typemax(T) / 2)
arr = (arr - zero) / range
play(arr, args...)
2013-12-21 20:39:47 -05:00
end
function stop(node::AudioNode)
2014-01-13 19:19:56 -05:00
deactivate(node)
node
end
function activate(node::AudioNode)
node.active = true
end
function deactivate(node::AudioNode)
node.active = false
2014-01-13 19:19:56 -05:00
notify(node.deactivate_cond)
end
function is_active(node::AudioNode)
node.active
end
function Base.wait(node::AudioNode)
if is_active(node)
wait(node.deactivate_cond)
end
end
end # module AudioIO