Add tone with buffer example (#117)
This commit is contained in:
parent
19a49931ad
commit
3939d47a8d
1 changed files with 21 additions and 0 deletions
21
examples/tone-buffer.jl
Normal file
21
examples/tone-buffer.jl
Normal 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)
|
Loading…
Reference in a new issue