Python Programing from Jupyter Notebook

How to Generate 440 Hz A(La) Note Sin Wave with 44.1 khz Sample Rate and 16-bit Depth? How to Play Composition with these Generated Note Sin Waves? A Standard 88-key Piano Review. Hapy Birthday DoReMi?

Let’s Revive an old Topic and Rediscover the Universal Language of Music with today’s Technology. Let’s open a small door for Audio Programming.

Photo by Techivation on Unsplash

Generated Hapy Birthday DoReMi in Python

Code Block:

Code Description:

  • Our Generate “HapyBirthday-DoReMi-MuCe.wav” Python Code consists of 10 steps:
  • The code produces stereo sound.
  • Sounds at different frequencies can be produced for the two channels.
  • Theoretically, All piano-based sounds can be produced with appropriate values.

step-1: We import python libraries to be used.

step-2: We define a function to create a Note. Resources related to the sine wave formula that we will use in our function are shared below. We define the variables in the formula inside our function.

# Signal Formula -data- y(t) = A*sin(2*π*f*t+ϕ) or f(t) = A*sin(ω*t+ϕ)
# signal basic settings — sound may vary by device type

— base_tone_freq = 440 # hertz (Hz) — 0.44kHz (Sound Frequency(Hz))-Note A4(La4)
— note_freq = base_freq * (2^(n/12))
— base_duration = .2 # (Basic Duration Unit in Seconds) or length = 0.2
— sampleRate = 44100 # Samples Per Second Hz — 44.1kHz (Sample Rate)-Other audio sample rates: 48 kHz, 88.2 kHz, 96 kHz, etc.
— samples = base_duration*sampleRate

Photo by Muhammed ÇELİK from Jupyter Notebook — Hapy-Birthday-DoReMi-Python-MuCe.wav

# Produces a t second Audio-File
— t = np.linspace(0, time, time*sampleRate) # timepoints
— A = np.iinfo(np.int16).max # Volume (Amplitude) — the volume measure (max is 32768)-(np.iinfo(np.int16).max)) or the Peak Value

The most common audio sample rate is 44.1 kHz, samples per second. This is not an arbitrary number. Humans can hear frequencies between 20 Hz and 20 kHz.

Most people lose their ability to hear upper frequencies over the course of their lives and can only hear frequencies up to 15 kHz–18 kHz. However, this “20-to-20” rule is still accepted as the standard range for everything we could hear.

The computer should be able to recreate waves with frequencies up to 20 kHz in order to preserve everything we can hear. Therefore, a sample rate of 40 kHz should technically do the trick.

step-3: We define Note letters and Frequencies via ‘ScaleFreqs.csv’ for Using. Links are available below.

step-4: We define samle composition sheet. You can write Notes you want in this field, there should be time next to it.

Syntax for two notes: '''Note-time(Space)Note-time'''

step-5: We generate all tone from defined composition

step-6: We make note data into a single array

step-7: We make single array into double for stereo sound

step-8: We make double array available for registration

step-9: We Save the sound file:“Hapy-Birthday-DoReMi-Python-MuCe.wav” to the Local Folder -working folder-.

step-10 Analyze tones_wave_stereo features

Photo by Muhammed ÇELİK from Jupyter Notebook — Hapy-Birthday-DoReMi-Python-MuCe.wav

What Are Some Technical Information and Technical Terms?

1–Defining Music Notes[¹]
2–Defining Music Frequency[²]
3–Defining Sine Signal Formula[³]
4–Defining Python Programing Language
5–Defining Jupyter Notebook

1–Defining Music Notes[¹]

Why does Western music divide the octave into 12 different notes? Why not 13, or 19 or 24 notes? For such a simple sounding question, the answer is actually a tangle of history, physics and human preference. Get ready for some serious music theory! — thanks for explanation David Bennett Piano

2–Defining Music Frequency[²]

— The file ‘ScaleFreqs.csv’ has been modified for general use. The official link is below.

