tkSnack manual v2.2
content="Mozilla/4.72 [en] (X11; U; Linux 2.2.14-5.0 i686) [Netscape]">
Snack manual, version 2.2
Installing SnackFirst you need to install Snack according to its installation instruction. In order to use Snack you need to put the file tkSnack.py somewhere in your Python path.
Using Snack: an overview
InitializingYou need to use Tkinter in order to use Snack. Even if you don't use any GUI elements that Tkinter offers, you will still need an active Tk object in your program. In order for Snack to identify the Tk object it should use, you will need to run the initializeSnack procedure.
The beginning of a program that uses Snack might look like:
from Tkinter import *
Using Sound objectsYou create sound objects the same way you create any Python objects.
Since we gave no additional arguments, the sound object created by this will contain no sound data. We can give it some data in a number of ways -- by recording from the current input channel, by reading from a file, and so on.mysound = tkSnack.Sound()
Let's try reading from a file. If you're using Windows, you have at least a few WAV files sitting conveniently on your hard drive. (If you're using another operating system, you'll have to locate your own sound files to read from :-).)
Now for the moment of truth. Try playing your sound with:mysound.read('c:/windows/media/chord.wav')
You can create a new sound object and load a file in the same step using the "load" option:mysound.play()
(Note: Another possibility is to use tkSnack.Sound(file='filename') . Using file instead of load will "link" the disk file to the sound object instead of immediately loading it into memory. This will limit what you can do to the object, since a number of Snack's usual sound methods are only available to "in-memory" sounds.)tada = tkSnack.Sound(load='c:/windows/media/tada.wav')
You can perform a number of manipulations on the sounds objects. For example, let's tack a couple of copies of the Windows chord sound onto the end of the ta-da sound. Then we'll delete a few thousand samples from the middle of the sound (those between sample 10,000 and 40,000), and finally reverse the sound.
We can write the sound back to a disk file, and even magically switch the format.tada.concatenate(mysound)
Audio and mixer controlsSnack has two objects that control aspects of your computer's sound system. The audio object gets and sets properties of the sound devices. To find out what the available input devices on your computer are and what sample rates the current input device can record at, try:
To turn the output volume up or down to 30%, try:tkSnack.audio.inputDevices()
The mixer object controls various aspects of your computer's sound mixers, such as which input jack is currently being used and whether it's recording in stereo or mono.tkSnack.audio.play_gain(30)
New canvas objectsSnack provides three new kinds of items that can be drawn on Tkinter Canvases:
c = tkSnack.SnackCanvas(root, height=400)
The Sound class
The options:mysound["encoding"] = "Lin32"
append (binary-string)Not yet implemented.
cget (option)Retrieves the value of an option for the sound. The possible options are listed above. It is also possible to access the options by treating the sound object as a dictionary, i.e., the following two expressions are equivalent:
concatenate (othersound)Concatenates the sample data from othersound to the end of this sound. The sounds must be of the same type, i.e., have the same sample rate, sample encoding format, and number of channels. This command applies to in-memory sounds only.
configure (option=value ...)Sets the options for the sound. The possible options are listed above. It is also possible to access the options by treating the sound object as a dictionary, i.e., the following two expressions are equivalent:
configure may be abbreviated as config.mysound["byteorder"] = "littleEndian"
convert (option=value)Converts a sound to a different sample encoding, sample rate, or number of channels. Options can be any of the following: -rate, -channels, or -encoding.
copy (othersound)Copies sample data from othersound. Optionally a range of samples to copy can be specified using the start and end options. Any active play operation is stopped before the command is executed if the format of the new sound differs from the current. This command applies to in-memory sounds only.
crop (start=n, end=n)Crops the sound to the given range [start..end], i.e., all samples before and after these limits will be removed. This command applies to in-memory sounds only.
data (variable=tclVariable)Not yet implemented.
destroy( )Removes the Tcl command associated with this sound and frees its storage.
dBPowerSpectrum ( )Computes the log FFT power spectrum of the sound (at the sample number given in the start option) and returns a list of dB values. See the section item for a description of the rest of the options. Optionally an ending point can be given, using the end option. In this case the result is the average of consecutive FFTs in the specified range. Their default spacing is taken from the fftlength but this can be changed using the skip option, which tells how many points to move the FFT window each step. Options:
filter (filter=filterobject)Applies the filter to the sound. This command applies to in-memory sounds only.
flush ( )Removes all audio data from the sound. This command applies to in-memory sounds only.
info ( )Returns a string with information about the sound. The elements of the string are [length, rate, max, min, encoding, channels, fileFormat, headerSize ].
insert (sound=othersound, position=sample ...)Inserts othersound at position sample. Optionally a range of samples to copy can be specified, using the start and end options. This command applies to in-memory sounds only.
length ( )Gets the length of the sound. With an additional numeric argument, it will set the length of the sound. The unit option specifies whether the sound should be measured in "SAMPLES" (the default) or "SECONDS". If the new length is larger than the current length, the sound is padded with additional silence.
max ( )Returns the largest positive sample value of the sound. A range of samples to be examined can be specified with the start and end options. The channel to be examined can be specified with the channel option. The default is to check all channels and return the maximum value.
min ( )Returns the largest negative sample value of the sound. A range of samples to be examined can be specified with the start and end options. The channel to be examined can be specified with the channel option. The default is to check all channels and return the minimum value.
mix (othersound)Mixes sample data from othersound. Optionally a range of samples, where the mix operation will be applied, can be specified using the start and end options. The mixscaling option controls how much to scale othersound before mixing. The option prescaling controls how much to scale the original sound before mixing. This command applies to in-memory sounds only.
pause ( )Pauses the current play/record operation. The next pause() invocation resumes play/record. If there is a number of instances playing of a sound object, all of them are paused.
pitch ( )Returns a list of pitch values computed using the AMDF method. The values are spaced 10 ms. A range of samples can be given using the start and end options. If a frequency range of valid pitch values is known, this can be specified using the maxpitch and minpitch options.
play ( )Plays the sound. All options are ignored if play() is used to resume a paused play options. If a play() command is issued while another one is in progress, the latter one is queued up and starts to play as soon as possible. The lag before this new sound is audible can be controlled using the audio.latency() command.
For in-memory sounds, a number of options are available.
read (filename)Reads new sound data from a file. Current supported file formats are WAV, MP3, AU, SND, AIFF, SD, SMP, CSL, and RAW binary. The command returns the file format detected. It is possible to force a file to be read as RAW using by setting the option fileformat=RAW. In this case, properties of the sound data can be specified by hand, using the rate, channels, encoding, skiphead, byteorder, and guessproperties options, as described above.
record ( )Starts recording data from the audio device into the sound object. You may use the input option to specify one of the available input ports (as returned by the audio.inputs() command) and the device option to select which audio input device to use.
For in-memory sounds, the append=1 option specifies that the new audio data should be appended to the end of the existing sound instead of replacing it.
For channel-based sounds, the fileformat option can be used to specify the file format to be used when writing data, since there is no filename to infer the format from.
reverse ( )Reverses the sound. A range of samples can be specified with the start and end options. This command applies to in-memory sounds only.
sample (sample)Gets the value of the specified sample number. Sets the value with an additional numeric argument. When setting samples, one value should be specified for each channel you want to change. Some examples of setting:
# Sets the 1000th sample to 0 (of a mono sound)
stop ( )Stops the current play or record operation. If there is a queue of sounds to play, each of them can stop playback using stop(). If a callback was registered using the command option to play(), it is not executed.
write (filename)Writes sound data to a file. A range of samples to save can be specified using the start and end options. The file format is guessed from the filename extension, but the guess can be overridden with the fileformat option. If you specify RAW file format, the sound will be saved to file without a header and using the natural byte order of the machine (overrideable with the byteorder option).
The audio objectThe audio object gives access to various properties of the available audio devices. It is created automatically by initializeSnack.
elapsedTime ( )Returns the time elapsed since the start of the last playback operation.
encodings ( )Returns a list of supported sample encoding formats for the currently selected device.
rates ( )Returns a list of supported sample rates for the currently selected device.
inputDevices ( )Returns a list of available audio input devices.
playLatency ( )Sets/queries (in ms) how much sound will be queued up at any time to the audio device for playback. A low value makes new sound reach the loudspeakers quickly at the risk of gaps in the output stream. An appropriate value should be chosen with regard to processor speed and load.
pause ( )Toggles between pause/play for all playback on the audio device.
play ( )Resumes paused playback on the audio device.
play_gain ( )Returns the current play gain value if invoked without a parameter. If an integer value is given, play gain is set to the given value. Valid values are in the range 0 to 100.
outputDevices ( )Returns a list of available audio output devices.
record_gain ( )Returns the current record gain value if invoked without a parameter. If an integer value is given, record gain is set to the given value. Valid values are in the range 0 to 100.
selectOutput (device)Selects an audio output device to be used as default.
selectInput (device)Selects an audio input device to be used as default.
stop ( )Stops all playback on the audio device.
The mixer objectThe mixer object gives access to various properties of mixer devices, such as input/output jack, supported ports, mixer lines, and gain. It is created automatically by initializeSnack.
channels (line)Returns a list with the names of the channels for the specified line .
devices ( )Returns a list of available mixer devices.
input ( )Gets/sets the current input jack. You can optionally give a boolean Tcl variable as an argument.
inputs ( )Returns a list of available input ports.
lines ( )Returns a list with the names of the lines of the mixer device.
output ( )Gets/sets the current output jack. You can optionally give a boolean Tcl variable as an argument.
outputs ( )Returns a list of available output ports.
update ( )Updates all linked variables to reflect the status of the mixer device.
volume (line)Return the current volume setting for mixer. You can optionally link a Tcl variable to the value by including it as an argument. If you link two Tcl variables, they are used for the left and right channels respectively.
select (device)Selects a mixer device to be used as the default.
The Filter classFilter objects can interact with sound objects either during playback or by using the filter() command of the sound object.
Filters in Snack are still in an early stage of development. Consult the Snack documentation for further details.
The SnackCanvas classSnackCanvas is a subclass of Tkinter.Canvas that has three additional kinds of canvas items: waveforms, spectrograms, and sections (power spectra).
WaveformsDraw waveform items on the canvas using the create_waveform method. Obligatory arguments are the x and y coordinates of the waveform's top-right corner. Options are:
SpectrogramsDraw a spectrogram of a sound on the canvas with the create_spectrogram method. Obligatory arguments are the x and y positions of the top-right corner of the spectrogram. Options are:
Currently spectrograms have a limit of 32767 pixels.
Sections (power spectra)Draw an FFT log power spectrum section of a sound on a canvas with the create_section method. Obligatory arguments are the x and y coordinates of the top-right corner of the section. Options are:
Putting SnackCanvas items on regular CanvasesIt's possible to draw these new canvas items onto any canvas in your program, not just those that are instances of SnackCanvas. You might need to do this if you're using elaborations or subclasses of Canvas that have been written by other people, for example, if you want to draw a waveform on a ScrolledCanvas from the Python Megawidget collection.
To accomplish this, tkSnack provides module-level versions of create_waveform , create_section, and create_spectrogram. Simply use the non-Snack canvas as the first argument. Instead of:
If you're using Pmw Scrolled Canvas, remember that you're drawing onto the ScrolledCanvas's Canvas object. In that case, you'll need to do:tkSnack.createWaveform(NonSnackCanvas, sound=tada)