Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pygame.mixer.Sound(buffer) does not attempt to identify stream type #556

Closed
baliame opened this issue Oct 10, 2018 · 4 comments
Closed

pygame.mixer.Sound(buffer) does not attempt to identify stream type #556

baliame opened this issue Oct 10, 2018 · 4 comments

Comments

@baliame
Copy link

baliame commented Oct 10, 2018

pygame.mixer.Sound(file="example.ogg")

Reads and loads an ogg file properly without a problem, however

with open("example.ogg", rb) as f:
  data = f.read()
  pygame.mixer.Sound(buffer=data)

attempts to read the buffer in a different format. Could we change the behaviour to interpret the buffer as the contents of a while or if ogg files don't have a header for identifying them, can we at least get a flag to note that the buffer should be interpreted as an ogg?

@llenck
Copy link

llenck commented Oct 16, 2018

I'm pretty sure the buffer / array arguments aren't meant to accept the contents of a file but rather raw samples which will be played back in the format that pygame.mixer.get_init() returns, which is quite useful if you are editing or generating audio on the fly.

I think if you can convince people that a flag for interpreting the buffer argument as file contents (ogg and wav files have easy, clear headers) would be a useful feature, someone (or me) might add this; but without anyone else approving this feature request I won't work on this.

@baliame
Copy link
Author

baliame commented Oct 16, 2018

My idea is that all the data I have is packed into a single data file to reduce file count, including a large number of sounds and images. While the images can be transmitted this way to pygame.image using a BytesIO, sounds are a problem and I am currently working around this problem by creating a temporary directory, writing them to a file there and then rereading using pygame.mixer.

Basically, I'd like to be able to reduce the number of files that are packaged with the game.

@illume
Copy link
Member

illume commented Oct 16, 2018

Hi,

are you having issues using BytesIO like below? This seems to work for me.

python 2 version...

$ python
Python 2.7.15 (default, Jul 15 2018, 21:21:29) 
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import StringIO
>>> f=StringIO.StringIO(open('data/house_lo.ogg').read())
>>> import pygame
pygame 1.9.5.dev0
Hello from the pygame community. https://www.pygame.org/contribute.html
>>> pygame.init()
objc[22691]: Class SDLApplication is implemented in both /usr/local/opt/sdl2/lib/libSDL2-2.0.0.dylib (0x101aca150) and /usr/local/lib/python2.7/site-packages/pygame-1.9.5.dev0-py2.7-macosx-10.13-x86_64.egg/pygame/sdlmain_osx.so (0x103543600). One of the two will be used. Which one is undefined.
(5, 0)
>>> pygame.mixer.Sound(f)
<Sound object at 0x1019628c8>
>>> s = _
>>> s.get_length()
7.104852676391602
>>> s2=pygame.mixer.Sound('data/house_lo.ogg')
>>> s2.get_length()
7.104852676391602

python 3 version

python3
Python 3.7.0 (default, Jul 17 2018, 00:00:00) 
[Clang 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
pygame 1.9.5.dev0
Hello from the pygame community. https://www.pygame.org/contribute.html
>>> from io import BytesIO
>>> f=BytesIO(open('data/house_lo.ogg', 'rb').read())
>>> pygame.init()
(6, 0)
>>> pygame.mixer.Sound(f).get_length()
7.104852676391602
>>> pygame.mixer.Sound(file='data/house_lo.ogg').get_length()
7.104852676391602

@baliame
Copy link
Author

baliame commented Oct 16, 2018

...you're absolutely right and I don't know why I didn't think of that considering I already used it for images. Thanks!

@baliame baliame closed this as completed Oct 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants