How to use the pydub.AudioSegment.silent function in pydub

To help you get started, we’ve selected a few pydub examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github PatrickDuncan / cleansio / cleansio / audio / audio_file.py View on Github external
def __last_overlapping_chunk(
            self, audio_segment, temp_dir, chunks_arr, overlapping):
        """ Check if the chunk is long enough to support overlapping """
        if overlapping and len(audio_segment) % CHUNK_LEN < 4000:
            chunk_path = self.__create_chunk(
                len(audio_segment) // CHUNK_LEN, # Last index
                AudioSegment.silent(frame_rate=44100),   # Silent chunk
                'wav', temp_dir, overlapping)
            chunks_arr.append(chunk_path)
github KeelyHill / Prime-Number-Audio-Book / prime_audio.py View on Github external
def main():

    print("Gettings raw number sound bites.")

    # get each sound as pydub audio segment and add to list for easy access
    for i in range(10):
        number_sounds.append(AudioSegment.from_ogg("sound_bites/%i.ogg" % i))

    # load in the beast by the lines of the file
    lines = loadBigNumFileToList()

    print("Creating blank audio file in memory.")
    output = AudioSegment.silent(duration=500) # 'blank' slate to append to.

    job_server = pp.Server()

    print("Splitting labor, and starting")
    # Define jobs, cpu cores/2 in my case
    #                                               give range    and other params
    job1 = job_server.submit(processRangeForLines, (range(0,10), lines, number_sounds))
    job2 = job_server.submit(processRangeForLines, (range(10,20), lines, number_sounds))

    # execute and grab value
    job1_audio = job1()
    job2_audio = job2()

    print("Final concatenation.")
    output += job1_audio + job2_audio
github kokimame / joytan / joytan / routine / dubbing.py View on Github external
for fi in self.flowlist:
            if isinstance(fi, Aseg):
                asegments.append((fi, ''))
                continue

            if fi['desc'] == 'INDEX':
                index = "%d " % (ew.row + 1)
                idx_file = os.path.join(curdir, "index") + ".mp3"
                self.routers['atop'](path=idx_file, text=index)
                for _ in range(fi['repeat']):
                    aseg = Aseg.from_mp3(idx_file)
                    rdbfs = reduce_dbfs(aseg.dBFS, (1 - fi['volume'] / 100))
                    asegments.append((aseg - rdbfs, index))
                    if fi['postrest'] > 0:
                        asegments.append((Aseg.silent(int(fi['postrest'] * 1000)), ''))

            else:
                ewkey = fi['desc']
                path = os.path.join(curdir, ewkey) + ".mp3"
                if ew[ewkey] != '':
                    self.routers[ewkey](path=path, text=ew[ewkey])
                    for _ in range(fi['repeat']):
                        aseg = Aseg.from_mp3(path)
                        rdbfs = reduce_dbfs(aseg.dBFS, (1 - fi['volume'] / 100))
                        asegments.append((aseg - rdbfs, ew[ewkey]))
                        if fi['postrest'] > 0:
                            asegments.append((Aseg.silent(int(fi['postrest'] * 1000)), ''))

        # '>><<' represents the end of one EntryWidget.
        # This lets you know the timing to switch images on video-making
        asegments.append((Aseg.silent(0), '>><<'))
github HA6Bots / Automatic-Youtube-Reddit-Text-To-Speech-Video-Generator-and-Uploader / Youtube Bot Video Generator / generatemovie.py View on Github external
def addFrameWithPause(self, image_file, audio_file, pause):
        audio_file = audio_file.replace("\\", "/")
        f = sf.SoundFile(audio_file)
        audio_clip = AudioSegment.from_wav(audio_file)
        duration = (len(f) / f.samplerate) + pause / 1000
        audio_clip_with_pause = audio_clip + AudioSegment.silent(duration=pause)
        self.imageframes.append(image_file)
        self.audiofiles.append(audio_clip_with_pause)
        self.durations.append(duration)
github MaxStrange / AudioSegment / audiosegment.py View on Github external
def silent(duration=1000, frame_rate=11025):
    """
    Creates an AudioSegment object of the specified duration/frame_rate filled with digital silence.

    :param duration: The duration of the returned object in ms.
    :param frame_rate: The samples per second of the returned object.
    :returns: AudioSegment object filled with pure digital silence.
    """
    seg = pydub.AudioSegment.silent(duration=duration, frame_rate=frame_rate)
    return AudioSegment(seg, "")
github corticph / MSTmodel / EndToEndClassification / Utilities / utilities.py View on Github external
def load_audio(path, duration=5000, framerate=22050, channel_nr=1):
    """
    Loads .ogg files and converts to np.array with specified nr of channels, and sample rate.

    Args:
        path (str): to the audio file.
        duration (int): duration in ms.
        framerate (int): sample rate.
        channel_nr (int): numer of channels.

    Returns:
        (np.array): raw waveform.

    """
    audio = pydub.AudioSegment.silent(duration=duration)
    audio = audio.overlay(pydub.AudioSegment.from_file(path).set_frame_rate(framerate).set_channels(channel_nr))[
            0:duration]
    raw = (np.fromstring(audio._data, dtype="int16") + 0.5) / (0x7FFF + 0.5)  # convert to float

    return raw
