### Navigation

- index
- next |
- previous |
- CS116 1.0 documentation »

# Sound

We represent sounds as a list of SoundSample objects.

## Example Usage

    # 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())

## Initialization

To initialize a silent sound with a given number n of samples:

`Sound.``Sound`(self, n)

  Parameters: | n (int) – number of samples long the sound will be.

To initialize from a file:

`Sound.``Sound`(self, filename)

  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')

## Methods

Play the sound.

`Sound.``play`(self)

    sound.play()

See a visual representation of the sound in the interactive sound explorer.

`Sound.``explore`(self)

    sound.explore()

Write your sound to a wav file.

`Sound.``write`(self, filename)

  Parameters: | filename (string) – filename for the written file.

    sound.write('mysound.wav')

## Getters

Return the length of a sound.

`Sound.``getLength`(self)

    length = sound.getLength()

Return the list of samples.

`Sound.``getSamples`(self)

    samples = sound.getSamples()

Return the sample at index.

`Sound.``getSample`(self, 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())

### Navigation

- index
- next |
- previous |
- CS116 1.0 documentation »