R : Copyright 2005, The R Foundation for Statistical Computing Version 2.1.0 (2005-04-18), ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for a HTML browser interface to help. Type 'q()' to quit R. > ### *
> ### > attach(NULL, name = "CheckExEnv") > assign(".CheckExEnv", as.environment(2), pos = length(search())) # base > ## add some hooks to label plot pages for base and grid graphics > setHook("plot.new", ".newplot.hook") > setHook("persp", ".newplot.hook") > setHook("grid.newpage", ".gridplot.hook") > > assign("cleanEx", + function(env = .GlobalEnv) { + rm(list = ls(envir = env, all.names = TRUE), envir = env) + RNGkind("default", "default") + set.seed(1) + options(warn = 1) + delayedAssign("T", stop("T used instead of TRUE"), + assign.env = .CheckExEnv) + delayedAssign("F", stop("F used instead of FALSE"), + assign.env = .CheckExEnv) + sch <- search() + newitems <- sch[! sch %in% .oldSearch] + for(item in rev(newitems)) + eval(substitute(detach(item), list(item=item))) + missitems <- .oldSearch[! .oldSearch %in% sch] + if(length(missitems)) + warning("items ", paste(missitems, collapse=", "), + " have been removed from the search path") + }, + env = .CheckExEnv) > assign("..nameEx", "__{must remake R-ex/*.R}__", env = .CheckExEnv) # for now > assign("ptime", proc.time(), env = .CheckExEnv) > grDevices::postscript("sound-Examples.ps") > assign("par.postscript", graphics::par(no.readonly = TRUE), env = .CheckExEnv) > options(contrasts = c(unordered = "contr.treatment", ordered = "contr.poly")) > options(warn = 1) > library('sound') Warning in findWavPlayer() : No standard wav player known for your system. Warning in setWavPlayer() : To play sounds you need to select a wav file player first. For more information type '?setWavPlayer'. > > assign(".oldSearch", search(), env = .CheckExEnv) > assign(".oldNS", loadedNamespaces(), env = .CheckExEnv) > cleanEx(); ..nameEx <- "Ops.Sample" > > ### * Ops.Sample > > flush(stderr()); flush(stdout()) > > ### Name: Ops.Sample > ### Title: Basic Operations for Sample objects > ### Aliases: Ops.Sample sum.Sample prod.Sample > ### Keywords: manip > > ### ** Examples > ## Not run: s1 <- Sine(440,1) > ##D s2 <- Sine(220,1) > ##D play((s1+s2)/2) # both samples at a time > ##D > ##D play(Sine(440,1)*Sine(5,1)) # vibrato > ## End(Not run) > > > > cleanEx(); ..nameEx <- "Sample" > > ### * Sample > > flush(stderr()); flush(stdout()) > > ### Name: Sample > ### Title: Sample Objects > ### Aliases: Sample as.Sample is.Sample > ### Keywords: classes > > ### ** Examples > ## Not run: waveLeft <- 2*((seq(0,80,length=88200)%%1^2)-.5) > ##D s <- as.Sample(waveLeft,44100,16) > ##D play(s) # a mono sample > ##D > ##D waveRight <- waveLeft[88200:1] > ##D s <- as.Sample(rbind(waveLeft,waveRight),44100,16) > ##D play(s) # a stereo Sample > ##D > ##D # How to use is.Sample to allow both a Sample object and a filename > ##D # as an argument: > ##D s <- anyargument > ##D sampletest <- is.Sample(s, argname="s ") > ##D if (!sampletest$test) stop(sampletest$error) #no valid argument > ##D t <- loadSample(s,filecheck=FALSE) > ##D # If s is Sample object, loadSample will return it immediately. > ##D # If s is a string, the Sample object will be loaded from disk. > ##D # No check for existance of the file will be performed since this > ##D # was already tested in is.Sample. > ##D # > ##D # Now s is a Sample object, continue with routine. > ## End(Not run) > > > > cleanEx(); ..nameEx <- "Sine" > > ### * Sine > > flush(stderr()); flush(stdout()) > > ### Name: Sine > ### Title: Create Sample Objects for the Basic waveforms > ### Aliases: Sine Sawtooth Square Silence Noise > ### Keywords: sysdata > > ### ** Examples > ## Not run: s1 <- Sine(440,1) > ##D play(s1) > ##D > ##D s2 <- Sawtooth(440,1) > ##D play(s2) > ##D > ##D play(Noise(1)) > ## End(Not run) > > > > cleanEx(); ..nameEx <- "WavPlayer" > > ### * WavPlayer > > flush(stderr()); flush(stdout()) > > ### Name: WavPlayer > ### Title: Set or Get the Command for Playing WAV Files > ### Aliases: WavPlayer findWavPlayer setWavPlayer > ### Keywords: sysdata > > ### ** Examples > ## Not run: > ##D setWavPlayer("playwave") > ##D # tries to set the command "playwave wavfile.wav" as the > ##D # preference for playing wav files with the play command. > ##D # If successful, > ##D WavPlayer() > ##D # returns the string "playwave" afterwards. > ## End(Not run) > > > > cleanEx(); ..nameEx <- "appendSample" > > ### * appendSample > > flush(stderr()); flush(stdout()) > > ### Name: appendSample > ### Title: Append Samples > ### Aliases: appendSample > ### Keywords: manip > > ### ** Examples > ## Not run: > ##D s1 <- Sine(440,1) > ##D s2 <- Sine(550,1) > ##D s3 <- Sine(660,1) > ##D s4 <- Sine(880,1) > ##D play(appendSample(s1,s2,s3,s4)) > ## End(Not run) > > > > cleanEx(); ..nameEx <- "bits" > > ### * bits > > flush(stderr()); flush(stdout()) > > ### Name: bits > ### Title: Bits per Sample > ### Aliases: bits bits<- setBits > ### Keywords: attribute > > ### ** Examples > ## Not run: > ##D s <- Sine(20000,1,rate=44100,bits=16) > ##D play(s) > ##D print(s) > ##D bits(s) <- 8 > ##D play(s) # now worse quality > ##D print(s) # but less disk space > ##D play(setBits(s,16)) # now better quality again, since waveform data was not changed. > ## End(Not run) > > > > cleanEx(); ..nameEx <- "center" > > ### * center > > flush(stderr()); flush(stdout()) > > ### Name: center > ### Title: Center a Sample Object. > ### Aliases: center > ### Keywords: manip > > ### ** Examples > ## Not run: > ##D x <- seq(0,50*pi,length=10000) > ##D waveform <- (sin(x))^2 + .6*cos(x/2)^2 > ##D s <- as.Sample(waveform,44100,16) > ##D plot(s) # nice idea, but wrong range for a sample > ##D play(s) # sounds ugly, too > ##D s <- center(s) > ##D plot(s) # now zero is the mean > ##D play(s) # sounds good, but too quiet > ##D s <- normalize(s) > ##D plot(s) # this looks like a perfect sample! > ##D play(s) # e voila! > ## End(Not run) > > > > cleanEx(); ..nameEx <- "channels" > > ### * channels > > flush(stderr()); flush(stdout()) > > ### Name: channels > ### Title: Number of Channels of a Sample Object > ### Aliases: channels channels<- setChannels > ### Keywords: attribute > > ### ** Examples > ## Not run: > ##D s <- stereo(Sine(440,1),Sine(220,1)) > ##D channels(s) # 2 > ##D play(s) > ##D channels(s) <- 1 # now a mono sample > ##D play(s) > ## End(Not run) > > > > cleanEx(); ..nameEx <- "cutSample" > > ### * cutSample > > flush(stderr()); flush(stdout()) > > ### Name: cutSample > ### Title: Cut Sample Objects > ### Aliases: cutSample "[.Sample" > ### Keywords: manip > > ### ** Examples > ## Not run: > ##D s <- appendSample(Sine(330,1),Sine(440,1)) > ##D play(cutSample(s,.8,1.8)) > ##D play(s[(44100*.8):(44100*1.8)]) # the same > ## End(Not run) > > > > cleanEx(); ..nameEx <- "cutSampleEnds" > > ### * cutSampleEnds > > flush(stderr()); flush(stdout()) > > ### Name: cutSampleEnds > ### Title: Prepare Sample Object for appendSample > ### Aliases: cutSampleEnds > ### Keywords: manip > > ### ** Examples > ## Not run: s1 <- Sine(440,.01) > ##D s2 <- Sine(550,.01) > ##D s3 <- Sine(660,.01) > ##D s4 <- Sine(880,.01) > ##D l <- list(s1,s2,s3,s4) > ##D # first without cutSampleEnds: > ##D s <- nullSample() > ##D for (i in 1:99) { > ##D s <- appendSample(s,l[[i%%4+1]]) > ##D } > ##D play(s) # ugly cracks > ##D # now with cutSampleEnds: > ##D s <- nullSample() > ##D for (i in 1:99) { > ##D s <- appendSample(s,cutSampleEnds(l[[i%%4+1]])) > ##D } > ##D play(s) # no cracks, > ##D > ##D # This is how it works: > ##D # The waveform is not smooth between s1 and s2: > ##D plot(appendSample(s1,s2)) > ##D # This is because s1 just ends somewhere at y=0.6: > ##D plot(s1) > ##D # Let's cut off the last positive part of it: > ##D plot(cutSampleEnds(s1)) > ##D # A similar cuttoff would be made at the beginning > ##D # of the sample (if it was necessary). > ##D # Now the two samples fit perfectly (the cut is at x=400): > ##D plot(appendSample(cutSampleEnds(s1),cutSampleEnds(s2))) > ## End(Not run) > > > > cleanEx(); ..nameEx <- "duration" > > ### * duration > > flush(stderr()); flush(stdout()) > > ### Name: duration > ### Title: Duration of a Sample Object > ### Aliases: duration duration<- setDuration > ### Keywords: attribute > > ### ** Examples > ## Not run: > ##D s <- Sine(440,3) > ##D duration(s) # 3 > ##D duration(s) <-.5 # sample is now .5 sec long > ##D play(setDuration(s,1)) # plays a .5 sec sine wave and then .5 sec silence > ## End(Not run) > > > > cleanEx(); ..nameEx <- "fitSampleParameters" > > ### * fitSampleParameters > > flush(stderr()); flush(stdout()) > > ### Name: fitSampleParameters > ### Title: Adjust Parameters of Two Sample Objects. > ### Aliases: fitSampleParameters > ### Keywords: manip > > ### ** Examples > ## Not run: > ##D s1 <- Sine(440,1,rate=22050,channels=1,bits=16) > ##D s2 <- Sawtooth(440,1,rate=44100,channels=2,bits=8) > ##D play(s1) > ##D play(s2) > ##D l <- fitSampleParameters(s1,s2) > ##D t1 <- l[[1]] > ##D t2 <- l[[2]] > ##D print(t1) > ##D print(t2) # both samples have the same parameters now > ##D play(t1) > ##D play(t2) # none of the samples sounds different now, > ##D # since only parameters with higher quality were chosen > ## End(Not run) > > > > cleanEx(); ..nameEx <- "left" > > ### * left > > flush(stderr()); flush(stdout()) > > ### Name: left > ### Title: Extract one Channel from a Stereo Sample > ### Aliases: left right > ### Keywords: manip > > ### ** Examples > ## Not run: > ##D sLeft <- Sine(440,1) > ##D sRight <- Sine(220,1) > ##D s <- stereo(sLeft,sRight) > ##D play(s) > ##D play(left(s)) # only the left channel > ##D play(right(s)) # only the right channel > ## End(Not run) > > > > cleanEx(); ..nameEx <- "loadSample" > > ### * loadSample > > flush(stderr()); flush(stdout()) > > ### Name: loadSample > ### Title: Load a WAV File from Disk > ### Aliases: loadSample > ### Keywords: file > > ### ** Examples > ## Not run: > ##D s <- loadSample("soundfile.wav") > ##D play(s) > ## End(Not run) > > > cleanEx(); ..nameEx <- "mirror" > > ### * mirror > > flush(stderr()); flush(stdout()) > > ### Name: mirror > ### Title: Mirror a Stereo Sample > ### Aliases: mirror > ### Keywords: manip > > ### ** Examples > ## Not run: > ##D s <- stereo(Sine(440,1),Sine(220,1)) > ##D play(s) # higher tone is on the left > ##D play(mirror(s)) # now higher tone is on the right > ## End(Not run) > > > > cleanEx(); ..nameEx <- "noSilence" > > ### * noSilence > > flush(stderr()); flush(stdout()) > > ### Name: noSilence > ### Title: Cut Off Silence from a Sample Object > ### Aliases: noSilence > ### Keywords: manip > > ### ** Examples > ## Not run: > ##D s <- Sine(440,5) > ##D sound(s) <- sound(s)*matrix(seq(1,0,length=5*44100),nrow=1) > ##D sampleLength(s) > ##D play(s) # fade out > ##D s <- noSilence(s,level=.05) > ##D sampleLength(s) # s is shorter now > ##D play(s) # although you don't hear that the end is missing > ## End(Not run) > > > > cleanEx(); ..nameEx <- "normalize" > > ### * normalize > > flush(stderr()); flush(stdout()) > > ### Name: normalize > ### Title: Rescale the Range of a Sample to [-1,1] > ### Aliases: normalize > ### Keywords: manip > > ### ** Examples > ## Not run: > ##D s <- .6*Sine(440,1) > ##D plot(s) > ##D plot(normalize(s)) # now it uses the full range > ##D play(s) > ##D play(normalize(s)) # this one is louder > ## End(Not run) > > > > cleanEx(); ..nameEx <- "nullSample" > > ### * nullSample > > flush(stderr()); flush(stdout()) > > ### Name: nullSample > ### Title: The NULL Sample Object > ### Aliases: nullSample > ### Keywords: sysdata > > ### ** Examples > ## Not run: > ##D scale <- 2^(seq(0,1,length=13))[c(1,3,5,6,8,10,12,13)] > ##D base <- 440 > ##D s <- nullSample() > ##D for (f in scale) > ##D s <- appendSample(s,Sine(f*base,1)) > ##D play(s) > ## End(Not run) > > > > cleanEx(); ..nameEx <- "panorama" > > ### * panorama > > flush(stderr()); flush(stdout()) > > ### Name: panorama > ### Title: Narrow the Panorama of a Stereo Sample > ### Aliases: panorama > ### Keywords: manip > > ### ** Examples > ## Not run: > ##D s <- stereo(Sine(440,1),Sine(330,1)) > ##D play(s) > ##D play(panorama(s,30)) # now right and left tones are closer to the center > ##D play(panorama(s,10)) # now even closer > ##D play(panorama(s,0)) # now both at the center, the same as setChannels(s,1) > ##D play(panorama(s,-30)) # again wider, but both sides switched > ##D play(panorama(s,-50)) # the same as mirror(s) > ## End(Not run) > > > > cleanEx(); ..nameEx <- "pitch" > > ### * pitch > > flush(stderr()); flush(stdout()) > > ### Name: pitch > ### Title: Pitch a Sample Object > ### Aliases: pitch > ### Keywords: manip > > ### ** Examples > ## Not run: > ##D s <- Sine(440,1) > ##D # Now play it 12 semitones = 1 octave deeper, > ##D # that is half the frequencies and twice the length, > ##D # or played at half speed. > ##D play(pitch(s,-12)) # is the same as... > ##D play(Sine(220,2)) > ## End(Not run) > > > > cleanEx(); ..nameEx <- "play" > > ### * play > > flush(stderr()); flush(stdout()) > > ### Name: play > ### Title: Play a Sample Object or a WAV File > ### Aliases: play play.Sample play.default > ### Keywords: IO > > ### ** Examples > ## Not run: > ##D s <- Sine(440,1) > ##D play(s) > ## End(Not run) > > > > cleanEx(); ..nameEx <- "plot.Sample" > > ### * plot.Sample > > flush(stderr()); flush(stdout()) > > ### Name: plot.Sample > ### Title: Plot a Sample Object > ### Aliases: plot.Sample > ### Keywords: hplot > > ### ** Examples > ## Not run: > ##D s <- Sine(440,1) + .4*Sine(1000,1) > ##D plot(s[1:1000]) > ##D play(s) > ##D s <- normalize(s) > ##D plot(s[1:1000]) # now the range of the waveform is in [-1,1] > ##D play(s) # no cracks! > ## End(Not run) > > > > cleanEx(); ..nameEx <- "print.Sample" > > ### * print.Sample > > flush(stderr()); flush(stdout()) > > ### Name: print.Sample > ### Title: Print a Sample Object > ### Aliases: print.Sample > ### Keywords: print > > ### ** Examples > ## Not run: > ##D s <- Sine(440,1) > ##D print(s) > ## End(Not run) > > > > cleanEx(); ..nameEx <- "rate" > > ### * rate > > flush(stderr()); flush(stdout()) > > ### Name: rate > ### Title: The Sampling Rate > ### Aliases: rate rate<- setRate > ### Keywords: attribute > > ### ** Examples > ## Not run: > ##D s <- Sine(440,1,rate=44100) > ##D rate(s) # 44100 > ##D play(s) > ##D print(s) > ##D rate(s) <- 8000 > ##D play(s) # s has worse quality now (noise and additional high frequencies) > ##D print(s) # but uses less memory > ## End(Not run) > > > > cleanEx(); ..nameEx <- "reverse" > > ### * reverse > > flush(stderr()); flush(stdout()) > > ### Name: reverse > ### Title: Play a Sample Object Backwards > ### Aliases: reverse > ### Keywords: manip > > ### ** Examples > ## Not run: > ##D waveform <- 2*((seq(0,80,length=88200)%%1^2)-.5) > ##D s <- as.Sample(waveform,44100,16) > ##D play(s) > ##D play(reverse(s)) # now played backwards > ## End(Not run) > > > > cleanEx(); ..nameEx <- "sampleLength" > > ### * sampleLength > > flush(stderr()); flush(stdout()) > > ### Name: sampleLength > ### Title: Length of a Sample Object > ### Aliases: sampleLength sampleLength<- sampleLength<-.Sample > ### setSampleLength > ### Keywords: attribute > > ### ** Examples > ## Not run: > ##D s <- Sine(440,3,rate=44100,bits=16,channels=2) > ##D sampleLength(s) # 132300 samples ( = 3 sec * 44100 samples/sec ) > ##D sampleLength(s) <- 22050 # sample is now .5 sec long > ##D play(setSampleLength(s,44100)) # plays a .5 sec sine wave and then .5 sec silence > ## End(Not run) > > > > cleanEx(); ..nameEx <- "saveSample" > > ### * saveSample > > flush(stderr()); flush(stdout()) > > ### Name: saveSample > ### Title: Save a Sample Object as a WAV File > ### Aliases: saveSample > ### Keywords: file > > ### ** Examples > ## Not run: > ##D s <- Sine(440,1) > ##D saveSample(s,"sine.wav") > ## End(Not run) > > > > cleanEx(); ..nameEx <- "sound" > > ### * sound > > flush(stderr()); flush(stdout()) > > ### Name: sound > ### Title: The Waveform Matrix of a Sample Object > ### Aliases: sound sound<- > ### Keywords: attribute > > ### ** Examples > ## Not run: > ##D s <- Sine(440,1,channels=2) # stereo sine wave > ##D sound(s)[2,] <- sound(s)[2,]*seq(1,0,length=sampleLength(s)) > ##D play(s) # right channel fades to zero > ## End(Not run) > > > > cleanEx(); ..nameEx <- "stereo" > > ### * stereo > > flush(stderr()); flush(stdout()) > > ### Name: stereo > ### Title: Create a Stereo Sample Object from Two Mono Samples > ### Aliases: stereo > ### Keywords: manip > > ### ** Examples > ## Not run: > ##D sLeft <- Sine(440,1) > ##D sRight <- Sine(220,1) > ##D s <- stereo(sLeft,sRight) > ##D play(s) > ## End(Not run) > > > > ### *