Last active
May 14, 2024 18:45
-
-
Save dimapaloskin/78be62f2216620cf5b80dc94306357e2 to your computer and use it in GitHub Desktop.
julia. play audio in pluto
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
begin | |
import WAV | |
import MIMEs | |
import URIs | |
function load_audio(audio, sr, html_attributes::Pair...) | |
buf = IOBuffer() | |
WAV.wavwrite(Int.(trunc.(0.99999999999 * audio * 2^23)), buf, Fs=sr, nbits=24) | |
mime = MIME("audio/wav") | |
src = "data:$( | |
string(something(mime,"")) | |
);base64,$( | |
base64encode(take!(buf)) | |
)" | |
PlutoUI.Resource(src, mime, html_attributes) | |
end | |
end | |
# usage | |
begin | |
sr = 48000.0 | |
time = 1 | |
f = 220 | |
x = 0.0:1.0/sr:time | |
y = sin.(2 * pi * f * x) | |
load_audio(y, sr, :autoplay => true) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment