def home(): if request.method == 'GET': text = "Heya" else: text = request.form["words"] inSound = "static/voice_sans.wav" outSound = "static/result.wav" # create 0.1 sec of silence audio segment space_segment = AudioSegment.silent( duration=100) #duration in milliseconds #read wav file to an audio segment song = AudioSegment.from_wav(inSound) try: speedy_song = speedup(song * 6, 10.0, crossfade=5) except: speedy_song = song outVoice = AudioSegment.silent(duration=10) for character in text: if not character.isalnum(): if character == " ": outVoice = outVoice + song + space_segment else: outVoice = outVoice + speedy_song outVoice = speedup(outVoice, 8.0) outVoice.export(outSound, format="wav") wave_obj = sa.WaveObject.from_wave_file(outSound) wave_obj.play() return render_template("underteller.html", message=text)
def compress_wav(file, max_silence_time): track = AudioSegment.from_file(file, format='wav') silences = detect_silence(track, min_silence_len=max_silence_time, silence_thresh=track.dBFS * 2, seek_step=1) compressed_track = AudioSegment.empty() prev_cursor = 0 for silence in silences: compressed_track = compressed_track + track[prev_cursor:silence[0]] silence_track = track[silence[0]:silence[1]] speedup_track = speedup(silence_track, playback_speed=(len(silence_track) / max_silence_time), chunk_size=150, crossfade=100) fade_in = speedup_track.fade( from_gain=0, end=0, duration=250 ) compressed_track = compressed_track + fade_in prev_cursor = silence[1] compressed_track = compressed_track + track[prev_cursor:] compressed_track.export(file, format="wav")
def speedSound(self, sound, keepTime): # 上一个的长度 beforeLen = 0 left = 100 right = 130 beforeSound = 0 curSound = 0 while left < right: middle = int((left + right) / 2) # 参数: 音频 倍数 块大小 过渡时间 curSound = effects.speedup(sound, middle / 100, 300, 100) soundLen = len(curSound) if soundLen == keepTime: return curSound elif right - left == 1 and soundLen < keepTime and soundLen < beforeLen: return beforeSound elif soundLen < keepTime: right = middle + 1 beforeSound = curSound beforeLen = soundLen elif soundLen > keepTime: left = middle + 1 beforeSound = curSound beforeLen = soundLen return curSound
def audioProcessing(sample_list): procs = [] for s in sample_list: sample = AudioSegment.from_file(s,s.split('.')[1]) if settings['NORMALIZE']: sample = normalize(sample) if settings['SILENCE']: sample = strip_silence(sample, 1000, silence_thresh=(sample.dBFS-6)) if settings['PADDING']: sample += AudioSegment.silent(duration=settings['PADDING']) if settings['PB_SPEED'] != 1: sample = speedup(sample, settings['PB_SPEED']) if not settings['CONCAT']: # Exporting indivudial samples sample_name = s.split('/')[-1].split('.')[0] export(sample, sample_name, settings['OUTPUT_FORMAT'], settings['OUTPUT_BITRATE']) else: procs.append(sample) if settings['CONCAT']: # Exporting sample chain concatSamples(procs, raw_input('Enter sample chain name: ')) if settings['REM_INDIVIDUAL_SAMPLES']: removeIndividualSamples(sample_list)
def trackSpeedUp(mSong, iFactor, sTitle, sArtist, sAlbum): tempSong = speedup(mSong, float(iFactor)) return { sTitle: tempSong.export("tempfiles/" + sTitle + ".mp3", format="mp3", tags={ "artist": sArtist, "album": sAlbum }) }
def speedChange(sound, time): speed = sound.duration_seconds / time if speed > 1: return speedup(sound, speed) elif speed < 1: silentt = AudioSegment.silent( duration=(time - sound.duration_seconds) * 1000) sound += silentt return sound else: return sound
def get_speak_file(message_content, lang, speed, tld): pre_processed = BytesIO() post_processed = BytesIO() try: spoken_google = gTTS(message_content, lang=lang, tld=tld) spoken_google.write_to_fp(fp=pre_processed) pre_processed.seek(0) segment = pydub.AudioSegment.from_file(pre_processed, bitrate=356000, format="mp3") segment = effects.speedup(segment, 1.25, 150, 25) # normally 1.25 frames = int(95000 * 1 / speed) segment.set_frame_rate(frames).export(post_processed, format="s16le") return post_processed except AssertionError: return post_processed
def adjust_audio_length(self, next_segment_start=None): ''' Adjusts self.audio length to fit a gap between TTS segments. params: next_segment_start - float, seconds if next_segment_start=None, audio length is intact ''' if not next_segment_start or not self.audio: return gap = datetime.timedelta(seconds=(next_segment_start - self.start)) l = datetime.timedelta(seconds=self.__len__()) if l > gap: rate = l / gap alt = speedup(self.audio, playback_speed=rate) elif l < gap: silence = gap - l silence_duration = silence.total_seconds() * 1000 silence_audio = AudioSegment.silent(duration=silence_duration) alt = AudioSegment.empty() alt += self.audio + silence_audio else: return self.audio = alt
def get_pronounces_mp3(self, pronounce_list, playback_speed=2): filename = "" for pronounce in pronounce_list: if pronounce: filename += pronounce if "" == filename: return None filename += ".mp3" filepath = os.path.join(self.words_audio_folder,filename) exist = os.path.isfile(filepath) if exist: return filepath else: pronounces_mp3 = None first = True for pronounce in pronounce_list: if pronounce: mp3 = self.get_mp3_pronounce(pronounce) if mp3: if first: pronounces_mp3 = mp3 first = False else: pronounces_mp3 = pronounces_mp3 + mp3 if not pronounces_mp3: return None else: pronounces_mp3 = speedup(pronounces_mp3,playback_speed) # #判断如果超过60秒截断为60秒,微信允许60秒内语音 # seconds = len(pronounces_mp3) / 1000 # if seconds > 60: # pronounces_mp3 = pronounces_mp3[:1000*60] # logging.info("{0} play time over 60 seconds".format(filename)) pronounces_mp3.export(filepath, format="mp3") return filepath
def get_pronounces_mp3(self, pronounce_list, playback_speed=2): filename = "" for pronounce in pronounce_list: if pronounce: filename += pronounce if "" == filename: return None filename += ".mp3" filepath = os.path.join(self.words_audio_folder, filename) exist = os.path.isfile(filepath) if exist: return filepath else: pronounces_mp3 = None first = True for pronounce in pronounce_list: if pronounce: mp3 = self.get_mp3_pronounce(pronounce) if mp3: if first: pronounces_mp3 = mp3 first = False else: pronounces_mp3 = pronounces_mp3 + mp3 if not pronounces_mp3: return None else: pronounces_mp3 = speedup(pronounces_mp3, playback_speed) # #判断如果超过60秒截断为60秒,微信允许60秒内语音 # seconds = len(pronounces_mp3) / 1000 # if seconds > 60: # pronounces_mp3 = pronounces_mp3[:1000*60] # logging.info("{0} play time over 60 seconds".format(filename)) pronounces_mp3.export(filepath, format="mp3") return filepath
def speedUp(sound, speed): return effects.speedup(sound, speed)
def applyEffects(self, track, title): if random.randint(0, 101) < self.probArray[0]: track = track * random.randint(2, 5) print("added repeat effect") if random.randint(0, 101) < self.probArray[1]: print("Doing shuffle") splitted = track[::1000] splitList = list(splitted) random.shuffle(splitList) result = AudioSegment.empty() for segment in splitList: result += segment track = result # random.shuffle(splitted) # track = splitted if random.randint(0, 101) < self.probArray[2]: print("adding overlay effect") overlayTrack, new_title = self.prepareNextTrack() title = title + " + " + new_title loop = bool(random.getrandbits(1)) position = random.randint(0, len(track)) gain = random.randint(-8, 9) track = track.overlay(overlayTrack, position=position, loop=loop, gain_during_overlay=gain) if random.randint(0, 101) < self.probArray[3]: print("adding fade effect") fromGain = round(random.uniform(-8, 0), 2) toGain = round(random.uniform(0.1, 10), 2) start = random.randint(0, len(track)) end = random.randint(start, len(track) + 1) track = track.fade(to_gain=toGain, from_gain=fromGain, start=start, end=end) if random.randint(0, 101) < self.probArray[4]: print("adding gain effect") gain = round(random.uniform(-20, 21), 2) track = track + gain if random.randint(0, 101) < self.probArray[5]: print("adding reverse effect") track = track.reverse() if random.randint(0, 101) < self.probArray[6]: print("adding pan effect") pan = round(random.uniform(-1.0, 1.1), 2) track = track.pan(pan) if random.randint(0, 101) < self.probArray[7]: print("adding invert phase effect") track = track.invert_phase() if random.randint(0, 101) < self.probArray[8]: #Look into this later print("should be doing lower quality") track.set_sample_width(1) track.set_channels(1) track.set_frame_rate(11025) if random.randint(0, 101) < self.probArray[9] and len(track) > 150: print("adding speed effect") speed = round(random.uniform(1, 2), 2) print("speed: " + str(speed)) track = speedup(track, playback_speed=speed) return track, title
def _speedup(self, audio_segment): print "Speeding up by %s" % self.speed return speedup(audio_segment, playback_speed=self.speed)
dSpeedRate = sys.argv[4] strFilePath = sys.argv[5] strFileName = sys.argv[6] nListenSpeech = sys.argv[7] myobj = gTTS(text=strText, lang=strLanguage, slow=False) if not os.path.exists(strFilePath): os.makedirs(strFilePath) #mp3_fp = BytesIO() #myobj.write_to_fp(mp3_fp) # Saving the converted audio in a mp3 file named myobj.save(strFilePath + "\\" + strFileName + ".mp3") sound = AudioSegment.from_file(strFilePath + "\\" + strFileName + ".mp3") if float(dSpeedRate) > 1.0: sound = effects.speedup(sound, float(dSpeedRate)) sound = sound._spawn(sound.raw_data, overrides={'frame_rate': int(nSampleRate)}) sound.export(strFilePath + "\\" + strFileName + ".mp3", format="mp3", bitrate="48k") # Playing the converted file if nListenSpeech == "1": os.system('"' + strFilePath + "\\" + strFileName + ".mp3" + '"')
editFile = audioInput(args.filename) minDB_silence = -20 num = 0 dBTable = {"low":-25,"moderate":-30,"high":-40} output = split_on_silence(editFile, 250, dBTable[args.environment_sound], 100,seek_step=1) try: outputFile = output[0] for a in range(1, len(output)): outputFile += output[a] #print(str(num)) num+=1 except: outputFile = editFile compression = int(100.0* round(1.0 - (float(len(outputFile))/float(len(editFile))),4)) #print("Trying with silence at " + str(minDB_silence) + "dB and the compression is " + str(compression)) exportFile = outputFile if(args.playback_speed != 1): newExport = speedup(exportFile, args.playback_speed, 150, 25) newExport.export("Cut_"+ args.filename, format='mp3') else: exportFile.export("Cut_"+ args.filename, format='mp3') #print("Done. Cut_" + args.filename + " was cut down by " + str(compression) + "%") print("Cut_"+ args.filename)
def change_speed(self, path, coefficient): song = AudioSegment.from_file(path) effects.speedup(song, coefficient).export(path[:len(path) - 4] + "_speedup.wav", format="wav") return "_speedup.wav is ready"
rel_pos += '..' + os.sep #print('Moving: %s -> %s' % (lastdir, os.getcwd())) samples = find_samples(name) #print('Got:', samples) os.chdir(restore) return AudioSegment.from_file(rel_pos + samples[0], fmt) do = my_sample('taiko4.wav') - 2 ko = my_sample('taiko2.wav') - 2 don = my_sample('taiko3.wav') + 2 kon = my_sample('taiko5.wav') + 2 rim = my_sample('rim.wav') - 12 sa = effects.speedup(my_sample('sa.wav')[307:], playback_speed=4.5) - 12 #sa = effects.speedup(my_sample('sa.wav')[310:800], playback_speed=3.5) - 12 shime_do = my_sample('shime1.wav') - 8 shime_ko = my_sample('shime2.wav') - 8 shime_don = my_sample('shime3.wav') Don = my_sample('taiko1.wav') #Don = don.overlay(kon) click = my_sample('click.wav') - 12 char_map = { 'd': do, 'k': ko, 'D': don,
if settings.Debug: print(f"-----Splitting file {file.name}-----") while end < length: start_segment = time.perf_counter() end = min(start + segment_length, length) if settings.Debug: print(f"Splitting segment {i}: {start/1000}s - {end/1000}s") start_action = time.perf_counter() segment = sound[start:end] end_action = time.perf_counter() if settings.Debug: print( f"Split complete in {end_action - start_action:0.4f} seconds") if (settings.PlaybackSpeed > 1): if settings.Debug: print("Adjusting playback speed") start_action = time.perf_counter() segment = effects.speedup(segment, playback_speed=settings.PlaybackSpeed) end_action = time.perf_counter() if settings.Debug: print( f"Playback speed ajdusted in {end_action - start_action:0.4f} seconds" ) start += segment_length - settings.OverlapLen outFile = "{}{}{}{}{}".format(settings.OutputDir, file.name[:3], "-p", i, ".mp3") if settings.Debug: print("Exporting to " + outFile) start_action = time.perf_counter() segment.export(outFile, format="mp3") end_action = time.perf_counter() if settings.Debug: print(