Skip to content

Instantly share code, notes, and snippets.

@jxramos
Created February 13, 2025 20:12
Show Gist options
  • Save jxramos/4cc0477e53af7fb4ffc0c51bfadfa73e to your computer and use it in GitHub Desktop.
Save jxramos/4cc0477e53af7fb4ffc0c51bfadfa73e to your computer and use it in GitHub Desktop.
Visible to audible calculations
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]
@jxramos
Copy link
Author

jxramos commented Feb 13, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment