matinal:python 处理音频

2023-10-14 15:50:20 浏览数 (1)

代码语言:javascript复制
from scipy.io import wavfile#处理wav文件
import numpy as np
import soundfile as sf #可以处理flac文件
path = 'C:\Users\shm\Desktop\python_data\music_data\yuxi.flac'                                                  
data, samplerate = sf.read(path)
print(data, samplerate)

所得结果: [[ 0.00000000e 00 0.00000000e 00] [ 0.00000000e 00 -6.10351562e-05] [ 0.00000000e 00 0.00000000e 00] … [ 0.00000000e 00 -3.05175781e-05] [ 0.00000000e 00 -3.05175781e-05] [ 0.00000000e 00 -3.05175781e-05]] 48000

data是一个二维矩阵,每一行两个量,代表左右声道,samplerate是频率,输出结果是每秒48000次

代码语言:javascript复制
把它写成wav文件:
 path = 'C:\Users\shm\Desktop\python_data\music_data\yuxi2.wav'
 wavfile.write(path,48000,data)

 把它倒放一遍吧:
 path = 'C:\Users\shm\Desktop\python_data\music_data\yuxi3.wav'
 wavfile.write(path,48000,data[::-1])
 #对音频进行其他操作也和上面类似,就只需要对data这个数组进行操作就可以了
 path = 'C:\Users\shm\Desktop\python_data\music_data\yuxi4.wav'
 wavfile.write(path,8000,data[::6])

0 人点赞