Skip to content

Instantly share code, notes, and snippets.

@ArcaneNibble
Created April 15, 2025 00:53
Show Gist options
  • Save ArcaneNibble/54b1754886352f321e7022cfecaa04c4 to your computer and use it in GitHub Desktop.
Save ArcaneNibble/54b1754886352f321e7022cfecaa04c4 to your computer and use it in GitHub Desktop.
# https://deutsch.ucsd.edu/psychology/pages.php?i=201
import numpy as np
import scipy.io.wavfile
import scipy.special
f_s = 44100
duration = 10
note1 = 392
note2 = 784
dt = 1 / f_s
samples = []
phase = 0
# note1 ramp note2 ramp
# ^ 0.25 sec
# ^ 0.5 sec
interp_len = 0.010
def get_freq(t):
tt = t % 0.5
# errf
if tt < 0.25 - interp_len:
return note1
elif tt < 0.25:
relt = tt - (0.25 - interp_len)
xx = relt / interp_len * 6 - 3
yy = 0.5 - scipy.special.erf(xx) / 2
return note1 * yy + note2 * (1 - yy)
elif tt < 0.5 - interp_len:
return note2
else:
relt = tt - (0.5 - interp_len)
xx = relt / interp_len * 6 - 3
yy = 0.5 - scipy.special.erf(xx) / 2
return note2 * yy + note1 * (1 - yy)
# linear
# if tt < 0.25 - interp_len:
# return note1
# elif tt < 0.25:
# return note1 + (note2-note1)*((tt-(0.25 - interp_len))/interp_len)
# elif tt < 0.5 - interp_len:
# return note2
# else:
# return note2 + (note1-note2)*((tt-(0.5 - interp_len))/interp_len)
for x in range(duration * f_s):
t = x * dt
freq = get_freq(t)
y = 16383 * np.cos(phase)
samples.append(y)
phase += 2*np.pi * freq * dt
# xxx lol lmao
samples2 = []
for x in range(duration * f_s):
t2 = int(x + 0.25*f_s) % (f_s * duration)
samples2.append((samples[x], samples[t2]))
scipy.io.wavfile.write('test.wav', f_s, np.int16(samples2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment