Home Schedule
Examples of splicing sound with media.py
Download Complete Example
        
        import media
        
        # Increase the amplitude of the first half
        #   of the sound while lowering the second
        #   half of the sound 
        def increase_and_decrease(source): 
            # Copy the sound to avoid editing the original audio 
            sound = media.duplicateSound(source) 
            
            # Start at the begining of the sound and double all the
            #   amplitude values. Stop at the middle. 
            for index in range(media.getLength(sound) // 2): 
                # Get the value of a sample at a specific index in the sound 
                value = media.getSampleValueAt(sound, index) 
                # Set the value of a specific sample at the index in the sound 
                media.setSampleValueAt(sound, index, value * 2) 
            
            # Start at the middle of the sound and decrease the
            #   rest of the sound by 80% 
            for index in range(media.getLength(sound) // 2, media.getLength(sound)):
                value = media.getSampleValueAt(sound, index)
                media.setSampleValueAt(sound,index, value * 0.2)
            
            return sound 
        
        
        # This is the same effect as the function above, but using
        #   the index (or subscript) operator. 
        def increase_and_decrease_index(source): 
            # Copy the sound to avoid editing the original audio
            sound = media.duplicateSound(source) 
            
            # Create a list of all the samples in the sound 
            samples = media.getSamples(sound) 
            
            # Again, amplitude doubled for the first half
            #   of the sound 
            for index in range(len(samples) // 2): 
                # Get the value of the sample in the list at an index 
                value = media.getSampleValue(samples[index]) 
                # Set the value of a specific sample to double 
                media.setSampleValue(samples[index], value * 2) 
        
            # Again, amplitude reduced by 80% for the last half
            #   of the sound 
            for index in range(len(samples) // 2, len(samples)): 
                value = media.getSampleValue(samples[index])
                media.setSampleValue(samples[index], value * 0.2)
            
            return sound
        
        
        # Create a new sound file by copying parts of the source
        #   file into the target 
        def splice_preamble(source): 
            # Create an empty sound file that is 10 seconds long 
            target = media.makeEmptySoundBySeconds(10) 
        
            # Get "We the" from the preamble 
            for source_index in range(0, 17408):
                value = media.getSampleValueAt(source, source_index)
                media.setSampleValueAt(target, source_index, value) 
            
            # Offset the index into the target 
            target_index = 17408 
            
            # Get "United" from the source 
            for source_index in range(33414, 40052):
                value = media.getSampleValueAt(source, source_index)
                media.setSampleValueAt(target, target_index, value)
                target_index = target_index + 1 
            
            # Get "People" from the source 
            for source_index in range(17408, 26726):
                value = media.getSampleValueAt(source, source_index)
                media.setSampleValueAt(target, target_index, value)
                target_index = target_index + 1 
            
            # Add some silence to the target
            #   1000 samples set to silence (0) 
            for index in range(0,1000):
                media.setSampleValueAt(target, target_index, 0)
                target_index = target_index + 1
        
            return target 
        
        # Create a new sound file by copying parts of the source
        #   file into the target  
        def splice_simple(source): 
            # Duplicate the sound 
            target = media.duplicateSound(source) 
            
            # target_index starts just after "We the" in the new sound
            target_index=17408 
            
            # Where the word "United" is in the sound 
            for source_index in range(33414, 40052):
                value = media.getSampleValueAt(source, source_index)
                media.setSampleValueAt(target, target_index, value)
                target_index = target_index + 1 
        
            # Copy the rest of the sound from "People" onward 
            for source_index in range(17408, 55510):
                value = media.getSampleValueAt(source, source_index)
                media.setSampleValueAt(target, target_index, value)
                target_index = target_index + 1
                
            return target 
        
        # Sound we are working with 
        preamble = media.makeSound("preamble10.wav") 
        
        # View the original sound 
        media.explore(preamble) 
        
        # Experiment with editing only a part of the audio 
        media.explore(increase_and_decrease(preamble))
        media.explore(increase_and_decrease_index(preamble)) 
        
        # Splicing parts of the preamble 
        media.explore(splice_preamble(preamble)) 
        
        # Simplifiying the splicing 
        media.explore(splice_simple(preamble))
        
        media.quit()