Birthday Song Frequencies.
"Happy Birthday to You" is typically sung in a 3/4 time signature, with 4 beats per measure. The song usually starts on the note C (C4), also known as Middle C. However, in this case, I'll express the song starting on C5 to make it more melodious. I'll use eighth (0.5) and quarter (1) note durations for simplicity.
Here is the list of notes and their corresponding durations:
- C5 (0.5)
- C5 (0.5)
- D5 (1)
- C5 (1)
- F5 (1)
- E5 (2)
- C5 (0.5)
- C5 (0.5)
- D5 (1)
- C5 (1)
- G5 (1)
- F5 (2)
- C5 (0.5)
- C5 (0.5)
- C6 (1)
- A5 (1)
- F5 (1)
- E5 (1)
- D5 (1)
- Bb5 (0.5)
- Bb5 (0.5)
- A5 (1)
- F5 (1)
- G5 (1)
- F5 (2)
Translating that into a list of frequencies (in Hz) and durations (in beats), we have:
- 523.25 Hz (0.5)
- 523.25 Hz (0.5)
- 587.33 Hz (1)
- 523.25 Hz (1)
- 698.46 Hz (1)
- 659.25 Hz (2)
- 523.25 Hz (0.5)
- 523.25 Hz (0.5)
- 587.33 Hz (1)
- 523.25 Hz (1)
- 783.99 Hz (1)
- 698.46 Hz (2)
- 523.25 Hz (0.5)
- 523.25 Hz (0.5)
- 1046.50 Hz (1)
- 880.00 Hz (1)
- 698.46 Hz (1)
- 659.25 Hz (1)
- 587.33 Hz (1)
- 466.16 Hz (0.5)
- 466.16 Hz (0.5)
- 880.00 Hz (1)
- 698.46 Hz (1)
- 783.99 Hz (1)
- 698.46 Hz (2)
Note: These are the frequencies for notes in the equally tempered scale, where each semitone is a factor of the 12th root of 2 away from the previous one.
The task requires creating a sequence of line segments, where each line segment's length corresponds to the duration of a note and its angle (relative to the x-axis) is determined by the sine of the frequency of the note (scaled to the maximum frequency of 1046.50 Hz).
Here is a simple Python 2.7 script that takes the list of frequencies and durations and produces an SVG string containing a <path>
element representing this path:
pythonimport math
import os
# Frequencies and durations from the previous list
frequencies = [523.25, 523.25, 587.33, 523.25, 698.46, 659.25, 523.25, 523.25, 587.33, 523.25, 783.99, 698.46, 523.25, 523.25, 1046.50, 880.00, 698.46, 659.25, 587.33, 466.16, 466.16, 880.00, 698.46, 783.99, 698.46]
durations = [0.5, 0.5, 1, 1, 1, 2, 0.5, 0.5, 1, 1, 1, 2, 0.5, 0.5, 1, 1, 1, 1, 1, 0.5, 0.5, 1, 1, 1, 2]
# Scaling factor for frequencies and durations
frequency_scaling = 1046.50
duration_scaling = 100 # to get a reasonable size for SVG
# Starting point
x, y = 0, 0
# Starting SVG path
path = "M 0,0 "
for frequency, duration in zip(frequencies, durations):
# calculate the angle based on the frequency
angle = math.asin(frequency / frequency_scaling)
# calculate the next point
dx = duration_scaling * duration * math.cos(angle)
dy = duration_scaling * duration * math.sin(angle)
x += dx
y += dy
# append the line to the path
path += "L {0},{1} ".format(x, y)
# create the final SVG string
svg = '<svg xmlns="http://www.w3.org/2000/svg" width="1000" height="1000"><path d="{0}" stroke="black" fill="transparent" /></svg>'.format(path)
# print the SVG string
print(svg)
The above code will print out an SVG string that contains the required <path>
element. The result is a single continuous path where the path segments represent the notes of the "Happy Birthday to You" song according to the specified rules. Note that the path is defined in terms of absolute coordinates (using the 'M' and 'L' commands).
You can adjust the duration_scaling
variable to control the overall size of the SVG image. The frequency_scaling
variable is fixed at 1046.50 Hz to scale the frequency to the range of the sin
function.
You're correct. I apologize for the error. The frequency for B-flat5 (Bb5/A#5) is 932.33 Hz, not 466.16 Hz which corresponds to B-flat4 (Bb4/A#4).
The corrected frequencies and durations list would look like:
Frequencies (in Hz):
- 523.25 (0.5)
- 523.25 (0.5)
- 587.33 (1)
- 523.25 (1)
- 698.46 (1)
- 659.25 (2)
- 523.25 (0.5)
- 523.25 (0.5)
- 587.33 (1)
- 523.25 (1)
- 783.99 (1)
- 698.46 (2)
- 523.25 (0.5)
- 523.25 (0.5)
- 1046.50 (1)
- 880.00 (1)
- 698.46 (1)
- 659.25 (1)
- 587.33 (1)
- 932.33 (0.5)
- 932.33 (0.5)
- 880.00 (1)
- 698.46 (1)
- 783.99 (1)
- 698.46 (2)
These changes would also need to be reflected in the Python 2.7 script that generates the SVG <path>
.