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

三世轮回 提交于 2020-01-06 06:54:39

问题


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.

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 returns an array filled with zeros: Screenshot of the 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.

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.

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.


回答1:


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/

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/



来源:https://stackoverflow.com/questions/58433344/recording-or-playing-audio-with-python-not-working-on-mac-no-errors-no-sound

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!