Why are you making it more complicated than it needs to be? Just use a chart and skip the shift register, the insert into array and especially the silly i+0.
You can set the loop duration to number of samples or time. The Elapsed Time function is basic.