Mission12_Buzzer_Music
In this project, let's play a known song "Twinkle twinkle little star" using a buzzer.
What you need
The parts you will use are all included in the Maker kit.
- SwiftIO board
- Shield
- Buzzer
- 4-pin cable
Circuit
- Plug the shield on top of your SwiftIO board.
- Connect the buzzer module to pin PWM2B (D10).
Example code
Open the project Mission12_Buzzer_Music
in the folder MadExamples
/Examples
/MakerKit
if you downloaded the folder.
- Mission12_Buzzer_Music.swift
- Twinkle.swift
- Package.swift
import SwiftIO
import MadBoard
import PWMTone
@main
public struct Mission12_Buzzer_Music {
public static func main() {
var halfStep = 0
var bpm = 60
let player = PWMTone(PWMOut(Id.PWM2B), bpm: bpm)
for _ in 0..<3 {
player.play(track: Music.twinkle)
bpm += 40
player.setBPM(bpm)
halfStep += 12
player.setFixedHalfStep(halfStep)
sleep(ms: 1000)
}
while true {
sleep(ms: 1000)
}
}
}
import PWMTone
struct Music {
static let twinkle: [(note: PWMTone.Note, value: Int)] = [
(.C4, 4),
(.C4, 4),
(.G4, 4),
(.G4, 4),
(.A4, 4),
(.A4, 4),
(.G4, 2),
(.F4, 4),
(.F4, 4),
(.E4, 4),
(.E4, 4),
(.D4, 4),
(.D4, 4),
(.C4, 2),
(.G4, 4),
(.G4, 4),
(.F4, 4),
(.F4, 4),
(.E4, 4),
(.E4, 4),
(.D4, 2),
(.G4, 4),
(.G4, 4),
(.F4, 4),
(.F4, 4),
(.E4, 4),
(.E4, 4),
(.D4, 2),
(.C4, 4),
(.C4, 4),
(.G4, 4),
(.G4, 4),
(.A4, 4),
(.A4, 4),
(.G4, 2),
(.F4, 4),
(.F4, 4),
(.E4, 4),
(.E4, 4),
(.D4, 4),
(.D4, 4),
(.C4, 2)
]
}
// swift-tools-version:5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "Mission12_Buzzer_Music",
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/madmachineio/SwiftIO.git", branch: "main"),
.package(url: "https://github.com/madmachineio/MadBoards.git", branch: "main"),
.package(url: "https://github.com/madmachineio/PWMTone.git", branch: "main"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.executableTarget(
name: "Mission12_Buzzer_Music",
dependencies: [
"SwiftIO",
"MadBoards",
"PWMTone"
]),
.testTarget(
name: "Mission12_Buzzer_MusicTests",
dependencies: ["Mission12_Buzzer_Music"]),
]
)
Background
Musical notes
At first, let's learn something about notes.
There are 88 keys in total on a piano keyboard, from A0 to C8. These notes correspond to different frequencies from low to high. And the note do, re, mi, fa, sol, la, ti correspond respectively to C, D, E, F, G, A, B.
A half step, or semitone, is the smallest interval between notes. The interval between the key A and key A# is a half step. Two half steps constitute a whole step, like the interval between A and B.
An octave consists of 12 half steps. It is the distance between a note and the next note with the same name, for example, the first key A to the second key A forms an octave.
A quarter note is the common note length in music and has one beat. Then other notes are based on it: a half note has two beats, a whole note has four beats, an eighth note has half beat, etc.
BPM, or beat per minute, is used to measure the tempo of a piece of music. For example, 60 BPM means there would be 60 beats in a minute. Each beat lasts 1 second.
Code analysis
You can find two files in the project:
- The file
Twinkle.swift
records the notes of the melody and the duration of each note. - The file
Mission12_Buzzer_Music.swift
sets the PWM signal to play the music.
Let's take a look at the file Mission12_Buzzer_Music.swift
.
import SwiftIO
import MadBoard
import PWMTone
SwiftIO and MadBoard are the basic libraries used in all these projects.
Besides, you will use another library PWMTone to set the PWM in accordance with the musical score. But before you use it, you need to add it to your project. In the file Package.swift
, add the library and indicate its location as the two highlighted lines.
var halfStep = 0
The value halfStep
is used to change the pitch. It equals 0 now, so the pitch remains unchanged.
var bpm = 60
Set the bpm to 60, so each beat will last one second.
let player = PWMTone(PWMOut(Id.PWM2B), bpm: bpm)
Initialize the player. init(_:bpm:noteGap:)
has three parameters:
- the first one is the PWM pin,
- the second is the bpm,
- the third one sets the interval between each note. It equals 0.1 by default.
player.play(track: Music.twinkle)
This statement is to play the music. The method play(track:halfstep)
has two parameters:
track
is an array of tuples that indicates notes and note length.Music.twinkle
refers to the score written in the fileTwinkle.swift
. In the structMusic
, there is a constanttwinkle
that stores the notes and the note length. So the track is written asMusic.twinkle
.halfStep
has a default value of 0. You will set it later.
bpm += 40
player.setBPM(bpm)
Then increase the bpm to make the tempo a little faster.
halfStep += 12
player.setFixedHalfStep(halfStep)
These two statements aim to raise an octave, so the pitch of the music sounds higher.
Reference
PWMOut - set the PWM signal.
PWMTone - play notes and change how it sounds by setting PWM signals.
MadBoard - find the corresponding pin ids of your board.