python语言录制声音和保存wav文件的库文件是什么

Python0169

python语言录制声音和保存wav文件的库文件是什么,第1张

是pyaudio。

因为录音,是由计算机内部声卡实现的,所以我们需要利用pyaudio库调用声卡进行声音录制,而保存音频文件我们可以使用python自带的wave库来将音频保存为wav音频文件,所以python语言录制声音和保存wav文件的库文件是pyaudio。

语言录音又称对白录音,有声影片电影录音的主要工作内容之一,根据影片的内容和艺术要求,把演员的对白、旁白和内心独白记录下来的过程。

#我可以帮你写一段代码,能够录音形成wav文件,不过要分析录音文件的波形,你可以另外找#工具,比如cooledit,也很方便。

from sys import byteorder

from array import array

from struct import pack

import pyaudio

import wave

THRESHOLD = 500

CHUNK_SIZE = 1024

FORMAT = pyaudio.paInt16

RATE = 44100

def is_silent(snd_data):

"Returns 'True' if below the 'silent' threshold"

return max(snd_data) <THRESHOLD

def normalize(snd_data):

"Average the volume out"

MAXIMUM = 16384

times = float(MAXIMUM)/max(abs(i) for i in snd_data)

r = array('h')

for i in snd_data:

r.append(int(i*times))

return r

def trim(snd_data):

"Trim the blank spots at the start and end"

def _trim(snd_data):

snd_started = False

r = array('h')

for i in snd_data:

if not snd_started and abs(i)>THRESHOLD:

snd_started = True

r.append(i)

elif snd_started:

r.append(i)

return r

# Trim to the left

snd_data = _trim(snd_data)

# Trim to the right

snd_data.reverse()

snd_data = _trim(snd_data)

snd_data.reverse()

return snd_data

def add_silence(snd_data, seconds):

"Add silence to the start and end of 'snd_data' of length 'seconds' (float)"

r = array('h', [0 for i in xrange(int(seconds*RATE))])

r.extend(snd_data)

r.extend([0 for i in xrange(int(seconds*RATE))])

return r

def record():

"""

Record a word or words from the microphone and

return the data as an array of signed shorts.

Normalizes the audio, trims silence from the

start and end, and pads with 0.5 seconds of

blank sound to make sure VLC et al can play

it without getting chopped off.

"""

p = pyaudio.PyAudio()

stream = p.open(format=FORMAT, channels=1, rate=RATE,

input=True, output=True,

frames_per_buffer=CHUNK_SIZE)

num_silent = 0

snd_started = False

r = array('h')

while 1:

# little endian, signed short

snd_data = array('h', stream.read(CHUNK_SIZE))

if byteorder == 'big':

snd_data.byteswap()

r.extend(snd_data)

silent = is_silent(snd_data)

if silent and snd_started:

num_silent += 1

elif not silent and not snd_started:

snd_started = True

if snd_started and num_silent >30:

break

sample_width = p.get_sample_size(FORMAT)

stream.stop_stream()

stream.close()

p.terminate()

r = normalize(r)

r = trim(r)

r = add_silence(r, 0.5)

return sample_width, r

def record_to_file(path):

"Records from the microphone and outputs the resulting data to 'path'"

sample_width, data = record()

data = pack('<' + ('h'*len(data)), *data)

wf = wave.open(path, 'wb')

wf.setnchannels(1)

wf.setsampwidth(sample_width)

wf.setframerate(RATE)

wf.writeframes(data)

wf.close()

if __name__ == '__main__':

print("please speak a word into the microphone")

record_to_file('demo.wav')

print("done - result written to demo.wav")

pyaudio库,使用这个可以进行录音,播放,生成wav文件等等。PyAudio 提供了 PortAudio 的 Python 语言版本,这是一个跨平台的音频 I/O 库,使用 PyAudio 你可以在 Python 程序中播放和录制音频。为PoTaTudio提供Python绑定,跨平台音频I/O库。使用PyAudio,您可以轻松地使用Python在各种平台上播放和录制音频,例如GNU/Linux、微软Windows和苹果Mac OS X/MACOS。

ALSA是Advanced Linux Sound Architecture的缩写,高级 Linux 声音架构的简称,它在Linux操作系统上提供了音频和MIDI(Musical Instrument Digital Interface,音乐设备数字化接口)的支持。

二者的关系是:pyaudio调用ALSA实现录音功能。

使用 pyaudio 录制声音的时候报错。直接使用ALSA命令录音无问题。

pyaudio是依赖于linux的ALSA版本,所以在安装的时候是要考虑linux发行版本的(ubuntu, centos,debian)。所以只需要在bash执行:

即可。