Ejemplo n.º 1
0
def apply_dynamic_range_compression(audio, threshold, ratio, attack, release):
    return Audio(
        sound=pydub_effects.compress_dynamic_range(
            audio.sound, threshold, ratio, attack, release
        ),
        old_audio=audio,
    )
Ejemplo n.º 2
0
def addCompression(sound):
    #print('play with compression')

    threshold = randF(-40, 0)
    ratio = randF(0, 10)
    attack = randF(0, 10)
    release = randF(0, 500)
    return compress_dynamic_range(sound, threshold=threshold, ratio=ratio, attack=attack, release=release)
Ejemplo n.º 3
0
def pydub_dynamic(file, file_ext, out_path, file_name):
	"""
	dynamics by pydub
	"""

	from pydub import AudioSegment
	from pydub import effects

	x = AudioSegment.from_file(file, format=file_ext)

	x = effects.normalize(x, headroom=0.1)

	x = effects.compress_dynamic_range(x, threshold=-3, ratio=4.0, attack=5.0, release=50.0)

	file_handle = x.export(out_path + file_name, format=file_ext)
Ejemplo n.º 4
0
def record():
    numChannels = config.getint('audio', 'channels')
    sampleRate = config.getint('audio', 'samplerate')
    frameSize = config.getint('audio', 'framesize')
    deviceIndex = config.getint('audio', 'deviceIndex')
    print('recording...')
    audio = pyaudio.PyAudio()
    stream = audio.open(format=pyaudio.paInt16,
                        channels=numChannels,
                        rate=sampleRate,
                        input=True,
                        frames_per_buffer=frameSize,
                        input_device_index=deviceIndex)

    frames = AudioSegment.empty()
    while recording:
        frames += AudioSegment(stream.read(frameSize),
                               sample_width=2,
                               frame_rate=sampleRate,
                               channels=1)
    stream.stop_stream()
    stream.close()
    audio.terminate()
    print('finished recording')

    trim = config.getfloat('audio', 'trimSeconds') * 1000
    treshold = config.getfloat('audio', 'compressorTreshold')
    ratio = config.getfloat('audio', 'compressorRatio')
    attack = config.getfloat('audio', 'compressorAttack')
    release = config.getfloat('audio', 'compressorRelease')
    frames = frames[trim:]  # trim begin
    frames = frames[:-trim]  # trim end
    frames = effects.compress_dynamic_range(frames,
                                            threshold=treshold,
                                            ratio=ratio,
                                            attack=attack,
                                            release=release)
    frames = effects.normalize(frames)  # normalize

    if (frames.duration_seconds > 0):
        filename = datetime.datetime.now().isoformat()
        dataDir = config.get('folders', 'data')
        frames.export(os.path.join(dataDir, filename) + '.wav', format='wav')
        frames.set_frame_rate(16000).export(os.path.join(dataDir, filename) +
                                            '.flac',
                                            format='flac')
        print("wrote", filename, 'duration:', frames.duration_seconds,
              'seconds')
Ejemplo n.º 5
0
def createClipFromRecording(fileName):
    fade = config.getfloat('vereinheiter', 'fadeDur') * 1000
    silenceLen = int(config.getfloat('vereinheiter', 'silenceLen') * 1000)
    silenceTresh = config.getfloat('vereinheiter', 'silenceTresh')
    treshold = config.getfloat('vereinheiter', 'compressorTreshold')
    ratio = config.getfloat('vereinheiter', 'compressorRatio')
    attack = config.getfloat('vereinheiter', 'compressorAttack')
    release = config.getfloat('vereinheiter', 'compressorRelease')
    frames = AudioSegment.from_wav(os.path.join(recordingsDir, fileName))
    if frames.dBFS < config.getfloat('vereinheiter', 'recordingMinDB'):
        print('recording level to low:', fileName)
        return [None, None]
    frames = frames.set_sample_width(4)
    frames = frames.fade_in(fade)
    frames = frames.fade_out(fade)
    frames = effects.normalize(frames)
    frames = frames.remove_dc_offset()
    nonsilent = silence.detect_nonsilent(frames, silenceLen, silenceTresh)
    chunks = [frames[chunk[0]:chunk[1]] for chunk in nonsilent]
    frames = AudioSegment.silent(100)
    for chunk in chunks:
        if len(chunk) > 100:
            chunk = chunk.fade_in(20)
            chunk = chunk.fade_out(20)
            frames = frames.append(AudioSegment.silent(250))
            frames = frames.append(chunk)
    frames = effects.compress_dynamic_range(frames,
                                            threshold=treshold,
                                            ratio=ratio,
                                            attack=attack,
                                            release=release)
    frames = effects.normalize(frames)
    if (len(frames) > config.getfloat('vereinheiter', 'minClipLen') * 1000):
        clipName = os.path.join(clipsDir, fileName)
        frames.export(clipName, format='wav')
        einheit = os.path.basename(fileName).split('_')[0]
        database.writeClip(einheit, clipName)
        print('new clip:', clipName)
        return [einheit, clipName]
    else:
        print('recording too short:', fileName)
        return [None, None]
Ejemplo n.º 6
0
def tools_process_wavs_call(sender, data):
    pname = get_value("tools_project_name")
    if not pname:
        return
    # creat sox transformer
    tfm = sox.Transformer()
    if get_value("tools_trimadd"):
        print("Trimming and padding with silence")
        set_value("tools_status", "Trimming and padding with silence")
        tfm.silence(1, .15, .1)
        tfm.silence(-1, .15, .1)
        tfm.pad(.25, .5)
    if get_value("tools_resample"):
        print("Resampling")
        set_value("tools_status", "Resampling")
        tfm.rate(22050)

    if not os.path.exists(pname + '\\processed'):
        os.mkdir(pname + '\\processed')
    if not os.path.exists(pname + '\\processed\\wavs'):
        os.mkdir(pname + '\\processed\\wavs')

    with open(pname + "\\output.csv", 'r') as f:
        lines = f.readlines()
        for line in lines:
            wav_path, text = line.split('|')
            processedpath = pname + '\\processed\\' + wav_path
            text = text.strip()  #remove \r\n
            tfm.build_file(pname + '\\' + wav_path, processedpath)
            print(f"Processing {wav_path}")
            set_value("tools_status", "Processing {}".format(wav_path))
            if get_value("tools_compress"):
                w = AudioSegment.from_wav(processedpath)
                w = effects.compress_dynamic_range(w, threshold=-10)
                w.export(processedpath, format='wav')

        print("Done processing wavs!")
        set_value(
            "tools_status",
            "Done processing wavs. Output at {}/processed/wavs.".format(pname))
        print('\a')  #system beep