Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def convert_mp3(new, lengths):
import pydub
wav.write("/tmp/load.wav", 16000,
np.array(np.clip(np.round(new[0][:lengths[0]]),
-2**15, 2**15-1),dtype=np.int16))
pydub.AudioSegment.from_wav("/tmp/load.wav").export("/tmp/saved.mp3")
raw = pydub.AudioSegment.from_mp3("/tmp/saved.mp3")
mp3ed = np.array([struct.unpack("
audio = AudioSegment.empty()
if layer:
total_time = max([s['end'] - s['start'] for s in segments]) * 1000
audio = AudioSegment.silent(duration=total_time)
for i, s in enumerate(segments):
try:
start = s['start'] * 1000
end = s['end'] * 1000
f = s['file'].replace('.transcription.txt', '')
if f not in files:
if f.endswith('.wav'):
files[f] = AudioSegment.from_wav(f)
elif f.endswith('.mp3'):
files[f] = AudioSegment.from_mp3(f)
segment = files[f][start:end]
print(start, end, f)
if layer:
audio = audio.overlay(segment, times=1)
else:
if i > 0:
audio = audio.append(segment, crossfade=crossfade)
else:
audio = audio + segment
if padding > 0:
audio = audio + AudioSegment.silent(duration=padding)
def getMp3Duration(mp3path, format="hhmmss"):
if format == "msec":
return len(Aseg.from_mp3(mp3path))
elif format == "hhmmss":
return msec2hhmmss(len(Aseg.from_mp3(mp3path)))
else:
raise Exception("Error: Wrong Duration format selected")
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), '>><<'))
# Concatenate all audio-segment and append it to the interim audiobook (acapellas).
acapella = sum(item[0] for item in asegments)
if self.setting['lrc']:
self._add_lyrics(asegments)
acapella.export(curdir + ".mp3")
self.acapellas.append(acapella)
alexa_client.connect()
while True:
directives = alexa_client.send_audio_file(
input_buffer,
dialog_request_id=dialog_request_id
)
stream.stop_stream()
if directives:
dialog_request_id = None
print('Alexa\'s turn.')
for directive in directives:
if directive.name == 'ExpectSpeech':
dialog_request_id = directive.dialog_request_id
if directive.name in ['Speak', 'Play']:
output_buffer = io.BytesIO(directive.audio_attachment)
track = AudioSegment.from_mp3(output_buffer)
play(track)
input_buffer = io.BytesIO()
stream.start_stream()
print('Your turn. Say something.')
time.sleep(1)
finally:
stream.stop_stream()
stream.close()
p.terminate()
def extract_words(files):
''' Extracts individual words form files and exports them to individual files. '''
output_directory = 'extracted_words'
if not os.path.exists(output_directory):
os.makedirs(output_directory)
for f in files:
file_format = None
source_segment = None
if f.lower().endswith('.mp3'):
file_format = 'mp3'
source_segment = AudioSegment.from_mp3(f)
elif f.lower().endswith('.wav'):
file_format = 'wav'
source_segment = AudioSegment.from_wav(f)
if not file_format or source_segment:
print('Unsupported audio format for ' + f)
sentences = convert_timestamps(files)
for s in sentences:
for word in s['words']:
start = float(word[1]) * 1000
end = float(word[2]) * 1000
word = word[0]
total_time = end - start
audio = AudioSegment.silent(duration=total_time)
audio = audio.overlay(source_segment[start:end])
number = 0
output_path = None
def mp3_to_wav(mp3_filename):
wav_filename = mp3_filename.replace(".mp3", ".wav")
sound = AudioSegment.from_mp3(mp3_filename)
wav = sound.export(wav_filename, format="wav")
return wav_filename
def say(text):
tts = gTTS(text=text, lang=language)
with tempfile.NamedTemporaryFile(suffix='.mp3', delete=False) as f:
tmpfile = f.name
tts.save(tmpfile)
sound = AudioSegment.from_mp3(tmpfile)
os.remove(tmpfile)
with tempfile.SpooledTemporaryFile() as f:
sound.export(f, format="wav")
f.seek(0)
w = wave.open(f, 'rb')
channels = w.getnchannels()
width = w.getsampwidth()
rate = w.getframerate()
chunksize = 1024*10
stream = audio.open(rate=rate, channels=channels, format=audio.get_format_from_width(width), output=True)
data = w.readframes(chunksize)
while data:
stream.write(data)
data = w.readframes(chunksize)
from matplotlib import pyplot as plt
from scipy.stats import gaussian_kde
from scipy.io import wavfile
import theano.tensor as T
import theano
import pydub
batch_size = 128
size_x = 4096
size_z = 256
size_z_fake = 256
assert (((size_z+64-1)*4 + 256 - 1)*4 - 2029 + 1) == size_x
print "loading data"
f = pydub.AudioSegment.from_mp3('../ml-music/07_-_Brad_Sucks_-_Total_Breakdown.mp3')
data = np.fromstring(f._data, np.int16)
data = data.astype(np.float32).reshape((-1,2))
print data.shape
data = data[:,0]+data[:,1]
#data = data[:,:subsample*int(len(data)/subsample)-1,:]
data -= data.min()
data /= data.max() / 2.
data -= 1.
print data.shape
print "Setting up decoder"
decoder = Sequential()
decoder.add(Dense(2048, input_dim = size_x, activation='relu'))
decoder.add(Dropout(0.5))
decoder.add(Dense(2048, activation='relu'))
def cover_music(music_path, cover_path, music_info):
if '.mp3' in music_path:
song=AudioSegment.from_mp3(music_path)
export_fun(song,music_path,cover_path,music_info)
os.remove(cover_path)
elif '.m4a' in music_path:
song=AudioSegment.from_file(music_path,'m4a')
music_path=music_path.replace(".m4a",".mp3")
export_fun(song,music_path,cover_path,music_info)
music_path = music_path.replace(".mp3", ".m4a")
os.remove(music_path)
os.remove(cover_path)
elif '.flac' in music_path:
song=AudioSegment.from_file(music_path,'flac')
export_fun(song,music_path,cover_path,music_info)