orlandpm / Math-for-Programmers

Source code for the book, Math for Programmers
840 stars 391 forks source link

Chapter 13 sound not playing. #9

Open sgoshaye opened 3 years ago

sgoshaye commented 3 years ago

When I run this cell:

sound = pygame.sndarray.make_sound(arr)
sound.play()

I get this error:

ValueError                                Traceback (most recent call last)
<ipython-input-18-f6cf0bf38cc3> in <module>
----> 1 sound = pygame.sndarray.make_sound(arr)
      2 sound.play()

~/anaconda3/envs/intern/lib/python3.7/site-packages/pygame/sndarray.py in make_sound(array)
     92     global numpysnd
     93     try:
---> 94         return numpysnd.make_sound(array)
     95     except AttributeError:
     96         import pygame._numpysndarray as numpysnd

~/anaconda3/envs/intern/lib/python3.7/site-packages/pygame/_numpysndarray.py in make_sound(array)
     72     """
     73 
---> 74     return mixer.Sound(array=array)

ValueError: Unsupported integer size 8

I am using Python 3.7.9 on Ubuntu 20.04 LTS.

suage commented 3 years ago

check this

https://github.com/orlandpm/Math-for-Programmers/issues/5#issue-792545823

SeveneduS commented 3 years ago

@sgoshaye try to reshape an arr : size = 44100 arr = np.repeat(arr.reshape(size, 1), 2, axis = 1)

JaHIY commented 4 months ago

You can try the following code:

import numpy as np
import pygame, pygame.sndarray

pygame.mixer.init(frequency=44100, size=-16, channels=1)
arr = np.random.randint(-10000, 10000, size=44100, dtype='int16') #set `dtype` to `int16`
sound = pygame.sndarray.make_sound(arr)
sound.play()