Computer Science Grade 9 20 min

Creating Music

Creating Music

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Define key digital music terms like Pitch, Duration, and MIDI. Explain how musical notes can be represented as data using lists or arrays. Use a programming library function to generate a single musical note. Store a sequence of notes in a list to create a simple melody. Use a 'for' loop to iterate through a list of notes and play a complete melody. Programmatically modify note properties such as pitch and duration to alter a melody. Ever wondered how your favorite video game music is made? 🎮 What if you could code your own soundtrack from scratch? In this lesson, you'll learn the building blocks of digital music and use programming to bring your own melodies to life. We'll explore how computers understand sound and how you can contr...
2

Key Concepts & Vocabulary

TermDefinitionExample PitchHow high or low a sound is. In music, this is represented by notes like C, D, and E.A high-pitched sound is like a whistle, while a low-pitched sound is like a bass drum. In code, we often use MIDI numbers to represent pitch, where 60 is Middle C. DurationHow long a note is held or played.A whole note lasts for four beats, while a quarter note lasts for one beat. In code, this can be represented in seconds (e.g., 1.0) or beats (e.g., 4). Volume (Amplitude)The loudness or softness of a sound.A loud drum hit has a high amplitude, while a soft whisper has a low amplitude. In code, this is often a number between 0.0 (silent) and 1.0 (full volume). MIDI (Musical Instrument Digital Interface)A set of instructions, not sound itself. It tells a device what note to play,...
3

Core Syntax & Patterns

Representing a Note as Data note = [pitch, duration, volume] To create music with code, we must store notes as data. A common way is to use a list or array where each element represents a property of the note, such as its pitch (a MIDI number), its duration (in seconds or beats), and its volume. Creating a Melody with a List melody = [note1, note2, note3, ...] A melody is a sequence of notes. We can represent an entire melody as a list of individual note data structures. A program can then loop through this list to play the song in order. The Playback Loop for note in melody: play_note(note) To play a melody stored in a list, use a `for` loop. The loop iterates through each `note` in the `melody` list and calls a function (e.g., `play_note`) to generate the sound f...

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
The tutorial uses `[pitch, duration, volume]` for one note. How could you best adapt this structure to represent a chord, which is multiple notes played simultaneously for the same duration?
A.[[pitch1, pitch2, pitch3], duration, volume]
B.[pitch1, duration, volume, pitch2, duration, volume]
C.separate list for each note, like `chord = [note1, note2, note3]`
D.By creating a new function `play_chord(pitch1, pitch2, pitch3)`
Challenging
You want to create a crescendo effect, where a melody gets progressively louder. Given `melody = [[60, 0.5, 0.2], [62, 0.5, 0.2], [64, 0.5, 0.2], [65, 0.5, 0.2]]`, which logic inside a loop would achieve this?
A.Increase the pitch (`note[0]`) by a small amount for each note before playing it.
B.Decrease the duration (`note[1]`) for each note so they play faster.
C.Use a counter or an increasing variable to add to the volume (`note[2]`) of each note during each loop iteration.
D.Set the volume (`note[2]`) to a constant high value like 0.9 for all notes from the start.
Challenging
Analyze the following code snippet. What kind of musical pattern will it generate? `melody = []` `pitch = 60` `for i in range(4):` `note = [pitch + i*2, 0.25, 0.8]` `melody.append(note)`
A.four-note scale where each note is held longer than the last.
B.four-note sequence with pitches that get progressively higher.
C.Four identical notes played in a row with a pitch of 60.
D.four-note sequence with decreasing volume.

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Digital Media Creation

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.