It is possible to convert an array of bytes to an AudioClip?

1

I'm making a cross platform application, with the game engine Unity, that simply takes images and audio files (.ogg) from a bucket made with the Amazon Simple Storage Service (Amazon S3). I'm having a problem converting audio files, represented in bytes, to AudioClips.

I have already tried the solutions from both links:

create AudioClip from byte

Noisy audio clip after decoding from base64

But both solutions doesn't resolve my problem, there is still the static sound after the conversion process from bytes to array.

Another solution I tried is to use the UnityWebRequestMultimedia and taking the audio from the url that every single object has in the bucket. This solution works only on the standalone version of the application (PC,Linux,Mac OS). When I try my application on an smartphone, with Android as OS, the UnityWebRequestMultimedia doesn't send the request and that's why I'm trying again to convert the bytes of the object taken from S3 to audioclip.

Every Audio file on S3 is an .ogg file with the following properties:

Properties of every file audio file

//Method that takes the bytes of the audioclip
    private AudioClip GetMediaBytesFromS3 (Amazon.Runtime.AmazonServiceResult<GetObjectRequest,GetObjectResponse> ServiceResult,string nameOfAudio)
        {
            //Take the bytes of the audio
            byte[] mediaBytes = GetBytesFromMultimediaObject(ServiceResult);

            //Make a float array
            float[] floatMediaBytes = new float[mediaBytes.Length / 4];



            for(int i=0;i<floatMediaBytes.Length; i++)
            {
                //Check if the architecture use the LittleEndian ordering of Bytes
                if(BitConverter.IsLittleEndian)
                {
                    Array.Reverse(mediaBytes,i*4,4);
                }

                floatMediaBytes[i] = ((float) BitConverter.ToInt16(mediaBytes,i*4)) / 0x80000000;
            }

            //Make a new audioclip
            AudioClip clipAudio = AudioClip.Create(nameOfAudio,floatMediaBytes.Length,1,16000,false);
            //See if the data are set
            if(clipAudio.SetData(floatMediaBytes,28))
            {

                return clipAudio;
            }

            return null;
        }

I simply want the audio be played correctly without the static noise.

c#
arrays
unity3d
audio
ogg
asked on Stack Overflow Apr 9, 2019 by Reaver • edited Apr 9, 2019 by Reaver

1 Answer

0

What you are doing looks quite all right - that has all the reasons to work fine - as long as data is in stored in the raw, uncompressed PCM stream. If its not - well its kind of the same as reading bytes from a JPG file and expect to find colors there. You will need to feed the data into an apropriate decoder first

quick google allowed me to find this link https://archive.codeplex.com/?p=nvorbis

I have not used it myself but it seems to fit the bill. Alternatively you could link to a .dll and do the decoding on the unmanaged side using some PInvokes

answered on Stack Overflow Apr 10, 2019 by zambari

User contributions licensed under CC BY-SA 3.0