The sound Module

The Sample classes that support the Sound class and allow manipulation of individual sample values.

class sound.MonoSample(samp_array, i)

A sample in a single-channeled Sound with a value.

get_index()

Return this Sample’s index.

get_value()

Return this Sample’s value.

set_value(v)

Set this Sample’s value to v.

class sound.Note(note, s, octave=0)

A Note class to create different notes of the C scale. Inherits from Sound, does everything Sounds do, and can be combined with Sounds.

class sound.Sound(filename=None, samples=None, seconds=None, sound=None)

A Sound class as a wrapper for the pygame.mixer.Sound object.

append(snd)

Append Sound snd to this Sound. Requires that snd has same number of channels as this Sound.

append_silence(s)

Append s samples of silence to each of this Sound’s channels.

copy()

Return a deep copy of this Sound.

crop(first, last)

Crop this Sound so that all Samples before int first and after int last are removed. Cannot crop to a single sample. Negative indices are supported

get_channels()

Return the number of channels in this Sound.

get_filename()

Return this Sound’s filename.

get_max()

Return this Sound’s highest sample value. If this Sound is stereo return the absolute highest for both channels.

get_min()

Return this Sound’s lowest sample value. If this Sound is stereo return the absolute lowest for both channels.

get_pygame_sound()

Return this Sound’s pygame Sound object.

get_sample(i)

Return this Sound’s Sample object at index i. Negative indices are supported.

get_sampling_rate()

Return the number of Samples this Sound plays per second.

insert(snd, i)

Insert Sound snd at index i. Requires that snd has same number of channels as this Sound. Negative indices are supported.

normalize()

Maximize the amplitude of this Sound’s wave. This will increase the volume of the Sound.

play(first=0, last=-1)

Play this Sound from sample index first to last. As default play the entire Sound.

save()

Save this Sound to its filename. If an extension is not specified the default is .wav.

save_as(filename)

Save this Sound to filename filename and set its filename.

set_filename(filename=None)

Set this Sound’s filename to filename. If filename is None set this Sound’s filename to the empty string.

set_samples(pygame_snd)

Set this Sound’s samples array and pygame object.

stop()

Stop playing this Sound.

class sound.StereoSample(samp_array, i)

A sample in a two-channeled Sound with a left and a right value.

get_index()

Return this Sample’s index.

get_left()

Return this StereoSample’s left value.

get_right()

Return this StereoSample’s right value.

get_values()

Return this StereoSample’s left and right values as a tuple (left, right) of two ints.

set_left(v)

Set this StereoSample’s left value to v.

set_right(v)

Set this StereoSample’s right value to v.

set_values(left, right)

Set this StereoSample’s left value to left and right value to right.

sound.append(snd1, snd2)

Append snd2 to snd1.

sound.append_silence(snd, samp)

Append samp samples of silence onto Sound snd.

sound.concatenate(snd1, snd2)

Return a new Sound object with Sound snd1 followed by Sound snd2.

sound.copy(obj)

Return a deep copy of sound obj.

sound.create_note(note, samp, octave=0)

Return a Sound samp samples long with the frequency according to str note. The following are acceptable arguments for note, starting at middle C:

‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘A’, and ‘B’

To raise or lower an octave specify the argument octave as a positive or negative int. Positive to raise by that many octaves and negative to lower by that many.

sound.create_pygame_sound(s)

Return a pygame sound object with s number of silent samples.

sound.create_sine_wave(hz, amp, samp)

Return a pygame sound samp samples long in the form of a sine wave with frequency hz and amplitude amp in the range [0, 32767].

sound.create_sound(samp)

Return a silent Sound samp samples long.

sound.crop_sound(snd, first, last)

Crop snd Sound so that all Samples before int first and after int last are removed. Cannot crop to a single sample. Negative indices are supported.

sound.envelope(samples, channels)

Add an envelope to numpy array samples to prevent clicking.

sound.get_index(samp)

Return Sample samp’s index.

sound.get_left(stereo_samp)

Return StereoSample stereo_samp’s left value.

sound.get_max_sample(snd)

Return Sound snd’s highest sample value. If snd is stereo return the absolute highest for both channels.

sound.get_min_sample(snd)

Return Sound snd’s lowest sample value. If snd is stereo return the absolute lowest for both channels.

sound.get_right(stereo_samp)

Return StereoSample stereo_samp’s right value.

sound.get_sample(snd, i)

Return Sound snd’s Sample object at index i.

sound.get_samples(snd)

Return a list of Samples in Sound snd.

sound.get_sampling_rate(snd)

Return the Sound snd’s sampling rate.

sound.get_value(mono_samp)

Return MonoSample mono_samp’s value.

sound.get_values(stereo_samp)

Return StereoSample stereo_samp’s values in a tuple, (left, right).

sound.init_sound(samp_rate=22050, encoding=-16, channels=1)

Initialize this module. Must be done before any sounds are created.

sound.insert(snd1, snd2, i)

Insert Sound snd2 in Sound snd1 at index i.

sound.load_pygame_sound(filepath)

Return a pygame Sound object from the file at str filepath. If that file is not a .wav or is corrupt in some way raise a TypeError.

sound.load_sound(filename)

Return the Sound at file filename. Requires: file is an uncompressed .wav file.

sound.play(snd)

Play Sound snd from beginning to end.

sound.play_in_range(snd, first, last)

Play Sound snd from index first to last.

sound.pygame_to_sample_array(pygame_snd)

Return a numpy array object, which allows direct access to specific sample values in the buffer of the pygame.mixer.Sound object pygame_snd.

sound.sample_array_to_pygame(samp_array)

Return a new pygame.mixer.Sound object from a numpy array object samp_array. Requires that samp_array is an appropriate 1D or 2D shape.

sound.save_as(snd, filename)

Save sound snd to filename.

sound.set_left(stereo_samp, v)

Set StereoSample stereo_samp’s left value to v.

sound.set_right(stereo_samp, v)

Set StereoSample stereo_samp’s right value to v.

sound.set_value(mono_samp, v)

Set MonoSample mono_samp’s value to v.

sound.set_values(stereo_samp, left, right)

Set StereoSample stereo_samp’s left value to left and right value to right.

sound.stop(snd)

Stop playing Sound snd.

Previous topic

The color Module

Next topic

Welcome to the PyGraphics Developer documentation

This Page