这个很简单的小玩意,大神误笑,小的就是刚刚开始接触这个东西哈。
卡拉OK原理其实就是先录声音,然后再把录得声音直接放出来,可以说不经过存储吧
录制是双通道的,采样率是44100格式pyaudio.paInt16.
不多说,上源码
#!/usr/bin/python
# *_* coding:utf-8 *_*
# python的pyaudio卡拉OK
import pyaudio
import wave
import random
import numpy as np
import time
p = pyaudio.PyAudio()
stream_in = p.open(format=pyaudio.paInt16,#格式
channels=2,#通道
rate=44100,#采样率
input=True,
frames_per_buffer=1024)
stream_out = p.open(format = pyaudio.paInt16,
channels = 2,
rate = 44100,
output = True)
print "recording"
frames=[]
for i in range(0,int(44100/1024*100)):# 100录音秒数
data = stream_in.read(1024)
data_array = np.fromstring(data,dtype = np.short)
for j in xrange(len(data_array)):
data_array[j] = data_array[j] + random.uniform(-5,5)
data = data_array.tostring()
stream_out.write(data)
frames.append(data)
print "recording ok"
stream_in.stop_stream()
stream_in.close()
p.terminate()
# 保存刚刚所录,其实如果只要实现卡拉OK用不着这不吧。
wf=wave.open("recording.wav",'wb')
wf.setnchannels(2)
wf.setsampwidth(p.get_sample_size(pyaudio.paInt16))
wf.setframerate(44100)
wf.writeframes(b''.join(frames))
wf.close()
这整个代码里头是有一些看上用不着的东西的,咋们可以自己去删。
至于运行,我的是在
windows10,cmd命令行下进行的,
python版本2.7
以下是简要
#!/usr/bin/python
# *_* coding:utf-8 *_*
# python的pyaudio卡拉OK简要声纹改变
import pyaudio
import wave
import random
import numpy as np
import time
p = pyaudio.PyAudio()
stream_in = p.open(format=pyaudio.paInt16,#格式
channels=2,#通道
rate=44100,#采样率
input=True,
frames_per_buffer=1024)
stream_out = p.open(format = pyaudio.paInt16,
channels = 2,
rate = 44100,
output = True)
print "recording"
frames=[]
k = 0
while True:
data = stream_in.read(1024)
data_array = np.fromstring(data,dtype = np.short)
print len(frames)
# print frames
if len(frames) > 2:
max_value_now = max(frames[len(frames) - 1])
max_value_late = max(frames[len(frames) - 2])
print max_value_now
print max_value_late
# rate_v_l = max_value_late / max_value_now
# if rate_v_l > 10:
# k = 1
for j in xrange(len(data_array)):
# data_array[j] = data_array[j] + random.uniform(-5,5)
# data_array[j] = max_value
# print data_array[j]
modulo = data_array[j] % 10
if modulo > 5:
data_array[j] = data_array[j] + (10 - modulo)
else:
data_array[j] = data_array[j] - modulo
data = data_array.tostring()
stream_out.write(data)
frames.append(data)
if k != 0:
k = 0
print "start"
time.sleep(5)
print "end"
# k += 1
print "recording ok"
stream_in.stop_stream()
stream_in.close()
p.terminate()
wf=wave.open("recording.wav",'wb')
wf.setnchannels(2)
wf.setsampwidth(p.get_sample_size(pyaudio.paInt16))
wf.setframerate(44100)
wf.writeframes(b''.join(frames))
wf.close()
这两个之间最大的不同在于,后面的更乱哈哈。
好吧,其实前一个也好,后一个也好,我都在所录的数据中对数据进行了修整吧,我也是小白,不太懂。
最后一个小门,代码的
http://download.csdn.net/download/qq_20081893/10273784