R Programming & Digital Audio Donald Byrd 2 Nov. 2006 Copyright © 2006, Donald Byrd.

15
R Programming & Digital Audio Donald Byrd 2 Nov. 2006 Copyright © 2006, Donald Byrd

Transcript of R Programming & Digital Audio Donald Byrd 2 Nov. 2006 Copyright © 2006, Donald Byrd.

Page 1: R Programming & Digital Audio Donald Byrd 2 Nov. 2006 Copyright © 2006, Donald Byrd.

R Programming & Digital Audio

Donald Byrd2 Nov. 2006

Copyright © 2006, Donald Byrd

Page 2: R Programming & Digital Audio Donald Byrd 2 Nov. 2006 Copyright © 2006, Donald Byrd.

rev. 22 Sep. 2006 2

Elements of Digital Audio (1)

• Requirements of discrete time sampling– Pohlmann’s “video of ride over bumpy road” analogy

• Sampling rate determines maximum frequency– Human hearing goes up to ca. 15-20 KHz– Sampling Theorem (Nyquist, Shannon, etc.): need 2

samples per cycle• “Shannon, distant relative of Edison, could…”• Less than 2 samples/cycle => aliasing• Practically, need more than 2• CD sampling rate = 44,100 = 20 KHz * 2.205

Page 3: R Programming & Digital Audio Donald Byrd 2 Nov. 2006 Copyright © 2006, Donald Byrd.

rev. 22 Sep. 2006 3

Elements of Digital Audio (2)

• Sampling rate determines maximum frequency– Human hearing goes up to ca. 15-20 KHz– Sampling Theorem (Nyquist, Shannon, etc.): need 2

samples per cycle• “Shannon, distant relative of Edison, could…”• Practically, need more than 2• CD sampling rate = 44,100 = 20 KHz * 2.205

• Sample width (bits per sample) determines SNR (Signal-to-Noise ratio)– About 6 dB per bit– For digital audio, almost always 8, 16, or 24

Page 4: R Programming & Digital Audio Donald Byrd 2 Nov. 2006 Copyright © 2006, Donald Byrd.

27 Sep. 2006 4

Elements of Digital Audio (3)

• A simple example– Input: sound waves => microphone => Analog to

Digital Converter (ADC) => computer, etc.– ADC includes low-pass filter to avoid aliasing– Output: computer, etc. => Digital to Analog

Converter (DAC) => loudspeaker => sound waves

Page 5: R Programming & Digital Audio Donald Byrd 2 Nov. 2006 Copyright © 2006, Donald Byrd.

15 Sep. 2006 5

Audio in R: the tuneR Package

• tuneR: an add-on library to R• Adds functions to work with & analyze

Wave (.wav audio) files• Installation: type “install.packages()”, or

(with the R GUI) use menu command Packages>Install Packages

• For more information, see “tuneR” under Packages at http://www.r-project.org/

Page 6: R Programming & Digital Audio Donald Byrd 2 Nov. 2006 Copyright © 2006, Donald Byrd.

rev. 18 Sep. 2006 6

Structure of the tuneR Wave Object

• left: vector containing samples for left channel

• right: vector containing samples for right channel (NULL if mono)

• stereo: a boolean for stereo or mono

• samp.rate: sampling rate(e.g., 44,100 = 44,100 samples per sec. for CDs)

• bit: sample width, in bits: controls quantization (usually 16, e.g., for CDs; can be 8 for low quality)

left

samp.rate

right

stereo

bit

An object in R has slots. The Wave object has 5 slots.

Page 7: R Programming & Digital Audio Donald Byrd 2 Nov. 2006 Copyright © 2006, Donald Byrd.

rev. 20 Sep. 2006 7

Creating a Wave Object from a File• install.packages() # do this only once• library(tuneR) # do this every time you need tuneR• #• # Set the working directory to the correct path for your computer.• setwd("work")• # wav is an object of type Wave. Read its waveform from the file.• wav = readWave("Piano.mf1st5sec.A4.wav")• play(wav)• # For later use, convert it from stereo to mono by averaging samples,• # and make a copy of the samples (right channel is empty for mono).• wavmono = mono(wav, "both")• sampdata = wavmono@left• # Also copy a few other things for later use.• sampleRate = [email protected]• sampleWidth = wavmono@bit• len = length(sampdata)

Page 8: R Programming & Digital Audio Donald Byrd 2 Nov. 2006 Copyright © 2006, Donald Byrd.

rev. 22 Sep. 2006 8

What Do We Have?• plot(sampdata) --------->

– Uses just vector of samples

• plot(wav, nr=1000)– Uses whole Wave object

• play(wav)– Uses whole Wave object

• wav– Shows the Wave’s 5 slots:

Wave Object Number of Samples: 198562 Duration (seconds): 4.5 Sampling rate (Hertz): 44100 Channels (Mono/Stereo): Mono Bit (8/16): 16

Page 9: R Programming & Digital Audio Donald Byrd 2 Nov. 2006 Copyright © 2006, Donald Byrd.

15 Sep. 2006 9

Wave Manipulation Example #1

• # Assumes “Creating a Wave Object” already done

• sampdata1 = sampdata*3

• plot(sampdata1)• wav1 =

Wave(left=sampdata1, samp.rate=sampleRate, bit=sampleWidth)

• play(wav1)

Page 10: R Programming & Digital Audio Donald Byrd 2 Nov. 2006 Copyright © 2006, Donald Byrd.

rev. 20 Sep. 2006 10

Wave Manipulation Example #2

• R code– # Assumes “Creating a Wave Object”

already done– newSampleRate = sampleRate/2^(6/12)– wav2 = Wave(left=sampdata1,

samp.rate=newSampleRate, bit=sampleWidth)

– play(wav2)

• Effect: pitch is 6 semitones = tritone lower

Page 11: R Programming & Digital Audio Donald Byrd 2 Nov. 2006 Copyright © 2006, Donald Byrd.

18 Sep. 2006 11

Wave Manipulation: More Techniques in R

• Not Wave-specific, just standard R– See “An Introduction to R” (R-intro.pdf)

• Under Manuals, at http://www.r-project.org/

• 1. Extract every nth element– sampdata3 = sampdata[seq(3, len, by=3)]

• 2. Make two sounds overlap– # Append 0’s to sampdata4, or there would

be NA which causes error later– sampdata4[len:round(0.5*sampleRate)] = 0– sampdata4 = sampdata4+sampdata

Page 12: R Programming & Digital Audio Donald Byrd 2 Nov. 2006 Copyright © 2006, Donald Byrd.

17 Oct. 2006 12

Programming in General

• Details are often vital (& errors are costly)– A great many really are. Commonly:

• Quote marks, including single/double• Capitalization

– “tuneR” isn’t “TuneR”; also “Wav”/“wav”

– TIP: “steal” as much as possible!• Via Copy & Paste is ideal: avoids typos

• Programs tend to be very hard to understand– TIP: include useful, readable comments– TIP: choose variable names for clarity

• “wavdata” isn’t good; how about “samples”?– TIP: consistency helps clarity and correctness

• Don’t mix “v = expr”, “v <- expr”, and “expr -> v”

Page 13: R Programming & Digital Audio Donald Byrd 2 Nov. 2006 Copyright © 2006, Donald Byrd.

rev. 19 Oct. 2006 13

Programming in R (1)• Offers to save workspace when you quit

– Are you sure it’s what you want?– TIP: Just say no.

• Can restore with ‘load(".Rdata")’ (?)– TIP: Use a text editor & files to save work

• If real text editor (not word processor) file, can run w/ R “source” command

• Can always Copy & Paste

• setwd() to correct path for your computer– Depends on where you have files– Can be tricky, esp. in Windows

• Typical Windows ex.: setwd("C:/Documents and Settings/donbyrd.ADS/Teaching/N560")

Page 14: R Programming & Digital Audio Donald Byrd 2 Nov. 2006 Copyright © 2006, Donald Byrd.

rev. 2 Nov. 2006 14

Programming in R (2)• Many useful built-in functions

– read.table– diff(v) => vector of consecutive differences

• no loop needed– sum(v) => sum of vector elements

• no loop needed– table (and related functions)– …and lots of others

• R (and tuneR) have excellent on-line help– TIP: Copy & Paste from help window!– Caveat: terminology is statistics oriented

Page 15: R Programming & Digital Audio Donald Byrd 2 Nov. 2006 Copyright © 2006, Donald Byrd.

20 Sep. 2006 15

Programming in R with tuneR

• Mac only: setWavPlayer() problem– Optional on Windows, required on Mac– Only choice I know is QuickTime Player

• Mac only: play() problems– With QuickTime Player, it works, but…

• Usually gives scary error messages• Must hit the escape key to get it to continue• Opens another QuickTime Player each time

– A UNIX/LINUX player probably better on OS X(?)