我正在构建一个目前播放音阶的应用程序。以下是代码:
from numpy import linspace, sin, pi, int16
import pygame.mixer, pygame.sndarray, time
# tone synthesis
def note(freq, len, amp = 1, rate = 44100):
t = linspace(0, len, len * rate)
data = sin(2 * pi * freq * t) * amp
return data.astype(int16) # two byte integers
pygame.mixer.init(22050, -16, 2, 1000)
pygame.mixer.init()
cleft=pygame.mixer.Channel(0)
cleft.set_volume(1, 0)
cright=pygame.mixer.Channel(1)
cright.set_volume(0, 1)
x=0
pitches=[523, 554, 587, 622, 659, 698, 739, 784, 830, 880, 932, 987, 1046]
majorscale=[0, 2, 4, 5, 7, 9, 11, 12]
minorscale=[0, 2, 3, 5, 7, 9, 10, 12]
while x<8:
toneL = note(pitches[minorscale[x]], .2, amp=8000)
toneR = note(pitches[minorscale[x]], .2, amp=0)
snd_outL = pygame.mixer.Sound(toneL)
snd_outR = pygame.mixer.Sound(toneR)
cleft.play(snd_outL)
cright.play(snd_outR)
x += 1
time.sleep(.2)
在我的while循环的前两行中,我设置'amp'属性,使得音频只会出现在左声道中。这适用于循环的第一次迭代,但是它绕着音频循环的剩余时间是单声道的,我听到两只耳朵完全相同的东西。我失去了立体声效果。任何人都知道为什么?您应该能够复制此代码并自行尝试,只要您下载了numpy和pygame。