Actions: White Noise Generator
Actions Using Parameters and Return TypesGoals
The goal of this lab is to learn the following:
- Write actions with different return types
- Use actions with parameters
Computer Science Principles Curriculum
- Big Idea: Creativity: EU 1.1, LO 1.1.1, EK 1.1.1A, EK 1.1.1B
- Big Idea: Creativity: EU 1.2, LO 1.2.1, EK 1.2.1A, EK 1.2.1B, EK 1.2.1C, EK 1.2.1D, EK 1.2.1E
- Big Idea: Creativity: EU 1.3, LO 1.3.1, EK 1.3.1A, EK 1.3.1B, EK 1.3.1E
- Big Idea: Abstraction: EU 2.1, LO 2.1.2, KE 2.1.2B, EK 2.1.2C, EK 2.1.2E, EK 2.1.2F
- Big Idea: Abstraction: EU 2.2, LO 2.2.1, KE 2.2.1A, KE 2.2.1B, KE 2.2.1C
- Big Idea: Algorithms: EU 4.1, LO 4.1.1, KE 4.1.1A, KE 4.1.1B, KE 4.1.1C, KE 4.1.1D, KE 4.1.1E, KE 4.1.1F, KE 4.1.1H
- Big Idea: Algorithms: EU 4.1, LO 4.1.2, KE 4.1.2A, KE 4.1.2B, KE 4.1.2C, KE 4.1.2D, KE 4.1.2G, KE 4.1.2I
- Big Idea: Algorithms: EU 5.1, LO 5.1.1, KE 5.1.1A, KE 5.1.1B, KE 5.1.1C, KE 5.1.1D, KE 5.1.1E, KE 5.1.1F
- Big Idea: Algorithms: EU 5.1, LO 5.1.2, KE 5.1.2A, KE 5.1.2B, KE 5.1.2C, KE 5.1.2D, KE 5.1.2E, KE 5.1.2F
- Big Idea: Algorithms: EU 5.2, LO 5.2.1, KE 5.2.1A, KE 5.2.1B, KE 5.2.1C, KE 5.2.1D, KE 5.2.1E, KE 5.2.1F, KE 5.2.1G, KE 5.2.1H
- Big Idea: Algorithms: EU 5.3, LO 5.3.1, KE 5.3.1A, KE 5.3.1B, KE 5.3.1C, KE 5.3.1D, KE 5.3.1E, KE 5.3.1F, KE 5.3.1G, KE 5.3.1H, KE 5.3.1I, KE 5.3.1J, KE 5.3.1K, KE 5.3.1L, KE 5.3.1M, KE 5.3.1N, KE 5.3.1O
- Big Idea: Algorithms: EU 5.4, LO 5.4.1, EK 5.4.1A, EK 5.4.1B, EK 5.4.1C, EK 5.4.1D
- Big Idea: Algorithms: EU 5.5, LO 5.5.1, EK 5.5.1A, EK 5.5.1B, EK 5.5.1C, EK 5.5.1D, EK 5.5.1E, EK 5.5.1G
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
- Actions
- Parameters
- Return Types
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 Quorum Studio to write your program.
If using Quorum Studio, create a new project using "Blank. "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.
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, then 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 Assignment 4.1, which describes an introduction to actions..