0

An error occured in the code below. The error happened in the function Sinewave and the line

sine_wave = pygame.sndarray.make_sound(SineWave(frequency,16000,2))

Sinewave function:

def SineWave(freq=1000, volume=16000, length=1):

num_steps = length * 22050
s = []

for n in range(num_steps):
    value = int(math.sin(n * freq * (6.28318/22050) * length) * volume)
    s.append( [value, value] )
return s

I am trying to make sine waves in different frequencies to make scales.

import pygame
import math

#Set the frequency for each note in the scale
scale = {
    'C': 261.63,
    'D': 293.66,
    'E': 329.63,
    'F': 349.23,
    'G': 392.00,
    'A': 440.00,
    'B': 493.88
}

#Set the duration of each note (in seconds)
duration = 0.5

#Initialize the pygame library
pygame.init()
def SineWave(freq=1000, volume=16000, length=1):

num_steps = length * 22050
s = []

for n in range(num_steps):
    value = int(math.sin(n * freq * (6.28318/22050) * length) * volume)
    s.append( [value, value] )
return s

#Set the volume
pygame.mixer.music.set_volume(1.0)

#Play the scale
for note in scale:
    frequency=scale[note]
    # Generate a sine wave for the note
    sine_wave = pygame.sndarray.make_sound(SineWave(frequency,16000,2))

# Play the note
sine_wave.play()
# Wait for the note to finish playing
pygame.time.wait(int(duration * 1000))

#Quit pygame
pygame.quit()

It produces the following error:

Traceback (most recent call last):
  File "C:/Users/(username)/AppData/Local/Programs/Python/Python310/Scripts/scales.py", line 36, in <module>
    sine_wave = pygame.sndarray.make_sound(SineWave(frequency,16000,2))
  File "C:\Users\(username)\AppData\Local\Programs\Python\Python310\lib\site-packages\pygame\sndarray.py", line 92, in make_sound
    return mixer.Sound(array=array)
ValueError: list object does not export an array buffer
cafce25
  • 15,907
  • 4
  • 25
  • 31

0 Answers0