Loading sounds from a saved game

I feel a little stupid posting this question, but I’ve search the wiki, the source code and the forum high and low and can’t seem to find anything on how to load a saved AudioNode. Here’s what I’m currently getting. When I save an AudioNode the object looks like this:

AudioNode[status=Playing, vol=0.05, pitch=1.4] W:(32.486195, -37.785606, -3.6162446E-6) L:(32.486195, -37.785606, -3.6162446E-6)

W: is the world translation L: is the local translation.

When I load that object from a save file I get this:

AudioNode[status=Stopped, vol=0.05, pitch=1.4] W:(0.0, 0.0, 0.0) L:(0.0, 0.0, 0.0)

So I assume somehow I’m going about this the wrong way. How exactly should you reconstruct your audio nodes after loading a saved file? Do I have to go through all the nodes and restart the audio manually? I can see how I would do this, but I’m wondering if there’s a slicker way to do it. Also, why is this object losing its positional information? And as a matter of fact the status is also incorrect in that it goes from playing to stopped.

I assume this is related to the fact that data, status, and channel in AudioNode are marked as transient and not saved. Why is that?

Many thanks.

It should save the local translation, since it is a subclass of node.
Could you show us your relevant code, please?

Thanks for your reply. I setup a simple test case and the issue with not saving the position seems to have gone away. Which is odd, but I’ll try to figure that out on my own. However, the issue with the AudioNode being in a stopped state is still there. Any guidance on what I should be doing in this situation would be helpful. Like I said, I could find no information on how these AudioNodes should be handled during load.

Hey, when you load your audio node you need to call the play() method to play the sound again. For that I would just search for audio nodes in the scene graph. For that you can use this method of the node class depthFirstTraversal.
If you only want to play certain sounds you have to add custom user data and evaluate them yourself for example.

Hmm, ok. Well it seems like bad form for me to be duplicating data, having to extend AudioNode and duplicating the state information that is already present, but not saved, in the superclass, just so I can save and load that data. I assume that’s the path I will have to take?

Seems so … Either you go like this or you add a custom user data, for example a boolean isPlaying and when you load your node you look for this and enable when neccessary.

But when you have to play all your sounds at the beginning anyway, you of course can play it instantly.

Why do you need this at all? Maybe there is an alternative way! When your AudioNode object is controlled by a control you can save the control together with the AudioNode.

However, I really would like to know what you need that for.

My setup is pretty straightforward and simple I think. I have an audionode (with an engine sound) attached to my model node (of for example an aircraft), that audio node is positional and looping of course. So I save the aircraft and reload and run into this issue.

I guess I could use user data and not extend audionode, however if I’m going to have to check the playing state and restart the sound anyway, I think it would be easiest to do that from the read method in my subclass. At least that’s what I’m thinking of doing off the top of my head.

Or you write a control which enables and disables your engine sound whenever needed and also save this as part of the model.

By the way, I just created a simple AudioNode, saved that one, read it again and for me the local translation is fine!

Yes I also wrote a simple AudioNode that worked ok from the translation standpoint. Not sure why it’s not working right elsewhere in my code. In some cases I’ve seen it where the localTranslation is correct, but the worldTranslation is not.

I do have code embedded into my object that plays the sound when the aircraft takes off, but doesn’t mess with the sound until the aircraft lands, except to change the pitch based on the engine rpm. The problem is if the object is loaded and in-flight already the sound is obviously not playing. I’ll be able to handle this just fine though, I just wanted to make sure that this wasn’t supposed to be working automagically for me.

I’m also curious if someone knows why the AudioNode status is transient. That’s very useful data that would help with the loading process if it were saved.

You shouldn’t care about world translation, it’s done by the engine.

Think so too :slight_smile:

I guess I was being stupid as the objects I was looking at were attached to an orphan node that was not attached in any way to the rootNode. That must’ve been causing the world trans to reset to three balls on load I guess even though they had a proper world trans in the original game before the save, despite still being detached. Doah. Oh well.

I do appreciate your help. Thank you.

Well, I can’t say anything about your scene graph from my position. I don’t know what you are doing with your nodes, but to me it seems like something is not clear at all here.

Well it’s not a problem. I for whatever reason, had commented out the line attaching the parent node to my scene graph. I uncommented it and everything is fine. Also I extended AudioNode and it works great. The nice thing is I didn’t have to duplicate the status from the superclass as I thought I might, but rather all I needed was to do all the work in the save and load methods.

I still don’t get why AudioNode doesn’t do this automatically. But at least it works.

1 Like

I guess because it can’t know where you want to play it. I mean, if we pretended to save all of the state then that would be the next thing folks complained about “Why is my audio playing from the beginning instead of 5 minutes in like when I saved it?”

The root of the problem is really treating scene graph objects like game objects. If you had real game objects and the scene graph was just a reflection of that then sound on/off would be part of the game object state… and that’s what you’d be saving/loading.

…then you also wouldn’t have to worry about a new version of JME messing up every one of your existing save games.

That makes sense I guess. Thanks for the explanation.

Glad it works now for you!