# Solution to I590/N560 tuneR assignment #1 of 15 Sep. 2006 - OS X version - Don Byrd # Load the tuneR package. (This assumes that the package is already installed. If it's # not, type "install.packages()" and choose tuneR from the list of packages displayed.) # Warning: in tuneR, 16-bit sample values beyond [-32767,32768] cause errors in later # processing. In contrast, with the earlier "sound" package, those values are simply clipped. library(tuneR) # With OS X, must setWavPlayer() before giving the "play" command -- but to what? QuickTime # Player works, but gives error messages, makes you hit the escape key to continue, and leaves # a new copy of the QuickTime Player open each time you use it. There must be a better way! setWavPlayer("/Applications/'QuickTime Player.app'/Contents/MacOS/'QuickTime Player'") # OS X: works, but...?? # setWavPlayer("C:/Progra~1/QuickTime/QuickTimePlayer.exe") # Windows: unnecessary but it works setwd("Documents/Music+AudioIR+DLs/IowaMusInstSamples") # OS X (Don's G4; use YOUR path!) # setwd("Desktop/TODAY") # OS X (M373 Mac) # setwd("Music/Teaching-N560Demo/RProgramming") # Windows (Don's PC) # Get the audio file. (This note, recorded in stereo, is from http://theremin.music.uiowa.edu/MIS.html .) wav = readWave("Piano.mf1st5sec.A4.wav") # wav is an object of type Wave plot(wav, nr=1000) # another way to display a waveform: gives an overview of whole sound play(wav) # Convert to mono, and make copies of a few things for later use. wavmono = mono(wav, "both") # convert stereo to mono by averaging the channels sampdata = wavmono@left # get the samples (the right channel is empty for mono sounds) sampleRate = wav@samp.rate bitsPerSample = wav@bit len = length(sampdata) # Problem 1. Multiply all samples by -1, i.e., change phase 180 degrees: should have no audible effect. sampdataNeg = sampdata*(-1) # wav@left[9001:9020] # sampdata[9001:9020] # sampdataNeg[9001:9020] plot(sampdataNeg[0:sampleRate]) wav1 = Wave(left=sampdataNeg, samp.rate=sampleRate, bit=bitsPerSample); # make a Wave object from sample data play(wav1) # Problem 2. Make a sound consisting of five copies of the first 100 millisec. sampdata100ms = sampdata[0:round(sampleRate*0.1)] sampdata2 = c(sampdata100ms, sampdata100ms, sampdata100ms, sampdata100ms, sampdata100ms) plot(sampdata2) wav2 = Wave(left=sampdata2, samp.rate=sampleRate, bit=bitsPerSample); # make a Wave object from sample data play(wav2) # Problem 3. Make a sound consisting of every third sample of the original. sampdata3 = sampdata[seq(3, len, by=3)] plot(sampdata3) wav3 = Wave(left=sampdata3, samp.rate=sampleRate, bit=bitsPerSample); # make a Wave object from sample data play(wav3) # Problem 4. Make two copies of the sample file overlap with a 10 millisec. offset padding = seq(from=0, to=0, length.out=0.01*sampleRate) # padding = seq(from=0, to=0, length.out=0.05*sampleRate) # a more interesting effect sampdataThenPadding = c(sampdata, padding) paddingThenSampdata = c(padding, sampdata) sampdata4 = sampdataThenPadding+paddingThenSampdata plot(sampdata4[0:sampleRate]) wav4 = Wave(left=sampdata4, samp.rate=sampleRate, bit=bitsPerSample); # make a Wave object from sample data play(wav4)