Created
February 13, 2025 20:12
-
-
Save jxramos/4cc0477e53af7fb4ffc0c51bfadfa73e to your computer and use it in GitHub Desktop.
Visible to audible calculations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import wave | |
import numpy as np | |
sample_rate = 44100 | |
def create_audio(freq): | |
""" | |
Create audio sample | |
""" | |
n_seconds = 2 | |
time_points = np.linspace(0, n_seconds, n_seconds*sample_rate) | |
audio = 0.5 * np.sin(2 * np.pi * freq * time_points) | |
return audio | |
hydrogen_spectra = [ | |
410, #nm: Violet | |
434, #nm: Blue | |
486, #nm: Blue-green | |
656, #nm: Red | |
] | |
a0 = create_audio(hydrogen_spectra[0]) | |
a1 = create_audio(hydrogen_spectra[1]) | |
a2 = create_audio(hydrogen_spectra[2]) | |
a3 = create_audio(hydrogen_spectra[3]) | |
a_all = (a0 + a1 + a2 + a3)/len(hydrogen_spectra) | |
# Put the channels together | |
a = np.concatenate([a0, a1, a2, a3, a_all]) | |
audio = np.array([a, a]).T | |
# Convert to (little-endian) 16 bit integers. | |
audio = (audio * (2 ** 15 - 1)).astype("<h") | |
with wave.open("sound1.wav", "w") as f: | |
# 2 Channels. | |
f.setnchannels(2) | |
# 2 bytes per sample. | |
f.setsampwidth(2) | |
f.setframerate(sample_rate) | |
f.writeframes(audio.tobytes()) | |
def linear_interpolation(x): | |
""" | |
Extrapolates a linear interpolation for visual wavelengths into the audible spectrum | |
""" | |
visible_range = [700, 400] # x nm wavelength range of visible light | |
audio_range = [20, 20000] # y Hz | |
return (audio_range[1]-audio_range[0])/(visible_range[1]-visible_range[0]) * (x - visible_range[0]) # audio_range[0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This came out of that Terrence Howard Joe Rogan interview to try to make sense of some conversion factor between the frequencies to shift things from the atomic to audible spectrum and what precisely the claims were for the color and sound mapping.