Page 1 of 2

If someone was going to intergrate a sound API into Celestia

Posted: 15.07.2005, 07:52
by ben_o_
... 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

Posted: 15.07.2005, 08:03
by d.m.falk
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.

Posted: 15.07.2005, 08:17
by t00fri
Anybody thought about the resources this takes away from Celestia's MAIN task: rendering the universe in 3d and exceptional precision...

Bye Fridger

Posted: 15.07.2005, 08:44
by d.m.falk
If all Celestia is about is rendering, why bother? What's the point?

d.m.f.

Posted: 15.07.2005, 10:42
by selden
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.

Posted: 15.07.2005, 11:49
by t00fri
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

Posted: 15.07.2005, 13:36
by selden
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.

Posted: 15.07.2005, 16:10
by TourqeGlare
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.)

Posted: 16.07.2005, 01:39
by d.m.falk

Posted: 07.08.2005, 03:37
by fsgregs
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

Posted: 26.09.2005, 05:48
by booger
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.

Posted: 08.12.2005, 22:28
by vhpgomes
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

Posted: 08.12.2005, 22:52
by Rassilon
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:

Posted: 09.12.2005, 15:10
by scratt
Hi vhpgomes,

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

Kind regards,
scratt

Posted: 09.12.2005, 17:56
by vhpgomes
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

Posted: 09.12.2005, 18:12
by scratt
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.

Posted: 09.12.2005, 21:10
by fsgregs
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

Posted: 10.12.2005, 00:50
by buggs_moran
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.

Posted: 11.12.2005, 11:57
by vhpgomes
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

Posted: 11.12.2005, 18:24
by Malenfant
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.