Add tone with buffer example (#117)

This commit is contained in:
Jeff Fessler 2022-04-05 14:32:13 -04:00 committed by GitHub
parent 19a49931ad
commit 3939d47a8d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

21
examples/tone-buffer.jl Normal file
View file

@ -0,0 +1,21 @@
#=
This example illustrates synthesizing a long tone in small pieces
and routing it to the default audio output device using `write()`.
=#
using PortAudio: PortAudioStream, write
stream = PortAudioStream(0, 1; warn_xruns=false)
function play_tone(stream, freq::Real, duration::Real; buf_size::Int = 1024)
S = stream.sample_rate
current = 1
while current < duration*S
x = 0.7 * sin.(2π * (current .+ (1:buf_size)) * freq / S)
write(stream, x)
current += buf_size
end
nothing
end
play_tone(stream, 440, 2)