02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School

Creating deeper sound controls

In the last lecture, we started to create animations with sound and by the end of this lesson, you will have created a really cool sound animation project.

Video Transcript

00:00This is our last section in chapter four where we've been exploring plugins. What I wanted to
00:05do next in the last section, we looked into how to create a fade of our sound object.
00:13So we have a sound playing and we faded it out. In this section what I want to do is I want to
00:18create an interactive element where the animation itself of our elements are on the stage. We'll
00:25get updated dynamically based on what's happening inside of our music. So to do that the first
00:30thing that I want to do is once we finish our part two and our transformation is complete.
00:37So once our fade out is complete I want to call show part two. So I'm going to add here an on
00:43complete and on my on complete I'm going to call the function show part two. Now show part two is
00:54going to work this way. First of all I'm just going to create that function quickly. The first
01:01step that I want to do is I want to create a new visual object or actually two and we're just
01:06going to put like two round circles exactly in the center of the stage separated from each other a
01:12bit. So let's do that. Let me first just really quickly create those two objects and to do that
01:18I'm going to create a kind of like a left object or right object. So I'm just going to
01:23go here and I'm going to create a private variable which is going to be our it's going to be a shape
01:29left which is going to be a shape and we're going to do the same thing create a shape
01:40right which is going to be a shape as well not a shader but a shape as well. All right so now
01:45that we've created and defined our variables I'm going to go right into our function and in my
01:51function I'm going to create that new shape. I'm going to do the same for shape right and the next
02:00step is I want to draw a circle. So I'm going to go into my shape into my shape left and I'm going
02:07to go to my graphics library. I'm going to define my fill color so to do that I'm going to set
02:15my fill which I know sometimes you might forget it like I just forgot right now the definition
02:21not a big deal just find it in your properties. So if we go in here we're going to search for our
02:28begin begin begin fill there we go begin begin fill and we're going to set a color in my case
02:35I want it to be white so I'm going to pass in the hexadeth mesel value of white which is six times f
02:43and the next shape left graphics draw a circle and we're going to draw a circle we're going to
02:53leave it at position zero zero and we'll just move our element and we want to set a radius so let's
02:58give it a radius of let's say 20 and I'm going to add that shape onto the stage so I'm going to add
03:04child shape left let me just position this shape left and I'm going to set the x position to be
03:14in our stage that stage width divided by three so it's going to be like in the third section of the
03:23of the stage and for my shape left height I'm going to set that to be stage stage height
03:31divided by two so it's going to be in the center of the height itself oh not height
03:35here we want to set it to be y so let me just run it really quickly just to see
03:39if we're getting what we're expecting oops let me close it up by mistake let me just
03:44set this here in the center of our stage so after that 50 after that fade in that's
03:50going to take us 10 seconds which maybe we'd want to call that step a bit earlier
03:55our our circle is positioned right there in that third quarter of our screen so we really want
04:00two of those so I'm going to create a second shape and the same well I'm going to take my
04:05second shape and I'm going to basically do the same thing to it so I'm going to um you know
04:11let me just I could just copy all this stuff I'm just going to change that to be the right one
04:21oh I made a mistake here all right so the right we're doing the exact same thing
04:26all that's left for me to do is to actually define the positions of the left and right
04:30differently so I'm going to set my right so first of all my right is going to be a little bit further
04:35down so that would be times two so it's going to be on the second third of of our screen
04:40so if I run this one more time and let me just um quicken our delay or actually quicken our fade
04:46out if I go back into my object where we're actually doing the sound transformation we'll
04:52see that our volume is going down it's taking it two seconds there's a five second delay
04:56let me drop it down to be a two second delay so we won't have to wait that long
05:01so again our our logo will fade out two seconds in it's going to fade down to 50%
05:08and then our two circles are just going to miraculously appear now obviously that's not very
05:14pretty so the first step that I want to do as soon as it's coming into the stage let's how
05:17how about we first step we'll animate them into the current volume level that is actually going on
05:24with our music that is being played now to do that I'm going to have to store my sound channel
05:29externally so I'll have access to it so I'm going to create here a private var
05:34for my sound channel not sound but sound channel and I'm going to go to my sound channel
05:42that we created in our part one and show a part one and not define a new variable but
05:48just save it into that so we can have access to that sound channel now once we have access
05:52to the sound channel um sound channel we're going to have a left peak and a right peak which is
06:02going to be a dynamic value between zero and one now with this value between zero and one then I
06:08could then assign my my element and fade it into it or animate it into that specific position
06:15so what I want to do is I want to actually initially start my application from uh maybe
06:21uh scale x scale y of zero and then animate it in really quickly into the position where it should
06:28be right now so let's do that so let me go ahead and go for my shape right dot scale x equals shape
06:38right dot scale y equals scale uh share shape left dot scale x shape left scale y all that equals
06:55zero so we're starting off from it at its zero um position next step that I want to do is I
07:01actually want to tween everything so I'm going to go to my tween light and I'm going to tween
07:06my shape right and I'm going to tween it from position of uh well the duration let's do a
07:14really quick duration so it's going to be the zero dot one let's say and we want to set our object
07:20so that's going to be our scale x and I'm going to set it up to be our channel sound channel
07:27left peak oh right peak in this case right peak um and same thing I'm going to do for my
07:34scale y and it's going to be the left well sound channel left peak okay so that's all I really
07:43needed to do uh for my oh sorry actually we're going to do the same thing for our um this is
07:49going to be for our right side and we're just going to duplicate this for our left just switching
07:54it to be left and this is going to be our left peak okay let's give it a go for a second let's
08:03see what's going on here so now music is going to start it's going to fade down our elements are
08:11going to fade into their position really quickly now we're not seeing anything so let me just put
08:16here a quick uh trace to see what are the actual values that are being set inside of our scale x
08:24and scale y so I'm just gonna take that left peak and I'm gonna write as well to see what what value
08:36is it actually is it a value that's just too low maybe and you can see exactly that's our problem
08:44our our value that we're getting in there is actually zero at that point in time so what
08:50what I want to do next is I want to create here maybe some sort of um interval so I'm going to
08:56import an interval because I want to be able to get an update of the actual sound channel to just
09:01see what what are the values that are coming back from our sound channel so to do that I'm going to
09:05import I'm going to use the old school way by importing the flash utilities utils set interval
09:15I'm going to create a new variable called private far interval channel it's going to be an
09:24int value and I'm going to go right into my part two I'm going to add here a new set interval
09:33and I'm going to put here my function which is going to be update
09:38update sound visuals and it's going to delay every let's say 100 milliseconds
09:46so it's going to happen 10 times a second private function that add that into my
09:52oopsie add this value as my function here and I'm just going to trace out the exact same thing
10:00that we were doing earlier I'm just going to trace out the left and right peak and we could get
10:05probably rid of that and I'm just going to let it run and I want to see if the values are changing
10:09and if they're not changing then we're going to have to look deeper into why aren't they
10:14actually changing and you can see that the values are changing and this is what we would
10:18hope to get so the next step for me is actually to take this tweening that we're doing up here
10:23that's taking us zero one seconds which is comparable to that 100 milliseconds that we
10:29defined here and I'm going to just add that in instead and we could basically delete all this
10:36and maybe just call this function for the first time before we actually call it
10:40and run it one more time so now what we're going to expect is we're going to expect to
10:45see our item starting to kind of like grow in and out now our problem seems to be that even though
10:52it's here it's incredibly small and that's because our values that our music is actually
10:57playing is incredibly low we're not getting to the max capacity of our channel so what I'm going to
11:03have to do to create here something that will actually be sustainable something that would
11:08actually work is we'd probably just from looking at these values we could see that we're never
11:12even crossing the zero dot something kind of like a level here we're not even crossing it
11:19so what I'm going to do is I'm going to create here basically a variable which will be our left
11:24peak left peak and it's going to be a number just like it was before and I'm just going to take my
11:34left peak duplicate it by let's say let's start by duplicating it by 10 just to extremify it a bit
11:41and we're just going to make sure that we're going to maximize the value so math.min so we're
11:49going to take the lower value between one and whatever that value comes making sure that we'll
11:54never cross one because that is the maximum amount of scale that we probably want to get to
11:59although in our case it doesn't really matter if we scale something above that so it doesn't
12:04really matter so in our case we could decide hey we're not going to do that but if it is
12:08important then we would we would set here that our max peak if we don't want our scale to go
12:12beyond that fold if in case there really was some sort of a massive sound that was really
12:16big that's waiting for a later period and we'll do that also for the right peak and I'm going
12:24to take that right peak right right peak and also for us is to feed in these variables into
12:34the right spot so for our scalex we're going to put here peak right oh sorry that was peak left
12:40and we're going to do peak right and put that in its place to replace that value that we were seeing
12:46that was extremely small so it was almost invisible to us so this time around we're
12:50going to hope that it's obviously going to be visual we're going to be able to see it
12:54and now that we could see it and our and our sound is actually animating and we could see the
12:59two channels animating at the same time we could decide how big we want it to be by defining that
13:04maximum radius radius so if in the beginning we drew a circle with a radius of 20 we could
13:09define ourselves a bigger radius so I'm going to create here a new rad for radius and it's going
13:14to be an integer and I'll just set that value to be 200 so we'll have your a nice big circle
13:21and on both the right and the left side and that's it this is the end of chapter two
13:26or sorry end of chapter four where we explored a lot of different properties of plugins we've
13:33looked into how to animate things that are not really visual such as how to animate sound
13:39so we've seen in our animations both how to fade in and out sound and now we've seen also how could
13:44we create a custom animation that is changing based on your music now when you're seeing this
13:49type of animation as it's running in the background imagine what you could do also with just taking
13:55these this type of information of some music and then creating random defined animations
14:01that would literally happen based on what's actually happening inside of your music
14:06now with this said our speed of our animations might not be the speed you would want them to be
14:12so if you'd want it to be a different speed all you have to do is reduce that speed even further
14:18so maybe you want to have it every 50 milliseconds and then you would basically duplicate or change
14:24your interval that is being called so it well not this is not the one that I wanted to change
14:28but in our part two where we're setting our visual we could change it to any value we would
14:33want here we could take for example one second and divide it by 24 because we want it to be 24
14:40times a second and this frame rate that we're getting here that is being called basically 24
14:46times a second which is the frame rate of our rendering more or less because that's what our
14:51current example our current file is running on it's running on 24 frames a second so we can
14:56make those refreshes 24 times a second and ask for that peak information 24 times a second
15:02so really it just depends on what is our animation and by the way maybe we'd want it or need to
15:07update our animation more quickly but really I don't think it matters too much because it's
15:12not that fine-tuned and it's not about being exactly accurate but just showing how responsive
15:17our animation is to changes that are happening inside of the music so it's okay if our
15:21animation doesn't actually complete so if we run this one more time we'll probably get more
15:25updates because before we were running at 10 frames a second if we have to refresh every
15:30100 milliseconds but now you can see it's a lot more responsive it's changing with any event than
15:35anything that's actually happening in our music and it doesn't really matter if it reaches a
15:40destination in the amount of time that it needs to reach it okay so that's it this is the end of
15:45chapter two oh sorry chapter four um and in the next chapter we're going to start exploring tween
15:50next

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!