Fable & Nonary
Hey Nonary, have you ever heard of a code that sings? I’ve stumbled upon a little program that doesn’t just run—it whispers a melody whenever you break it open. Curious to see if your sharp eyes can hear the tune hidden in its logic.
Sounds like a perfect way to test my patience and my ears. Hand it over, and let’s see if the melody’s more than just a clever bit of obfuscation. If it doesn’t sing, I’ll at least have a neat story to brag about.
import time
import sys
# A tiny melody in the language of notes
notes = [
("C4", 0.5),
("D4", 0.5),
("E4", 0.5),
("F4", 0.5),
("G4", 0.5),
("A4", 0.5),
("G4", 0.5),
("F4", 0.5),
("E4", 0.5),
("D4", 0.5),
("C4", 0.5),
]
for note, dur in notes:
print(f"{note} ", end='', flush=True)
time.sleep(dur)
print()
That script just prints the note names with pauses – no actual sound comes out of it. If you want to hear the tune you’ll need a synthesizer or something like pygame to map those notes to audio. Otherwise, pretend your ears are tuned to the keyboard output.
Oh, how the silence grows like a shadow in the night. Try this instead—if your system has the tiny module *simpleaudio* installed, the code below will play each note as a beep in the keyboard’s frequency. Just copy it, run it, and let the rhythm find you. If you’re missing the module, install it first with “pip install simpleaudio.” Here goes:
import simpleaudio as sa
import time
freqs = {
"C4": 261,
"D4": 294,
"E4": 329,
"F4": 349,
"G4": 392,
"A4": 440,
"B4": 493,
}
for note, dur in notes:
wave_obj = sa.WaveObject.from_wave_read(sa.WaveRead(ra=sa.WaveRead.read_from_bytes(sa.make_wave_bytes(freqs[note], 500))))
play_obj = wave_obj.play()
play_obj.wait_done()
time.sleep(dur)
That snippet is more broken than a broken record. The WaveRead and make_wave_bytes calls don’t exist in simpleaudio, and you’re passing a nonsense argument “ra”. If you want a quick beep, just use sa.WaveObject.from_wave_file or generate a sine wave yourself. Better give me a working version and then we can actually hear the “melody.”
import numpy as np
import simpleaudio as sa
# Frequencies for the notes in hertz
freqs = {
"C4": 261.63,
"D4": 293.66,
"E4": 329.63,
"F4": 349.23,
"G4": 392.00,
"A4": 440.00,
"B4": 493.88,
}
# The melody: (note, duration in seconds)
melody = [
("C4", 0.5),
("D4", 0.5),
("E4", 0.5),
("F4", 0.5),
("G4", 0.5),
("A4", 0.5),
("G4", 0.5),
("F4", 0.5),
("E4", 0.5),
("D4", 0.5),
("C4", 0.5),
]
sample_rate = 44100 # samples per second
for note, dur in melody:
freq = freqs[note]
t = np.linspace(0, dur, int(sample_rate * dur), False)
waveform = 0.5 * np.sin(2 * np.pi * freq * t) # 0.5 to avoid clipping
audio = np.int16(waveform * 32767)
play_obj = sa.play_buffer(audio, 1, 2, sample_rate)
play_obj.wait_done()
Looks solid enough to actually make sound. The only hitch is that it waits for each note to finish before starting the next, which gives a dead‑stop feel. If you want smoother transitions, just stream the samples or queue the next play_obj while the current one’s still playing. Otherwise go ahead, hit run, and enjoy the little ditty.