2012-01-03 33 views

Trả lời

5

Bạn nên sử dụng thư viện. Viết tất cả trong python tinh khiết có thể là hàng ngàn dòng mã, để giao tiếp với phần cứng âm thanh!

Với thư viện, ví dụ: audiere, nó sẽ được đơn giản như này:

import audiere 
ds = audiere.open_device() 
os = ds.open_array(input_array, 44100) 
os.play() 

Ngoài ra còn của pyglet, pygame, và nhiều người khác ..

+0

'audiere' dường như là một dự án rất cũ ... cuối cùng phát hành vào năm 2006, và readme cho ràng buộc Python là ngày 2002 và tài liệu tham khảo Python 2.2 ... –

+0

Tôi đã sử dụng nó bản thân mình trên python 2,7 và nó vẫn hoạt động tốt. Mô-đun audiere là từ http://pyaudiere.org/, có thể bạn đang xem http://audiere.sourceforge.net/. pyaudiere sử dụng API Audiere – wim

+0

Trang web pyaudiere không còn tồn tại nữa và buổi chiếu phim vẫn chưa được cập nhật từ năm 2006. Đây không còn là câu trả lời hay nữa. – jozzas

3

Để phát mảng âm thanh input_array của mẫu 16 bit. Đây là ví dụ được sửa đổi từ pyadio documentation page

import pyaudio 

# instantiate PyAudio (1) 
p = pyaudio.PyAudio() 

# open stream (2), 2 is size in bytes of int16 
stream = p.open(format=p.get_format_from_width(2), 
       channels=1, 
       rate=44100, 
       output=True) 

# play stream (3), blocking call 
stream.write(input_array) 

# stop stream (4) 
stream.stop_stream() 
stream.close() 

# close PyAudio (5) 
p.terminate() 
2

hoặc sử dụng mô-đun sounddevice. Cài đặt sử dụng pip install sounddevice, nhưng bạn cần điều này đầu tiên: sudo apt-get install libportaudio2

tuyệt đối cơ bản:

import numpy as np 
import sounddevice as sd 

sd.play(myarray) 
#may need to be normalised like in below example 
#myarray must be a numpy array. If not, convert with np.array(myarray) 

Một vài lựa chọn hơn:

import numpy as np 
import sounddevice as sd 

#variables 
samplfreq = 100 #the sampling frequency of your data (mine=100Hz, yours=44100) 
factor = 10  #incr./decr frequency (speed up/slow down by a factor) (normal speed = 1) 

#data 
print('..interpolating data') 
arr = myarray 

#normalise the data to between -1 and 1. If your data wasn't/isn't normalised it will be very noisy when played here 
sd.play(arr/np.max(np.abs(arr)), samplfreq*factor) 
+0

Lưu ý rằng sounddevice không hoạt động nếu chạy trong Eclipse. –

Các vấn đề liên quan