If someone was going to intergrate a sound API into Celestia

The place to discuss creating, porting and modifying Celestia's source code.
Topic author
ben_o_
Posts: 9
Joined: 09.07.2005
With us: 19 years 4 months
Location: Oxford, UK

If someone was going to intergrate a sound API into Celestia

Post #1by ben_o_ » 15.07.2005, 07:52

... what would it be?

Does anyone have any feelings about using OpenAL? Or would it actually be less time consuming to use platform specific due to OpenAL quirks? (unlikely in my view.)

And most importantly, why would you like to see a sound system in Celestia? To play mp3's as you float around, play a narration for a .cel perhaps, or even event driven sounds, such as proximity to a satellite?

Are there any good alternatives to OpenAL that anyone can suggest?

Ben

d.m.falk
Posts: 105
Joined: 03.07.2005
With us: 19 years 4 months
Location: Eureka, California

Post #2by d.m.falk » 15.07.2005, 08:03

I was thinking of offering just such a suggestion for an offshoot of celestia, if not Celestia itself- And for the reasons you mention.

These could include magnetospheric sounds and pulsar sounds (which WOULD make sense!), telemetry sounds from probes and such...

Narration in a CELX script would be nice... :)

Sound's a bit more universal, since all you have to do is pipe it through stdout, which would be interpreted in certain OS' UIs going through the common sound drivers.

d.m.f.
There IS such a thing as a stupid question, but it's not the question first asked. It's the question repeated when the answer has already been given. -d.m.f.

Avatar
t00fri
Developer
Posts: 8772
Joined: 29.03.2002
Age: 22
With us: 22 years 8 months
Location: Hamburg, Germany

Post #3by t00fri » 15.07.2005, 08:17

Anybody thought about the resources this takes away from Celestia's MAIN task: rendering the universe in 3d and exceptional precision...

Bye Fridger

d.m.falk
Posts: 105
Joined: 03.07.2005
With us: 19 years 4 months
Location: Eureka, California

Post #4by d.m.falk » 15.07.2005, 08:44

If all Celestia is about is rendering, why bother? What's the point?

d.m.f.
There IS such a thing as a stupid question, but it's not the question first asked. It's the question repeated when the answer has already been given. -d.m.f.

Avatar
selden
Developer
Posts: 10192
Joined: 04.09.2002
With us: 22 years 2 months
Location: NY, USA

Post #5by selden » 15.07.2005, 10:42

Fridger,

I wouldn't consider this to be "taking resources away". You're assuming that the people who might be working on adding sound would otherwise be working on some other feature. More likely they wouldn't be adding anything.

I think synchronized sound that's triggered by particular events in Celestia could be very important for educational features, for example. People tend to remember more when related information is available in several different ways: sound, vision, touch, etc.
Selden

Avatar
t00fri
Developer
Posts: 8772
Joined: 29.03.2002
Age: 22
With us: 22 years 8 months
Location: Hamburg, Germany

Post #6by t00fri » 15.07.2005, 11:49

selden wrote:Fridger,

I wouldn't consider this to be "taking resources away". You're assuming that the people who might be working on adding sound would otherwise be working on some other feature. More likely they wouldn't be adding anything.


I think synchronized sound that's triggered by particular events in Celestia could be very important for educational features, for example. People tend to remember more when related information is available in several different ways: sound, vision, touch, etc.


Selden,

did you perhaps misunderstand? I was taking about CPU power nothing else. We are spending a hell of a time to keep the fps rare up through clever culling techniques. If I just switch on some music player in the background in addition, Celestia stops being fun, even with my fast graphics card and 3GB of RAM.

That was what I was alluding to.

Bye Fridger

Avatar
selden
Developer
Posts: 10192
Joined: 04.09.2002
With us: 22 years 2 months
Location: NY, USA

Post #7by selden » 15.07.2005, 13:36

Aha, I did misunderstand.

However, I still disagree with you :)

I think that whatever audio routines are used by Celestia must allow the user to decide what resolution is to be used, just as with other types of Addons: none, low sampling rate monaural through full resolution 7 channel surround sound. I suspect that'll be hard to accomplish, though.
Selden

