Goals

In this assignment, we will practice the following computer science concepts

Computer Science Principles Curriculum

Common Core Standards

Overview

This lab demonstrates how to map certain sounds to specific keys on the keyboard. When that key is pressed the program will play the designated sound. The Quorum Games and Audio libraries will be used, along with the InputMonitor and KeyboardEvent libraries to handle keyboard input during the program. You will write a program that will play certain sounds upon a key being pressed. Download this zip file for a template to help you get started.

You may also use the IDE below for this assignment. The sounds we recommend using for this assignment are:

Controlling Audio

Goal 1: Using Conditional Statements

Open the provided template. In the main.quorum file code needs to be written in two places. The first place is in an action called CreateGame on lines 38 through 39. In this action, you set up your Audio objects. The second place you write code is in the action called Update(number seconds) on lines 48 through 49. In this action, you write the code that plays the Audio objects when keys are pressed. Three Audio objects have already been declared for you. A folder called media has been provided to you that includes sounds for you to use for this program.

Activity: Load sounds to three Audio objects and enable looping on the three Audio objects.

Go through the media folder and choose three sounds you want to play during your program. Once you have chosen your sounds, you need to load them to your Audio objects. You may want to enable looping on these sounds once you have loaded them so that the sounds play over and over again while a key is pressed.

Choose the three keys you want to play sounds when pressed and choose which Audio object to play when that key is pressed. In the Update(number seconds) action beginning on line 27, check for these keys being pressed. If they are, play the Audio object for that key.


Example: Play a sound when a key is pressed.

// this line checks if the "A" key is pressed.
if inputMonitor:IsKeyPressed(keys:A)
   // this line plays the sound we chose to associate with the A key
   sound1:Play()
end

Activity: Choose 2 more keys and play the other two Audio objects when those two keys are pressed.

When you run your program and press those keys, the sounds you associated with those keys should play. You can keep a key pressed down to play a sound for a longer period of time (due to the loops you created earlier) or just tap the key to play the sound for a short period of time. You will only hear the sound for the period you press the key. If you tap it you will barely hear a sound! Additionally, you may press more than one key at once to play multiple sounds.

Compile and run your program (after fixing any errors). You may notice that the sounds do not sound right if you hold a key down. Why might this be?

Goal 2: Using Logical Operations

Recall that the Update(number seconds) action is called on every frame of your program. Also recall that the Play action plays the audio file from the beginning. So each time the Update(number seconds) action is called, any audio object currently playing will be played again from the beginning if the appropriate key is still pressed down. How might you fix this?

One easy way to fix this is to use the IsPlaying action. IsPlaying returns a booleantrue if the audio is playing and false if not. We can use this action to our advantage because we only want to start playing our Audio objects if the appropriate key is pressed AND if the Audio object is not already playing.


Example: Only start playing the sound if the key is pressed and it is not already playing.

// this line checks if the "A" key is pressed and that the sound is not already
// playing
if inputMonitor:IsKeyPressed(keys:A) and not sound1::IsPlaying()
   // this line plays the sound we chose to associate with the A key
   sound1:Play()
end

Activity: Add the additional condition for the other Audio objects.

For extra fun, instantiate more Audio objects (near the top of the file, above the CreateGame action), load files to them, and play them on certain key presses!

Sample Output

The program should play at least 3 different sounds upon three different key presses. The sounds should continue to play smoothly while a key is held down.

Next Tutorial

In the next tutorial, we will discuss Assignment 3.4, which describes how work Key Press Madness - Visual in Quorum..