vastze.blogg.se

Greenfoot sound
Greenfoot sound






  1. #GREENFOOT SOUND HOW TO#
  2. #GREENFOOT SOUND CODE#

You can play with the finished game (including a couple of simple AI players) over on the Greenfoot site. The 8000 in the last line is simply the volume of the sound (the height of the sine wave). instead of: aySound ('sound. The 0.25 and the 3200 on the middle line of the loop adjust the amount that the height of the projectile affects the frequency of the sine wave. To stop it or pause it you have to use the class GreenfootSound.

#GREENFOOT SOUND CODE#

There are three constants in this code that I fiddled with until they sounded right (literally!). Here’s some code that does just that, using the height calculation from our previous post:įor (int i = 0 i < soundWave.length i++)ĭouble t = (double)i / (timePerFrame * 44000.0) Ī += 0.25 + (t * vy - 0.5 * g * t * (t - 1)) / 3200.0 What this means is that instead of using a steadily-increasing number to feed to the sine wave function, you increase the number faster when the banana is higher. The gist is something like this - the sine wave gets more frequent (the peaks get closer together, horizontally), the higher the projectile is in the air: This creates a more familiar flight sound (such as you hear in cartoons). If we change the frequency in proportion to the height of the banana, we get a sound that starts low, increases in pitch as the banana reaches the apex of its flight, then decreases again as it comes back down.

greenfoot sound

We can greatly improve the suitability of the sound if we vary the frequency of the sine wave during the flight. Just playing a simple sine wave for the sound of the flight is dull and incongruous. I subtract 5 frames off the total, to make sure that the sound is finished before we need to play the impact sound.

#GREENFOOT SOUND HOW TO#

The length of the sound in seconds will be the time it takes to execute one frame in Greenfoot, multiplied by the number of frames before impact (“impactTime”, above - which we saw how to calculate last post). So, instead of Greenfoot.<< sound command >, you would (maybe) use Settings.<< sound command >.

The sound will play at 44000 samples per second, so we need to multiply the number of seconds by 44000 to get the number of samples. We can use this information to construct a sound that’s exactly as long as the time the banana will spend in the air:ĭouble lengthInSecs = timePerFrame * (impactTime - 5) void: setVolume(int level) Set the current volume of. void: playLoop() Play this sound repeatedly in a loop. void: pause() Pauses the current sound if it is currently playing.

greenfoot sound

We saw in our last post that for our monkeys-throwing-bananas game, we can work out how long the banana will be in flight for. Get the current volume of the sound, between 0 (off) and 100 (loudest.) boolean: isPlaying() True if the sound is currently playing. Here’s an example of a sine wave on youtube. If you treat this as a sound wave, then you get a dull tone that we recognise as an electronic beep, since it’s used so often by electronic devices. If you take an ever increasing number and pass it to the sine function, you get back this graph: As well as being useful for working with triangles, the sine function can also be used for generating sounds.








Greenfoot sound