CS170: Programming for the World Around Us - Semitone Note Generator (100 Points)

Assignment Goals

The goals of this assignment are:
  1. To write a Python program that implements a mathematical expression
  2. To obtain user input from the keyboard in a Python program
  3. To explore the relationship between audio pitch and frequency

The Assignment

In this assignment, you will write a program to execute a mathematical formula to calculate the frequency for a note. Specifically, we will use this formula to compute a note’s frequency some number of half-steps above or below a base frequency. Often, we use the “Concert A” frequency of 440 Hz (known as the A4 note), which you can hear on the Online Tone Generator website. If you know your base frequency (440), and the number of half-steps you wish to go above or below that frequency, you can calculate the new frequency using this formula:

\(pitch = base \times (2^{\frac{1}{12}})^{step} = base \times 1.05946309436^{step}\)

For example, one half step above A4 is A#4 (“A sharp 4”), and its frequency of 466.16 is obtained by calculating \(pitch = base \times (2^{\frac{1}{12}})^{step} = 440 \times (2^{\frac{1}{12}})^{1} = 466.16\). Two half-steps above A4 is B4, whose frequency of 493.88 can be obtained by computing \(pitch = base \times (2^{\frac{1}{12}})^{step} = 440 \times (2^{\frac{1}{12}})^{2} = 493.88\). One half-step below A4 is G#4, whose frequency of 392 is computed via \(pitch = base \times (2^{\frac{1}{12}})^{step} = 440 \times (2^{\frac{1}{12}})^{-1} = 415.3\). You can find a table of notes and frequencies here.

What to Do

Calculate the Frequency

Create a Python project and ask the user to input the base frequency and the number of half-steps you want to calculate. To get user input from the keyboard on-screen, you can use this code:

# First, print a prompt to the screen to ask the user for a value, and store what they type into a variable called base
base = input("Please enter the base frequency as a whole number (example: 440):")

# Since we know this should be a whole number, convert the variable's value to an integer
base = int(base)

Calculate the new frequency and print that value to the screen. Call your variable frequency. Verify that it is correct on the table of frequencies, and play the notes on the Online Tone Generator to hear what they would sound like when played on an instrument.

As a hint, the mathematical operators are: * to multiply, / to divide, + to add, - to subtract, and ** to raise a number to an exponent power.

Installing Required Software

In your terminal window (not in your code!), type this command and press enter, which will install some library software that we’ll use:

python3 -m pip install scipy pyaudio

Play the Note

Download this file which contains code to play a note on your computer given its frequency. You can save it into your project folder to use it. At the top of your program, add this line:

# import code into my program that I can use
import sounds

Now, you can use that code to play sounds! To play a tone, write this line at the bottom of your program:

sounds.sine_tone(frequency, 1, volume=0.25) # play the given frequency for 1 seconds and 25% volume

Re-run your program and try it for a few different notes. Verify that they’re correct using the Online Tone Generator.

As the frequency increases, what happens to the pitch of the sound?

Play a Song

Create a new Python file within your project. Using the sounds.sine_tone function, play several frequency notes to put together a song. You can make up your own song, or use the table of frequencies to play a known song. For example, here’s the “alphabet song” (based on Twinkle Twinkle Little Star):

C4 C4 G4 G4 A4 A4 G4(1/2 note)
F4 F4 E4 E4 D4(1/8th note) D4(1/8th note) D4(1/8th note) D4(1/8th note) C4(1/2 note)
G4 G4 F4 (rest 1/16th note) E4 E4 D4(1/2 note)
G4(1/8th note) G4(1/8th note) G4(1/8th note) F4 (rest 1/16th note) E4 E4 D4(1/2 note)
C4 C4 G4 G4 A4 A4 G4(1/2 note)
F4 F4 E4 E4 D4 D4 C4(1/2 note)

A 1/2 note just means to play at half the duration of your other notes. You can create a variable called duration and set it to 1/2, 1/8, or 1/16 as approrpiate. All the other notes are 1/4 notes.

For fun, you might notice there is a “rest” in the song. A rest means to pause. We can actually pause our program by importing another software package into our program called time (at the top):

import time

and calling time.sleep(1/16) to pause for one sixteenth of a second.

Mixing Sounds

A chord effectively mixes to sounds together - the sound waves are added together to create a mixed wave.

In a new Python file, call this function:

sounds.dtmf_tone(350, 440, 2, volume=0.25)

Note that we put two numbers into the function for the frequency instead of just one. This will cause them to mix together. We use brackets in Python to store variables that are collections of things (for example, multiple frequencies instead of a single frequency value). Run this program; does it sound familiar? Landline telephones use mixed tones (called dual-tones) to represent each button you could push on the phone.

