Lab 2.3: Music

Types

Goals

The goal of this lab is to learn the following:

  • Understanding the Music Library
  • Using the Music Library

Computer Science Principles Curriculum

  • Big Idea: Programming: EK 5.1.2B, EK 5.1.2E, EK 5.2.1F, EK 5.1.2J, EK 5.2.1C, EK 5.3.1D, EK 5.3.1I, EK 5.4.1

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.6, CCSS.ELA-Literacy.RST.11-12.2, CCSS.ELA-Literacy.RST.11-12.3, CCSS.ELA-Literacy.RST.9-10.1, CCSS.ELA-Literacy.RST.9-10.2, CCSS.ELA-Literacy.RST.9-10.7, CCSS.ELA-Literacy.RST.9-10.8, CCSS.ELA-Literacy.RST.9-10.9
  • Standards For Mathmatical Practice: CCSS.Math.Practice.MP1, CCSS.Math.Practice.MP2, CCSS.Math.Practice.MP5, CCSS.Math.Practice.MP6, CCSS.Math.Practice.MP7, CCSS.Math.Practice.MP8,

Vocabulary

  • Code completion
  • Library
  • Class
  • Instantiate
  • Objects
  • Actions
  • Parameters

Hotkeys

A list of Hotkeys can be found on the Getting Started with Quorum Studio page.

Overview

This lab demonstrates the use of the Music library in Quorum. The Music library can be used to create Music objects, to play individual notes, chords, or even entire musical compositions for the appropriate length of time. You will write a program that plays a song of your own composition, and you will share this song with your classmates. While building your musical piece, you will also take advantage of a feature of Quorum Studio known as code completion. This lesson must be completed in the offline environment of Quorum Studio.

Goal 1: Understanding the Music Library

You will start by creating a new blank Quorum project in Quorum Studio. Name this project Lab2_3. Open the main.quorum file in the Sodbeans editor.

Before you can begin using the Music library, you must first understand a new keyword. Quorum, in a new project, is only aware of the code you place inside the file main.quorum. You can tell Quorum you wish to make use of a library by using the use statement.

Begin by typing the word use followed by a space. You will use the code completion feature of Quorum Studio to find the music library. Let's use code completion to find the Music library. After typing usefollowed by a space, press CTRL + SPACE to use the code completion to select Libraries . To select this option, press Enter. Now type a single period and a new code completion window will appear with new options, select Sound by using the arrow keys and pressing Enter. Type a period to bring up the code completion menu again. You should see the library you are looking for, called Music.

If you know the name of the library you want to use that in this case is Libraries.Sound.Music you can type the code directly as in the line below.

Use Libraries.Sound.Music 

Quorum now knows that you want to use the Music library. To put the Music library to use, you need to instantiate a new Music object. In object-oriented programming languages, like Quorum, you have the option of creating multiple objects from the same class. The action of creating new objects of a given class (like the Music class) is called instantiating.

Example: Add a line of code that instantiates a Music object, named muse.

Music muse

You can choose any name for your object, just remember that the class cannot be renamed. Now erase your example and do the activity.

Activity: Add a line of code that instantiates a music object

You can choose the name you want for your object. The name rules for declaring the variables also apply to objects.

Goal 2: Using the music Library

In Quorum, you use the colon character to indicate that you wish to call an action on a variable. On a new line, type the name of your Music object followed by a colon. The code completion window will again appear. Notice the number of available actions in the code completion window for a Music object. Go through this list of actions using the arrow keys until finding the action named Play(integer note, number duration). This action will allow us to play a given note for a specified length of time, in seconds. Press enter to accept this option.

To tell the Play action what notes you wish to hear, you must pass it this information in the form of parameter. In programming, parameters are values (or variable names) that are passed to an action.

Example: Use the play action to play a middle C for one second

muse:Play(60, 1)

Instead of muse remember to use your object name to test the example. Notice that because it is more than one parameter inside the parenthesis, we use a comma to separate them. Sound should be heard from the computer speakers (make sure the volume is turned up)!

Activity: In two different lines use the play action, changing the note and duration.

Choose any number between 0 and 127 to change your note and any amount of seconds you want to change the duration. To see a list with the notes and their respective numbers, click here. The note will sound on a piano, if you want to change the instrument you will have to use another library, the code below it is an example of changing the instrument.

Example: Change the instrument.

use Libraries.Sound.Music
use Libraries.Sound.Instrument

Music muse
Instrument i = muse:GetInstrument(10)

muse:SetInstrument(i)
muse:Play(60, 1)

You can choose instruments between 0 and 127. To go to the list with all of the options and theirs respective number, click here.

Activity: It's time to create a custom song!

Take time in creating a song and keep testing the program to ensure that the program plays a melody. If you are confident enough, attempt to re-create the melody of a song you know.

When the song is finished and ready to show off, play the song for the instructor, some friends, or the class.

Next Tutorial

In the next tutorial, we will discuss Challenge 2.4, which describes how work Choose your own pattern in Quorum..