Goals

The goal of this lab is to learn the following:

Computer Science Principles Curriculum

Common Core Standards

Vocabulary

Overview

In this lesson, you will practice creating and calling actions using parameters and return types. You will write a random noise generator. This program will use objects Audio and AudioSample libraries. As well as the Random library.

Getting Started

Either use the IDE that will open in another window, or Sodbeans offline to write your program. If using Sodbeans, create a new project using "Quorum Application. "Add the three libraries mentioned above at the top of your program. You will also need at add class Main and action Main. Try to write these yourself, then check the sample code under the online IDE.

Write Your Program Here

You can use this IDE for all your coding in this lesson instead of using Sodbeans

When your project opens in Sodbeans, main.quourm will contain the most of code listed below. You need to add the libraries.


use Libraries.Sound.Audio
use Libraries.Sound.AudioSamples
use Libraries.Compute.Random

class Main
//You will declare objects here

action Main //You will write the majority of the code in this action.
end
//You will add new actions here, between the end of the main action and the main class.
end

Goal 1: Write actions with different return types


Example: Create action SamplesPerSecond().
The following code creates an action that returns an integer. You will use this action to fill in the parameter of the SetSamplesPerSecond() action. This action will be called on the AudioSample object.

//First, you must create the action, the add "returns integer".
//This will allow the action to pass an integer on to whatever calls it.
action SamplesPerSecond() returns integer
//This line creates a variable and uses the Random object to create a random integer between 1 and 44100. //This integer will be used to define how many samples will be used in a second. The default is 44100.
integer sample = random:RandomIntegerBetween(1, 44100)
//This line declares what variable will be returned.
return sample
//As with control stuctures, remember that all actions have to have an end.
end

Activity: Create Seconds().
The Seconds() needs to return a number. You will need to declare a variable and instantiate it to a random number. Write your action under the action SamplesPerSecond() action.

Goal 2: Use actions with parameters

You have written two actions to be used a parameters for other actions. What are parameters? Parameters are a kind of variable that is part of an action. Parameters go in the (). Some actions do not need parameters, others do. You will be using the AudioSample library to create audio samples. These samples can use different channels, gather different numbers of samples, and play the for different lengths of time. The actions you have written will be used to randomize the number of samples, and the length of time it plays. The example below will show you several actions with parameters, as well as help set up the rest of your program.

Example: Write actions with parameters.
You will need to add a new variable, and use the samples and noise actions. You will write the rest of your program in action Main.

//First, declare a variable to hold the return from the SamplesPerSecond action.
integer sample = SamplesPerSecond()
//Setting the number of channels used take a parameter. The example sets it to 1, but you can experiment, and see how it changes the function.
samples:SetChannels(1)
//This line uses the variable sample as the parameter for the number of samples gathered per second.
samples:SetSamplesPerSecond(sample)//The next line creates a counter, so the number of samples stays within the size set above.
integer counter = 0
//The loop that uses the counter you just made to gather the right number of samples. //It then sets the sample, so it can be loaded and played.
repeat while counter < samples:GetSize() samples:Set(counter, random:RandomNumber() * 2 - 1, 0) counter = counter + 1 end

Activity: Use actions with parameters to finish your prgram.
Use the example above to add an number variable that will hold the return from Seconds(). Then call SetSizeInSeconds() on the AudioSample object. You will also have to Load() the samples to the Audio object. To hear the entire sample, you will also need to use PlayUntilDone().

Extension

After you have created your Random Noise Generator, try adding control structures to load and play multiple noises. To use the same Audio object, you need to also use the Dispose() action. Have fun!

Next Tutorial

In the next tutorial, we will discuss Lab 4.1, which describes an introduction to actions..