在Mac上无法使用Python录制或播放音频:无错误&没有声音 [英] Recording or Playing Audio with Python not working on Mac: No Errors & No Sound

查看:320
本文介绍了在Mac上无法使用Python录制或播放音频:无错误&没有声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试仅使用内置的麦克风和扬声器在Mac(Catalina)上的Python 3.7上使用音频. 我的问题是,使用我尝试的任何代码,在录制时我什么也没收到,而在播放声音时什么也没发出. 我尝试了以下问题的答案:首先,我尝试使用PyAudio像这样:

I'm trying to work with Audio on Python 3.7 on Mac(Catalina) only with the built-in Microphone and Speakers. My Problem is that with any code I tried, when recording I receive nothing and when playing sound nothing comes out. I tried the answers from this question: first I tried with PyAudio like this:

import pyaudio
import wave

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"

p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                frames_per_buffer=CHUNK)

print("* recording")
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    frames.append(data)
print("* done recording")

stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()

哪个返回给我一个静默文件.

Which returns me a silent file.

然后我尝试使用SoundDevice:

Then I tried with SoundDevice:

import sounddevice as sd
import matplotlib.pyplot as plt

fs = 44100                      # frames per sec
sd.default.samplerate = fs
sd.default.channels = 2

duration = 3.0                  # Aufnahmezeit
recording = sd.rec( int( duration * fs) )
print("* recording")
sd.wait()
print("* done!")

t = [ i for i in range( int( duration * fs) )]

plt.plot(t, recording, 'r-')
plt.show()

Wich返回一个填充有零的数组:情节的截图. 两者均未引起任何错误或警告.

Wich returns an array filled with zeros: Screenshot of the Plot. Both didn't cause any errors or warnings.

之后,我尝试播放一个简单的440 Hz的正弦波,扬声器保持沉默.

After that I tried to play a simple Sin-Wave with 440 Hz, the speaker stayed silent.

相同的代码可以在我的朋友mac上正常使用.麦克风与放大器演讲者的工作也很好.在系统偏好设置"中,我允许每个应用程序都使用麦克风.

The same code, works on my friends mac without problems. The Microphone & Speakers are also working fine. And in System Preferences I allowed every app to use the microphone.

这个 person 似乎也有类似的问题.还尝试了此代码,但没有结果. :(

This person seems to have a similar issue. Also tried this code without result. :(

我不知道我还能尝试解决什么问题.

I have no idea what else I could try to fix this.

推荐答案

失败原因和两个解决方案可以在

The failure reason and two solutions can be found at https://www.reddit.com/r/MacOS/comments/9lwyz0/mojave_not_privacy_settings_blocking_all_mic/

要重置PRAM,请按照 https://thenextweb.com/lifehacks/2017/06/14/how-when-why-to-reset-the-pram-smc-on-your-mac/

To reset PRAM, follow the instructions at https://thenextweb.com/lifehacks/2017/06/14/how-when-why-to-reset-the-pram-smc-on-your-mac/

这篇关于在Mac上无法使用Python录制或播放音频:无错误&没有声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