From e48c6361fd71a035b92b18f62f31ccdf9813b79f Mon Sep 17 00:00:00 2001 From: Spencer Russell Date: Fri, 23 May 2014 20:59:22 -0400 Subject: [PATCH] adds Gain node with * operator --- src/AudioIO.jl | 5 +++++ src/nodes.jl | 26 ++++++++++++++++++++++++-- src/operators.jl | 2 ++ src/portaudio.jl | 10 +++++----- 4 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 src/operators.jl diff --git a/src/AudioIO.jl b/src/AudioIO.jl index d519e8c..1acf400 100644 --- a/src/AudioIO.jl +++ b/src/AudioIO.jl @@ -19,6 +19,10 @@ abstract AudioNode # All AudioStream subtypes should have a mixer and info field abstract AudioStream +# An audio interface is usually a physical sound card, but could +# be anything you'd want to connect a stream to +abstract AudioInterface + # Info about the hardware device type DeviceInfo sample_rate::Integer @@ -28,6 +32,7 @@ end include("nodes.jl") include("portaudio.jl") include("sndfile.jl") +include("operators.jl") ############ Exported Functions ############# diff --git a/src/nodes.jl b/src/nodes.jl index c5d42a8..c38ecdc 100644 --- a/src/nodes.jl +++ b/src/nodes.jl @@ -1,9 +1,8 @@ -export SinOsc, AudioMixer, ArrayPlayer, AudioInput - #### SinOsc #### # Generates a sin tone at the given frequency +export SinOsc type SinOsc <: AudioNode active::Bool deactivate_cond::Condition @@ -22,6 +21,26 @@ function render(node::SinOsc, device_input::AudioBuf, info::DeviceInfo) return sin(phase), is_active(node) end +#### Gain #### +export Gain +type Gain <: AudioNode + active::Bool + deactivate_cond::Condition + in_node::AudioNode + gain::Float32 + + function Gain(in_node::AudioNode, gain::Real) + new(false, Condition(), in_node, gain) + end +end + +function render(node::Gain, device_input::AudioBuf, info::DeviceInfo) + input, child_active = render(node.in_node, device_input, info) + # TODO: should we check the active flag of the input? + return input .* node.gain, is_active(node) +end + + #### AudioMixer #### # Mixes a set of inputs equally @@ -30,6 +49,7 @@ end typealias MaybeAudioNode Union(AudioNode, Nothing) const MAX_MIXER_INPUTS = 32 +export AudioMixer type AudioMixer <: AudioNode active::Bool deactivate_cond::Condition @@ -96,6 +116,7 @@ end # Plays a AudioBuf by rendering it out piece-by-piece +export ArrayPlayer type ArrayPlayer <: AudioNode active::Bool deactivate_cond::Condition @@ -154,6 +175,7 @@ end # Renders incoming audio input from the hardware +export AudioInput type AudioInput <: AudioNode active::Bool deactivate_cond::Condition diff --git a/src/operators.jl b/src/operators.jl new file mode 100644 index 0000000..864c64e --- /dev/null +++ b/src/operators.jl @@ -0,0 +1,2 @@ +*(node::AudioNode, coef::Real) = Gain(node, coef) +*(coef::Real, node::AudioNode) = Gain(node, coef) diff --git a/src/portaudio.jl b/src/portaudio.jl index 52a6371..18ea73d 100644 --- a/src/portaudio.jl +++ b/src/portaudio.jl @@ -135,7 +135,7 @@ type PaHostApiInfo defaultOutputDevice::PaDeviceIndex end -type AudioDevice +type PortAudioInterface <: AudioInterface name::String host_api::String max_input_channels::Int @@ -152,10 +152,10 @@ function get_portaudio_devices() init_portaudio() device_count = ccall((:Pa_GetDeviceCount, "libportaudio"), PaDeviceIndex, ()) pa_devices = [get_device_info(i) for i in 0:(device_count - 1)] - [AudioDevice(bytestring(d.name), - bytestring(get_host_api_info(d.host_api).name), - d.max_input_channels, - d.max_output_channels) + [PortAudioInterface(bytestring(d.name), + bytestring(get_host_api_info(d.host_api).name), + d.max_input_channels, + d.max_output_channels) for d in pa_devices] end