github bobgonzalez / spEdit404 / depricated / note.py View on Github external
self.length = length
            self.next_note = next
            self.teenth = self.time_start / 24
            pd = str(self.pad)
            if len(pd) == 1:
                pd = "0" + pd
            self.path = "/home/robert/PycharmProjects/spEdit404/SP-404SX/SMPL/" + self.bank.upper() + \
                        "00000" + pd + ".WAV"
            print(self.path)
            silence = AudioSegment.from_file("/home/robert/PycharmProjects/spEdit404/silence.wav")
            s1 = silence[:self.timer.milli_per_tick]
            start = s1 * self.time_start
            self.audio = start + AudioSegment.from_file(self.path)[:self.length * self.timer.milli_per_tick]
            #self.audio.export("/home/robert/PycharmProjects/spEdit404/mixed.wav", format='wav')
        else:
            self.audio = AudioSegment.silent(duration=10000)
            self.time_start = start
            self.time_end = 60
            self.pad = 0
            self.bank = 0
            self.bank_switch = 0
            self.pad_code = str(hex(128))[2:]
            self.velocity = 127
            self.length = 60
            self.next_note = next
github junzew / HanTTS / main.py View on Github external
dst is the destination folder to save the synthesized file
        """
        print("Synthesizing ...")
        delay = 0
        increment = 355 # milliseconds
        pause = 500 # pause for punctuation
        syllables = lazy_pinyin(text, style=pypinyin.TONE3)

        # initialize to be complete silence, each character takes up ~500ms
        result = AudioSegment.silent(duration=500*len(text))
        for syllable in syllables:
            path = src+syllable+".wav"
            sound_file = Path(path)
            # insert 500 ms silence for punctuation marks
            if syllable in TextToSpeech.punctuation:
                short_silence = AudioSegment.silent(duration=pause)
                result = result.overlay(short_silence, position=delay)
                delay += increment
                continue
            # skip sound file that doesn't exist
            if not sound_file.is_file():
                continue
            segment = AudioSegment.from_wav(path)
            result = result.overlay(segment, position=delay)
            delay += increment

        directory = dst
        if not os.path.exists(directory):
            os.makedirs(directory)

        result.export(directory+"generated.wav", format="wav")
        print("Exported.")
github HA6Bots / Automatic-Youtube-Reddit-Text-To-Speech-Video-Generator-and-Uploader / Youtube Bot Video Generator / generatemovie.py View on Github external
#gc.collect()
        main_vid_duration = 0
        for i in range(1, len(clips), 1):
            main_vid_duration += clips[i].duration

        print("Generating Audio Loop (%s) " % main_vid_duration)
        print("Using Audio Loop %s" % self.background_music_name)
        music_loop = afx.audio_loop(AudioFileClip(self.background_music_name).fx(afx.volumex, 0.2),
                                    duration=int(main_vid_duration))
        music_loop.to_audiofile("%s/music-loop.wav" % settings.tempPath)
        #del music_loop
        #gc.collect()
        pause_time = int(clips[0].duration * 1000)
        print("Adding pause to start of Audio Loop (%s) " % (pause_time / 1000))
        audio_clip = AudioSegment.from_wav("%s/music-loop.wav" % settings.tempPath)
        new_audio = AudioSegment.silent(duration=(pause_time)) + audio_clip
        new_audio.export("%s/music-loop2.wav" % settings.tempPath, format='wav')
        #del new_audio
        #gc.collect()
        #video_with_audio = main_vid.set_audio(CompositeAudioClip([main_vid.audio, music_loop]))

        # here we are combining the first clip with the last
        print("Combining all Video Clips %s" % (pause_time / 1000))
        main_vid_combined = concatenate_videoclips(clips)
        #del clips
        #gc.collect()
        main_vid_with_audio = main_vid_combined.set_audio(CompositeAudioClip([main_vid_combined.audio, AudioFileClip("%s/music-loop2.wav" % settings.tempPath)]))
        #del main_vid_combined
        #gc.collect()

        folder_location = settings.finishedvideosdirectory + "/vid%s" % self.scriptno
        if not os.path.exists(folder_location):
github xiaoyiT / Course-Project---Speech-Driven-Facial-Animation / CNN_RNN / preprocess_train.py View on Github external
temp=list(filename)
        temp[1]='3'
        filename="".join(temp)
        fname = "./speech/"+filename.replace('mp4','wav')
        audio_fft =[]
        old_speech = AudioSegment.from_wav(fname)
        cnt1=-1
        cnt2=1
        while cnt1=0:
                newAudio = old_speech[t1:t2]
            else:
                newAudio= AudioSegment.silent(duration=step,frame_rate=old_speech.frame_rate)
                newAudio=newAudio+old_speech[0:t2]
            data = newAudio.get_array_of_samples()

            normalize_data=[(ele/2**16.)*2-1 for ele in data] # this is 8-bit track, b is now normalized on [-1,1)
            if len(normalize_data)!=0:
                fft_step=128;
                fft_cnt=0;
                fft_size=len(normalize_data)
                result=[]
                while (fft_cnt)*fft_step