Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

How can I make sounds in my Perl game scripts?

by apprentice (Scribe)
on Oct 08, 2004 at 13:40 UTC ( [id://397645]=perlquestion: print w/replies, xml ) Need Help??

apprentice has asked for the wisdom of the Perl Monks concerning the following question:

Hi Fellow Monksters, I've been writing a game (yet another perl minesweeper) as a way of teaching myself Perl/Tk and have come to the part where I want to add sounds for various actions:
- ticking sound for the clock/timer
- click sound (harder than the ticking) for mouse clicks
- harsh buzzer or explosion for clicking on a mine
- ding-ding-ding or some other appropriate sound for winning

I know I can play a bell sound by printing "\a", but it isn't that great and I'd like to play better sounds. I do what it to be cross-platform compatible if possible, i.e., both MS and *NIX. I have searched CPAN and the various 'SOUND' modules there seem to be platform-specific and I have not found any good examples.

Therefore, I am asking for the wisdom of the Perl adherents who have gone this way before. What say ye? What modules have you used or methods devised for making/playing such sounds? Can you point me to any script examples? Are there any cross-platform solutions?

I am your humble supplicant.


"Peace, love, and Perl...well, okay, mostly just Perl!" --me

Apprentice
  • Comment on How can I make sounds in my Perl game scripts?

Replies are listed 'Best First'.
Re: How can I make sounds in my Perl game scripts?
by Jouke (Curate) on Oct 08, 2004 at 15:06 UTC
    If you need a cross-platform solution, you could use either Audio::Play or switch from Tk to Wx and use Wx's Wx::Wave class to play .wav files.


    Jouke Visser
    Using Perl to enable the disabled: pVoice
Re: How can I make sounds in my Perl game scripts?
by Fletch (Bishop) on Oct 08, 2004 at 13:51 UTC

    There's SDL bindings for Perl, which would probably be the most cross platform solution. I don't know about them coexisting well with Tk, but it's worth a look.

      I try to report things when I see them, but I generally find (even with the uploaded snapshots) that getting SDL bindings to Perl to work is a PITA ... which is why frozen bubble's Deb-package, instead of requiring sdl-perl, actually brings along it's own tweaked copy. Chromatic has "snapshots" on his web site that may work better than the ones on CPAN, but in general you have about a 50% chance of getting it to work on a given system ...

      I'm seriously considering just doing my gaming development in C -- same story with trying to get decent OpenGL bindings to work. (I don't know if OpenAL even exists for Perl yet). C just works. You won't be able to use Inline::C because both of these things have event loops that want to take over your program, and the embedded perl interpreter requirements are kind of hostile. As much as a love high level languages, library support (when callbacks are required) is always a sore point. Possibly I might embed lua...

      I know, it's not the answer you want. Heck, it's not the answer I want ...

        I use OpenGL::Simple. I've been very happy with it so far, although I haven't tried to do anything very complicated. (Although I mix it in with some pretty complicated OpenGL calls made directly from C++ code, and that works fine.)

        I also am doing what you say doesn't work very well -- I have a large C++ application with an embedded perl interpreter, and I use both OpenGL::Simple and Inline::CPP (extensively) to provide the glue between perl and C/C++. And I use my own event loop. For the embedded interpreter, I just had to give up on any notion of modularity and allow it to use all of its global variables and related nastiness. The result seems to work fine. I don't make heavy use of callbacks, but I do use them.

        That said, it's quite a big chunk to get working nicely together, and it's busywork that has little to do with the main part of writing a game. It would be nice to have a prefabricated framework with all these things mixed in. (I didn't mind doing it, because I was getting paid for it. And I was really just adding scripting to an existing framework. Can't release it as open source, sorry.)

        I can't really address the OP's question. For sound, I use SDL_mixer, but I only call it from within C++. In practice, I often trigger sounds via perl scripts, but they're just manipulating a sound node written in C++, not directly triggering sound playing. Again, probably too much framework for a straightforward game. Although an Inline::C wrapper to call SDL_mixer functions would be pretty darn easy to write.

        Inline::C is awesome. Just thought I should mention that.

Re: How can I make sounds in my Perl game scripts?
by halley (Prior) on Oct 08, 2004 at 13:53 UTC

    You might check out SDL (Simple DirectMedia Layer) and the SDL-perl bindings package. The SDL-perl project has a few management issues but you're not asking for the bleeding-edge latest and greatest features.

    SDL itself is a cross-platform media system, offering a unified native API to reach common sound and 2D/3D graphics features. SDL-perl just provides bindings to reach that API from Perl scripts.

    --
    [ e d @ h a l l e y . c c ]

Re: How can I make sounds in my Perl game scripts?
by Anonymous Monk on Oct 09, 2004 at 13:45 UTC
    I'm personally familiar with ecasound, whose library functions are available to perl through the Audio::Ecasound module. I can't say whether its heavyweight nature (intended for multitrack recording/playback and signal processing) will allow it to serve your purposes or not, but it's worth familiarizing yourself with if you have any interest in working with sound. OTOH, perhaps some command-line MIDI player accessed through system() calls would meet your requirements for something lightweight and cross-platform usable. Good luck!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://397645]
Approved by pikus
Front-paged by pikus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2024-04-25 20:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found