PRC DSP Lab 1:  Time Domain PCM Fun
by Perry Cook, 1998-2009

If you're reading this and haven't gotten everything
copied and compiled yet, please doso before going on.

"Real-Pitch Time Shifting" --------------------------------

Get a shell, and type:

cd TimeStuf

>  timeshif 2.0 Trumpt44.wav Trumpt44b.wav
>  sndplay Trumpt44.wav
>  sndplay Trumpt44b.wav

Open both wave files in snd or Goldwave or Matlab and compare 
spectra and sounds

> timeshif

with no arguments.  It will give you a list of options
you can play with.  Try messing with the window size and
random options.

Try it on some more interesting files, like LoveMe or 
Lions or a Trumpet.  

Sample Rate Conversion and Interpolation ------------------

type 

> srconvrt 2.0 -f 0.5 Trumpt44.wav Trumpt44c.wav
> sndplay Trumpt44c.wav

Cool huh?

type srconvrt without arguments and see what's available.

Find a nice high quality mono music soundfile and try the
-d and -l arguments (drop sample and linear interpolation).
See if you can hear the artifacts.  Use headphones if possible
to hear the most you can.

Gain, Time Reversal ---------------------------------------

The gain and reverse files do what you would expect.

useage: gain GainAmt fileIn fileOut
	GainAmt is a float, 1.0 = leave file alone

useage: reverse fileIn fileOut
 
Quantize --------------------------------------------------

Not really very interesting musically, but included for 
completeness and pedagogical purposes.
quantize NBits FileIn.wav FileOut.wav quantizes to N bits

useage: quantize NumBits fileIn fileOut

Waveform Compression --------------------------------------

These aren't so interesting either, unless you're into 
compression algorithms.  But they're here for your amusement,
and if you ever do a project that needs some simple compression,
you can start with or refer to this code.

mulaw.c
demulaw.c
acpcmcod.c
adpcmdec.c

Constant-Time Pitch Shifting ------------------------------

type

> srconvrt 1.5 Trumpt44.wav temp.wav
> timeshif 1.5 temp.wav Trumpt44d.wav
> sndplay Trumpt44d.wav

this has shifted the pitch without changing the time
(compare to Trumpt44.wav).  

Welcome to ChucK!! ----------------------------------------

ChucK is a hot new real-time "on the fly" programming language
for sound and music.  Conceived by Ge Wang, and brewed by 
lots of folks in the Princeton Soundlab, it's quickly gaining
acceptance, enthusiasm, and a large user base.  It looks 
pretty much like Java and C++, but there are major differences.
We'll learn more about ChucK as we go along, but for now we'll
look at a couple of examples related to pitch and time shifting.
First go download and compile ChucK.  There's lots of help on 
installing, etc. at chuck.cs.princeton.edu

LINUX USERS: ChucK uses Jack for audio I/O under LINUX.  Start 
the Jack server by launching qjackctl 

At the shell, type 

chuck pitshift.ck

then look at how little code was required for that!!

chuck interp.ck

compares sinc, linear, and drop-sample interpolation

chuck timeshif.ck 

allows you to do both pitch and time shifting.
You'll need to edit the file to change the 
pfactor and tfactor variables, or change the
soundfile to one of your own.

chuck gain.ck:gain:inFile.wav:outFile.wav

does the same as gain.c, but look at the code!!
Bug/Feature: It also converts to 44.1 at the same time.
44.1 is the native ChucK sample rate.

You can use the command line argument feature
to change pitshift.ck or interp.ck or timeshif.ck 
to process a file of your choice.

But ChucK isn't really for such drudgery, it's
for much more fun things than simple things...
-----------------------------------------------------------

For All: 
  Play with ChucK some more.  Read the documentation online.
  Look at some examples.  It's really really really cool.

For Everyone, Assignment 1 Musical Statement:
  Make a short piece using only time-domain programs and hacks.
  Note that by selecting the window size in timeshif, you can 
  impose a pitch onto the file.  You might want to mix some 
  files and do some envelopes too.  

Extra fun things to do (if you like):

For EEs:
  Implement one or both of timeshif and/or srconvrt
  in MATLAB.  Or, hack the srconvrt program to add 
  one more mode for polynomial (lagrange) interpolation.
  Check out the Karjalainen paper(s) to see what's up with that.

For Computer Scientists/Programmers:
  Merge the two programs, timeshif and srconvrt, to
  make one program that does both functions.  
  Or, make a version of srconvrt that takes time-varying
  envelopes as input (not just initial and final conversion
  factors, but rather a set of times and values (maybe read
  from a file).

-----------------------------------------------------------
