Lab 2.4: Playing Audio

Playing Audio

Goals

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

  • Accessing the Quorum Standard Library
  • Creating Objects
  • Calling Actions on Objects

Computer Science Principles Curriculum

  • Big Idea: Creativity: EU 1.2, LO 1.2.1, EK 1.2.1A, EK 1.2.1B, EK 1.2.1C, EU 1.3, LO 1.3.1, EK 1.3.1A, EK 1.3.1B
  • Big Idea: Programming: EU 5.1, LO 5.1.1, EK 5.1.1A

Common Core Standards

  • English Language Arts Standards ยป Science & Technical Subjects: CCSS.ELA-Literacy.RST.9-10.2, 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
  • 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

  • Library
  • Instantiation
  • Action Call
  • Parameters
  • Parameter Passing
  • Samples
  • OGG file extension
  • WAV file extension

Overview

This lab demonstrates the use of the Audio library in Quorum. The Audio library can be used to play music files in the ".ogg" or the ".wav" formats. You will write a program that plays a music file that can be shared with classmates. Download this "zip file" for a template to help you get started. To open the template, follow these steps:

1. Download and extract the zip file

2. Open Quorum Studio

3. Under File, click Open Project

4. Navigate to where you downloaded the zip file

5. Select your project and click Open Project

You may also use the online versions of the development environment to test your code. For this, we have stored the song file at media/song.ogg.

Goal 1: Accessing the Quorum Standard Library

Open the provided template and navigate to the main.quorum file.

This assignment makes use of the Quorum Audio Library in the Quorum Standard Library. You need to type code at the very top of the main.quorum file that lets Quorum know you want to use this library. Begin by typing the appropriate code to use the Quorum Audio Library in your project. The Audio Library is called Libraries.Sound.Audio.

Example: Access Quorum

Example: Tell Quorum we want access to Audio.

// this line tells Quorum we will use the Audio library
use Libraries.Sound.Audio

Any time you want to use a Quorum library, you need to include a statement like this at the top of your program code that lets Quorum know that you will be using that library.

In order to work with files, you also need to use the File library. The File library is called Libraries.System.File. Add a statement to your project that allows you to use the Quorum File library, below the other usestatement.

You now are able to use these Quorum libraries in your program.

Goal 2: Creating Object

To put the Audio and File libraries to use, instantiate a new Audio object, calling it audio.

Example: Instantiate an Audio object.

// this creates an Audio object and calls it "audio"
Audio audio

Now, in order for the Audio object to be able to play your song, it needs the file that contains the song. Create a new File object, naming it file.

In order to use the File object, you need to tell the File object what file it is. Since this file will represent the sound file for your Audio object, your file should be an audio file. Audio files save data that represents all the samples of a sound. The file formats that the Quorum Audio Library supports are ".ogg" or the ".wav" formats.

To set the name of your file, you use the SetPath action. The SetPath action takes a text parameter that represents the name of the file.

Example: Tell the File object the file path.

//This line of code sets the path in the file to media/song.ogg
file:SetPath("media/song.ogg")

Now that you have your file set up, you can now tell the Audio object what to do. The Audio object needs to load a file, the file you just set up! Use the Load action to give the Audio object the file you created in your program.

Example: Load the file to the Audio object.

//This line loads a file into the audio object
audio:Load(file)

You are now ready to play your audio file. You can do this by calling the PlayUntilDone action for your Audio object. The PlayUntilDone plays your Audio object until it is done, ensuring that your program does not end until the audio file is done playing.

You can now run your program. There should be no errors, and your audio file should play. If your program does not compile, fix any errors and try again.

Goal 3: Calling Actions on Objects

When playing audio files, you may want the audio to play in only the right ear or the left ear. You can do this in Quorum by calling the SetBalance action on your Audio object.

The SetBalance action takes one parameter, a number . The values for this number can be between -1 and 1. Each has the following behavior:

  • A value of -1 sets the audio to play in only the left ear
  • A value between -1 and 0 sets the audio to play from the left, moving increasingly to the left as the number gets closer to -1
  • A value of 0 sets the audio to play in both ears
  • A value between 0 and 1 sets the audio to play from the right, moving increasingly to the right as the number gets closer to 1
  • A value of 1 sets the audio to play in only the right ear

0 is the default value for the balance of Audio objects. If you never call the SetBalance action, the audio will automatically play in both ears.

Example: Change the balance of your Audio object to the left ear.

// this line of code tells your audio object that its balance should be the 
// number you are passing to it.
audio:SetBalance(-1)

Play around with the balance options by changing the parameter of the SetBalance action.

You can compile and run your program to hear the differences.

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 2.5, which describes how work displaying an image in Quorum..