Lab 3.4: Control the Audio

Use Conditionals to control audio

Example: Load the audio file:

Goals

In this lab, we will learn the following computer science concepts:

  • Creating Objects
  • Using conditional statements

Computer Science Principles Curriculum

  • Big Idea: Algorithms: EK 4.1.1A, EK 4.1.1C, EK 4.1.1D

Common Core Standards

  • English Language Arts Standards » Science & Technical Subjects: CCSS.ELA-Literacy.RST.9-10.3, CCSS.ELA-Literacy.RST.9-10.4, CCSS.ELA-Literacy.RST.9-10.5, CCSS.ELA-Literacy.RST.9-10.7, CCSS.ELA-Literacy.RST.9-10.10, CCSS.ELA-Literacy.RST.11-12.2, CCSS.ELA-Literacy.RST.11-12.3, CCSS.ELA-Literacy.RST.11-12.4, CCSS.ELA-Literacy.RST.11-12.5, CCSS.ELA-Literacy.RST.11-12.10
  • Standards For Mathmatical Practice: CCSS.Math.Practice.MP1, CCSS.Math.Practice.MP2, CCSS.Math.Practice.MP4, CCSS.Math.Practice.MP5, CCSS.Math.Practice.MP6, CCSS.Math.Practice.MP7, CCSS.Math.Practice.MP8

Vocabulary

  • Control Structures
  • If statement
  • Loops
  • Input Manager

Overview

This lab demonstrates how to use key presses on the keyboard to perform some tasks. The task you will perform is to write a program that changes the balance of a sound file from left to right or right to left upon pressing of the left and right arrow keys. The program will also increase the volume upon pressing of the up arrow key and decrease the volume upon pressing the down arrow key. Download this zip file for a template to help you get started.

Understanding the InputMonitor and KeyboardEvent Libraries

Open the provided template and navigate to the main.quorum file . Near the top of the file, there are two lines that look like this on lines 15 and 16:

Goal 1: Creating Objects

//This line declares an InputMonitor object called "inputMonitor"
InputMonitor inputMonitor

Line 16 declares one of the objects you use to determine if a specific key has been pressed on the keyboard. The InputMonitor object can tell you when a key is pressed down, and the KeyboardEvent object, which you will add in the first activity, can let you know which key is pressed down. You use these objects to determine when a key is pressed down and which key is pressed down.

Activity: Declare the KeyboardEvent object. Call it keys.
Add this new line of code below the InputMonitor.

The InputMonitor object has an action called IsKeyPressed that takes a KeyboardEvent object variable and returns a booleantrue if the passed key is pressed, false if not. You need to pass the KeyboardEvent object variable to the IsKeyPressed action, telling the KeyboardEvent object which key you want to check is pressed.

Activity: Declare the Audio object. Call it song.
Add this new line of code below the KeyboardEvent.

Navigate to the action called CreateGame on line 36 through line 37. This is where you load the song for your Audio object. Recall that to load an audio file to an Audio object, we call the Load action, passing the file name of the file we want to load.

//This line will load the song to the audio object
song:Load("media/music.wav")

Activity: EnableLooping, and Play your song.
Once you have your Audio loaded, you should enable looping so that it can play over and over during your program. This is done by calling the EnableLooping action on your Audio object. Finally, you want to play your Audio object. Recall that this is done by calling the Play action on the Audio object. Now that you have your Audio loaded and set up, you can begin writing the code to change the volume and balance upon key presses.

Goal 2: Using Conditional Statements

Navigate to the action called Update(number seconds) around line 49. This action is called on every frame of your game. This is where you write your code to handle the key presses. This action should check whether some keys are pressed down and, if they are, begin the appropriate task for those keys.

The volume should be changed upon pressing of the up or down arrow keys. You need to check whether the up and down arrow keys are pressed. The names of the variables in KeyboardEvent objects that represent the up and down arrow keys are UP and DOWN, respectively.

If the up arrow key is pressed, the volume should be increased. To do this, you need to get the current volume of the Audio object by calling the GetVolume action. The new volume should be set to the current volume plus some increment value. The SetVolume action takes one number parameter that represents the percentage of the volume to play. A number of 1 means to play the song at 100% of its normal volume. A number of 0.8 means to play the song at 80% of its normal volume. A number of 0.5 means to play the song at 50% of its normal volume, and so on.

Choose a good increment value for the volume to use when the up key is pressed down. A good one is to increase the volume by 10% with each key press.

Example: Write the code that increases the volume when the Up key is pressed.

//This if statement checks when the Up key is pressed
if inputMonitor:IsKeyPressed(keys:UP)
   //This line gets the current volume of the audio
   number currentVolume = song:GetVolume()
   //This line uses the current volume and adds an increment value to it
   number newVolume = currentVolume + 0.1
   //This line sets the new volume
   song:SetVolume(newVolume)
end

Activity: Lower the volume of the song.
If the down arrow key is pressed, the volume should be decreased. To do this, you need to get the current volume of the Audio object by calling the GetVolume action. The new volume should be set to the current volume minus some decrement value. Choose a good decrement value for the volume to use when the down key is pressed down. A good one is to decrease the volume by 10% with each key press. Write the code to do this.

The balance should be changed upon pressing the right or left arrow keys. You need to check whether the left or right arrow keys are pressed. The names of the variables in KeyboardEvent objects that represent the left and right arrow keys are LEFT and RIGHT, respectively.

If the left arrow key is pressed, the balance should move towards the left ear. To do this, you need to get the current balance of the Audio object by calling the GetBalance action. The new balance should be set to the current balance minus some decrement value. The SetBalance action takes one number parameter between -1 and 1 that represents the audio balance. A number of -1 means to play the audio fully in the left ear. A number of 1 means to play the audio fully in the right ear. A number of 0 means to play the audio equally in both ears, and so on.

Choose a good decrement value for the balance to use when the left key is pressed down. A good one is to decrease the balance by 0.1 with each key press.

Example: Write the code to move the balance towards the left when the Left key is pressed.

//This line checks for the left key being pressed
if inputMonitor:IsKeyPressed(keys:LEFT)
   //This line get the current balance of the audio
   number currentBalance = song:GetBalance()
  //This line uses the current balance and subtracts a decrement value
   number newBalance = currentBalance - 0.1
  //This line sets the new balance
   song:SetBalance(newBalance)
end

Activity: Change the balance towards the right ear.
If the right arrow key is pressed, the balance should move towards the right ear. To do this, you need to get the current balance of the Audio object by calling the GetBalance action. The new balance should be set to the current balance plus some increment value. Choose a good increment value for the balance to use when the right arrow key is pressed down. A good one is to increase the balance by 0.1 with each key press.

You may now compile and run your code. There should be no errors. If there are errors, fix them and try again. You should be able to change the volume and balance by pressing the appropriate keys.

Resources

The music used for this lab is Beachfront Celebration by Kevin MacLeod (incompetech.com). Licensed under Creative Commons: By Attribution 3.0 License.

Next Tutorial

In the next tutorial, we will discuss Lab 3.5, which describes how to Control the Quorum Bunny in Quorum..