Home Schedule
Examples of modifying the volume of a sound file with media.py Download Complete Example

import media

# Adjust the volume of a sound by a factor.
#   loudness_factor > 0 increase the loudness
#   0 < loudness_factor < 1 decrease the loudness 

def change_volume(sound, loudness_factor): 

    # Make a copy of the original sound 

    new_sound = media.duplicateSound(sound) 


    # Access all samples in the sound

    for sample in media.getSamples(new_sound):
        value = media.getSampleValue(sample) 

        # Change them all by the factor

        media.setSampleValue(sample, value * loudness_factor)
    
    return new_sound


# Adjust the volume of a sound by a factor.
#   loudness_factor > 0 increase the loudness
#   0 < loudness_factor < 1 decrease the loudness
#   start and end define the range of samples to change 

def change_volume_range(sound, loudness_factor, start, end): 

    # Make a copy of the original sound

    new_sound = media.duplicateSound(sound) 

    
    # Access samples within a range

    for index in range(start, end): 

        # Another way to access sample values 

        value = media.getSampleValueAt(new_sound, index) 

        # Change the samples within the range 

        media.setSampleValueAt(new_sound, index, value * loudness_factor)
    
    return new_sound 


# Helper function to find the largest value in the sound

def find_loudest(sound):
    loudest = 0
    samples = media.getSamples(sound) 

    
    # Use a range to access samples 

    for index in range(len(samples)):
        sample = samples[index] 

        # Get the largest value between our current
        #   loudest value and the newest sample 

        loudest = max(loudest, media.getSampleValue(sample))
    
    return loudest 


# Maximize the volume of a sound file without
#   causing clipping 

def normalize(sound): 

    # Make a copy of the original sound 

    new_sound = media.duplicateSound(sound) 

    
    # Call the function to find the highest sample value 

    largest = find_loudest(new_sound) 

    
    # Calculate the amount we can increase all samples
    #   ration of the largest sample value and the maximum
    #   possible value 

    multiplier = 32767 / largest
    print ("Largest sample value in original sound was",largest)
    print ("Multiplier is", multiplier) 

    
    # Modifiy all sample values 

    for sample in media.getSamples(new_sound):
        louder = multiplier * media.getSampleValue(sample)
        media.setSampleValue(sample,louder)
    
    return new_sound 


# Force a sound to generate clipping 

def clip(sound):   
    # Make a copy of the original sound
    new_sound = media.duplicateSound(sound) 

    
    # Loop over all the samples 

    for sample in media.getSamples(new_sound): 

        # Set 0 or positive values to the maximum
        #   positive value. Otherwise set it to the
        #   lowest value 

        if media.getSampleValue(sample) >= 0:
            media.setSampleValue(sample, 32767)
        else:
            media.setSampleValue(sample, -32768)
    
    return new_sound 


# Load an audio file (like makePicture) 

audio = media.makeSound('sounds/thisisatest.wav') 


# NOTE: You are going to hear this sound a lot and
#   it will get annoying :) 


# Preview the sound and show the wavform 

media.explore(audio) 


# This is how we play the sound 

media.blockingPlay(audio) 


# Increase the sound volume by a factor of 5 

louder_audio = change_volume(audio, 5)
media.explore(louder_audio)
media.blockingPlay(louder_audio) 


# We can print out the sample values at the same
#   position in the sound to make sure we changed
#   the values 

print(media.getSampleValueAt(audio, 17741))
print(media.getSampleValueAt(louder_audio, 17741)) 


# Make a portion of the sound louder 

media.explore(audio)
media.blockingPlay(audio)
partially_louder_audio = change_volume_range(audio, 7, 46341, 57864)
media.explore(partially_louder_audio)
media.blockingPlay(partially_louder_audio) 


# Show a portion that was not changed 

print(media.getSampleValueAt(audio, 20000))
print(media.getSampleValueAt(partially_louder_audio, 20000)) 


# Show a portion that was changed 

print(media.getSampleValueAt(audio, 50000))
print(media.getSampleValueAt(partially_louder_audio, 50000)) 


# Apply normalization to the audio 

normalized_audio = normalize(audio)
media.blockingPlay(audio)
media.blockingPlay(normalized_audio) 


# Demonstrating Clipping
#   WARNING: Not plesant sounding.... 

clipped = clip(audio)
media.explore(clipped)
media.blockingPlay(clipped)

media.quit()