AudioSample objects are used for playing external audio files (supported formats are WAV and AIF — 16, 24 and 32 bit PCM, and 32-bit float). They can be played, pitch-shifted, looped, paused, resumed, and stopped, among other things.
An application may have several AudioSample objects playing at the same time. In fact, it is possible to create complex timbres by loading several simpler sound objects and manipulating them (e.g., change their frequency and/or volume) in real-time. AudioSample objects open endless timbral possibilities for interactive applications.
NOTE: One limitation is that AudioSamples are very expensive in terms of memory. Although it is possible to load whole songs, you should load only smaller files (e.g., a few seconds long). Since AudioSamples can be looped, one may load specially edited loops, and use them to create longer sound artifacts. For example, see here.
AudioSample is included in the music library, so, you need the following in your program:
from music import *
The simplest way to create an AudioSample object is this (where “sound.wav” is stored in the same folder as your program):
a = AudioSample("sound.wav")
Audio samples may also be created using one of the following functions:
| Function | Description |
| AudioSample(filename) | Creates an audio sample from the filename provided (supported formats are WAV and AIF — 16, 24 and 32 bit PCM, and 32-bit float). |
| AudioSample(filename, actualPitch) | Creates an audio sample from the filename provided (supported formats are WAV and AIF — 16, 24 and 32 bit PCM, and 32-bit float). Parameter actualPitch specifies the actual tone of the sound as a MIDI pitch (integer), or frequency in Hz (float) – default is A4 or 440.0. |
| AudioSample(filename, actualPitch, volume) | Creates an audio sample from the filename provided (supported formats are WAV and AIF — 16, 24 and 32 bit PCM, and 32-bit float). Parameter actualPitch specifies the actual tone of the sound as a MIDI pitch (integer), or frequency in Hz (float) – default is A4 or 440.0. Parameter volume specifies how loud (0 to 127) is the audio sample (default is 127). |
| AudioSample(filename, actualPitch, volume, voices) | Creates an audio sample from the filename provided (supported formats are WAV and AIF — 16, 24 and 32 bit PCM, and 32-bit float). Parameter actualPitch specifies the actual tone of the sound as a MIDI pitch (integer), or frequency in Hz (float) – default is A4 or 440.0. Parameter volume specifies how loud (0 to 127) is the audio sample (default is 127). Parameter voices specifies the number of parallel voices for polyphony (default is 16). |
Once an audio sample, a, has been created, the following functions are available:
| Function | Description |
| a.play()
a.play(start, size, voice) | Play the sample once. If optional start and size are provided, the sample is played from millisecond start until millisecond start+size (default is 0 and -1, respectively, meaning from beginning to end). If voice is provided, the corespondonding voice is used to play the sample (default is 0). |
| a.loop()
a.loop(times, start, size, voice) | Repeat the sample indefinitely. Optional parameter times specifies the number of times to repeat (default is -1, indefinitely). If start and size are provided, looping occurs between millisecond start and millisecond start+size (default is 0 and -1, respectively, meaning from beginning to end). If voice is provided, the corespondonding voice is used to loop the sample (default is 0). |
| a.stop() a.stop(voice) | Stops sample playback immediately. If optional voice is provided, the corespondonding voice is stopped (default is 0). |
| a.pause() a.pause(voice) | Pauses sample playback (remembers current position for resume). If optional voice is provided, the corespondonding voice is paused (default is 0). |
| a.resume() a.resume(voice) | Resumes sample playback (from the paused position). If optional voice is provided, the corespondonding voice is resumed (default is 0). |
| a.isPlaying() a.isPlaying(voice) | Returns True if the sample is still playing, False otherwise. If optional voice is provided, the corespondonding voice is checked (default is 0). |
| a.setPitch(pitch) a.setPitch(pitch, voice) | Sets the sample pitch (0-127) through pitch shifting from sample’s base pitch. If optional voice is provided, the pitch of the corespondonding voice is set (default is 0). |
| a.getPitch() a.getPitch(voice) | Returns the sample’s current pitch (it may be different from the default pitch). If optional voice is provided, the pitch of the corespondonding voice is returned (default is 0). |
| a.setFrequency(freq) a.setFrequency(freq, voice) | Sets the sample frequency (in Hz). This is equivalent to setPitch(), except it provides more detail (microtonality). For instance, pitch A4 is the same as frequency 440 Hz. If optional voice is provided, the frequency of the corespondonding voice is set (default is 0). |
| a.getFrequency() a.getFrequency(voice) | Returns the current sample frequency. If optional voice is provided, the frequency of the corespondonding voice is returned (default is 0). |
| a.setVolume(volume) a.setVolume(volume, delay, voice) | Sets the volume (amplitude) of the sample (volume ranges from 0 – 127). Optional delay indicates speed with which to adjust volume (in milliseconds – default is 2). If voice is provided, the volume of the corespondonding voice is set (default is 0). |
| a.getVolume() a.getVolume(voice) | Returns the current volume (amplitude) of the sample (volume ranges from 0 – 127). If optional voice is provided, the volume of the corespondonding voice is returned (default is 0). |
| a.setPanning(panning) a.setPanning(panning, voice) | Sets the panning of the sample (panning ranges from 0 – 127). If optional voice is provided, the panning of the corespondonding voice is set (default is 0). |
| a.getPanning() a.getPanning(voice) | Returns the current panning of the sample (panning ranges from 0 – 127). If optional voice is provided, the panning of the corespondonding voice is returned (default is 0). |
| a.getFrameRate() | Returns the sample’s recording rate (e.g., 44100.0 Hz). All sample voices have the same frame rate. This is determined by the audio file, so it cannot be set. Use setFrequency() and setPitch() instead. |
NOTE: All the above functions have an optional parameter, voice – to indicate a particular voice, if desired. For most practical situations this is not necessary to use.
Also, see Play.audio().