尝试用python sounddevice录制音频,但结果只是静态的

mbzjlibv  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(296)

我正在尝试使用python中的sounddevice库录制一些音频。这是为扫描仪创建声音激活录音。
我不能使用record函数,因为它需要指定的时间,这不适合我的程序,因为每次传输的长度可能不同,因此我向流中添加了一个回调,当音频高于某个级别时,将通过创建新文件并将缓冲区数据写入wave文件来触发录制。
我已经成功地实现了触发器,但是当我尝试写入文件并打开它时,它不是传输的内容,而是静态损坏。
这是我的密码:

  1. import time as timer
  2. import wave
  3. import numpy as np
  4. import sounddevice as sd
  5. RecordActivate = False
  6. StartTime = 0
  7. def print_sound(indata, outdata, frames, time, status):
  8. global RecordActivate
  9. global StartTime
  10. volume_norm = np.linalg.norm(indata) * 10
  11. VolumeLevel = int(volume_norm)
  12. global f
  13. if RecordActivate == False:
  14. print("itls false" + str(VolumeLevel))
  15. if VolumeLevel > 16:
  16. RecordActivate = True
  17. print("Begin")
  18. StartTime = timer.time()
  19. ThingTime = timer.strftime(
  20. "%Y-%m-%d %H:%M:%S", timer.localtime(timer.time())
  21. )
  22. print("Transmission detected at " + ThingTime)
  23. f = wave.open("scan.wav", "w")
  24. f.setnchannels(1)
  25. f.setsampwidth(1)
  26. f.setframerate(44100)
  27. if RecordActivate == True:
  28. if VolumeLevel < 16:
  29. print("Transmission ceased.")
  30. RecordActivate = False
  31. else:
  32. f.writeframes(indata.tobytes())
  33. print("recording..")
  34. with sd.Stream(callback=print_sound):
  35. while True:
  36. thing = 0

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题