Esempio n. 1
0
        public static AudioClip Load(Stream dataStream, AudioFormat audioFormat, string unityAudioClipName, bool doStream = false, bool loadInBackground = true, bool diposeDataStreamIfNotNeeded = true)
        {
            AudioClip             audioClip = null;
            CustomAudioFileReader reader    = null;

            try
            {
                reader = new CustomAudioFileReader(dataStream, audioFormat);
                AudioInstance audioInstance = new AudioInstance
                {
                    reader       = reader,
                    samplesCount = (int)(reader.Length / (reader.WaveFormat.BitsPerSample / 8))
                };
                if (!doStream)
                {
                    audioClip = (audioInstance.audioClip = AudioClip.Create(unityAudioClipName, audioInstance.samplesCount / audioInstance.channels, audioInstance.channels, audioInstance.sampleRate, doStream));
                    if (diposeDataStreamIfNotNeeded)
                    {
                        audioInstance.streamToDisposeOnceDone = dataStream;
                    }
                    SetAudioClipLoadType(audioInstance, AudioClipLoadType.DecompressOnLoad);
                    SetAudioClipLoadState(audioInstance, AudioDataLoadState.Loading);
                    if (!loadInBackground)
                    {
                        audioInstance.dataToSet = new float[audioInstance.samplesCount];
                        audioInstance.reader.Read(audioInstance.dataToSet, 0, audioInstance.dataToSet.Length);
                        audioInstance.audioClip.SetData(audioInstance.dataToSet, 0);
                        SetAudioClipLoadState(audioInstance, AudioDataLoadState.Loaded);
                        return(audioClip);
                    }
                    lock (deferredLoadQueue)
                    {
                        deferredLoadQueue.Enqueue(audioInstance);
                    }
                    RunDeferredLoaderThread();
                    EnsureInstanceExists();
                    return(audioClip);
                }
                audioClip = (audioInstance.audioClip = AudioClip.Create(unityAudioClipName, audioInstance.samplesCount / audioInstance.channels, audioInstance.channels, audioInstance.sampleRate, doStream, delegate(float[] target)
                {
                    reader.Read(target, 0, target.Length);
                }, delegate(int target)
                {
                    reader.Seek(target, SeekOrigin.Begin);
                }));
                SetAudioClipLoadType(audioInstance, AudioClipLoadType.Streaming);
                SetAudioClipLoadState(audioInstance, AudioDataLoadState.Loaded);
                return(audioClip);
            }
            catch (Exception ex)
            {
                SetAudioClipLoadState(audioClip, AudioDataLoadState.Failed);
                Debug.LogError("Could not load AudioClip named '" + unityAudioClipName + "', exception:" + ex);
                return(audioClip);
            }
        }
Esempio n. 2
0
        public static AudioClip Load(Stream dataStream, AudioFormat audioFormat, string unityAudioClipName, bool doStream = false, bool loadInBackground = true, bool diposeDataStreamIfNotNeeded = true)
        {
            AudioClip             audioClip = null;
            CustomAudioFileReader reader    = null;

            try
            {
                reader = new CustomAudioFileReader(dataStream, audioFormat);
                AudioInstance audioInstance = new AudioInstance
                {
                    reader       = reader,
                    samplesCount = (int)(reader.Length / (reader.WaveFormat.BitsPerSample / 8))
                };
                if (doStream)
                {
                    audioClip = AudioClip.Create(unityAudioClipName, audioInstance.samplesCount / audioInstance.channels, audioInstance.channels, audioInstance.sampleRate, doStream, delegate(float[] target)
                    {
                        reader.Read(target, 0, target.Length);
                    }, delegate(int target)
                    {
                        if (audioInstance.channels == 1)
                        {
                            reader.Seek(target * 4, SeekOrigin.Begin);
                        }
                        else
                        {
                            reader.Seek(target * 8, SeekOrigin.Begin);
                        }
                    });
                    audioInstance.audioClip = audioClip;
                    SetAudioClipLoadType(audioInstance, AudioClipLoadType.Streaming);
                    SetAudioClipLoadState(audioInstance, AudioDataLoadState.Loaded);
                }
                else
                {
                    audioClip = AudioClip.Create(unityAudioClipName, audioInstance.samplesCount / audioInstance.channels, audioInstance.channels, audioInstance.sampleRate, doStream);
                    audioInstance.audioClip = audioClip;
                    if (diposeDataStreamIfNotNeeded)
                    {
                        audioInstance.streamToDisposeOnceDone = dataStream;
                    }
                    SetAudioClipLoadType(audioInstance, AudioClipLoadType.DecompressOnLoad);
                    SetAudioClipLoadState(audioInstance, AudioDataLoadState.Loading);
                    if (loadInBackground)
                    {
                        object obj = deferredLoadQueue;
                        lock (obj)
                        {
                            deferredLoadQueue.Enqueue(audioInstance);
                        }
                        RunDeferredLoaderThread();
                        EnsureInstanceExists();
                    }
                    else
                    {
                        audioInstance.dataToSet = new float[audioInstance.samplesCount];
                        audioInstance.reader.Read(audioInstance.dataToSet, 0, audioInstance.dataToSet.Length);
                        audioInstance.audioClip.SetData(audioInstance.dataToSet, 0);
                        SetAudioClipLoadState(audioInstance, AudioDataLoadState.Loaded);
                    }
                }
            }
            catch (Exception ex)
            {
                //SetAudioClipLoadState(audioClip, AudioDataLoadState.Failed);
                MSCLoader.ModConsole.Error(string.Concat(new object[]
                {
                    unityAudioClipName,
                    " - Failed:",
                    ex.Message
                }));
                Debug.LogError(string.Concat(new object[]
                {
                    "Could not load AudioClip named '",
                    unityAudioClipName,
                    "', exception:",
                    ex
                }));
            }
            return(audioClip);
        }