def addFx(sound, effects, pad=3000, fade_in=100, fade_out=100): # Add padding if pad > 0: sound += AudioSegment.silent(duration=pad, frame_rate=sound.frame_rate) # convert pydub sound to np array samples = np.array(sound.get_array_of_samples()) samples = samples.astype(np.int16) chain = AudioEffectsChain() for effect, value in effects: if effect == "reverb" and value > 0: chain.reverb(reverberance=value) elif effect == "distortion" and value > 0: chain.overdrive(gain=value) elif effect == "highpass" and value > 0: chain.highpass(value) elif effect == "lowpass" and value > 0: chain.lowpass(value) elif effect == "bass": frequency = 100 gain = value if isinstance(value, tuple): gain, frequency = value print("%s, %s" % (gain, frequency)) chain.highshelf(gain=gain, frequency=frequency) elif effect == "echo": echoStr = "echo 0.8 0.9" amount = value count = 1 # check if we have echo count indicated if isinstance(value, tuple): amount, count = value for i in range(count): # amount between 10 (robot) and 1000 (mountains) echoStr += " %s 0.3" % amount chain.custom(echoStr) elif effect == "tempo" and value != 1.0 and value != 1: chain.tempo(factor=value) # apply reverb effect fx = (chain) y = fx(samples) # convert it back to an array and create a new sound clip newData = array.array(sound.array_type, y) newSound = sound._spawn(newData) dur = len(newSound) newSound = newSound.fade_in(min(fade_in, dur)).fade_out(min(fade_out, dur)) return newSound
def randAudioAugment(): fx = AudioEffectsChain() effect = [random.randint(0, 1) for i in range(6)] if effect[0] == 1: # lowshelf randGain = random.randint(0, 12) * random.choice([-1, 1]) randFreq = random.randint(20, 300) randSlop = random.uniform(1, 7) / 10 # 0.1~0.7 fx.lowshelf(gain=randGain, frequency=randFreq, slope=randSlop) if effect[1] == 1: # highshelf randGain = random.randint(0, 12) * random.choice([-1, 1]) randFreq = random.randint(1000, 3000) randSlop = random.uniform(1, 7) / 10 # 0.1~0.7 fx.highshelf(gain=randGain, frequency=randFreq, slope=randSlop) if effect[2] == 1: # equalizer randFreq = random.randint(100, 3000) randQ = random.uniform(5, 15) / 10 # 0.5~1.5 randDB = random.randint(0, 6) * random.choice([-1, 1]) fx.equalizer(frequency=randFreq, q=randQ, db=randDB) if effect[3] == 1: # overdrive randGain = random.randint(3, 7) fx.overdrive(gain=randGain, colour=40) if effect[4] == 1: # phaser fx.phaser( gain_in=0.9, gain_out=0.8, delay=1, decay=0.25, speed=2, triangular=False ) if effect[5] == 1: # reverb randReverb = random.randint(30, 70) randDamp = random.randint(30, 70) randRoom = random.randint(30, 70) randWet = random.randint(1, 6) fx.reverb( reverberance=randReverb, hf_damping=randDamp, room_scale=randRoom, stereo_depth=100, pre_delay=20, wet_gain=randWet, wet_only=False, ) return fx
def _highshelf(self, chain: AudioEffectsChain): return chain.highshelf(frequency=3000)