Get examples running

This commit is contained in:
Brandon Taylor 2022-02-13 22:40:39 -05:00
parent aa8e9fc7cd
commit f708835e5a
6 changed files with 58 additions and 48 deletions

View file

@ -19,8 +19,11 @@ Suppressor = "0.2"
[extras] [extras]
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
DSP = "717857b8-e6f2-59f4-9121-6e50c889abd2"
FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341"
GR = "28b8d3ca-fb5f-59d9-8090-bfdbd6d07a71"
LibSndFile = "b13ce0c6-77b0-50c6-a2db-140568b8d1a5" LibSndFile = "b13ce0c6-77b0-50c6-a2db-140568b8d1a5"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
[targets] [targets]
test = ["Documenter", "LibSndFile", "Test"] test = ["DSP", "Documenter", "FFTW", "GR", "LibSndFile", "Test"]

View file

@ -4,18 +4,23 @@ using PortAudio
Continuously read from the default audio input and plot an Continuously read from the default audio input and plot an
ASCII level/peak meter ASCII level/peak meter
""" """
function micmeter(metersize) function micmeter(seconds; metersize = 80)
mic = PortAudioStream(1, 0; latency = 0.1) PortAudioStream(1, 0; latency = 0.1) do mic
done = false
signalmax = zero(eltype(mic)) signalmax = zero(eltype(mic))
println("Press Ctrl-C to quit") @sync begin
while true @async while !done
block = read(mic, 512) block = read(mic, 512)
blockmax = maximum(abs.(block)) # find the maximum value in the block blockmax = maximum(abs.(block)) # find the maximum value in the block
signalmax = max(signalmax, blockmax) # keep the maximum value ever signalmax = max(signalmax, blockmax) # keep the maximum value ever
print("\r") # reset the cursor to the beginning of the line print("\r") # reset the cursor to the beginning of the line
printmeter(metersize, blockmax, signalmax) printmeter(metersize, blockmax, signalmax)
end
sleep(seconds)
done = true
end
end end
nothing
end end
""" """
@ -52,4 +57,4 @@ function barcolor(metersize, position)
end end
end end
micmeter(80) micmeter(5)

View file