Using these frequency combinations, try writing a program that plays the tones for your phone number. Interestingly, if you held your computer speaker up to an old telephone and ran this program, it would actually dial that number!

Digit Frequency 1 Frequency 2
1 697 1209
2 697 1336
3 697 1477
4 770 1209
5 770 1336
6 770 1477
7 852 1209
8 852 1336
9 852 1477
0 941 1336

If you looked at an audio sound graph like you might find on a stereo system, and saw the following output, what digit would you guess is being dialed on the phone?

DTMF Spectrogram

What to Turn In

When you’re done, write a README for your project, and save all your files, before exporting your project to ZIP. In your README, answer any bolded questions presented on this page. In addition, write a few paragraphs describing what you did, how you did it, and how to use your program. If your program requires the user to type something in, describe that here. If you wrote functions to help solve your problem, what are they, and what do they do? Imagine that you are giving your program to another student in the class, and you want to explain to them how to use it. What would you tell them? Imagine also that another student had given you the functions that you wrote for your program: what would you have wished that you knew about how to call those functions?

Note: For Mac Users

If you get an error while trying to install pyaudio using the pip command above, you can execute the following commands in a Terminal window:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
/opt/homebrew/brew install portaudio
CFLAGS="-I/opt/homebrew/include -L/opt/homebrew/lib" python3 -m pip install pyaudio

You may be prompted to enter your password and press enter once or twice by the first command - this is OK!

Submission

In your submission, please include answers to any questions asked on the assignment page in your README file. If you wrote code as part of this assignment, please describe your design, approach, and implementation in your README file as well. Finally, include answers to the following questions:
  • Describe what you did, how you did it, what challenges you encountered, and how you solved them.
  • Please answer any questions found throughout the narrative of this assignment.
  • If collaboration with a buddy was permitted, did you work with a buddy on this assignment? If so, who? If not, do you certify that this submission represents your own original work?
  • Please identify any and all portions of your submission that were not originally written by you (for example, code originally written by your buddy, or anything taken or adapted from a non-classroom resource). It is always OK to use your textbook and instructor notes; however, you are certifying that any portions not designated as coming from an outside person or source are your own original work.
  • Approximately how many hours it took you to finish this assignment (I will not judge you for this at all...I am simply using it to gauge if the assignments are too easy or hard)?
  • Your overall impression of the assignment. Did you love it, hate it, or were you neutral? One word answers are fine, but if you have any suggestions for the future let me know.
  • Using the grading specifications on this page, discuss briefly the grade you would give yourself and why. Discuss each item in the grading specification.
  • Any other concerns that you have. For instance, if you have a bug that you were unable to solve but you made progress, write that here. The more you articulate the problem the more partial credit you will receive (it is fine to leave this blank).

Assignment Rubric

Description Pre-Emerging (< 50%) Beginning (50%) Progressing (85%) Proficient (100%)
Algorithm Implementation (60%) The algorithm fails on the test inputs due to major issues, or the program fails to compile and/or run The algorithm fails on the test inputs due to one or more minor issues The algorithm is implemented to solve the problem correctly according to given test inputs, but would fail if executed in a general case due to a minor issue or omission in the algorithm design or implementation A reasonable algorithm is implemented to solve the problem which correctly solves the problem according to the given test inputs, and would be reasonably expected to solve the problem in the general case
Code Indentation and Spacing (10%) Code indentation and spacing are generally inappropriate or inconsistent Code indentation or spacing are generally appropriate but inconsistent in a few isolated instances Code indentation or spacing are appropriate or consistent, with minor adjustments needed Code indentation and spacing are appropriate and consistent
Code Quality (10%) Prior code quality feedback and style guide standards are not reflected in the submitted code to a great extent Code quality conforms to several standards in the course Style Guide, and progress is demonstrated in improving code quality from prior feedback Code quality conforms to the standards in the course Style Guide to a great extent, with a few identified areas of improvement Code quality substantially conforms to the standards in the course Style Guide
Code Documentation (10%) Code commenting and structure are absent, or code structure departs significantly from best practice Code commenting and structure is limited in ways that reduce the readability of the program; specifically, descriptive comments are present for some functions Code documentation is present that re-states the explicit code definitions Code is documented at non-trivial points in a manner that enhances the readability of the program; specifically, descriptive comments are present for all functions
Writeup and Submission (10%) An incomplete submission is provided, or the README file submitted is blank The program is submitted, but not according to the directions in one or more ways (for example, because it is lacking a readme writeup or missing answers to written questions) The program is submitted according to the directions with a minor omission or correction needed, including a readme writeup describing the solution and answering nearly all questions posed in the instructions The program is submitted according to the directions, including a readme writeup describing the solution and answering all questions posed in the instructions

Please refer to the Style Guide for code quality examples and guidelines.