How to save audio clips?

Discussion and help for Easy Save 3
doodlinbee
Posts: 23
Joined: Sat Sep 05, 2020 12:53 pm

How to save audio clips?

Post by doodlinbee »

I have an audio source that has it's clip change over time.
When I save the clip it produces giant save file... can't I just save the reference of the clip?
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: How to save audio clips?

Post by Joel »

Hi there,

If you save the clip directly using ES3.Save or ES3.SaveAudio then it will be saved by value and reference. However, if you save the clip as a field of another class using ES3.Save (e.g. by saving your AudioSource) then it will be saved by reference (assuming a reference to the AudioClip exists in your scene).

Alternatively you can create an ES3Settings object which specifies a referenceMode of ES3.ReferenceMode.ByRef and provide this as a parameter to the ES3.Save method when saving.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
doodlinbee
Posts: 23
Joined: Sat Sep 05, 2020 12:53 pm

Re: How to save audio clips?

Post by doodlinbee »

Hi there, thanks for answering. Unfortunately both of the solutions you proposed don't work.

For the first solution I saved the audiosource

Code: Select all

ES3.Save("audioSource", audioSource, saveSO.saveName)
and loaded with

Code: Select all

audioSource = ES3.Load("audioSource", saveSO.saveName, audioSource)
It retrieves the audiosource's ref but it doesn't change it's clip to what it was at the moment of saving, in fact, it doesnt change its values at all.


So I tried to save the clip by reference

Code: Select all

ES3Settings settings = new ES3Settings();
settings.referenceMode = ES3.ReferenceMode.ByRef;

ES3.Save("audioSourceClip", audioSource.clip, saveSO.saveName, settings);
But it still generate a huge file, here's how it look like

Code: Select all

	"audioSourceClip" : {
		"__type" : "UnityEngine.AudioClip,UnityEngine.AudioModule",
		"value" : {
			"_ES3Ref" : "1843017990479932329",
			"name" : "acid burn",
			"samples" : 63360,
			"channels" : 2,
			"frequency" : 44100,
			"sampleData" : [
			 **a huge array of numbers**
			]
		}
	}
	
which is too bad because by the look of it, it retrieves the reference which is "_ES3Ref" : "1843017990479932329". But it also adds everything about the clip like the name and all the sample data which we shouldn't need because we have the ref anyway.
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: How to save audio clips?

Post by Joel »

Hi there,

Apologies, I didn’t realise that Unity didn’t make the AudioSource class serializable at runtime. In this case you would need to go to Tools > Easy Save 3 > Types, search for AudioSource and select the ‘clip’ property.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
doodlinbee
Posts: 23
Joined: Sat Sep 05, 2020 12:53 pm

Re: How to save audio clips?

Post by doodlinbee »

Thanks! that did the trick :)
doodlinbee
Posts: 23
Joined: Sat Sep 05, 2020 12:53 pm

Re: How to save audio clips?

Post by doodlinbee »

I've got another problem, the "_ES3Ref" of a clip changes everytime I log out and re enter the game, same for the music scriptable object. This means that everything seem to work fine during a session of gameplay but If I quit the game then re enter the game and I load, the music will not play because the _ES3Ref is no longer valid.

Right clicking on the scriptable object and clicking "Add Reference to manager" did not solve the issue
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: How to save audio clips?

Post by Joel »

Hi there,

This usually means that your AudioClip is created or modified at runtime (which creates a new instance of the AudioClip), meaning that would be impossible to save it by reference between sessions.

If you don't feel this is the case for you, please could you create a new project with a very simple scene which replicates this and private message it to me with instructions.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
doodlinbee
Posts: 23
Joined: Sat Sep 05, 2020 12:53 pm

Re: How to save audio clips?

Post by doodlinbee »

no no I don't create or modify clips or scriptable objects during runtime, is there a way to know if somthing's on the ES3 reference mgr list without having to open the whole reference list that cause the editor to freeze?
doodlinbee
Posts: 23
Joined: Sat Sep 05, 2020 12:53 pm

Re: How to save audio clips?

Post by doodlinbee »

Also adding multiple references at once to the manager doesnt seem to work. I select a bunch of files and right click -> Add reference to manager, and only the first one is added
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: How to save audio clips?

Post by Joel »

Hi there,

You can use ES3ReferenceMgr.Current.Get(Object obj) to determine whether something is in the reference manager.
Also adding multiple references at once to the manager doesnt seem to work. I select a bunch of files and right click -> Add reference to manager, and only the first one is added
This appears to be working fine for me, and I've had no other reports of this. Are you on the latest version of Easy Save?

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Post Reply