#using synthesized oboe tone, demonstrate that random changes in phase don't
#produce a difference in timbre.
library(tuneR)
N = 512 # number of pts we examine -- FFT works best with powers of 2
t = seq(from=0,by=2*pi/N,length=N) # N evenly spaced pts 0 -- 2*pi
win = (1 + cos(t-pi))/2 # the "raised cosine" or "Hanning" window
midi = 58 # Bb below middle c
hz = 440*2^((midi-69)/12) # frequency in Hz of this pitch
w = readWave("sound/octaves.wav") # take actual sound data from file
#play(w,"play")
sr = w@samp.rate # grab sampling rate from Wave struct
# (try str(w) to see parts of Wave struct)
bin0 = hz*N/sr # fft "bin" assoc with freq hz (Y[bin0-1] in R)
harms = 16 # will compute amplitudes of this many harmonics
start = 4000 # the Bb starts around this sample
y = w@left # grab the left channel (or only channel if mono)
y = win*y[start:(start+N-1)] # just analyze short segment of audio
Y = fft(y) # take the fft
#plot(Mod(Y[1:(N/2)]),type='l') # fft of nearly periodic sound has evenly spaced peaks
amp = rep(0,harms) # store amplitudes here
for (h in 1:harms) { # for each harmonic take the maximum amplitude in neighborhood of peak
lo = floor(h*bin0-3)
hi = ceiling(h*bin0+3)
amp[h] = max(Mod(Y[lo:hi]))
}
amp = amp/max(amp) # sale the amplitudes to have max value 1
# Now synthesize an oboe-like sound from this "harmonic profile"
t = seq(0,3,by=1/sr);
y = rep(0,length(t))
for (h in 1:harms) { # sum sine waves at harmonics with amplitude estimated from data
y = y + amp[h]*sin(2*pi*h*hz*t + rnorm(1)) # note random phase
}
u = Wave(round(2^(bits-5)*y), samp.rate = sr, bit=bits) # make wave struct
plot(y[1:100],type='l')
play(u,"play") # sounds like oboe --- sort of