TourqeGlare
Posts: 100
Joined: 07.05.2005
With us: 19 years 6 months
Location: Wherever Vger went

Post #8by TourqeGlare » 15.07.2005, 16:10

There is no sound in space.... Image




(I agree with both Fridger and Seldon.
My thought on this is lets wait a few years and see how this goes.)

d.m.falk
Posts: 105
Joined: 03.07.2005
With us: 19 years 4 months
Location: Eureka, California

Post #9by d.m.falk » 16.07.2005, 01:39

There IS such a thing as a stupid question, but it's not the question first asked. It's the question repeated when the answer has already been given. -d.m.f.

Avatar
fsgregs
Posts: 1307
Joined: 07.10.2002
With us: 22 years 1 month
Location: Manassas, VA

Post #10by fsgregs » 07.08.2005, 03:37

I for one would love to hear sound added to Celestia. My students would really enjoy hearing the crab nebula pulsing as it spins, or the roar of air passing over wings as one flies the "Celestia ship" through Earth's atmosphere. It becomes even more immersive an experience.

Narration in scripts would also be a great feature. So, anyone who wishes to code sound into the program, I encourage you to do so. Fridger's point is right on the money, however. The sound file that plays should not be commanding so much CPU time that it slows down Celestia. The program is already getting very topheavy.

:)

Regards,

Frank

booger
Posts: 4
Joined: 16.06.2002
With us: 22 years 5 months
Location: Redondo Beach CA
Contact:

Post #11by booger » 26.09.2005, 05:48

I would also love to see sound added to Celestia. I'm a professional voice actor, and my sister is an elementary school teacher. I would love to be able to record some educational narration to go along with some celestia scripts. I think a lot of teachers could benefit from the addition of audio.

I understand if it isn't high on the priority list, but it would definitely be a useful feature.

vhpgomes
Posts: 15
Joined: 08.12.2005
With us: 18 years 11 months
Location: Brazil

Post #12by vhpgomes » 08.12.2005, 22:28

For whom it may concern,

I implemented a way to play sounds within Celestia...

I created a new script command : play

and you can use it like that: play { file "teste.wav" }

I'd be happy to share my code with anyone BUT:

- It works ONLY on Linux;
- It plays ONLY raw .wav files;
- It works just fine on my computer, running Fedora Core 4, and I give no guarantees that it will work on other systems

The changes I made fullfills all my needs so I'll not make any other...
Unless that the Celestia's developers want to add the play sound functionality in the program,.. if so, I would love to help to make mine code (or a new one) more portable, with more file formats supported, etc...

[]'s

Victor

Rassilon
Posts: 1887
Joined: 29.01.2002
With us: 22 years 10 months
Location: Altair

Post #13by Rassilon » 08.12.2005, 22:52

Actually the cpu cycles OpenAL would eat up would all be dependant on the format of the sound clip. If you use MP3 you will eat up precious cpu cycles. But if you use WAV files you will use up very little ESPECIALLY if the sound is a trigger effect and not a loop.

But how large are quality wav files? :wink:
I'm trying to teach the cavemen how to play scrabble, its uphill work. The only word they know is Uhh and they dont know how to spell it!

scratt
Posts: 36
Joined: 05.12.2005
With us: 18 years 11 months
Location: Thailand

Post #14by scratt » 09.12.2005, 15:10

Hi vhpgomes,

I would love to see that sound code if at all possible.

Kind regards,
scratt
Extreme Sports Cafe - Remember, measure life by the moments that take your breath away, not by how many breaths you take...

vhpgomes
Posts: 15
Joined: 08.12.2005
With us: 18 years 11 months
Location: Brazil

Post #15by vhpgomes » 09.12.2005, 17:56

ok

The modifications I made for sound were:

In src/celengine/command.h (this will create the class that will be instantied when you call the play command in a script)

Code: Select all

class CommandPlay : public InstantaneousCommand
{
 public:
    CommandPlay(const std::string);
    void process(ExecutionEnvironment&);

 private:
    std::string filename;
};

