Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from pydub import AudioSegment
- import os
- import random
- from pydub.playback import play
- #aggiunta file audio
- class AudioAugmentor:
- def __init__(self, sound, noise, nameFile, volume):
- self.sound = sound
- self.noise = noise
- self.nameFile = nameFile
- self.volume = volume
- #self.category = category
- def addSound(self, sound1, noise1, nameFile, volume):
- noise1 = noise1 - volume # reduce volume noise
- combined = sound1.overlay(noise1) # combine two sound: noise + sound
- n_files = len(os.listdir("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio1/Mixed/")) # take the lenght of the directory: here you know how many files you have
- combined.export("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio1/Mixed/" + nameFile + "_" + str('{0:04}'.format(n_files)) + ".wav", format='wav') # export the new mixed file
- def main():
- randomRealAudio = random.choice(os.listdir("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio1/Real audio/"))
- nameRandomRealAudio = os.path.splitext(randomRealAudio)[0]
- sound1 = AudioSegment.from_file("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio/Real audio/" + randomRealAudio)
- randomNoise = random.choice(os.listdir("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio1/Noise/Urban Acoustic Environment/"))
- nameRandomNoise = os.path.splitext(randomNoise)[0]
- print nameRandomNoise
- if nameRandomNoise == "Nature":
- randomNoiseNature = random.choice(os.listdir("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio1/Noise/Urban Acoustic Environment/Nature/"))
- nameRandomNoiseNature = os.path.splitext(randomNoiseNature)[0]
- print nameRandomNoiseNature
- if nameRandomNoiseNature == "Animals":
- randomNoiseNatureAnimals = random.choice(os.listdir("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio1/Noise/Urban Acoustic Environment/Nature/Animals/"))
- nameRandomNoiseNatureAnimals = os.path.splitext(randomNoiseNatureAnimals)[0]
- print nameRandomNoiseNatureAnimals
- noiseAnimals = AudioSegment.from_file("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio/Noise/Urban Acoustic Environment/Nature/Animals/" + randomNoiseNatureAnimals)
- nameFile = nameRandomRealAudio + "_" + nameRandomNoiseNatureAnimals
- volume = 10
- sound = AudioAugmentor(sound1, noiseAnimals, nameFile, volume)
- sound.addSound(sound1, noiseAnimals, nameFile, volume)
- else:
- if nameRandomNoiseNature == "Elements":
- randomNoiseNatureElements = random.choice(os.listdir("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio1/Noise/Urban Acoustic Environment/Nature/Elements/"))
- nameRandomNoiseNatureElements = os.path.splitext(randomNoiseNatureElements)[0]
- print nameRandomNoiseNatureElements
- noiseElements = AudioSegment.from_file("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio/Noise/Urban Acoustic Environment/Nature/Elements/" + randomNoiseNatureElements)
- nameFile = nameRandomRealAudio + "_" + nameRandomNoiseNatureElements
- volume = 10
- sound = AudioAugmentor(sound1, noiseElements, nameFile, volume)
- sound.addSound(sound1, noiseElements, nameFile, volume)
- else:
- if nameRandomNoiseNature == "PlantsVegetation":
- randomNoiseNaturePlantsVegetation = random.choice(os.listdir("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio1/Noise/Urban Acoustic Environment/Nature/PlantsVegetation/"))
- nameRandomNoiseNaturePlantsVegetation = os.path.splitext(randomNoiseNaturePlantsVegetation)[0]
- print nameRandomNoiseNaturePlantsVegetation
- noisePlantsVegetation = AudioSegment.from_file("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio/Noise/Urban Acoustic Environment/Nature/PlantsVegetation/" + randomNoiseNaturePlantsVegetation)
- nameFile = nameRandomRealAudio + "_" + nameRandomNoiseNaturePlantsVegetation
- volume = 10
- sound = AudioAugmentor(sound1, noisePlantsVegetation, nameFile, volume)
- sound.addSound(sound1, noisePlantsVegetation, nameFile, volume)
- # else:
- # if nameRandomNoise == "Mechanical":
- # randomNoiseMechanical = random.choice(os.listdir("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio1/Noise/Urban Acoustic Environment/Mechanical/"))
- # nameRandomNoiseMechanical = os.path.splitext(randomNoiseMechanical)[0]
- # if nameRandomNoiseMechanical == "Motorized Transport":
- # randomNoiseNatureAnimals = random.choice(os.listdir("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio1/Noise/Urban Acoustic Environment/Mechanical/Motorized Transport/"))
- # nameRandomNoiseMechanical = os.path.splitext(randomNoiseMechanical)[0]
- # noiseMechanical = AudioSegment.from_file("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio/Noise/Urban Acoustic Environment/Mechanical/Motorized Transport/" + randomNoiseMechanical)
- # else:
- # if nameRandomNoiseMechanical == "Non-motorized Transport":
- # randomNoiseNMTransport = random.choice(os.listdir("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio1/Noise/Urban Acoustic Environment/Mechanical/Non-motorized Transport/"))
- # nameRandomNoiseNMTransport = os.path.splitext(randomNoiseNMTransport)[0]
- # noiseNMTransport = AudioSegment.from_file("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio/Noise/Urban Acoustic Environment/Mechanical/Non-motorized Transport/" + randomNoiseNMTransport)
- # else:
- # if nameRandomNoiseMechanical == "SocialSignals":
- # randomNoiseSocialSignals = random.choice(os.listdir("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio1/Noise/Urban Acoustic Environment/Mechanical/SocialSignals/"))
- # nameRandomNoiseSocialSignals = os.path.splitext(randomNoiseSocialSignals)[0]
- # noiseSocialSignals = AudioSegment.from_file("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio/Noise/Urban Acoustic Environment/Mechanical/SocialSignals/" + randomNoiseSocialSignals)
- # else:
- # if nameRandomNoiseMechanical == "Ventilation":
- # randomNoiseVentilation = random.choice(os.listdir("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio1/Noise/Urban Acoustic Environment/Mechanical/Ventilation/"))
- # nameRandomNoiseVentilation = os.path.splitext(randomNoiseVentilation)[0]
- # noiseVentilation = AudioSegment.from_file("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio/Noise/Urban Acoustic Environment/Mechanical/Ventilation/" + randomNoiseVentilation)
- #
- #
- # else:
- # if nameRandomNoise == "Human":
- # randomNoiseHuman = random.choice(os.listdir("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio1/Noise/Urban Acoustic Environment/Human/"))
- # nameRandomNoiseHuman = os.path.splitext(randomNoiseHuman)[0]
- # if nameRandomNoiseHuman == "Movement":
- # randomNoiseHumanMovement = random.choice(os.listdir("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio1/Noise/Urban Acoustic Environment/Human/Movement/"))
- # nameRandomNoiseMovement = os.path.splitext(randomNoiseHumanMovement)[0]
- # noiseMovement = AudioSegment.from_file("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio/Noise/Urban Acoustic Environment/Human/Movement/" + randomNoiseHumanMovement)
- #
- # else:
- # if nameRandomNoiseHuman == "Voice":
- # randomNoiseVoice = random.choice(os.listdir("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio1/Noise/Urban Acoustic Environment/Human/Voice/"))
- # nameRandomNoiseVoice = os.path.splitext(randomNoiseVoice)[0]
- # noiseVoice = AudioSegment.from_file("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio/Noise/Urban Acoustic Environment/Human/Voice/" + randomNoiseVoice)
- #
- # if nameRandomNoiseVoice == "Voice disorder":
- # randomNoiseVoice = random.choice(os.listdir("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio1/Noise/Urban Acoustic Environment/Human/Voice/"))
- # nameRandomNoiseVoice = os.path.splitext(randomNoiseVoice)[0]
- # noiseVoice = AudioSegment.from_file("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio/Noise/Urban Acoustic Environment/Human/Voice/" + randomNoiseVoice)
- #
- # .DS_Store
- # # this line of code is useful to reduce the volume of the noise
- # volume = 10
- # nameFile = nameRandomRealAudio + "_" + nameRandomNoise
- #
- # sound = AudioAugmentor(sound1, noise1, nameFile, volume)
- # sound.addSound(sound1, noise1, nameFile, volume)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement