当前位置:   article > 正文

python计算pcm分贝值_python获取音频音量大小

python获取音频音量大小
  1. import array
  2. import numpy as np
  3. import os
  4. from math import log, exp
  5. pcm_path = 'test_audio.pcm'#改
  6. def readPCM(fileName):
  7. file = open(fileName, 'rb')
  8. pcm_data = array.array('h')
  9. size = int(os.path.getsize(fileName) / pcm_data.itemsize)
  10. pcm_data.fromfile(file, size)
  11. file.close()
  12. return np.array(pcm_data)#/ 32768
  13. data = readPCM(pcm_path)
  14. sum_ = 0
  15. for k in range(len(data)):
  16. sum_+= abs(data[k])
  17. sum_ = sum_/len(data)
  18. print(len(data),max(data),min(data),sum(data>=0),len(data),data.shape)
  19. fenbei = 20*log(max(data)/65535,10)
  20. fenbei = 20*log(sum_,10)
  21. print('fenbei = ',fenbei)
  22. # import os
  23. # from pydub import AudioSegment
  24. # import wave
  25. # pcm_path = r'test_audio.pcm'
  26. # with open(pcm_path, 'rb') as pcmfile:
  27. # pcmdata = pcmfile.read()
  28. # with wave.open(pcm_path[:-4] + '.wav', 'wb') as wavfile:
  29. # wavfile.setparams((1, 2, 16000, 0, 'NONE', 'NONE'))
  30. # wavfile.writeframes(pcmdata)
  31. # def match_target_amplitude(sound, target_dBFS):
  32. # change_in_dBFS = target_dBFS - sound.dBFS
  33. # return sound.apply_gain(change_in_dBFS)
  34. # # pcm2wav('test_audio.pcm','test_audio.wav')
  35. # sound = AudioSegment.from_file('test_audio.wav', format="wav") # 加载WAV文件
  36. # # print(sound.channels)
  37. # # normalized_sound = sound.apply_gain(-sound.max_dBFS)
  38. # # print(normalized_sound)
  39. # db = sound[0].dBFS # 取得WAV文件的声音分贝值
  40. # dbmax = sound[0].max_dBFS
  41. # print('db = ',dbmax,db)
  42. # normalized_sound = match_target_amplitude(sound, db + 10) # db+10表示比原来的声音大10db,需要加大音量就加多少,反之则减多少
  43. # normalized_sound.export(path2, format="wav")

参考网址:获取PCM音频数据的声音分贝值_kuangben2000的博客-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/967080
推荐阅读
相关标签
  

闽ICP备14008679号