

The 8000 in the last line is simply the volume of the sound (the height of the sine wave). 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.
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. To gain more control of the playing of a sound file, use the GreenfootSound constructor and methods. 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 framework was designed to make the creation of interactive, animated graphics and production of sound easy for programming novices. 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). 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) 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. 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: If you have the variable outside the method, it will be available to all your code inside the class, and wont ever change unless you tell it to. As well as being useful for working with triangles, the sine function can also be used for generating sounds. The sound keeps looping because you made a variable inside a method, that turns back to false every time the method is called (which is every frame).