Official Link ScaleFreqs.xls (
Frequencies of Musical Notes — Equal Temp. vs Meantone (
Formula for frequency table (

Pythagorean Scale

Around 500 BC Pythagoras studied the musical scale and the ratios between the lengths of vibrating strings needed to produce them. Since the string length (for equal tension) depends on 1/frequency, those ratios also provide a relationship between the frequencies of the notes. He developed what may be the first completely mathematically based scale which resulted by considering intervals of the octave (a factor of 2 in frequency) and intervals of fifths (a factor of 3/2 in frequency). The procedure is described in the book by Jeans. The resulting scale divides the octave with intervals of “Tones” (a ratio of 9/8) and “Hemitones” (a ratio of 256/243). Here is a table for a C scale based on this scheme.

base_freq= 440,00
note_freq = base_freq * (2^(n/12))

— La3 to La4

— A3 220,00 — A#3 233,08 — B3 246,94 — C4 261,63 — C#4 277,18
— D4 293,66 — D#4 311,13 — E4 329,63 — F4 349,23 — F#4 369,99
— G4 392,00 — G#4 415,30 — A4 440,00

Equations for the Frequency Table

The basic formula for the frequencies of the notes of the equal tempered scale is given by
fn = f0 * (a)n
f0 = the frequency of one fixed note which must be defined. A common choice is setting the A above middle C (A4) at f0 = 440 Hz.
n = the number of half steps away from the fixed note you are. If you are at a higher note, n is positive. If you are on a lower note, n is negative.
fn = the frequency of the note n half steps away.
a = (2)1/12 = the twelth root of 2 = the number which when multiplied by itself 12 times equals 2 = 1.059463094359…

The wavelength of the sound for the notes is found from
Wn = c/fn
where W is the wavelength and c is the speed of sound. The speed of sound depends on temperature, but is approximately 345 m/s at “room temperature.”

Examples using A4 = 440 Hz:

C5 = the C an octave above middle C. This is 3 half steps above A4 and so the frequency is
f3 = 440 * (1.059463..)3 = 523.3 Hz
If your calculator does not have the ability to raise to powers, then use the fact that
(1.059463..)3 = (1.059463..)*(1.059463..)*(1.059463..)
That is, you multiply it by itself 3 times.

Middle C is 9 half steps below A4 and the frequency is:
f -9 = 440 * (1.059463..)-9 = 261.6 Hz
If you don’t have powers on your calculator, remember that the negative sign on the power means you divide instead of multiply. For this example, you divide by (1.059463..) 9 times.

This calculation is easily accomplished using a Spread Sheet (excel spreadsheet).
If your browswer is blocking xls downloads, use this version and replace the “.txt” extension with “.xls” before using.

Questions/Comments to:

3–Defining Sine Signal Formula[³]

Its most basic form as a function of time (t) is:

y(t) = A*sin(2*π*f*t+φ) = A*sin(ω*t+φ)
— A, amplitude, the peak deviation of the function from zero.
— f, ordinary frequency, the number of oscillations (cycles) that occur each second of time.
— ω = 2πf, angular frequency, the rate of change of the function argument in units of radians per second
— φ , phase, specifies (in radians) where in its cycle the oscillation is at t = 0.

When φ is non-zero, the entire waveform appears to be shifted in time by the amount φ/ω seconds. A negative value represents a delay, and a positive value represents an advance.

4–Defining Python Programing Language
5–Defining Jupyter Notebook

  • For explanations on this topic, you can check their official sites.
  • For official links, you can refer to my medium article in the link below.

Useful Links & Resources:

1–Defining Music Notes[¹]
2–Defining Music Frequency[²]
3–Defining Sine Signal Formula[³]

Resources for A Primer on Scientific Programming with Python (Page:690 Title:A.2 Programming with Sound), but I couldn’t run the code there, you can check stack overflow for more. —

Have a nice Day, Folks.

I just started writing in Medium. if possible, can you follow my medium account.

thanks for support & giving time.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store