In src/celengine/command.cpp (implements the class above)

Code: Select all

////////////////
// Play command: plays a .wav file

CommandPlay::CommandPlay(string _filename) :
    filename(_filename)
{
}

void CommandPlay::process(ExecutionEnvironment& env)
{
    env.getCelestiaCore()->playRawWavFile(filename);
}

In src/celestia/celestiacore.h (put the code below anywhere inside the CelestiaCore class)

Code: Select all

 private:
    FILE* sound_file;

    pthread_t       sound_tid;
    pthread_mutex_t sound_file_mutex;

    static void* sound_thread(void*);

 public:
    void playRawWavFile(std::string);

In src/celestia/celestiacore.cpp (in the CelestiaCore class constructor, find the distanceToScreen(400), add a ',' and two lines... it'll become something like that)

Code: Select all

    distanceToScreen(400), // don't forget to add the ','
    sound_file(0),
    sound_tid(0)

In the same file src/celestia/celestiacore.cpp add these somewhere

Code: Select all

void CelestiaCore::playRawWavFile(std::string filename)
{
    // check if the thread was already created
    if(!sound_tid)
    {
        pthread_mutex_init(&sound_file_mutex,NULL);
        pthread_create(&sound_tid,NULL,sound_thread,this);
    }

    string real_file_name = "sounds/" + filename;
    pthread_mutex_lock(&sound_file_mutex);

        if(sound_file)
            fclose(sound_file);

        sound_file = fopen(real_file_name.c_str(),"r");

        if(!sound_file)
            cerr << "Failed to open sound file: " << real_file_name << endl;

    pthread_mutex_unlock(&sound_file_mutex);
}

void* CelestiaCore::sound_thread(void* arg)
{
    pthread_detach(pthread_self());
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);

    CelestiaCore* mthis = (CelestiaCore*) arg;

    FILE* sound_device = 0;

    unsigned char sound_buffer[128];

    sound_device = fopen("/dev/dsp","w");

    if(!sound_device)
    {
        cerr << "Failed to open /dev/dsp" << endl;
        return NULL;
    }

    while(true)
    {
        pthread_mutex_lock(&mthis->sound_file_mutex);

        if(!mthis->sound_file || feof(mthis->sound_file))
        {
            pthread_mutex_unlock(&mthis->sound_file_mutex);
            usleep(100);
            continue;
        }

        fread(sound_buffer,1,128,mthis->sound_file);

        pthread_mutex_unlock(&mthis->sound_file_mutex);

        fwrite(sound_buffer,1,128,sound_device);

        fflush(sound_device);
    }
}

Finally, in src/celengine/cmdparser.cpp find the method Command* CommandParser::parseCommand() , inside this function you will find a lot of

Code: Select all

else if(commandName == "something")
here is where celestia recognizes the scripts commands, so we have to add a new one for our new command. Put a new else if statement anywhere you like (I placed it after the else if (commandName == "unmarkall") which is the last one, just before the last else...). The result is:

Code: Select all

    else if (commandName == "unmarkall")
    {
        cmd = new CommandUnmarkAll();
    }
    else if (commandName == "play")
    {
        string filename;
        paramList->getString("file", filename);
        cmd = new CommandPlay(filename);
    }
    else
    {
        error("Unknown command name '" + commandName + "'");
        cmd = NULL;
    }



And we're done :D

Now a few comments:

As you can see I created a thread, just to play the sound. Maybe this isn't the best way, but this allows the program to continue running while it plays the file. So you can call a play command in the script and continue to issue other commands.

The way I implemented, celestia will play the sound 'till it ends OR 'till you issue another play command. So if you want to play two wav files in a row, you'll have to issue a wait command between them, If you don't do that, you'll only listen to the second file.


Well any question don't hesitate to ask, and forgive me for any error in my writing, since Englsh is not my main language...

Hope it helped,

Victor

scratt
Posts: 36
Joined: 05.12.2005
With us: 18 years 11 months
Location: Thailand

Post #16by scratt » 09.12.2005, 18:12

Thanks for that Victor..

Will update you on progess over the weekend..

Kind regards,
scratt

EDIT : All worked fine. I tweaked it for my system: OS X and got it working suprisingly easy. But then the instructions were very clear... Thanks.
Last edited by scratt on 12.12.2005, 08:11, edited 1 time in total.
Extreme Sports Cafe - Remember, measure life by the moments that take your breath away, not by how many breaths you take...

Avatar
fsgregs
Posts: 1307
Joined: 07.10.2002
With us: 22 years 1 month
Location: Manassas, VA

Post #17by fsgregs » 09.12.2005, 21:10

Hi.

The code for using sound is very exciting. I've been waiting for it for a LONG time. I am not a coder and have to wait for someone to add this to the Windows and MAC versions of CMS, but please clarify something.

Am I correct in assuming that if the hard code for playing sound was added to Celestia, a user could create a short script celx file in which he would name the .wav file he wants Celestia to play when a keystroke is pressed? In other words, I assume the name of the .wav file does not have to be hard-coded into Celestia and can be changed by simply changing one line in a dynamic script command .... is that correct?

Frank

buggs_moran
Posts: 835
Joined: 27.09.2004
With us: 20 years 2 months
Location: Massachusetts, USA

Post #18by buggs_moran » 10.12.2005, 00:50

I understand the apprehension of coding sound directly into Celestia which might soak up some resources. However, if it were something you could call from the program, say the radio pulse of a spinning pulsar (from a context menu like alternate surfaces), it would dramatically increase the value of Celestia as a learning tool.
Homebrew:
WinXP Pro SP2
Asus A7N8X-E Deluxe
AMD Athlon XP 3000/333 2.16 GHz
1 GB Crucial RAM
80 GB WD SATA drive
ATI AIW 9600XT 128M

vhpgomes
Posts: 15
Joined: 08.12.2005
With us: 18 years 11 months
Location: Brazil

Post #19by vhpgomes » 11.12.2005, 11:57

fsgregs wrote:In other words, I assume the name of the .wav file does not have to be hard-coded into Celestia and can be changed by simply changing one line in a dynamic script command .... is that correct?
Frank, the little code I made do just that... You can change the sound just in the script.... Unfortunatelly it'll only work in Linux, so you can't use it yet... (MAYBE in the near future I'll port it to Windows... MAYBE...)

buggs_moran wrote:I understand the apprehension of coding sound directly into Celestia which might soak up some resources.
I understand that to... but I also understand that fast cpu's can easyly do both (Celestia and sound)... so I think the sound should be treated like textures: if you computer is a little old, use low-res textures (no sound), if you have a faster one you can use high-res textures (and hear the entire 9th symphony along with it)... That functionality could, like you said, be activated/deactivated with a context menu or a shortcut...

buggs_moran wrote:it would dramatically increase the value of Celestia as a learning tool.

Certainly it would...

Victor

Malenfant
Posts: 1412
Joined: 24.08.2005
With us: 19 years 3 months

Post #20by Malenfant » 11.12.2005, 18:24

t00fri wrote:Anybody thought about the resources this takes away from Celestia's MAIN task: rendering the universe in 3d and exceptional precision...


I think that's a spurious argument if you consider that in all probability people do other things while using Celestia. e.g. I usually have a web browser in the background and an mp3 player playing some music, maybe an email client too or Word or Excel - all of these are supposedly 'taking away resources' from Celestia, but it still manages just fine.

The capability to play sound in Celestia would be great and very useful, especially for educational narrations - and to be honest I think education is Celestia's main task.

When it comes to "rendering the universe in 3D and exceptional precision" Celestia is really far from this goal at the moment - it doesn't even render planets with the realistic photometric functions and its multiple lighting algorithm is currently extremely flawed and so far these do not seem to be on the table for fixing, and nobody has even bothered to comment on these issues when I raised them. Your galaxies and binary stars are excellent and very useful add-ons and certainly allow for a more fully rendered universe, but there are other important areas that are still badly flawed when it comes to realistically rendering the universe.
My Celestia page: Spica system, planetary magnitudes script, updated demo.cel, Quad system


Return to “Development”