From cf7f170c729bf98945c167de5c02c5a797d5cb88 Mon Sep 17 00:00:00 2001 From: Spencer Russell Date: Sun, 20 Mar 2016 04:05:21 -0400 Subject: [PATCH] adds audiometer example --- examples/audiometer.jl | 47 ++++++++++++++++++++++++++++++ examples/lilyplay.jl | 8 +++-- examples/mutable_streaming_node.jl | 40 ------------------------- examples/ramp.jl | 32 -------------------- 4 files changed, 53 insertions(+), 74 deletions(-) create mode 100644 examples/audiometer.jl delete mode 100644 examples/mutable_streaming_node.jl delete mode 100644 examples/ramp.jl diff --git a/examples/audiometer.jl b/examples/audiometer.jl new file mode 100644 index 0000000..1b8d261 --- /dev/null +++ b/examples/audiometer.jl @@ -0,0 +1,47 @@ +using PortAudio + +"""Continuously read from the default audio input and plot an +ASCII level/peak meter""" +function micmeter(metersize) + mic = PortAudioSource() + + signalmax = zero(eltype(mic)) + println("Press Ctrl-C to quit") + while true + block = read(mic, 4096) + blockmax = maximum(abs(block)) # find the maximum value in the block + signalmax = max(signalmax, blockmax) # keep the maximum value ever + print("\r") # reset the cursor to the beginning of the line + printmeter(metersize, blockmax, signalmax) + end +end + +"""Print an ASCII level meter of the given size. Signal and peak +levels are assumed to be scaled from 0.0-1.0, with peak >= signal""" +function printmeter(metersize, signal, peak) + # calculate the positions in terms of characters + peakpos = clamp(round(Int, peak * metersize), 0, metersize) + meterchars = clamp(round(Int, signal * metersize), 0, peakpos-1) + blankchars = max(0, peakpos-meterchars-1) + + for position in 1:meterchars + print_with_color(barcolor(metersize, position), ">") + end + + print(" " ^ blankchars) + print_with_color(barcolor(metersize, peakpos), "|") + print(" " ^ (metersize - peakpos)) +end + +"""Compute the proper color for a given position in the bar graph. The first +half of the bar should be green, then the remainder is yellow except the final +character, which is red.""" +function barcolor(metersize, position) + if position/metersize <= 0.5 + :green + elseif position == metersize + :red + else + :yellow + end +end diff --git a/examples/lilyplay.jl b/examples/lilyplay.jl index 339c50b..6120bb5 100644 --- a/examples/lilyplay.jl +++ b/examples/lilyplay.jl @@ -1,5 +1,9 @@ # Thanks to Jiahao Chen for this great example! +## +## NOTE: THIS NEEDS TO BE PORTED OVER TO THE NEW ARCHITECTURE +## + using AudioIO import AudioIO.play @@ -129,10 +133,10 @@ soprano = @async parsevoice(""" f'#.4 f'#. g'. a'. a'. g'. f'#. e'. d'. d'. e'. f'#. e'.~ e' d'8 d'4~ d'2 """, lyrics="Wir be- tre- ten feu- er- trun- ken, Himm- li- sche, dein Hei- - lig- thum!") alto = @async parsevoice(""" -a.4 a. b. c'. c'. b. a. g. f#. f#. g. f#. g.~ g4 f#8 f#~ f#2 +a.4 a. b. c'. c'. b. a. g. f#. f#. g. f#. g.~ g4 f#8 f#~ f#2 """) tenor = @async parsevoice(""" -d.4 d. d. d. d. d. d. d. d. d. c#. d. c#.~ c# d8 d d2 +d.4 d. d. d. d. d. d. d. d. d. c#. d. c#.~ c# d8 d d2 """) bass = @async parsevoice(""" d.4 d. d. d. a,. a,. a,. a., a., a., a., a., a.,~ a, a,8 d, d,2 diff --git a/examples/mutable_streaming_node.jl b/examples/mutable_streaming_node.jl deleted file mode 100644 index ba6b681..0000000 --- a/examples/mutable_streaming_node.jl +++ /dev/null @@ -1,40 +0,0 @@ -# This demos how real-time audio manipulation can be done using AudioNodes. To -# run it, hook up some input audio to your default recording device and run the -# script. The demo will run for 10 seconds alternating the node between a muted -# and unmuted state -using AudioIO - -type MutableNode <: AudioIO.AudioNode - active::Bool - deactivate_cond::Condition - muted::Bool - - function MutableNode(muted::Bool) - new(false, Condition(), muted) - end -end - -function MutableNode() - MutableNode(false) -end - -import AudioIO.render -function render(node::MutableNode, device_input::AudioIO.AudioBuf, info::AudioIO.DeviceInfo) - return device_input .* !node.muted, AudioIO.is_active(node) -end - -function mute(node::MutableNode) - node.muted = true -end - -function unmute(node::MutableNode) - node.muted = false -end - -mutableNode = MutableNode() -AudioIO.play(mutableNode) -muteTransitions = { true => unmute, false => mute } -for i in 1:10 - sleep(1) - muteTransitions[mutableNode.muted](mutableNode) -end diff --git a/examples/ramp.jl b/examples/ramp.jl deleted file mode 100644 index f1c5b5c..0000000 --- a/examples/ramp.jl +++ /dev/null @@ -1,32 +0,0 @@ -using AudioIO - -# Give PortAudio time to load -play([0]) -sleep(2) - -println(""" - * - * * - * * * - * * * * - * * * * * -* * * * * * -""") -wave = SinOsc(440) * LinRamp(0.0, 1.0, 2.0) -play(wave) -sleep(2) -stop(wave) - - -println(""" - * - * * * - * * * * * - * * * * * * * - * * * * * * * * * -* * * * * * * * * * * -""") -wave = SinOsc(440) * LinRamp([0.0, 1.0, 0.0], [2.0, 2.0]) -play(wave) -sleep(4) -stop(wave) \ No newline at end of file