Python, Error audio Recording in 16000Hz using Pyaudio

2024/11/17 9:59:31

I use Python 2.7.3, Mac OS 10.8.2 and Xcode 4.5.1

I am trying to record sound using PyAudio following the instructions in http://people.csail.mit.edu/hubert/pyaudio/

and using the program

"""PyAudio example: Record a few seconds of audio and save to a WAVE file."""import pyaudio
import waveCHUNK = 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()

It works well with RATE = 44100. But I want to record with RATE = 16000 and CHANNELS = 1

Changing the values give me an error asenter image description here

How will I be able to record with RATE = 16000?

Answer

I experienced the same problem. The issue was fixed in portaudio, but brew installed an old version for me. I upgraded with

brew install portaudio --HEAD

and then was able to run your code with CHANNELS=1 and RATE=16000

https://en.xdnf.cn/q/71238.html

Related Q&A

FastAPI passing json in get request via TestClient

Im try to test the api I wrote with Fastapi. I have the following method in my router : @app.get(/webrecord/check_if_object_exist) async def check_if_object_exist(payload: WebRecord) -> bool:key = g…

Python TDD directory structure

Is there a particular directory structure used for TDD in Python?Tutorials talk about the content of the tests, but not where to place themFrom poking around Python Koans, suspect its something like:/…

Pillow was built without XCB support

Im working on a program that uses ImageGrab in Pillow. I am getting the error mentioned in the title. I notice in the documentation that it says the generic pip install Pillow doesnt come with libxcb. …

Set equal aspect in plot with colorbar

I need to generate a plot with equal aspect in both axis and a colorbar to the right. Ive tried setting aspect=auto, aspect=1, and aspect=equal with no good results. See below for examples and the MWE.…

How to emit dataChanged in PyQt5

The code below breaks on self.emit line. It works fine in PyQt4. How to fix this code so it works in PyQt5?from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtCore import QObject, pyqtSignalclass …

django: gettext and coercing to unicode

I have following code in my django application.class Status(object):def __init__(self, id, desc):self.id = idself.desc = descdef __unicode__(self):return self.descSTATUS = Status(0, _(u"Some text&…

Telegram bot api keyboard

I have problem with Telegram Bot Api and with "ReplyKeyboard". Im using Python 2.7 and I send post request:TelegramAPI.post(TELEGRAM_URL + "sendMessage", data=dict(chat_id=CHAT_ID, …

Use of torch.stack()

t1 = torch.tensor([1,2,3]) t2 = torch.tensor([4,5,6]) t3 = torch.tensor([7,8,9])torch.stack((t1,t2,t3),dim=1)When implementing the torch.stack(), I cant understand how stacking is done for different di…

Is it possible to sort a list with reduce?

I was given this as an exercise. I could of course sort a list by using sorted() or other ways from Python Standard Library, but I cant in this case. I think Im only supposed to use reduce().from funct…

Flask-WTF set time limit on CSRF token

Im currently using Flask-WTF v0.13.1, i have a few forms on my website, all created including the CSRF token.For some reasons i have to set a different expiration on each form, so far i could set manua…