本文目录一览:
- 1、python 怎么录制系统声音不只是麦克风声音
- 2、python3.0怎样读取另一台电脑的系统声音,并在本机上报警提示?
- 3、PYTHON语言如何取到声音的频率(其他语言也可行)
- 4、如何调用python语言中的winsound模块静态常量
- 5、python实时获取麦克风输入音量?
python 怎么录制系统声音不只是麦克风声音
#我可以帮你写一段代码,能够录音形成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")
python3.0怎样读取另一台电脑的系统声音,并在本机上报警提示?
python版本切换全局版本切换:pyenv global anaconda-2.4.0全局切换为anaconda科学计算环境,因为,我现在也不做其他python开发,所以,无需再安装其他环境了。查看现在的python版本:michael@michael-ThinkCentre-XXXX:~$ pyenv versionssystem2.7.13.4.1* anaconda-2.4.0 (set by /home/michael/.pyenv/version)有全局版本切换,当然也会有局部环境的切换:在test文件夹下希望切换到python3.4.1:pyenv local python3.4.1
PYTHON语言如何取到声音的频率(其他语言也可行)
先得到时域信号,然后做傅立叶变换,得到频谱。
感觉题主可能对python比较熟悉?那就别换语言了。稍微百度谷歌以下肯定能找到python的傅立叶变换的库。
如何调用python语言中的winsound模块静态常量
第一步,显示wav文件名称,利用
winsound.SND_FILENAME
如下图所示:
第二步,显示从注册表的声音名称,利用
winsound.SND_ALIAS
如下图所示:
第三步,如果要使声音重复播放,利用
winsound.SND_LOOP
如下图所示:
第四步,从记忆图片的wav文件播放声音,利用
winsound.SND_MEMORY
如下图所示:
第五步,停止所有具体声音实例,利用
winsound.SND_PURGE
如下图所示:
第六步,立刻返回到上次,准许声音异步播放,如下图所示:
python实时获取麦克风输入音量?
import sounddevice as sd
import numpy as np
duration = 10 # 延迟(单位秒)
def print_sound(indata, outdata, frames, time, status):
volume_norm = np.linalg.norm(indata)*10
print "|" * int(volume_norm)
with sd.Stream(callback=print_sound):
sd.sleep(duration * 1000)