In this assignment we will practice the following computer science concepts:
In this assignment, you will write a program that plays a simple race against two car sounds. This is an example of how you can make an accessible game or program. You will set the balance and volume of the sounds to make them appear as if they are coming in your direction and passing you. Download this zip file for a template to help you get started. We've included two .wav audio files for you. Once you get your program running you can consider trying it with different .ogg files you download as well. Recall that Quorum will play .ogg files as well.
You may also use the IDE below to write and test code.
Open the provided template and navigate to the main.quorum file.
Code needs to be added in two places in the template. The first place is in an action that begins on the line that starts with
action CreateGame. This action loads up the sounds, sets initial volumes and balances, and plays the sounds.
Here is what the action looks like, on lines 35 through line 36.
action CreateGame end
You write your code to move the sounds in the action labeled
Update(number seconds). This action is found on line 45 through line 46.
The action looks like this:
action Update(number seconds) end
Before we can begin racing the sounds, we need to set up the
Audio objects. Navigate to the above mentioned
action CreateGame line, on line 17. You need to set up the audio files to your
Audio objects here.
Activity: Load sounds to the two
Audio objects, set the balances to the left ear, and play the
The first thing you need to do with the
Audio objects is load files to them. If you have trouble with this review Lab 2.4. Once you have done this, you need to set the initial balances for the
Audio objects. Since the sounds should move from the left to the right, the balances should start playing completely in the left ear.
After this, you can play the
Now that you have the set-up out of the way, you can begin writing the code to move the sounds.
Navigate to the above mentioned
Update(number seconds) line, on line 45.
In order to simulate the sounds coming near you and then passing you, we need the balance and volume to change with time. The balance should change from playing only in the left ear to playing equally in both ears to playing only in the right ear. The volume should start off quietly and then get progressively louder as the balance approaches 0, and then get progressively quieter until the balance reaches 1.
The balance should change linearly with time.
To do this, you need to update the balance using the current balance (the
GetBalance action returns the current balance) and add in the speed of the sound multiplied by
seconds the parameter for the
Example: Set the new balance of one of the
// This line gets the current balance of the audio object number balance1 = car1:GetBalance() // This line sets the new balance to be the current balance + speed * seconds car1:SetBalance(balance1 + speed1 * seconds)
Activity: Set the balance of the other
A good function that gives us the relationship we want between the balance and volume is the cosine function.
If you think of the balance as the x-axis and the volume of the y-axis, the cosine function has the behavior we want for the relationship between balance and volume.
At the x-value of -1, the cosine function is close to 0.
At the x-value of 0, the cosine function is at its maximum of 1.
At the x-value of 1, the cosine function is back to being close to 0.
To use this function, you need to use the
Libraries.Compute.Math library and declare a
Activity: Instantiate a
The cosine action in Quorum is called
Cosine, and takes a
number as a parameter that represents the value (in radians) to take the cosine of and returns a
number that represents the cosine of that value.
Example: Use the
Cosine action on the
Math object with the balance of one of the
Audio objects to set the new volume of the
// This line uses the Math object to calculate the volume based on the balance number volume1 = math:Cosine(balance1) // This line sets the new volume car1:SetVolume(volume1)
Activity: Set the new volume of the other
Audio object in the same manner.
You should now be able to compile and run your program. The two sound files for the cars should play, starting from the right ear and moving towards the left ear, getting first louder and then quieter. If there are any errors, fix them and try again.
The two audio sounds should start off playing in just the left ear. With time, the audio sound should progressively change balance from playing in just the left ear to playing in both ears to playing in just the right ear. At the same time, the volume should progressively change from quiet to louder to quiet again. This gives the effect of the car sounds passing you.
In the next tutorial, we will discuss Assignment 2.4, which describes how work Tortoise vs. the Hare in Quorum..