本文整理汇总了Python中soundfile.SoundFile方法的典型用法代码示例。如果您正苦于以下问题:Python soundfile.SoundFile方法的具体用法?Python soundfile.SoundFile怎么用?Python soundfile.SoundFile使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类soundfile
的用法示例。
在下文中一共展示了soundfile.SoundFile方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: addFrameWithTransition
# 需要导入模块: import soundfile [as 别名]
# 或者: from soundfile import SoundFile [as 别名]
def addFrameWithTransition(self, image_file, audio_file, transition_file):
media_info = MediaInfo.parse(transition_file)
duration_in_ms = media_info.tracks[0].duration
audio_file = audio_file.replace("\\", "/")
try:
audio_clip = AudioSegment.from_wav(r"%s"%audio_file)
f = sf.SoundFile(r"%s"%audio_file)
except Exception as e:
print(e)
audio_clip = AudioSegment.from_wav("%s/pause.wav" % settings.assetPath)
f = sf.SoundFile("%s/pause.wav" % settings.assetPath)
duration = (len(f) / f.samplerate)
audio_clip_with_pause = audio_clip
self.imageframes.append(image_file)
self.audiofiles.append(audio_clip_with_pause)
self.durations.append(duration)
self.transitions.append((transition_file, len(self.imageframes) - 1, duration_in_ms / 1000))
开发者ID:HA6Bots,项目名称:Automatic-Youtube-Reddit-Text-To-Speech-Video-Generator-and-Uploader,代码行数:19,代码来源:generatemovie.py
示例2: addFrameWithTransitionAndPause
# 需要导入模块: import soundfile [as 别名]
# 或者: from soundfile import SoundFile [as 别名]
def addFrameWithTransitionAndPause(self, image_file, audio_file, transition_file, pause):
media_info = MediaInfo.parse(transition_file)
duration_in_ms = media_info.tracks[0].duration
audio_file = r"%s"%audio_file
f = sf.SoundFile(audio_file)
try:
audio_clip = AudioSegment.from_wav(audio_file)
except:
print("error with frame audio transition pause for %s" % audio_file)
audio_clip = AudioSegment.silent(duration=pause)
duration = (len(f) / f.samplerate)
audio_clip_with_pause = audio_clip
self.imageframes.append(image_file)
self.audiofiles.append(audio_clip_with_pause)
self.durations.append(duration + (pause/1000))
self.transitions.append((transition_file, len(self.imageframes) - 1, (duration_in_ms / 1000) + (pause/1000)))
开发者ID:HA6Bots,项目名称:Automatic-Youtube-Reddit-Text-To-Speech-Video-Generator-and-Uploader,代码行数:18,代码来源:generatemovie.py
示例3: from_file
# 需要导入模块: import soundfile [as 别名]
# 或者: from soundfile import SoundFile [as 别名]
def from_file(cls, filename, target_sr=None, int_values=False, offset=0,
duration=0, trim=False):
"""
Load a file supported by librosa and return as an AudioSegment.
:param filename: path of file to load
:param target_sr: the desired sample rate
:param int_values: if true, load samples as 32-bit integers
:param offset: offset in seconds when loading audio
:param duration: duration in seconds when loading audio
:return: numpy array of samples
"""
with sf.SoundFile(filename, 'r') as f:
dtype = 'int32' if int_values else 'float32'
sample_rate = f.samplerate
if offset > 0:
f.seek(int(offset * sample_rate))
if duration > 0:
samples = f.read(int(duration * sample_rate), dtype=dtype)
else:
samples = f.read(dtype=dtype)
samples = samples.transpose()
return cls(samples, sample_rate, target_sr=target_sr, trim=trim)
示例4: readWave
# 需要导入模块: import soundfile [as 别名]
# 或者: from soundfile import SoundFile [as 别名]
def readWave(audio_path, start_frame, end_frame, mono=True, sample_rate=None, clip=True):
snd_file = SoundFile(audio_path, mode='r')
inf = snd_file._info
audio_sr = inf.samplerate
snd_file.seek(start_frame)
audio = snd_file.read(end_frame - start_frame, dtype='float32')
snd_file.close()
audio = audio.T # Tuple to numpy, transpose axis to (channels, frames)
# Convert to mono if desired
if mono and len(audio.shape) > 1 and audio.shape[0] > 1:
audio = np.mean(audio, axis=0)
# Resample if needed
if sample_rate is not None and sample_rate != audio_sr:
audio = Utils.resample(audio, audio_sr, sample_rate)
audio_sr = sample_rate
# Clip to [-1,1] if desired
if clip:
audio = np.minimum(np.maximum(audio, -1.0), 1.0)
return audio, audio_sr
示例5: get_audio_metadata
# 需要导入模块: import soundfile [as 别名]
# 或者: from soundfile import SoundFile [as 别名]
def get_audio_metadata(audioPath, sphereType=False):
'''
Returns sampling rate, number of channels and duration of an audio file
:param audioPath:
:param sphereType:
:return:
'''
ext = os.path.splitext(audioPath)[1][1:].lower()
if ext=="aiff" or sphereType: # SPHERE headers for the TIMIT dataset
audio = scikits.audiolab.Sndfile(audioPath)
sr = audio.samplerate
channels = audio.channels
duration = float(audio.nframes) / float(audio.samplerate)
elif ext=="mp3": # Use ffmpeg/ffprobe
sr, channels, duration = get_mp3_metadata(audioPath)
else:
snd_file = SoundFile(audioPath, mode='r')
inf = snd_file._info
sr = inf.samplerate
channels = inf.channels
duration = float(inf.frames) / float(inf.samplerate)
return int(sr), int(channels), float(duration)
示例6: audio_read
# 需要导入模块: import soundfile [as 别名]
# 或者: from soundfile import SoundFile [as 别名]
def audio_read(example):
"""
:param example: example dict
:return: example dict with audio_data added
"""
audio_keys = ['rir', 'speech_source']
keys = list(example['audio_path'].keys())
example['audio_data'] = dict()
for audio_key in audio_keys:
assert audio_key in keys, (
f'Trying to read {audio_key} but only {keys} are available'
)
audio_data = list()
for wav_file in example['audio_path'][audio_key]:
with soundfile.SoundFile(wav_file, mode='r') as f:
audio_data.append(f.read().T)
example['audio_data'][audio_key] = np.array(audio_data)
return example
示例7: from_file
# 需要导入模块: import soundfile [as 别名]
# 或者: from soundfile import SoundFile [as 别名]
def from_file(
cls, audio_file, target_sr=None, int_values=False, offset=0, duration=0, trim=False,
):
"""
Load a file supported by librosa and return as an AudioSegment.
:param audio_file: path of file to load
:param target_sr: the desired sample rate
:param int_values: if true, load samples as 32-bit integers
:param offset: offset in seconds when loading audio
:param duration: duration in seconds when loading audio
:return: numpy array of samples
"""
with sf.SoundFile(audio_file, 'r') as f:
dtype = 'int32' if int_values else 'float32'
sample_rate = f.samplerate
if offset > 0:
f.seek(int(offset * sample_rate))
if duration > 0:
samples = f.read(int(duration * sample_rate), dtype=dtype)
else:
samples = f.read(dtype=dtype)
samples = samples.transpose()
return cls(samples, sample_rate, target_sr=target_sr, trim=trim)
示例8: segment_from_file
# 需要导入模块: import soundfile [as 别名]
# 或者: from soundfile import SoundFile [as 别名]
def segment_from_file(cls, audio_file, target_sr=None, n_segments=0, trim=False):
"""Grabs n_segments number of samples from audio_file randomly from the
file as opposed to at a specified offset.
Note that audio_file can be either the file path, or a file-like object.
"""
with sf.SoundFile(audio_file, 'r') as f:
sample_rate = f.samplerate
if n_segments > 0 and len(f) > n_segments:
max_audio_start = len(f) - n_segments
audio_start = random.randint(0, max_audio_start)
f.seek(audio_start)
samples = f.read(n_segments, dtype='float32')
else:
samples = f.read(dtype='float32')
samples = samples.transpose()
return cls(samples, sample_rate, target_sr=target_sr, trim=trim)
示例9: read_wav
# 需要导入模块: import soundfile [as 别名]
# 或者: from soundfile import SoundFile [as 别名]
def read_wav(self, wavfilename):
if wavfilename.find('@' + '/') >= 0:
zip_obj, file_inzip = self.get_zip_obj_and_filename(wavfilename, zip_mode='r')
byte_chunk = zip_obj.read(file_inzip)
byte_stream = io.BytesIO(byte_chunk)
with soundfile.SoundFile(byte_stream, 'r') as f:
fs_read = f.samplerate
x = f.read()
else:
with soundfile.SoundFile(wavfilename, 'r') as f:
fs_read = f.samplerate
x = f.read()
if fs_read != self.fs:
x = resampy.resample(x, fs_read, self.fs)
fs_read = self.fs
return fs_read, x.astype(self.dtype)
示例10: from_file
# 需要导入模块: import soundfile [as 别名]
# 或者: from soundfile import SoundFile [as 别名]
def from_file(cls, filename, target_sr=None, int_values=False, offset=0,
duration=0, trim=False):
"""
Load a file supported by librosa and return as an AudioSegment.
:param filename: path of file to load
:param target_sr: the desired sample rate
:param int_values: if true, load samples as 32-bit integers
:param offset: offset in seconds when loading audio
:param duration: duration in seconds when loading audio
:return: numpy array of samples
"""
with sf.SoundFile(filename, 'r') as f:
dtype = 'int32' if int_values else 'float32'
sample_rate = f.samplerate
if offset > 0:
f.seek(int(offset * sample_rate))
if duration > 0:
samples = f.read(int(duration * sample_rate), dtype=dtype)
else:
samples = f.read(dtype=dtype)
samples = samples.transpose()
return cls(samples, sample_rate, target_sr=target_sr, trim=trim)
示例11: preprocess_one_dir
# 需要导入模块: import soundfile [as 别名]
# 或者: from soundfile import SoundFile [as 别名]
def preprocess_one_dir(in_dir, out_dir, out_filename):
""" Create .json file for one condition."""
file_infos = []
in_dir = os.path.abspath(in_dir)
wav_list = os.listdir(in_dir)
wav_list.sort()
for wav_file in wav_list:
if not wav_file.endswith('.wav'):
continue
wav_path = os.path.join(in_dir, wav_file)
samples = sf.SoundFile(wav_path)
file_infos.append((wav_path, len(samples)))
if not os.path.exists(out_dir):
os.makedirs(out_dir)
with open(os.path.join(out_dir, out_filename + '.json'), 'w') as f:
json.dump(file_infos, f, indent=4)
示例12: audio_shape
# 需要导入模块: import soundfile [as 别名]
# 或者: from soundfile import SoundFile [as 别名]
def audio_shape(path):
"""
>>> path = '/net/fastdb/chime3/audio/16kHz/isolated/dt05_caf_real/F01_050C0102_CAF.CH1.wav'
>>> audio_shape(path)
122111
>>> path = '/net/db/voiceHome/audio/noises/dev/home3_room2_arrayGeo3_arrayPos2_noiseCond1.wav'
>>> audio_shape(path) # correct for multichannel
(8, 960000)
>>> audioread(path)[0].shape
(8, 960000)
"""
with soundfile.SoundFile(str(path)) as f:
channels = f.channels
if channels == 1:
return len(f)
else:
return channels, len(f)
示例13: spectrogram_from_file
# 需要导入模块: import soundfile [as 别名]
# 或者: from soundfile import SoundFile [as 别名]
def spectrogram_from_file(filename, step=10, window=20, max_freq=None,
eps=1e-14, overwrite=False, save_feature_as_csvfile=False):
""" Calculate the log of linear spectrogram from FFT energy
Params:
filename (str): Path to the audio file
step (int): Step size in milliseconds between windows
window (int): FFT window size in milliseconds
max_freq (int): Only FFT bins corresponding to frequencies between
[0, max_freq] are returned
eps (float): Small value to ensure numerical stability (for ln(x))
"""
csvfilename = filename.replace(".wav", ".csv")
if (os.path.isfile(csvfilename) is False) or overwrite:
with soundfile.SoundFile(filename) as sound_file:
audio = sound_file.read(dtype='float32')
sample_rate = sound_file.samplerate
if audio.ndim >= 2:
audio = np.mean(audio, 1)
if max_freq is None:
max_freq = sample_rate / 2
if max_freq > sample_rate / 2:
raise ValueError("max_freq must not be greater than half of "
" sample rate")
if step > window:
raise ValueError("step size must not be greater than window size")
hop_length = int(0.001 * step * sample_rate)
fft_length = int(0.001 * window * sample_rate)
pxx, freqs = spectrogram(
audio, fft_length=fft_length, sample_rate=sample_rate,
hop_length=hop_length)
ind = np.where(freqs <= max_freq)[0][-1] + 1
res = np.transpose(np.log(pxx[:ind, :] + eps))
if save_feature_as_csvfile:
np.savetxt(csvfilename, res)
return res
else:
return np.loadtxt(csvfilename)
示例14: addFrame
# 需要导入模块: import soundfile [as 别名]
# 或者: from soundfile import SoundFile [as 别名]
def addFrame(self, image_file, audio_file):
audio_file = audio_file.replace("\\", "/")
try:
audio_clip = AudioSegment.from_wav(r"%s"%audio_file)
f = sf.SoundFile(r"%s"%audio_file)
except Exception as e:
print(e)
audio_clip = AudioSegment.from_wav("%s/pause.wav" % settings.assetPath)
f = sf.SoundFile("%s/pause.wav" % settings.assetPath)
duration = len(f) / f.samplerate
self.imageframes.append(image_file)
self.audiofiles.append(audio_clip)
self.durations.append(duration)
开发者ID:HA6Bots,项目名称:Automatic-Youtube-Reddit-Text-To-Speech-Video-Generator-and-Uploader,代码行数:16,代码来源:generatemovie.py
示例15: addFrameWithPause
# 需要导入模块: import soundfile [as 别名]
# 或者: from soundfile import SoundFile [as 别名]
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)
开发者ID:HA6Bots,项目名称:Automatic-Youtube-Reddit-Text-To-Speech-Video-Generator-and-Uploader,代码行数:11,代码来源:generatemovie.py