Hướng dẫn real time audio recording python - python ghi âm thời gian thực

Tôi đã cố gắng thực hiện xử lý tín hiệu âm thanh thời gian thực bằng mô-đun 'Pyaudio' trong Python. Những gì tôi đã làm là một trường hợp đơn giản là đọc dữ liệu âm thanh từ micrô và phát nó thông qua tai nghe. Tôi đã thử với mã sau (cả phiên bản Python và Cython). Nghĩ rằng nó hoạt động nhưng thật không may, nó là quầy hàng và không đủ mượt mà. Làm thế nào tôi có thể cải thiện mã để nó sẽ chạy trơn tru. PC của tôi là i7, RAM 8GB.

Phiên bản Python

import pyaudio
import numpy as np

RATE    = 16000
CHUNK   = 256

p               =   pyaudio.PyAudio()

player = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, output=True, 
frames_per_buffer=CHUNK)
stream = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, input=True, frames_per_buffer=CHUNK)

for i in range(int(20*RATE/CHUNK)): #do this for 10 seconds
player.write(np.fromstring(stream.read(CHUNK),dtype=np.int16))
stream.stop_stream()
stream.close()
p.terminate()

Phiên bản Cython

import pyaudio
import numpy as np

cdef int RATE   = 16000
cdef int CHUNK  = 1024
cdef int i      
p               =   pyaudio.PyAudio()

player = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, output=True, frames_per_buffer=CHUNK)
stream = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, input=True, frames_per_buffer=CHUNK)

for i in range(500): #do this for 10 seconds
    player.write(np.fromstring(stream.read(CHUNK),dtype=np.int16))
stream.stop_stream()
stream.close()
p.terminate()

Hỏi ngày 24 tháng 9 năm 2017 lúc 2:29Sep 24, 2017 at 2:29

Hướng dẫn real time audio recording python - python ghi âm thời gian thực

6

Tôi tin rằng bạn đang thiếu CHUNK như là đối số thứ hai cho cuộc gọi player.write.

player.write(np.fromstring(stream.read(CHUNK),dtype=np.int16),CHUNK)

Ngoài ra, không chắc chắn nếu lỗi định dạng của nó. Nhưng player.write cần được lập bảng vào vòng lặp for

Và trên mỗi trang web pyaudio bạn cần phải có

import pyaudio
import numpy as np

cdef int RATE   = 16000
cdef int CHUNK  = 1024
cdef int i      
p               =   pyaudio.PyAudio()

player = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, output=True, frames_per_buffer=CHUNK)
stream = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, input=True, frames_per_buffer=CHUNK)

for i in range(500): #do this for 10 seconds
    player.write(np.fromstring(stream.read(CHUNK),dtype=np.int16))
stream.stop_stream()
stream.close()
p.terminate()
0 chứ không phải
import pyaudio
import numpy as np

cdef int RATE   = 16000
cdef int CHUNK  = 1024
cdef int i      
p               =   pyaudio.PyAudio()

player = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, output=True, frames_per_buffer=CHUNK)
stream = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, input=True, frames_per_buffer=CHUNK)

for i in range(500): #do this for 10 seconds
    player.write(np.fromstring(stream.read(CHUNK),dtype=np.int16))
stream.stop_stream()
stream.close()
p.terminate()
1 khi
import pyaudio
import numpy as np

cdef int RATE   = 16000
cdef int CHUNK  = 1024
cdef int i      
p               =   pyaudio.PyAudio()

player = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, output=True, frames_per_buffer=CHUNK)
stream = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, input=True, frames_per_buffer=CHUNK)

for i in range(500): #do this for 10 seconds
    player.write(np.fromstring(stream.read(CHUNK),dtype=np.int16))
stream.stop_stream()
stream.close()
p.terminate()
2 thực hiện nhân
import pyaudio
import numpy as np

cdef int RATE   = 16000
cdef int CHUNK  = 1024
cdef int i      
p               =   pyaudio.PyAudio()

player = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, output=True, frames_per_buffer=CHUNK)
stream = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, input=True, frames_per_buffer=CHUNK)

for i in range(500): #do this for 10 seconds
    player.write(np.fromstring(stream.read(CHUNK),dtype=np.int16))
stream.stop_stream()
stream.close()
p.terminate()
3 trước khi chia
import pyaudio
import numpy as np

cdef int RATE   = 16000
cdef int CHUNK  = 1024
cdef int i      
p               =   pyaudio.PyAudio()

player = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, output=True, frames_per_buffer=CHUNK)
stream = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, input=True, frames_per_buffer=CHUNK)

for i in range(500): #do this for 10 seconds
    player.write(np.fromstring(stream.read(CHUNK),dtype=np.int16))
stream.stop_stream()
stream.close()
p.terminate()
4.

for i in range(int(20*RATE/CHUNK)): #do this for 10 seconds
    player.write(np.fromstring(stream.read(CHUNK),dtype=np.int16),CHUNK)

stream.stop_stream()
stream.close()
p.terminate()

Cuối cùng, bạn có thể muốn tăng

import pyaudio
import numpy as np

