[librosa]0.8版本后不再支持write_wav

librosa在0.8版本后,整个output下的方法都被删除。

从设计角度来看,librosa是想专注于音频数据的处理,至于io独写操作,是交给了其他模块。

官方文档来看,librosa的音频读取主要依赖于soundfileaudioread两个库;而写主要依赖于soundfile库

代码实例

基本读取

import librosa
import soundfile as sf

# Get example audio file
filename = librosa.ex('trumpet')

data, samplerate = sf.read(filename, dtype='float32')
data = data.T
data_22k = librosa.resample(data, samplerate, 22050)

通过URL读取

import soundfile as sf
import io

from six.moves.urllib.request import urlopen

url = "https://raw.githubusercontent.com/librosa/librosa/master/tests/data/test1_44100.wav"

data, samplerate = sf.read(io.BytesIO(urlopen(url).read()))

分块读取

librosa保留了之前通过stream的方式分块读取大文件的方式,代码如下,

import librosa

sr = librosa.get_samplerate('/path/to/file.wav')
 
# Set the frame parameters to be equivalent to the librosa defaults
# in the file's native sampling rate
frame_length = (2048 * sr) // 22050
hop_length = (512 * sr) // 22050
 
# Stream the data, working on 128 frames at a time
stream = librosa.stream('path/to/file.wav',
                       block_length=128,
                       frame_length=frame_length,
                       hop_length=hop_length)

chromas = []
for y in stream:
   chroma_block = librosa.feature.chroma_stft(y=y, sr=sr,
                                              n_fft=frame_length,
                                              hop_length=hop_length,
                                              center=False)
   chromas.append(chromas)

写入文件

import numpy as np
import soundfile as sf

rate = 44100
data = np.random.uniform(-1, 1, size=(rate * 10, 2))

# Write out audio as 24bit PCM WAV
sf.write('stereo_file.wav', data, samplerate, subtype='PCM_24')

# Write out audio as 24bit Flac
sf.write('stereo_file.flac', data, samplerate, format='flac', subtype='PCM_24')

# Write out audio as 16bit OGG
sf.write('stereo_file.ogg', data, samplerate, format='ogg', subtype='vorbis')
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值