From 3939d47a8d7b1761b05681a582e397addb352fb5 Mon Sep 17 00:00:00 2001 From: Jeff Fessler Date: Tue, 5 Apr 2022 14:32:13 -0400 Subject: [PATCH] Add tone with buffer example (#117) --- examples/tone-buffer.jl | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 examples/tone-buffer.jl diff --git a/examples/tone-buffer.jl b/examples/tone-buffer.jl new file mode 100644 index 0000000..fffd1eb --- /dev/null +++ b/examples/tone-buffer.jl @@ -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)