cdef int RATE   = 16000
cdef int CHUNK  = 1024
cdef int i      
p               =   pyaudio.PyAudio()

player = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, output=True, frames_per_buffer=CHUNK)
stream = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, input=True, frames_per_buffer=CHUNK)

for i in range(500): #do this for 10 seconds
    player.write(np.fromstring(stream.read(CHUNK),dtype=np.int16))
stream.stop_stream()
stream.close()
p.terminate()
5 lên
import pyaudio
import numpy as np

cdef int RATE   = 16000
cdef int CHUNK  = 1024
cdef int i      
p               =   pyaudio.PyAudio()

player = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, output=True, frames_per_buffer=CHUNK)
stream = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, input=True, frames_per_buffer=CHUNK)

for i in range(500): #do this for 10 seconds
    player.write(np.fromstring(stream.read(CHUNK),dtype=np.int16))
stream.stop_stream()
stream.close()
p.terminate()
6, CHUNK lên
import pyaudio
import numpy as np

cdef int RATE   = 16000
cdef int CHUNK  = 1024
cdef int i      
p               =   pyaudio.PyAudio()

player = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, output=True, frames_per_buffer=CHUNK)
stream = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, input=True, frames_per_buffer=CHUNK)

for i in range(500): #do this for 10 seconds
    player.write(np.fromstring(stream.read(CHUNK),dtype=np.int16))
stream.stop_stream()
stream.close()
p.terminate()
8 và
import pyaudio
import numpy as np

cdef int RATE   = 16000
cdef int CHUNK  = 1024
cdef int i      
p               =   pyaudio.PyAudio()

player = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, output=True, frames_per_buffer=CHUNK)
stream = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, input=True, frames_per_buffer=CHUNK)

for i in range(500): #do this for 10 seconds
    player.write(np.fromstring(stream.read(CHUNK),dtype=np.int16))
stream.stop_stream()
stream.close()
p.terminate()
9 lên
player.write(np.fromstring(stream.read(CHUNK),dtype=np.int16),CHUNK)
0 để có độ trung thực tốt hơn.

Đã trả lời ngày 27 tháng 9 năm 2017 lúc 16:39Sep 27, 2017 at 16:39

Anil_MAnil_MAnil_M

10,2K6 Huy hiệu vàng41 Huy hiệu bạc69 Huy hiệu đồng6 gold badges41 silver badges69 bronze badges

5

Mã bên dưới sẽ lấy thiết bị đầu vào mặc định và xuất những gì được ghi vào thiết bị đầu ra mặc định.

import PyAudio
import numpy as np

p = pyaudio.PyAudio()

CHANNELS = 2
RATE = 44100

def callback(in_data, frame_count, time_info, flag):
    # using Numpy to convert to array for processing
    # audio_data = np.fromstring(in_data, dtype=np.float32)
    return in_data, pyaudio.paContinue

stream = p.open(format=pyaudio.paFloat32,
                channels=CHANNELS,
                rate=RATE,
                output=True,
                input=True,
                stream_callback=callback)

stream.start_stream()

while stream.is_active():
    time.sleep(20)
    stream.stop_stream()
    print("Stream is stopped")

stream.close()

p.terminate()

Điều này sẽ chạy trong 20 giây và dừng lại. Phương thức gọi lại là nơi bạn có thể xử lý tín hiệu:

player.write(np.fromstring(stream.read(CHUNK),dtype=np.int16),CHUNK)
1

player.write(np.fromstring(stream.read(CHUNK),dtype=np.int16),CHUNK)
2 là nơi bạn gửi lại dữ liệu sau xử lý cho thiết bị đầu ra.

Lưu ý Chunk có một đối số mặc định là 1024 như đã lưu ý trong tài liệu Pyaudio: http://people.csail.mit.edu/hubert/pyaudio/docs/#pyaudio.pyaudio.open

Đã trả lời ngày 27 tháng 5 năm 2018 lúc 18:11May 27, 2018 at 18:11

KCKAIweIKCKAIweikckaiwei

3965 Huy hiệu bạc18 Huy hiệu Đồng5 silver badges18 bronze badges

Tôi đang làm việc trên một dự án tương tự. Tôi đã sửa đổi mã của bạn và các quầy hàng bây giờ đã biến mất. Chunk càng lớn thì độ trễ càng lớn. Đó là lý do tại sao tôi giữ nó thấp.

import pyaudio
import numpy as np

CHUNK = 2**5
RATE = 44100
LEN = 10

p = pyaudio.PyAudio()

stream = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, input=True, frames_per_buffer=CHUNK)
player = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, output=True, frames_per_buffer=CHUNK)


for i in range(int(LEN*RATE/CHUNK)): #go for a LEN seconds
    data = np.fromstring(stream.read(CHUNK),dtype=np.int16)
    player.write(data,CHUNK)


stream.stop_stream()
stream.close()
p.terminate()

Đã trả lời ngày 29 tháng 6 năm 2018 lúc 13:02Jun 29, 2018 at 13:02

Hướng dẫn real time audio recording python - python ghi âm thời gian thực

Ra'wRa'wRa'w

1031 Huy hiệu bạc10 Huy hiệu đồng1 silver badge10 bronze badges