@ -1,22 +1,8 @@
using Distributed, PortAudio using PortAudio
using Base.Threads: @spawn
# Modified from Jiahao Chen's example in the obsolete AudioIO module. # Modified from Jiahao Chen's example in the obsolete AudioIO module.
# Will use first output device found in system's listing or DEFAULTDEVICE if set below # Will use first output device found in system's listing or DEFAULTDEVICE if set below
const DEFAULTDEVICE = -1
function paudio()
devs = PortAudio.devices()
if DEFAULTDEVICE < 0
devnum = findfirst(x -> x.maxoutchans > 0, devs)
(devnum == nothing) && error("No output device for audio found")
else
devnum = DEFAULTDEVICE + 1
end
return ostream = PortAudioStream(devs[devnum].name, 0, 2)
end
play(ostream, sample::Array{Float64, 1}) = write(ostream, sample)
play(ostr, sample::Array{Int64, 1}) = play(ostr, Float64.(sample))
struct Note{S <: Real, T <: Real} struct Note{S <: Real, T <: Real}
pitch::S pitch::S
@ -35,14 +21,13 @@ function play(
if !A.sustained if !A.sustained
decay_length = div(length(timesamples), 5) decay_length = div(length(timesamples), 5)
v[(end - decay_length):(end - 1)] = v[(end - decay_length):(end - 1)] =
v[(end - decay_length):(end - 1)] .* LinRange(1, 0, decay_length) v[(end - decay_length):(end - 1)] .* LinRange(1, 0, decay_length)
end end
play(ostream, v) write(ostream, v)
sleep(A.duration)
end end
function parsevoice(melody::String; tempo = 132, beatunit = 4, lyrics = nothing) function parsevoice(melody::String; tempo = 132, beatunit = 4, lyrics = nothing)
ostream = paudio() # initialize audio for output ostream = PortAudioStream(0, 2; warn_xruns = false) # initialize audio for output
lyrics_syllables = lyrics == nothing ? nothing : split(lyrics) lyrics_syllables = lyrics == nothing ? nothing : split(lyrics)
lyrics_syllables != nothing && (lyrics_syllables[end] *= "\n") lyrics_syllables != nothing && (lyrics_syllables[end] *= "\n")
note_idx = 1 note_idx = 1

View file

@ -1,20 +1,26 @@
# plot a real-time spectrogram. This example is adapted from the GR example # plot a real-time spectrogram. This example is adapted from the GR example
# at http://gr-framework.org/examples/audio_ex.html # at http://gr-framework.org/examples/audio_ex.html
module SpectrumExample
using GR, PortAudio, SampledSignals, FFTW using GR, PortAudio, SampledSignals, FFTW
const N = 1024 function plot_spectrogram(seconds;
const stream = PortAudioStream(1, 0) N = 1024,
const buf = read(stream, N) fmin = 0Hz,
const fmin = 0Hz fmax = 10000Hz
const fmax = 10000Hz )
const fs = Float32[float(f) for f in domain(fft(buf)[fmin..fmax])] PortAudioStream(1, 0) do stream
done = false
while true buf = read(stream, N)
read!(stream, buf) fs = Float32[float(f) for f in domain(fft(buf)[fmin..fmax])]
plot(fs, abs.(fft(buf)[fmin..fmax]), xlim = (fs[1], fs[end]), ylim = (0, 100)) @sync begin
@async while !done
read!(stream, buf)
plot(fs, abs.(fft(buf)[fmin..fmax]), xlim = (fs[1], fs[end]), ylim = (0, 100))
end
sleep(seconds)
done = true
end
end
end end
end plot_spectrogram(5)

View file

@ -1005,6 +1005,9 @@ function exchange(messenger, arguments...)
take!(messenger.output_channel) take!(messenger.output_channel)
end end
as_matrix(matrix::Matrix) = matrix
as_matrix(vector::Vector) = reshape(vector, length(vector), 1)
# these will only work with sampledsignals scribes # these will only work with sampledsignals scribes
function unsafe_write( function unsafe_write(
sink::PortAudioSink{<:Messenger{<:Any, <:SampledSignalsWriter}}, sink::PortAudioSink{<:Messenger{<:Any, <:SampledSignalsWriter}},
@ -1012,7 +1015,7 @@ function unsafe_write(
already, already,
frame_count, frame_count,
) )
exchange(sink.stream.sink_messanger, julia_buffer, already, frame_count) exchange(sink.stream.sink_messanger, as_matrix(julia_buffer), already, frame_count)
end end
function unsafe_read!( function unsafe_read!(
@ -1021,7 +1024,7 @@ function unsafe_read!(
already, already,
frame_count, frame_count,
) )
exchange(source.stream.source_messanger, julia_buffer, already, frame_count) exchange(source.stream.source_messanger, as_matrix(julia_buffer), already, frame_count)
end end
end # module PortAudio end # module PortAudio

View file

@ -247,6 +247,14 @@ if !isempty(devices())
handle_status(Pa_SetStreamFinishedCallback(pointer_to, C_NULL)), handle_status(Pa_SetStreamFinishedCallback(pointer_to, C_NULL)),
) == paNoError ) == paNoError
end end
@testset "Make sure examples run" begin
include(joinpath(pkgdir(PortAudio), "examples", "audiometer.jl"))
include(joinpath(pkgdir(PortAudio), "examples", "lilyplay.jl"))
include(joinpath(pkgdir(PortAudio), "examples", "measure_latency.jl"))
include(joinpath(pkgdir(PortAudio), "examples", "spectrum.jl"))
# include("waterfall_heatmap.jl")
# include("waterfall_lines.jl")
end
end end
doctest(PortAudio) doctest(PortAudio)
end end