我有一個函數用於創建Python中的方波,我似乎無法得到一個聲音出來,但是當我改變這樣的:Python方波函數 - 這裏發生了什麼?
value = state * volume
s.append([value, value])
這樣:
value = state * volume
s.append([0, value])
我聽到了一個聲音,但它的頻率遠高於我打算製作的130.81頻率。下面是完整的代碼:
elif current_type == SQUARE_WAVE_TYPE:
if event.type == KEYDOWN:
#lower notes DOWN
if event.key == K_z:
print current_type, 130.81
#current_played['z'] = MakeSineWave(80.81)
current_played['z'] = MakeSquareWave(130.81)
current_played['z'].play(-1)
elif event.key == K_c:
print current_type, 180.81
#current_played['c'] = MakeSineWave(80.81)
current_played['c'] = MakeSquareWave(180.81)
current_played['c'].play(-1)
任何人都可以看到,爲什麼這種情況正在發生:
def SquareWave(freq=1000, volume=10000, length=1):
num_steps = length * SAMPLE_RATE
s = []
length_of_plateau = SAMPLE_RATE/(2*freq)
counter = 0
state = 1
for n in range(num_steps):
value = state * volume
s.append([value, value])
counter += 1
if counter == length_of_plateau:
counter = 0
state *= -1
return numpy.array(s)
def MakeSound(arr):
return pygame.sndarray.make_sound(arr)
def MakeSquareWave(freq=1000):
return MakeSound(SquareWave(freq))
的代碼調用這些功能如下塊?這個方波函數實際上是否正確?
循環內'counter'與'length_of_plateau'的值是多少?您可以調試代碼並找到這兩個值的具體示例嗎? –
使用'print value'來查看你有什麼值。 – furas
我問的原因是我懷疑'length_of_plateau'是一個浮點值,而'counter'是一個整數,因此當頻率爲130.81時,它們在任何點都相等的機會根本就沒有,或者可能一次藍色月亮更正確。無論如何,請驗證這些變量的值,看看它是否與我在這裏說的相符。 –