赞
踩
- import array
- import numpy as np
- import os
- from math import log, exp
-
- pcm_path = 'test_audio.pcm'#改
- def readPCM(fileName):
- file = open(fileName, 'rb')
- pcm_data = array.array('h')
- size = int(os.path.getsize(fileName) / pcm_data.itemsize)
- pcm_data.fromfile(file, size)
- file.close()
- return np.array(pcm_data)#/ 32768
-
- data = readPCM(pcm_path)
- sum_ = 0
- for k in range(len(data)):
- sum_+= abs(data[k])
- sum_ = sum_/len(data)
- print(len(data),max(data),min(data),sum(data>=0),len(data),data.shape)
-
- fenbei = 20*log(max(data)/65535,10)
- fenbei = 20*log(sum_,10)
- print('fenbei = ',fenbei)
-
-
-
- # import os
- # from pydub import AudioSegment
- # import wave
- # pcm_path = r'test_audio.pcm'
- # with open(pcm_path, 'rb') as pcmfile:
- # pcmdata = pcmfile.read()
- # with wave.open(pcm_path[:-4] + '.wav', 'wb') as wavfile:
- # wavfile.setparams((1, 2, 16000, 0, 'NONE', 'NONE'))
- # wavfile.writeframes(pcmdata)
-
- # def match_target_amplitude(sound, target_dBFS):
- # change_in_dBFS = target_dBFS - sound.dBFS
- # return sound.apply_gain(change_in_dBFS)
-
- # # pcm2wav('test_audio.pcm','test_audio.wav')
- # sound = AudioSegment.from_file('test_audio.wav', format="wav") # 加载WAV文件
- # # print(sound.channels)
- # # normalized_sound = sound.apply_gain(-sound.max_dBFS)
- # # print(normalized_sound)
-
-
- # db = sound[0].dBFS # 取得WAV文件的声音分贝值
- # dbmax = sound[0].max_dBFS
- # print('db = ',dbmax,db)
- # normalized_sound = match_target_amplitude(sound, db + 10) # db+10表示比原来的声音大10db,需要加大音量就加多少,反之则减多少
- # normalized_sound.export(path2, format="wav")
-
-
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。