# create a random period function having frequency f by add
# sine waves with random amplitude and phase at freqs
# f,2f,3f etc.
# plot a period as well as listening to sound.
library(tuneR) # need to like with the sound library
f = 440 # our base frequency
sr = 32000 # sampling rate
bits = 16 # bit depth
secs = 2. # length of each note
harms = 10 # number of sine waves
period = round(sr/f) # length of period in samples
pers = 1 # plot this many periods
t = seq(0,secs,1/sr) # the time points we create samples for
y = rep(0,length(t)) # accumulate result here
for (n in 1:harms) {
amp = runif(1);
phase = runif(1);
y = y + amp * sin(2*pi*n*f*t + phase)
}
plot(y[1:(pers*period)],type='l') # plot with line
u = Wave(round(2^(bits-5)*y), samp.rate = sr, bit=16) # make wave struct
play(u,"play") # play it