Temporary file that uses ram only in Android.

Wanted to know if there are such thing as ramdrive in android or are there anyway for me to use virtual path for temporary file?

I have encrypted data files that need to be decrypted and read using www(), currently im decrypting the file in SD card which it will be slow and waste write cycle of the SD card, the file will no longer be needed after decryption and read, so ill need to delete it too, if i can make use of ram instead of disk, the process should be alot faster, any ideas ? Thanks.

How are you decrypting these? C#/.NET has various memory streams and such that look just like real files to other parts of .NET…

but the problem is i need to read it into www class, www class cannot accept anything that is not file based.

Sorry, I don’t understand. Can you post some code?

fct_DecryptFile(strEncryptedFile, strDecryptedFile);
WWW www = new WWW(strDecryptedFile);
audio.clip = www.GetAudioClip(true,true);

the strEncryptedFile and strDecryptedFile is path of file

OK, I see. Both WWW.GetAudioClip, and probably your fct_DecryptFile function, require a real file.

I’m stumped. Hopefully somebody else has an idea!

my function just a sample, the main issue is there is no way to push a stream or array of bytes into www class.

Instead of using WWW you can use the UnityWebRequest and either a DownloadHandler or an UploadHandler. These are intended to be replacements for the WWW class. They require at least Unity 5.2/5.3 for the experimental release and 5.4 for the official release.

1 Like

I dont see how these classes can help, i have the Encrypted files in the device, and my code will have to decrypt the file, then i need to pass the decrypted data(bytes or stream) into WWW class so i can call the GetAudioClip function from the www class.

Ah. I finally see what you’re trying to do. If you have the data for the audio clip then you don’t need to use WWW. You can simply create it yourself with the AudioClip class.

https://docs.unity3d.com/ScriptReference/AudioClip.Create.html

Then feed it the decrypted data.

https://docs.unity3d.com/ScriptReference/AudioClip.SetData.html

1 Like

I think i tried this, but failed, the main reason is the file i have is mp3 format and AudioClip.SetData is expecting WAV data format. Currently my program works, but its very slow due to i need to write to device 1st(after decrypt) then read it back via WWW class(so it takes time to write then read the mp3), if the alternative that i am trying to do takes the same amount of time or more(converting mp3 to wav then feet it into AudioClip.SetData), then it will defeat the purpose. I havent tried looking for method to convert mp3 to wav yet, but i think it will be slow, looking at the SetData function where i need to loop through every single audio sample, should take few seconds to loop through a 20mB wav file(normal size mp3 convert to wav will be huge).

Instead of converting it and making an AudioClip, and assuming your goal is just to play this sound, you could hook into the procedural audio pipeline to provide the samples to the audio system on demand.

Perhaps this article on how we did procedural audio for RocketPlume will help you get started.

https://github.com/naudio/NAudio

Just about anything will be faster than the current method you are using. An SD card is way slower than system memory with the typical read speeds being double digit megabytes per second and write speeds being quite a ways behind that. That’s assuming they bought a quality card.

For the record Amazon’s best selling memory card is a Class 10 which is only guaranteed a 10MB/sec write speed. For the audio example you gave of 20MB that’s a two second task. Beating two seconds isn’t hard when keeping the data in memory.

https://www.sdcard.org/developers/overview/speed_class/
https://www.amazon.com/SanDisk-micro-Memory-Tablets-All-New/dp/B013TMNKAW

Using audacity to convert a mp3 to wav takes a few seconds(~10) with a fast desktop computer, then need to loop through all the samples (44100 samples per second), then assuming a long mp3 takes up to 15 mins, thats looping about 40 millions times, by calculation its going to take alot more than just 2 seconds when running on android devices.

I think my previous idea of using ramdrive(if there is such a thing) should be the ideal solution.

Three seconds on my end for a 15 minute MP3. My processor is eight years old. That said I wouldn’t use Audacity as a performance benchmark for audio conversion.

The ideal solution will be whichever the profiler gives the best results for. Keep in mind that a ramdrive brings with it overhead since the OS has to manage the file system among other things which means it will always be slower than direct access to the memory.

Plus it isn’t like the WWW class is magically eliminating the conversion step. It’s taking place somewhere under the hood.

I tough www class able to directly playing mp3 file without converting it to wav first ? Or at least able to do it on the fly without waiting the entire file completely processed.