Search Unity

Temporary file that uses ram only in Android.

Discussion in 'Getting Started' started by narf03, May 10, 2017.

  1. narf03

    narf03

    Joined:
    Aug 11, 2014
    Posts:
    223
    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.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    How are you decrypting these? C#/.NET has various memory streams and such that look just like real files to other parts of .NET...
     
  3. narf03

    narf03

    Joined:
    Aug 11, 2014
    Posts:
    223
    but the problem is i need to read it into www class, www class cannot accept anything that is not file based.
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Sorry, I don't understand. Can you post some code?
     
  5. narf03

    narf03

    Joined:
    Aug 11, 2014
    Posts:
    223
    Code (CSharp):
    1.  
    2. fct_DecryptFile(strEncryptedFile, strDecryptedFile);
    3. WWW www = new WWW(strDecryptedFile);
    4. audio.clip = www.GetAudioClip(true,true);
    5.  
    the strEncryptedFile and strDecryptedFile is path of file
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    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!
     
  7. narf03

    narf03

    Joined:
    Aug 11, 2014
    Posts:
    223
    my function just a sample, the main issue is there is no way to push a stream or array of bytes into www class.
     
  8. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,203
  9. narf03

    narf03

    Joined:
    Aug 11, 2014
    Posts:
    223
    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.
     
  10. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,203
    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
     
    JoeStrout likes this.
  11. narf03

    narf03

    Joined:
    Aug 11, 2014
    Posts:
    223
    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).
     
  12. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    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.
     
  13. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,203
    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
     
    Last edited: May 11, 2017
  14. narf03

    narf03

    Joined:
    Aug 11, 2014
    Posts:
    223
    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.
     
  15. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,203
    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.
     
    Last edited: May 11, 2017
  16. narf03

    narf03

    Joined:
    Aug 11, 2014
    Posts:
    223
    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.