We represent sounds as a list of SoundSample objects.
# Create a Sound object called 'sound' with 256 samples.
sound = Sound(256)
# Find its length. This will be 256.
length = sound.getLength()
# Load a sound from a file.
otherSound = Sound('media/always.wav')
# Play it.
otherSound.play()
# Open it in the interactive sound explorer.
otherSound.explore()
# Get its samples.
samples = otherSound.getSamples()
# Print the first sample's value.
samp = samples[0]
print(samp.getValue())
To initialize a silent sound with a given number n of samples:
Parameters: | n (int) – number of samples long the sound will be. |
---|
To initialize from a file:
Parameters: | filename (string) – the filename of the sound file. |
---|
# Loads the sound 'always.wav' that lives in the 'media' folder.
s = Sound('media/always.wav')
Play the sound.
sound.play()
See a visual representation of the sound in the interactive sound explorer.
sound.explore()
Write your sound to a wav file.
Parameters: | filename (string) – filename for the written file. |
---|
sound.write('mysound.wav')
Return the length of a sound.
length = sound.getLength()
Return the list of samples.
samples = sound.getSamples()
Return the sample at index.
Parameters: | index (int) – the index of the desired sample. |
---|
aSample = sound.getSample(0)
samples = sound.getSamples()
anotherSample = samples[0]
# This will print True.
print(aSample.getValue() == anotherSample.getValue())