简体   繁体   English

使用 Python 录制或播放音频无法在 Mac 上运行:无错误且无声音

[英]Recording or Playing Audio with Python not working on Mac: No Errors & No Sound

I'm trying to work with Audio on Python 3.7 on Mac(Catalina) only with the built-in Microphone and Speakers.我正在尝试仅使用内置麦克风和扬声器在 Mac(Catalina)上的 Python 3.7 上使用音频。 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:我尝试了这个问题的答案:首先我尝试了这样的 PyAudio:

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.这给我一个无声的文件。

Then I tried with SoundDevice:然后我尝试使用 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 returns an array filled with zeros: Screenshot of the Plot . Wich 返回一个用零填充的数组: Plot 的屏幕截图 Both didn't cause any errors or warnings.两者都没有引起任何错误或警告。

After that I tried to play a simple Sin-Wave with 440 Hz, the speaker stayed silent.之后我尝试播放 440 Hz 的简单 Sin-Wave,扬声器保持沉默。

The same code, works on my friends mac without problems.相同的代码,在我朋友的 mac 上运行没有问题。 The Microphone & Speakers are also working fine.麦克风和扬声器也工作正常。 And in System Preferences I allowed every app to use the microphone.在系统偏好设置中,我允许每个应用程序使用麦克风。

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.我不知道我还能尝试什么来解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM