CSC111 Basic JES Sound Functions

From dftwiki3
Jump to: navigation, search

--D. Thiebaut (talk) 10:16, 24 February 2014 (EST)


JES Sound Functions


This is just reference material taken from Depaw U. that you may want to play with.


A sound is treated as a collection of sample values, much as a picture is a collection of pixels. Here are the functions on sounds:




makeSound(file)                     Return a sound loaded from a WAV, AU, or AIFF file
makeEmptySound( noSamples) Return a blank sound (all samples values are 0) with that number of samples
writeSoundTo(s, file) Save s in the given file; uses the same format as when loaded (makeEmptySound creates a sound in WAV format)
openSoundTool(s) Open the sound exploration window for s
play(s) Play sound s in the background
playAtRate(s, r) Play s at speed r--1.0 is normal, 2.0 is twice as fast, etc.
playInRange(s, b, e) Play s from sample number b to e
playAtRateInRange(s, r, b, e) Play s from b to e at speed r
blockingPlay(s) Play s and wait for it to finish (also, blockingPlayInRange and blockingPlayAtRateInRange)
getSamples(s) Return the collection of samples in s (not quite a list, but indexable and usable in a for loop)
getLength(s) Return the number of samples in s
getSamplingRate(s) Return the number of samples per second in s



There are also some useful methods on sound and sample objects:

s.getSampleValue(i)                Return the (left channel, if stereo) value of the ith sample in s (the first sample is at index 1)
s.getLeftSampleValue(i) Return the left channel value at index i
s.getRightSampleValue(i) Return the right channel value at index i
s.setSampleValue(i, v) Set the (left) value of sample i to v; the range of sample values is -32,768 to +32,767
s.setLeftSampleValue(i, v) Same
s.setRightSampleValue(i, v) Guess what?
s.getSampleObjectAt(i) Extracts the sample object at index i, without creating a whole collection of samples
samp.value Return the value of sample samp; change the value by assigning to this
samp.left Return or change the left channel value of samp
samp.right Return or change the right channel value of samp



Finally, JES provides a simple function to play a MIDI note:

playNote(n, dur, vol)         plays note number n (middle C is 60, C-sharp is 61, etc.; the range is 0-127) for dur milliseconds at volume vol (maximum is 127).

The function pauses execution until the note is finished. For much more complete support of MIDI music, see the next section.