Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
print("to process ./recording/{key}.wav")
sys.exit(1)
key = sys.argv[1:][0]
syllables = data[key]
path = "./recording/" + key + ".wav"
file = Path(path)
if not file.is_file():
raise Exception(path + " doesn't exist")
sound_file = AudioSegment.from_wav(path)
audio_chunks = split_on_silence(sound_file,
# must be silent for at least 300ms
min_silence_len=300,
# consider it silent if quieter than -48 dBFS
silence_thresh=-48
)
flag = False
if len(syllables) * 5 != len(audio_chunks):
flag = True
for i, chunk in enumerate(audio_chunks):
syllable = syllables[i // 5]
print(syllable)
j = i % 5
if j != 4: # 1st, 2nd, 3rd, 4th tone
out_file = "./pre/" + syllable + str(j + 1) + ".wav"
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)
setattr(self, attr, val)
else:
# normal construction
try:
data = data if isinstance(data, (basestring, bytes)) else data.read()
except(OSError):
d = b''
reader = data.read(2 ** 31 - 1)
while reader:
d += reader
reader = data.read(2 ** 31 - 1)
data = d
wav_data = read_wav_audio(data)
if not wav_data:
raise CouldntDecodeError("Couldn't read wav audio from data")
self.channels = wav_data.channels
self.sample_width = wav_data.bits_per_sample // 8
self.frame_rate = wav_data.sample_rate
self.frame_width = self.channels * self.sample_width
self._data = wav_data.raw_data
# Convert 24-bit audio to 32-bit audio.
# (stdlib audioop and array modules do not support 24-bit data)
if self.sample_width == 3:
byte_buffer = BytesIO()
# Workaround for python 2 vs python 3. _data in 2.x are length-1 strings,
# And in 3.x are ints.
pack_fmt = 'BBB' if isinstance(self._data[0], int) else 'ccc'
def get_length_audio(audiopath, extension):
"""
Returns length of audio in seconds.
Returns None if format isn't supported or in case of error.
"""
try:
audio = AudioSegment.from_file(audiopath, extension.replace(".", ""))
except:
print "Error in get_length_audio(): %s" % traceback.format_exc()
return None
return int(len(audio) / 1000.0)
def load_audio_file(filename,resize=False):
sound = None
try:
if filename.endswith('.mp3') or filename.endswith('.MP3'):
sound = AudioSegment.from_mp3(filename)
elif filename.endswith('.wav') or filename.endswith('.WAV'):
sound = AudioSegment.from_wav(filename)
elif filename.endswith('.ogg'):
sound = AudioSegment.from_ogg(filename)
elif filename.endswith('.flac'):
sound = AudioSegment.from_file(filename, "flac")
elif filename.endswith('.3gp'):
sound = AudioSegment.from_file(filename, "3gp")
elif filename.endswith('.3g'):
sound = AudioSegment.from_file(filename, "3gp")
sound = sound.set_frame_rate(samplerate)
sound = sound.set_channels(1)
sound = sound.set_sample_width(2)
duration = sound.duration_seconds
except:
print("Couldn't load file")
return None,None
return sound,duration
def load_audio_file(filename,resize=False):
sound = None
try:
if filename.endswith('.mp3') or filename.endswith('.MP3'):
sound = AudioSegment.from_mp3(filename)
elif filename.endswith('.wav') or filename.endswith('.WAV'):
sound = AudioSegment.from_wav(filename)
elif filename.endswith('.ogg'):
sound = AudioSegment.from_ogg(filename)
elif filename.endswith('.flac'):
sound = AudioSegment.from_file(filename, "flac")
elif filename.endswith('.3gp'):
sound = AudioSegment.from_file(filename, "3gp")
elif filename.endswith('.3g'):
sound = AudioSegment.from_file(filename, "3gp")
sound = sound.set_frame_rate(samplerate)
sound = sound.set_channels(1)
sound = sound.set_sample_width(2)
duration = sound.duration_seconds
except:
print("Couldn't load file")
return None,None
return sound,duration
def play_audio(mp3_filename):
playback.play(AudioSegment.from_mp3(mp3_filename))
def load_audio_file(filename,resize=False):
sound = None
try:
if filename.endswith('.mp3') or filename.endswith('.MP3'):
sound = AudioSegment.from_mp3(filename)
elif filename.endswith('.wav') or filename.endswith('.WAV'):
sound = AudioSegment.from_wav(filename)
elif filename.endswith('.ogg'):
sound = AudioSegment.from_ogg(filename)
elif filename.endswith('.flac'):
sound = AudioSegment.from_file(filename, "flac")
elif filename.endswith('.3gp'):
sound = AudioSegment.from_file(filename, "3gp")
elif filename.endswith('.3g'):
sound = AudioSegment.from_file(filename, "3gp")
sound = sound.set_frame_rate(samplerate)
sound = sound.set_channels(1)
sound = sound.set_sample_width(2)
duration = sound.duration_seconds
except:
print("Couldn't load file")
return None,None
# -*- coding: utf-8 -*-
from pydub import AudioSegment
import librosa
#abertura do arquivo
arq = open('/home/douglas/Música/musicas/wav/tristes/tristes.txt','r')
lines = arq.readlines()
arq.close()
lista = []
count=0
for l in lines:
#carregamento dos arquivos
music, erro = l.split("\n",1)
y, sr = librosa.load('/home/douglas/Música/musicas/wav/tristes/'+music,sr=44100)
sound = AudioSegment.from_wav("/home/douglas/Música/musicas/wav/tristes/"+music)
cent = librosa.feature.rmse(y=y, frame_length=2048, hop_length=512)
a = sound.max_dBFS/cent.mean()
print(music,a)
lista.append(a)
arq = open('/home/douglas/Documentos/tcc_code/resultado/resultados_tristes.csv','r')
musics = arq.readlines()
arq.close()
count=0
arq = open('/home/douglas/Documentos/tcc_code/resultado/resultados_tristes.csv','w')
for m in musics:
music, erro = m.split("\n",1)
arq.write(music+","+str(lista[count])+"\n")
count+=1
# Get a temp file to put our data and a temp file to store the result
tmp = _get_random_tmp_file()
othertmp = _get_random_tmp_file()
# Store our data in the temp file
self.export(tmp.name, format="WAV")
# Write the command to sox
stdout = stderr = subprocess.PIPE if console_output else subprocess.DEVNULL
command = cmd.format(inputfile=tmp.name, outputfile=othertmp.name)
res = subprocess.call(command.split(' '), stdout=stdout, stderr=stderr)
assert res == 0, "Sox did not work as intended, or perhaps you don't have Sox installed?"
# Create a new AudioSegment from the other temp file (where Sox put the result)
other = AudioSegment(pydub.AudioSegment.from_wav(othertmp.name), self.name)
# Clean up the temp files
if on_windows:
os.remove(tmp.name)
os.remove(othertmp.name)
else:
tmp.close()
othertmp.close()
return other