python中float32_用python创建32位float wav文件?

这是我的贡献。。。包括任意字长和任意数量的频道。

我已经冒昧地更改了float32_wav_文件,以包含一个保存用于测试的文件。注意,文件结构的多通道数据部分是交错的。我敢肯定,这个循环可能会被大Python化。在# see http://stackoverflow.com/questions/15576798/create-32bit-float-wav-file-in-python

# see... http://blog.theroyweb.com/extracting-wav-file-header-information-using-a-python-script

import struct

def float32_wav_file(file_name, sample_array, sample_rate):

(M,N)=sample_array.shape

#print "len sample_array=(%d,%d)" % (M,N)

byte_count = M * N * 4 # (len(sample_array)) * 4 # 32-bit floats

wav_file = ""

# write the header

wav_file += struct.pack('

'R', 'I', 'F', 'F',

byte_count + 0x2c - 8, # header size

'W', 'A', 'V', 'E', 'f', 'm', 't', ' ',

0x10, # size of 'fmt ' header

3, # format 3 = floating-point PCM

M, # channels

sample_rate, # samples / second

sample_rate * 4, # bytes / second

4, # block alignment

32) # bits / sample

wav_file += struct.pack('

'd', 'a', 't', 'a', byte_count)

print "packing..."

for j in range(0,N):

for k in range(0,M):

wav_file += struct.pack("

print "saving..."

fi=open(file_name,'wb')

for value in wav_file:

fi.write(value)

fi.close()

return wav_file

import numpy as np

def wav_file_read(filename):

fi=open(filename,'rb')

data=fi.read()

fi.close()

A, B, C, D =struct.unpack('4c', data[0:4]) # 'RIFF'

ChunkSize =struct.unpack('

A, B, C, D =struct.unpack('4c', data[8:12]) # 'WAVE'

A, B, C, D =struct.unpack('4c', data[12:16]) # 'fmt '

Subchunk1Size =struct.unpack('

AudioFormat =struct.unpack('

NumChannels =struct.unpack('

SampleRate =struct.unpack('

ByteRate =struct.unpack('

BlockAlign =struct.unpack('

BitsPerSample =struct.unpack('

A, B, C, D =struct.unpack('4c', data[36:40]) # BIG ENDIAN, char*4

SubChunk2Size =struct.unpack('

waveData=data[44:]

(M,N)=(len(waveData),len(waveData[0]))

print("ChunkSize =%d\nSubchunk1Size =%d\nAudioFormat =%d\nNumChannels =%d\nSampleRate =%d\nByteRate =%d\nBlockAlign =%d\nBitsPerSample =%d\nA:%c, B:%c, C:%c, D:%c\nSubChunk2Size =%d" %

(ChunkSize ,

Subchunk1Size,

AudioFormat ,

NumChannels ,

SampleRate ,

ByteRate ,

BlockAlign ,

BitsPerSample ,

A, B, C, D ,

SubChunk2Size ))

if BitsPerSample==8:

print "Unpacking 8 bits on len(waveData)=%d" % len(waveData)

d=np.fromstring(waveData,np.uint8)

floatdata=d.astype(np.float64)/np.float(127)

elif BitsPerSample==16:

print "Unpacking 16 bits on len(waveData)=%d" % len(waveData)

d=np.zeros(SubChunk2Size/2, dtype=np.int16)

j=0

for k in range(0, SubChunk2Size, 2):

d[j]=struct.unpack('

j=j+1

floatdata=d.astype(np.float64)/np.float(32767)

elif BitsPerSample==24:

print "Unpacking 24 bits on len(waveData)=%d" % len(waveData)

d=np.zeros(SubChunk2Size/3, dtype=np.int32)

j=0

for k in range(0, SubChunk2Size, 3):

d[j]=struct.unpack('

j=j+1

floatdata=d.astype(np.float64)/np.float(2147483647)

else: # anything else will be considered 32 bits

print "Unpacking 32 bits on len(waveData)=%d" % len(waveData)

d=np.fromstring(waveData,np.int32)

floatdata=d.astype(np.float64)/np.float(2147483647)

v=floatdata[0::NumChannels]

for i in range(1,NumChannels):

v=np.vstack((v,floatdata[i::NumChannels]))

#return (np.vstack((floatdata[0::2],floatdata[1::2])), SampleRate, NumChannels, BitsPerSample)

return (v, SampleRate, NumChannels, BitsPerSample)

if __name__ == "__main__":

(array,SampleRate,NumChannels,BitsPerSample)=wav_file_read("my_input_file.wav")

wavefile=float32_wav_file("test_file.wav",array,SampleRate)

要去除 WAV 文件的人声,可以使用声音分离技术,常用的方法是独立分量分析(Independent Component Analysis, ICA),它可以将混合在一起的声音信号分离成不同的独立信号。 以下是使用 Python 实现声音分离的基本步骤: 1. 载入 WAV 文件,并将其转换为 numpy 数组。 ```python import wave import numpy as np with wave.open('example.wav', mode='rb') as wav_file: # 获取声道数、采样宽度、采样率和帧数 num_channels = wav_file.getnchannels() sample_width = wav_file.getsampwidth() sample_rate = wav_file.getframerate() num_frames = wav_file.getnframes() # 读取所有帧数据 wav_data = wav_file.readframes(num_frames) # 将二进制数据转换为 numpy 数组 audio_array = np.frombuffer(wav_data, dtype=np.int16) ``` 2. 对声音信号进行预处理,例如将采样率调整为 44100Hz,并将数据类型转换为 float32。 ```python from scipy import signal # 将采样率调整为 44100Hz resampled_audio, resampled_rate = signal.resample(audio_array, num_frames * 44100 // sample_rate, audio_array) # 将数据类型转换为 float32 audio_data = resampled_audio.astype(np.float32) / 32768.0 ``` 3. 使用 ICA 对声音信号进行分离,得到不同的独立信号。 ```python from sklearn.decomposition import FastICA # 创建 FastICA 对象 ica = FastICA(n_components=num_channels) # 对声音信号进行分离 separated_audio = ica.fit_transform(audio_data.reshape(-1, num_channels)).T ``` 4. 将分离后的信号保存为 WAV 文件。 ```python # 将数据类型转换为 int16 separated_audio_int = (separated_audio * 32768.0).astype(np.int16) # 创建 WAV 文件对象 with wave.open('separated.wav', mode='wb') as wav_file: # 设置声道数、采样宽度和采样率 wav_file.setnchannels(num_channels) wav_file.setsampwidth(sample_width) wav_file.setframerate(resampled_rate) # 将分离后的数据写入 WAV 文件 wav_file.writeframes(separated_audio_int.T.tobytes()) ``` 以上是基本的声音分离流程,具体实现还需要根据实际情况进行调整和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值