Advertisement
Guest User

Untitled

a guest
Feb 14th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.14 KB | None | 0 0
  1. from pydub import AudioSegment
  2. import os
  3. import random
  4. from pydub.playback import play
  5.  
  6. #aggiunta file audio
  7.  
  8. class AudioAugmentor:
  9.  
  10.     def __init__(self, sound, noise, nameFile, volume):
  11.         self.sound = sound
  12.         self.noise = noise
  13.         self.nameFile = nameFile
  14.         self.volume = volume
  15.         #self.category = category
  16.  
  17.  
  18.     def addSound(self, sound1, noise1, nameFile, volume):
  19.         noise1 = noise1 - volume    # reduce volume noise
  20.         combined = sound1.overlay(noise1)   # combine two sound: noise + sound
  21.         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
  22.         combined.export("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio1/Mixed/" + nameFile + "_" + str('{0:04}'.format(n_files)) + ".wav", format='wav')        # export the new mixed file
  23.  
  24. def main():
  25.     randomRealAudio = random.choice(os.listdir("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio1/Real audio/"))
  26.     nameRandomRealAudio = os.path.splitext(randomRealAudio)[0]
  27.     sound1 = AudioSegment.from_file("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio/Real audio/" + randomRealAudio)
  28.  
  29.     randomNoise = random.choice(os.listdir("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio1/Noise/Urban Acoustic Environment/"))
  30.     nameRandomNoise = os.path.splitext(randomNoise)[0]
  31.     print nameRandomNoise
  32.  
  33.     if nameRandomNoise == "Nature":
  34.         randomNoiseNature = random.choice(os.listdir("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio1/Noise/Urban Acoustic Environment/Nature/"))
  35.         nameRandomNoiseNature = os.path.splitext(randomNoiseNature)[0]
  36.         print nameRandomNoiseNature
  37.         if nameRandomNoiseNature == "Animals":
  38.             randomNoiseNatureAnimals = random.choice(os.listdir("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio1/Noise/Urban Acoustic Environment/Nature/Animals/"))
  39.             nameRandomNoiseNatureAnimals = os.path.splitext(randomNoiseNatureAnimals)[0]
  40.             print nameRandomNoiseNatureAnimals
  41.             noiseAnimals = AudioSegment.from_file("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio/Noise/Urban Acoustic Environment/Nature/Animals/" + randomNoiseNatureAnimals)
  42.             nameFile = nameRandomRealAudio + "_" + nameRandomNoiseNatureAnimals
  43.             volume = 10
  44.             sound = AudioAugmentor(sound1, noiseAnimals, nameFile, volume)
  45.             sound.addSound(sound1, noiseAnimals, nameFile, volume)
  46.  
  47.         else:
  48.             if nameRandomNoiseNature == "Elements":
  49.                 randomNoiseNatureElements = random.choice(os.listdir("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio1/Noise/Urban Acoustic Environment/Nature/Elements/"))
  50.                 nameRandomNoiseNatureElements = os.path.splitext(randomNoiseNatureElements)[0]
  51.                 print nameRandomNoiseNatureElements
  52.                 noiseElements = AudioSegment.from_file("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio/Noise/Urban Acoustic Environment/Nature/Elements/" + randomNoiseNatureElements)
  53.                 nameFile = nameRandomRealAudio + "_" + nameRandomNoiseNatureElements
  54.                 volume = 10
  55.                 sound = AudioAugmentor(sound1, noiseElements, nameFile, volume)
  56.                 sound.addSound(sound1, noiseElements, nameFile, volume)
  57.  
  58.             else:
  59.                 if nameRandomNoiseNature == "PlantsVegetation":
  60.                     randomNoiseNaturePlantsVegetation = random.choice(os.listdir("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio1/Noise/Urban Acoustic Environment/Nature/PlantsVegetation/"))
  61.                     nameRandomNoiseNaturePlantsVegetation = os.path.splitext(randomNoiseNaturePlantsVegetation)[0]
  62.                     print nameRandomNoiseNaturePlantsVegetation
  63.                     noisePlantsVegetation = AudioSegment.from_file("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio/Noise/Urban Acoustic Environment/Nature/PlantsVegetation/" + randomNoiseNaturePlantsVegetation)
  64.                     nameFile = nameRandomRealAudio + "_" + nameRandomNoiseNaturePlantsVegetation
  65.                     volume = 10
  66.                     sound = AudioAugmentor(sound1, noisePlantsVegetation, nameFile, volume)
  67.                     sound.addSound(sound1, noisePlantsVegetation, nameFile, volume)
  68.     # else:
  69.     #     if nameRandomNoise == "Mechanical":
  70.     #         randomNoiseMechanical = random.choice(os.listdir("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio1/Noise/Urban Acoustic Environment/Mechanical/"))
  71.     #         nameRandomNoiseMechanical = os.path.splitext(randomNoiseMechanical)[0]
  72.     #         if nameRandomNoiseMechanical == "Motorized Transport":
  73.     #             randomNoiseNatureAnimals = random.choice(os.listdir("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio1/Noise/Urban Acoustic Environment/Mechanical/Motorized Transport/"))
  74.     #             nameRandomNoiseMechanical = os.path.splitext(randomNoiseMechanical)[0]
  75.     #             noiseMechanical = AudioSegment.from_file("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio/Noise/Urban Acoustic Environment/Mechanical/Motorized Transport/" + randomNoiseMechanical)
  76.     #         else:
  77.     #             if nameRandomNoiseMechanical == "Non-motorized Transport":
  78.     #                 randomNoiseNMTransport = random.choice(os.listdir("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio1/Noise/Urban Acoustic Environment/Mechanical/Non-motorized Transport/"))
  79.     #                 nameRandomNoiseNMTransport = os.path.splitext(randomNoiseNMTransport)[0]
  80.     #                 noiseNMTransport = AudioSegment.from_file("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio/Noise/Urban Acoustic Environment/Mechanical/Non-motorized Transport/" + randomNoiseNMTransport)
  81.     #             else:
  82.     #                 if nameRandomNoiseMechanical == "SocialSignals":
  83.     #                     randomNoiseSocialSignals = random.choice(os.listdir("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio1/Noise/Urban Acoustic Environment/Mechanical/SocialSignals/"))
  84.     #                     nameRandomNoiseSocialSignals = os.path.splitext(randomNoiseSocialSignals)[0]
  85.     #                     noiseSocialSignals = AudioSegment.from_file("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio/Noise/Urban Acoustic Environment/Mechanical/SocialSignals/" + randomNoiseSocialSignals)
  86.     #                 else:
  87.     #                     if nameRandomNoiseMechanical == "Ventilation":
  88.     #                         randomNoiseVentilation = random.choice(os.listdir("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio1/Noise/Urban Acoustic Environment/Mechanical/Ventilation/"))
  89.     #                         nameRandomNoiseVentilation = os.path.splitext(randomNoiseVentilation)[0]
  90.     #                         noiseVentilation = AudioSegment.from_file("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio/Noise/Urban Acoustic Environment/Mechanical/Ventilation/" + randomNoiseVentilation)
  91.     #
  92.     #
  93.     #     else:
  94.     #         if nameRandomNoise == "Human":
  95.     #             randomNoiseHuman = random.choice(os.listdir("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio1/Noise/Urban Acoustic Environment/Human/"))
  96.     #             nameRandomNoiseHuman = os.path.splitext(randomNoiseHuman)[0]
  97.     #             if nameRandomNoiseHuman == "Movement":
  98.     #                 randomNoiseHumanMovement = random.choice(os.listdir("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio1/Noise/Urban Acoustic Environment/Human/Movement/"))
  99.     #                 nameRandomNoiseMovement = os.path.splitext(randomNoiseHumanMovement)[0]
  100.     #                 noiseMovement = AudioSegment.from_file("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio/Noise/Urban Acoustic Environment/Human/Movement/" + randomNoiseHumanMovement)
  101.     #
  102.     #             else:
  103.     #                 if nameRandomNoiseHuman == "Voice":
  104.     #                     randomNoiseVoice = random.choice(os.listdir("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio1/Noise/Urban Acoustic Environment/Human/Voice/"))
  105.     #                     nameRandomNoiseVoice = os.path.splitext(randomNoiseVoice)[0]
  106.     #                     noiseVoice = AudioSegment.from_file("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio/Noise/Urban Acoustic Environment/Human/Voice/" + randomNoiseVoice)
  107.     #
  108.     #                     if nameRandomNoiseVoice == "Voice disorder":
  109.     #                         randomNoiseVoice = random.choice(os.listdir("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio1/Noise/Urban Acoustic Environment/Human/Voice/"))
  110.     #                         nameRandomNoiseVoice = os.path.splitext(randomNoiseVoice)[0]
  111.     #                         noiseVoice = AudioSegment.from_file("/Users/robertagalici/Desktop/ Magistrale/Tesi/Audio/Noise/Urban Acoustic Environment/Human/Voice/" + randomNoiseVoice)
  112.     #
  113.     # .DS_Store
  114.  
  115.  
  116.     # # this line of code is useful to reduce the volume of the noise
  117.     # volume = 10
  118.     # nameFile = nameRandomRealAudio + "_" + nameRandomNoise
  119.     #
  120.     # sound = AudioAugmentor(sound1, noise1, nameFile, volume)
  121.     # sound.addSound(sound1, noise1, nameFile, volume)
  122.  
  123.  
  124. if __name__ == "__main__":
  125.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement