문제

I need to record 24-bit audio (because it's the archival standard for audio digitization). However, the wave library seems to only go up to 16-bit.

It looks like pyaudio can work with 24-bit audio but every example I've found shows pyaudio using the wave library, meaning it has to save 16-bit.

Is it possible to record and playback 24-bit audio with pyaudio?

도움이 되었습니까?

해결책

Yes it is possible. You just have to take the record example of PyAudio and replace pyaudio.paInt16 by pyaudio.paInt24.

import pyaudio
import wave

CHUNK = 1024
FORMAT = pyaudio.paInt24
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()

다른 팁

check if the top end 8 bits are 0. If they are not, you have a true 24 bit recording.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top