# We generate continuous audio by choosing a single frame (column) in the
# STFT and replicating it over and over. The resulting audio doesn't
# sound like what we'd like.
stft = function(y,H,N) {
v = seq(from=0,by=2*pi/N,length=N)
win = (1 + cos(v-pi))/2
cols = floor((length(y)-N)/H) + 1
stft = matrix(0,N,cols)
for (t in 1:cols) {
range = (1+(t-1)*H): ((t-1)*H + N)
chunk = y[range]
stft[,t] = fft(chunk*win)
}
stft
}
istft = function(Y,H,N) {
v = seq(from=0,by=2*pi/N,length=N)
win = (1 + cos(v-pi))/2
y = rep(0,N + H*ncol(Y))
for (t in 1:ncol(Y)) {
chunk = fft(Y[,t],inverse=T)/N
range = (1+(t-1)*H): ((t-1)*H + N)
y[range] = y[range] + win*Re(chunk)
}
y
}
library(tuneR)
N = 1024
H = N/4
bits = 16
w = readWave("sound/octaves.wav")
sr = w@samp.rate
y = w@left
Y = stft(y,H,N)
Ybar = matrix(0,nrow(Y),ncol(Y))
for (i in 1:ncol(Y)) Ybar[,i] = Y[,50]; # replicate the 50th frame
ybar = istft(Ybar,H,N)
u = Wave(round(ybar), samp.rate = sr, bit=bits) # make wave struct
play(u,"play")