adds audiometer example
This commit is contained in:
parent
641de1e92b
commit
cf7f170c72
4 changed files with 53 additions and 74 deletions
47
examples/audiometer.jl
Normal file
47
examples/audiometer.jl
Normal file
|
@ -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
|
|
@ -1,5 +1,9 @@
|
||||||
# Thanks to Jiahao Chen for this great example!
|
# Thanks to Jiahao Chen for this great example!
|
||||||
|
|
||||||
|
##
|
||||||
|
## NOTE: THIS NEEDS TO BE PORTED OVER TO THE NEW ARCHITECTURE
|
||||||
|
##
|
||||||
|
|
||||||
using AudioIO
|
using AudioIO
|
||||||
import AudioIO.play
|
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
|
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!")
|
""", lyrics="Wir be- tre- ten feu- er- trun- ken, Himm- li- sche, dein Hei- - lig- thum!")
|
||||||
alto = @async parsevoice("""
|
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("""
|
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("""
|
bass = @async parsevoice("""
|
||||||
d.4 d. d. d. a,. a,. a,. a., a., a., a., a., a.,~ a, a,8 d, d,2
|
d.4 d. d. d. a,. a,. a,. a., a., a., a., a., a.,~ a, a,8 d, d,2
|
||||||
|
|
|
@ -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
|
|
|
@ -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)
|
|
Loading…
Reference in a new issue