import numpy as np def speedx(sound_array, factor): """ 将音频速度乘以任意系数`factor` """ indices = np.round( np.arange(0, len(snd_array), factor) ) indices = indices[indices < len(snd_array)].astype(int) return sound_array[ indices.astype(int) ]
def stretch(sound_array, f, window_size, h):
""" 将音频按系数`f`拉伸 """
phase = np.zeros(window_size)
hanning_window = np.hanning(window_size)
result = np.zeros( len(sound_array) /f + window_size)
for i in np.arange(0, len(sound_array)-(window_size+h), h*f):
# 两个可能互相重叠的子数列
a1 = sound_array[i: i + window_size]
a2 = sound_array[i + h: i + window_size + h]
# 按第一个数列重新同步第二个数列
s1 = np.fft.fft(hanning_window * a1)
s2 = np.fft.fft(hanning_window * a2)
phase = (phase + np.angle(s2/s1)) % 2*np.pi
a2_rephased = np.fft.ifft(np.abs(s2)*np.exp(1j*phase))
# 加入到结果中
i2 = int(i/f)
result[i2 : i2 + window_size] += hanning_window*a2_rephased
result = ((2**(16-4)) * result/result.max()) # 归一化 (16bit)
return result.astype('int16')
def pitchshift(snd_array, n, window_size=2**13, h=2**11): """ 将一段音频的音高提高``n``个半音 """ factor = 2**(1.0 * n / 12.0) stretched = stretch(snd_array, 1.0/factor, window_size, h) return speedx(stretched[window_size:], factor)
from scipy.io import wavfile
fps, bowl_sound = wavfile.read("bowl.wav")
tones = range(-25,25)
transposed = [pitchshift(bowl_sound, n) for n in tones]
import pygame
pygame.mixer.init(fps, -16, 1, 512) # 太灵活了 <img src="http://python.jobbole.com/wp-includes/images/smilies/icon_wink.gif" alt=";)" class="wp-smiley">
screen = pygame.display.set_mode((640,480)) # 设置焦点
# 得到键盘的键位的正确顺序的列表
# ``keys`` 如 ['Q','W','E','R' ...] 一样排列
keys = open('typewriter.kb').read().split('\n')
sounds = map(pygame.sndarray.make_sound, transposed)
key_sound = dict( zip(keys, sounds) )
is_playing = {k: False for k in keys}
while True:
event = pygame.event.wait()
if event.type in (pygame.KEYDOWN, pygame.KEYUP):
key = pygame.key.name(event.key)
if event.type == pygame.KEYDOWN:
if (key in key_sound.keys()) and (not is_playing[key]):
key_sound[key].play(fade_ms=50)
is_playing[key] = True
elif event.key == pygame.K_ESCAPE:
pygame.quit()
raise KeyboardInterrupt
elif event.type == pygame.KEYUP and key in key_sound.keys():
key_sound[key].fadeout(50) # 停止播放并50ms淡出
is_playing[key] = False
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有