#----------------------------------------------------- # Use of seewave to produce a gated sinusoidal wave # # GdA 21/5/2016 #----------------------------------------------------- library(seewave) sinusoidal <- function(nu=440, t.max=1/nu, sampling=44100) { sin(2*pi*nu*seq(0, t.max, length.out=t.max*sampling)) } nu <- 440 # Hz t.max <- 10 # seconds sampling <- 192000 # Hz (samples/s) (41000, 96000, 192000) Ampl <- 0.2 # relative amplitude n.off <- 10 # nr of periods OFF n.on <- 10 # nr of periods oscillating (ON) wave.sin <- Ampl*sinusoidal(nu, sampling=sampling) # 1 period npw <- length(wave.sin) # nr of points wave.off <- rep(0, npw) # zero (OFF) for 1T T <- 1/nu # period n.periods <- t.max/T # nr of periods in t.max n.cycles <- t.max / ( (n.off + n.on) * T) # nr of OFF/ON periods # build the gated wave wave <- rep( c( rep(wave.off, n.off), rep(wave.sin[1:(npw-1)], n.on) # (*) ), n.cycles) # (*) overlapping zero's are removed [ "1:(npw-1)" ] # uncomment to inspect the resulting wave # plot(wave[1:20000], ty='l') # save wave as wav file savewav(wave, f=sampling) # play it (or comment the line to play from console) system("play wave.wav")