Jacob Lowe

Visualizing sound on the web

by Jacob Lowe on

This was a talk I had given in early 2017 that showed off some webaudio APIs and how they can be used to create some interesting visualizations. I also talked about some of the creative coding tools I use to make these things.

Transcripts (auto-generate)

  • all right my name is Jacob I am JCB LW
  • everywhere on the web and that's not bad
  • i don't really care about that social
  • website and also i'm an organizer of
  • jail a because i love the Jazz community
  • today I'm going to be talking about
  • visualizing audio on the web very
  • descriptive title nothing fancy no click
  • bait or anything you know you're getting
  • what you see there I probably can work
  • on that especially when we put up the
  • video you want to make it a little bit
  • more enticing and do something with it
  • but right now it's just visualizing the
  • audio on the web so before we go forward
  • and talk about visualizing audio I kind
  • of want steak a step back I mean I'm
  • getting older I turned 29 this month and
  • you know got here the older the older I
  • get the more I like to look back and be
  • nostalgic about the things that of my
  • path and one thing is you know
  • visualizing audio or visualizing music
  • was like it's something that I really
  • really liked now who remembers this yeah
  • so this is windows media player this was
  • actually my first visualizations that I
  • ever you know fall or probably even
  • dealt with as a young child and is it
  • like when I see this I'm like oh it
  • brings me back to a place where I'm
  • sitting in front of my computer and I'm
  • shoving CDs and my you know CD Drive and
  • like opening up a program is before
  • streaming or anything without now this
  • is a YouTube video and let's see
  • comments like this it really makes me
  • sad because it's like this is no longer
  • going to be available to me to be able
  • to use this even though granted i
  • probably wouldn't use it but there's no
  • there's no chance than we actually using
  • it and it's being a YouTube video of
  • course there is like funny comments okay
  • out of trip balls on your 10 this was
  • kind of funny because like I was
  • probably like 10 when i was using
  • windows media player and actually i
  • think this is a pretty good talk title
  • if you don't mind me i'm gonna change
  • this real quick
  • [Music]
  • there we go I think that's gonna that's
  • gonna get two clicks right there all
  • right going forward all right enough
  • about going in the past let's talk about
  • the present so I don't know if you guys
  • have checked out like modern interfaces
  • for playing audio pretty sure you spot a
  • Phi or iTunes or something and one thing
  • about that is that music visualizations
  • are usually gone from those interfaces
  • it's more or less like these are behind
  • some tab or like they're non-existent or
  • the some static visualization which is
  • like on soundcloud you actually have
  • like these very static visualizations
  • and it's pretty much just a progress bar
  • that you're going through and back to
  • myself I actually just started a new job
  • and it's not this really cool place
  • called headspace the meditation app we
  • actually hosted their last month but one
  • thing that's interesting as this is a
  • progression in my like career is also
  • that my prior places I was working with
  • maps and latitude longitude putting pins
  • on a map and if I ever wanted to do
  • anything with audio is really on my own
  • time and there was no really like
  • practical use for me to do it our main
  • product that headspace is actually audio
  • files so me getting to look into these
  • new Web Audio API is actually beneficial
  • for my job as well which I kind of love
  • that like I'm actually doing something
  • that I wanted you as well as it's kind
  • of like benefiting me a little bit and
  • before like started I was like checking
  • out the product that headspace and they
  • have this really cool player it's very
  • light but you can see that the player is
  • kind of moving a little bit it has like
  • a little bit of animation supposed to
  • just be smooth and like soothing it's
  • not really like it's not doing any audio
  • visualization even though you kind of
  • say it's like a vibe type of
  • visualization or something because it is
  • the meditation app but I thought it was
  • really intriguing like I usually you see
  • audio players and it's just a play
  • button and then a stop button maybe a
  • progress bar but you really don't have
  • much innovation and besides doing that
  • you have you know album art as big as
  • you can get it or something and the
  • first thing that went through my mind
  • which I think probably goes through a
  • lot of our minds
  • time to time is I just want to implement
  • it in JavaScript like why we actually
  • have a web player at head space but it
  • looks nothing like that cool one that I
  • was talking about that was like our
  • mobile player and that was actually a
  • youtube video that I was showing that so
  • before I started I actually took off a
  • month and I rode my skateboard around
  • New York and DC but during that time
  • actually stopped in a bunch of coffee
  • shops and I would just hack on stuff and
  • it was mostly trying to implement this
  • thing in JavaScript now the next slide
  • that i'm going to show you is my first
  • try or not first try it's the first step
  • in a progression of trying to implement
  • this player now when you look at this
  • and sorry for the blurry quality but
  • this looks nothing like that player at
  • all so let me kind of like go through my
  • thought process of this so the player is
  • kind of like this random blob and
  • there's like pieces of it moving in a
  • certain direction so what I did was like
  • I want to pick make a bunch of points
  • and put them around a circle and then
  • move them in a random direction and
  • that's what this is except I'm really
  • bad at math so as you can see it's not a
  • circle it actually came out something
  • that's more like a sideways donut I
  • thought it was really cool and like when
  • I built something really cool i liked
  • like at least take a screenshot or put
  • it on twitter or something so that's
  • what i did with this mess just a
  • progression in it as you can see i got
  • better at math there's another
  • visualization why didn't get better i
  • just like figured out what my problem
  • was and fix it and yeah so this is just
  • like a i'm changing the colors around as
  • you can see it's a little bit more
  • coherence getting a little bit closer
  • still doesn't look anything like it and
  • what you'll see in some of my demos as
  • well as just some of the other slides is
  • I like to like I make things very
  • declarative so I can just like take
  • these like numbers and just twist them
  • and like turn them to 11 and then
  • probably turn it back to nine because my
  • computer is lagging too much and that's
  • what you'll see in a lot of these demos
  • is me just kind of like messing around
  • with numbers and I also have another
  • step in here but the thing is is that I
  • did a lot of this without source control
  • and I really wish that I
  • I used sorts of control the entire time
  • because then I can show you a better
  • progression of this because the last
  • slide to this next slide is way
  • different so more or less like there's a
  • big gap in this entire history of this
  • Wiz players came and more or less what I
  • did when I connected those points made
  • quadratic curves between them and made
  • it one solid shape and then I actually
  • started playing audio with it and this
  • next step and fun so this is what it
  • looks like there's a little bit of music
  • here as you can see revision we actually
  • are starting to visualize audio in this
  • and francha this is nothing is not very
  • calm if you look at it this would not be
  • soothing to be inside of a meditation
  • else but more or less this was just me
  • taking that knob and dialing it to 11
  • and so this is like a very simple
  • visualization so I'm taking those random
  • points and usually looks like a small
  • threshold and that's kind of like at a
  • rest 8 and when the one there's like a
  • snare head or a beat drop or something
  • the frequency goes up and you're able to
  • have a larger variation and that's where
  • you feel like the little spikes because
  • it goes in and it goes out now have
  • another one go and turn it back on this
  • one is not so much a demo of the player
  • i'm kind of like showing off some of the
  • functionality because I I kind of build
  • this in stats so but as you can see the
  • animation got a little bit more subtle
  • this one's a little bit better you could
  • actually see this probably in kind of
  • like an interface you could say the
  • animation change instead of just doing
  • like the random points and having a
  • bigger threshold to them this one
  • actually the border gets a little bit
  • larger but it's actually what I found
  • interesting at this point is there's a
  • lot of different things you can do with
  • audio visualization that's not so
  • straightforward I mean this is like a
  • subtle animation if you thought about
  • windows media player that I had in the
  • very first slide or second slide it
  • actually that was like a full screen
  • immersion sometimes a little too much I
  • could say where this is a little bit
  • more subtle granted it probably would
  • get annoying after a while and this
  • really sparked my interest and kind of
  • like audio visualization so much so that
  • I actually built
  • npm module it's called sound dashboard
  • it's on NPM and also you can find it on
  • my github will actually post this to the
  • flak channel afterwards so you guys can
  • check it out if you want what this
  • allows me to do is load up like an audio
  • buffer file and then be able to like
  • analyze the data of it when it's like
  • playing and grab that data out really
  • conveniently instead of hooking up these
  • AP is now you you might be thinking like
  • oh that's cool and all but like how are
  • you actually doing this like what is
  • actually going on that maybe somebody
  • else wants to do audio visualization and
  • like what is the steps that I had to
  • take to get to that point so I have a
  • couple little pieces of code and I'll
  • try to walk through this and then i'll
  • answer questions at the end and i'm
  • pretty sure it'll be mostly about these
  • slides but essentially i'm using the
  • audio contacts API it's I want to say
  • it's you but it's been around for a
  • couple years now the the support for it
  • is pretty good there is some methods
  • that are a little bit spotty on in terms
  • of browser support and more or less like
  • my demos only work in chrome and firefox
  • right now but more or less I'm creating
  • the audio context and that's what I'm
  • doing that new audio context I'm
  • creating a buffer source what a buffer
  • source is essentially I can attach like
  • an audio buffer to it and that will
  • allow me to like play and stop the audio
  • buffer and then I'm also creating an
  • analyzer now the analyzer does exactly
  • what it sounds like it's analyzing the
  • data that is going through it in the way
  • that I can pump data through the
  • analyzer is down below as you can see i
  • am adding the audio buffer now that's
  • like a un aid array which is like a
  • array of arrays that's holding like
  • binary data inside of it and i'm
  • connecting it to the analyzer which i
  • think it's spelled one of these wrong
  • but analyzer connect and then i'm
  • connecting the analyzer straight to the
  • destination you can think of this as
  • with speakers of your computer and then
  • i'm just starting it you can add a bunch
  • of params for that to start it at a
  • certain point
  • now to get the data out of the analyzed
  • because I just showed you how to connect
  • the analyzer this is how you would get
  • it out so you get a buffered lang that's
  • pretty much a the audio that it's
  • playing at a certain time because we're
  • going to call this function over and
  • over and over again so whenever you're
  • playing like an audio file and you want
  • to get a certain data out of a certain
  • time when it's playing you can get the
  • length of the actual binary how long it
  • is and then I'm going to create a UN 28
  • array with that length which is going to
  • just create a blank one and then call it
  • calling audio analyzer get byte time
  • domain data I really love that method
  • name but then call it coin it with data
  • array is actually going to hydrate that
  • data array with a bunch of binary data
  • from that current buffer and then as you
  • can see here I'm just using
  • requestanimationframe to call this thing
  • over and over and over again and get out
  • the the data for each sequence and this
  • is what actually creating the
  • visualization looks like now like I said
  • I'm not good at math so I don't know
  • exactly what's going on here granted
  • there's only a little bit of code but
  • more or less what's happening is we're
  • we're calculating a waveform and we're
  • just getting x and y coordinates for it
  • that's what these are and then you can
  • take those x and y coordinates and you
  • can put absolute this gives or more
  • likely you probably want to render those
  • to a canvas and then you would have like
  • an actual waveform if you just rendered
  • those that's like circles on a canvas
  • which is really cool because it's like
  • that's not that much code to be able to
  • render out a waveform and if you want to
  • look at more there's actually a good mdn
  • doc that i have in like some of my
  • github repos that is the place where i
  • actually learned how to do a lot of this
  • stuff because they have a doc on how to
  • analyze audio data on the web and that's
  • where a lot of this code came from now
  • as a few demos the first one I have here
  • I was just playing around with stuff
  • cranking the knobs on things and this is
  • like a very sporadic like it's exactly
  • like a small snippet of like an
  • animation that you'll see later on but
  • more or less like I call this one that
  • event horizon I just thought it was cool
  • yeah it's a very chaotic
  • and then now I have another demo and
  • this is actually turn this is actually
  • the blob player that I was showing you i
  • just have it down here kind of as a
  • player it's not I mean the whole
  • interface that doesn't revolve around
  • this audio visualization exactly there's
  • a lot of times where I think audio
  • visualization is very good at when it's
  • subtle just these subtle things I mean
  • you talking to your Google assistant or
  • theory or something they're doing a
  • little bit of visualization of the input
  • that's coming into your phone and it's
  • nice that you're getting that feedback
  • that there actually is like audio going
  • through it so I kind of like you know
  • just doing these subtle things but then
  • again I don't always do very subtle
  • things and I have some demos that are a
  • little bit more chaotic now this is a 3d
  • demo it's very it moves very quickly and
  • it's very jagged and this is one trouble
  • that I've been having with visualizing
  • audio it's just that you're getting so
  • much data coming in I like you saw I'm
  • actually i'm just doing
  • requestanimationframe i could delay that
  • but the more i delay it where's the
  • visualizations become and when you have
  • these things happening in every like 100
  • milliseconds or probably shorter than
  • that it's hard to do tween between the
  • different points and be able to do
  • smooth animations it's possible but it's
  • not that easy but this is showing you
  • you can actually like integrate this
  • kind of stuff into anything even like 3d
  • objects then I have one more same song
  • but this is actually just a waveform but
  • it's around a surrounded circle so
  • essentially took that same math that i
  • was able to like to put points around
  • the circle and then I just apply to
  • waveform to it and then that way when it
  • hits like a snare drum or beat drop or
  • something you'll see that the kind of
  • like fan out I'm actually caching like
  • five versions of you know the waveform
  • and then cycling through those and then
  • this outer one is the oldest and the
  • inner
  • it's the newest and actually i'll show
  • you really quick how easy it is to like
  • change this around and this is this is
  • why i have so many just random looking
  • visualization I want to go to my code
  • real quick and as you can see I just
  • have like a bunch of variables up at the
  • top but what's cool is that I could just
  • say like 100 and then that should update
  • and that's not I can refresh as you see
  • it changed around the whole
  • visualization I mean granted i think the
  • other one was cooler looking this is not
  • bad as you can see this is actually the
  • one that the event horizon originally
  • came from because you see the lines from
  • the circles the outline and then i could
  • even do things like this which this is
  • actually going to just make my computer
  • go super slow so now i'm catching up to
  • 10 of these things and doing the
  • visualizations with that but it once you
  • get something like this it actually
  • starts to slow down the animation it
  • looks very laggy but it's you know it
  • looks really cool especially when you're
  • not hearing the audio because when
  • you're not hearing the audio it just
  • looks like a cool animation but that is
  • it and thank you we have the second mic
  • up here yeah he's got the mic for you
  • um I was using at weaning library uh and
  • the it was just in a blob demo a lot of
  • those things I'm just taking the raw
  • audio data and then I'm trying to make a
  • some type of wave for more like
  • frequency something and then just
  • putting that straight into the points
  • with the blob player and I I'm not
  • really using any animations and like the
  • main thing here but you'll see when I
  • actually press like pause it's kind of
  • like you know tweens out and from those
  • different points that's the only place
  • when i'm actually using animation
  • library at all but definitely if i
  • wanted to smooth things out for instance
  • in like a 3d one it's very jarring
  • sometimes and I'm definitely thinking
  • about integrating some types of
  • animation library or some tweeting
  • library to be able to submit those
  • things out
  • are you kidding a drum so really a lot
  • of the visualizations are more or less
  • based off of what would be the part of
  • the waveform I'm taking like a highest
  • peak of the wave form and saying that's
  • kind of like the highest bit inside of
  • so essentially like based off of that
  • I'm making like the border fatter or the
  • threshold for the the random points
  • larger because you can't really like I
  • mean you can get into a lot of like and
  • like breaking apart the the song and
  • whatnot you can actually do that with
  • the stuff smarter people than I am can
  • do that with that stuff I really just
  • like to make things look cool right off
  • the bat but more or less like what
  • you're seeing here has nothing to do
  • with like separating any of the sounds
  • it's just more or less heavy taking part
  • of the waveform at its peak and being
  • like okay this is a very loud part of
  • the song or something like that no
  • that's a good question because I was
  • going to say something but sometimes I
  • forget to say something so you might see
  • in this slide I actually connect instead
  • of connecting I could connect the source
  • directly to the destination and I that's
  • actually I think inside of the the mdn
  • doc that I was referencing earlier
  • that's how they did it one thing I
  • noticed when I did that though is that
  • the visualization sometimes got out of
  • sync so the way that I was able to kind
  • of fix it was that I would straight from
  • the analyzer go to Z the speaker so that
  • way it always had to go through the
  • analyzer before it hit the speakers so a
  • lot of that latency wasn't there just
  • because the analyzer had already gotten
  • the data out of the buffer that was
  • playing at that current time by the time
  • it hit the speakers
  • alright
  • [Applause]