Beispiel #1
0
    public static AudioClip LoadMp3(string targetFile, long SongLength)
    {
        // ISSUE: method pointer
        mp3AudioClip.SongReadLoop = new AudioClip.PCMReaderCallback((object)null, __methodptr(Song_Stream_Loop));
        Stream stream = (Stream)File.OpenRead(targetFile);

        if (mp3AudioClip.readFullyStream != null)
        {
            ((Stream)mp3AudioClip.readFullyStream).Dispose();
        }
        mp3AudioClip.readFullyStream = new ReadFullyStream(stream);
        mp3AudioClip.readFullyStream.stream_CanSeek = (__Null)1;
        byte[]   numArray = new byte[1024];
        MpegFile mpegFile = new MpegFile((Stream)mp3AudioClip.readFullyStream, true);

        mpegFile.ReadSamples(numArray, 0, numArray.Length);
        mp3AudioClip.playbackDevice = mpegFile;
        long num1 = SongLength;
        int  num2;

        if (num1 > (long)int.MaxValue)
        {
            Debug.LogWarning((object)"uAudioPlayer - Song size over size on int #4sgh54h45h45");
            num2 = int.MaxValue;
        }
        else
        {
            num2 = (int)num1;
        }
        return(AudioClip.Create("uAudio_song", num2, mpegFile.get_WaveFormat().get_Channels(), mpegFile.get_WaveFormat().get_SampleRate(), true, mp3AudioClip.SongReadLoop));
    }
 public static Delegate AudioClip_PCMReaderCallback(LuaFunction func)
 {
     AudioClip.PCMReaderCallback d = (param0) =>
     {
         int    top = func.BeginPCall();
         IntPtr L   = func.GetLuaState();
         LuaScriptMgr.PushArray(L, param0);
         func.PCall(top, 1);
         func.EndPCall(top);
     };
     return(d);
 }
Beispiel #3
0
        private void loadOgg(string filePath)
        {
            if (string.IsNullOrWhiteSpace(filePath) || !Path.GetExtension(filePath).Equals(".ogg"))
            {
                Debug.Log($"Unable to load audio clip from path '{filePath}', only OGG format supported.");
                return;
            }

            _vorbis = new VorbisReader(filePath);

            AudioClip.PCMReaderCallback      onAudioRead        = data => { _vorbis.ReadSamples(data, 0, data.Length); };
            AudioClip.PCMSetPositionCallback onAudioSetPosition = newPos => { _vorbis.DecodedPosition = newPos; };

            string fileName = Path.GetFileName(filePath);

            Clip = AudioClip.Create(fileName, (int)_vorbis.TotalSamples, _vorbis.Channels, _vorbis.SampleRate,
                                    true, onAudioRead, onAudioSetPosition);
        }
Beispiel #4
0
        public static AudioClip Create(string name, int lengthSamples, int channels, int frequency, bool stream, AudioClip.PCMReaderCallback pcmreadercallback, AudioClip.PCMSetPositionCallback pcmsetpositioncallback)
        {
            if (name == null)
            {
                throw new NullReferenceException();
            }
            if (lengthSamples <= 0)
            {
                throw new ArgumentException("Length of created clip must be larger than 0");
            }
            if (channels <= 0)
            {
                throw new ArgumentException("Number of channels in created clip must be greater than 0");
            }
            if (frequency <= 0)
            {
                throw new ArgumentException("Frequency in created clip must be greater than 0");
            }
            AudioClip audioClip = AudioClip.Construct_Internal();

            if (pcmreadercallback != null)
            {
                audioClip.m_PCMReaderCallback += pcmreadercallback;
            }
            if (pcmsetpositioncallback != null)
            {
                audioClip.m_PCMSetPositionCallback += pcmsetpositioncallback;
            }
            audioClip.Init_Internal(name, lengthSamples, channels, frequency, stream);
            return(audioClip);
        }
Beispiel #5
0
 public static AudioClip Create(string name, int lengthSamples, int channels, int frequency, bool stream, AudioClip.PCMReaderCallback pcmreadercallback)
 {
     return(AudioClip.Create(name, lengthSamples, channels, frequency, stream, pcmreadercallback, null));
 }
Beispiel #6
0
 public static AudioClip Create(string name, int lengthSamples, int channels, int frequency, bool _3D, bool stream, AudioClip.PCMReaderCallback pcmreadercallback, AudioClip.PCMSetPositionCallback pcmsetpositioncallback)
 {
     return(AudioClip.Create(name, lengthSamples, channels, frequency, stream, pcmreadercallback, pcmsetpositioncallback));
 }
Beispiel #7
0
    static int Create(IntPtr L)
    {
        int count = LuaDLL.lua_gettop(L);

        if (count == 5)
        {
            string    arg0 = LuaScriptMgr.GetLuaString(L, 1);
            int       arg1 = (int)LuaScriptMgr.GetNumber(L, 2);
            int       arg2 = (int)LuaScriptMgr.GetNumber(L, 3);
            int       arg3 = (int)LuaScriptMgr.GetNumber(L, 4);
            bool      arg4 = LuaScriptMgr.GetBoolean(L, 5);
            AudioClip o    = AudioClip.Create(arg0, arg1, arg2, arg3, arg4);
            LuaScriptMgr.Push(L, o);
            return(1);
        }
        else if (count == 6)
        {
            string arg0 = LuaScriptMgr.GetLuaString(L, 1);
            int    arg1 = (int)LuaScriptMgr.GetNumber(L, 2);
            int    arg2 = (int)LuaScriptMgr.GetNumber(L, 3);
            int    arg3 = (int)LuaScriptMgr.GetNumber(L, 4);
            bool   arg4 = LuaScriptMgr.GetBoolean(L, 5);
            AudioClip.PCMReaderCallback arg5 = null;
            LuaTypes funcType5 = LuaDLL.lua_type(L, 5);

            if (funcType5 != LuaTypes.LUA_TFUNCTION)
            {
                arg5 = (AudioClip.PCMReaderCallback)LuaScriptMgr.GetNetObject(L, 6, typeof(AudioClip.PCMReaderCallback));
            }
            else
            {
                LuaFunction func = LuaScriptMgr.GetLuaFunction(L, 6);
                arg5 = (param0) =>
                {
                    int top = func.BeginPCall();
                    LuaScriptMgr.PushArray(L, param0);
                    func.PCall(top, 1);
                    func.EndPCall(top);
                };
            }

            AudioClip o = AudioClip.Create(arg0, arg1, arg2, arg3, arg4, arg5);
            LuaScriptMgr.Push(L, o);
            return(1);
        }
        else if (count == 7)
        {
            string arg0 = LuaScriptMgr.GetLuaString(L, 1);
            int    arg1 = (int)LuaScriptMgr.GetNumber(L, 2);
            int    arg2 = (int)LuaScriptMgr.GetNumber(L, 3);
            int    arg3 = (int)LuaScriptMgr.GetNumber(L, 4);
            bool   arg4 = LuaScriptMgr.GetBoolean(L, 5);
            AudioClip.PCMReaderCallback arg5 = null;
            LuaTypes funcType5 = LuaDLL.lua_type(L, 5);

            if (funcType5 != LuaTypes.LUA_TFUNCTION)
            {
                arg5 = (AudioClip.PCMReaderCallback)LuaScriptMgr.GetNetObject(L, 6, typeof(AudioClip.PCMReaderCallback));
            }
            else
            {
                LuaFunction func = LuaScriptMgr.GetLuaFunction(L, 6);
                arg5 = (param0) =>
                {
                    int top = func.BeginPCall();
                    LuaScriptMgr.PushArray(L, param0);
                    func.PCall(top, 1);
                    func.EndPCall(top);
                };
            }

            AudioClip.PCMSetPositionCallback arg6 = null;
            LuaTypes funcType6 = LuaDLL.lua_type(L, 6);

            if (funcType6 != LuaTypes.LUA_TFUNCTION)
            {
                arg6 = (AudioClip.PCMSetPositionCallback)LuaScriptMgr.GetNetObject(L, 7, typeof(AudioClip.PCMSetPositionCallback));
            }
            else
            {
                LuaFunction func = LuaScriptMgr.GetLuaFunction(L, 7);
                arg6 = (param0) =>
                {
                    int top = func.BeginPCall();
                    LuaScriptMgr.Push(L, param0);
                    func.PCall(top, 1);
                    func.EndPCall(top);
                };
            }

            AudioClip o = AudioClip.Create(arg0, arg1, arg2, arg3, arg4, arg5, arg6);
            LuaScriptMgr.Push(L, o);
            return(1);
        }
        else
        {
            LuaDLL.luaL_error(L, "invalid arguments to method: AudioClip.Create");
        }

        return(0);
    }
Beispiel #8
0
 public AudioClip GenerateSinusoidalClip(AudioClip.PCMReaderCallback pcmReaderCallback, int duration, int clipFrequency = 44100)
 {
     return(AudioClip.Create("sinwave", duration * clipFrequency, 1, clipFrequency, true, pcmReaderCallback));
 }
Beispiel #9
0
        public void Play(TimeSpan?startOff = null)
        {
#if uAudio_debug
            UnityEngine.Debug.LogWarning("&c&");
#endif
            if (State != uAudio_backend.PlayBackState.Playing)
            {
#if uAudio_debug
                UnityEngine.Debug.LogWarning("&d&");
#endif
                if (State == uAudio_backend.PlayBackState.Paused)
                {
                    Pause();
                }
                else
                {
                    State = uAudio_backend.PlayBackState.Playing;

                    try
                    {
                        //LoadFile(targetFile);
#if uAudio_debug
                        UnityEngine.Debug.LogWarning("B");
#endif
                        SongDone          = false;
                        flare_SongEnd     = false;
                        UAudio.targetFile = targetFile;

                        if (myAudioSource.clip == null)
                        {
#if uAudio_debug
                            UnityEngine.Debug.LogWarning("%B1%");
#endif
                            if (UAudio.LoadMainOutputStream())
                            {
#if uAudio_debug
                                UnityEngine.Debug.LogWarning("%B2%");
#endif

#if !UNITY_STANDALONE_WIN && blank
                                //WWW w = new WWW(targetFile);

                                //int song_sampleSize;
                                //SongReadLoop = new AudioClip.PCMReaderCallback(Song_Stream_Loop);
#if uAudio_debug
                                UnityEngine.Debug.LogWarning("%B4%");
#endif
                                //song_sampleSize = (int)UAudio.uwa.audioPlayback.TotalSamples;
#if uAudio_debug
                                UnityEngine.Debug.LogWarning("%B5%");
#endif
                                if (!targetFile.StartsWith("file://"))
                                {
                                    targetFile = "file://" + targetFile;
                                }

                                var www = new UnityEngine.WWW(targetFile);

                                UnityEngine.AudioClip myAudioClip = www.GetAudioClip(true, false, UnityEngine.AudioType.MPEG);                                // UnityEngine.AudioType.MPEG);

                                //  yield return www;
                                // Next line hangs
                                var clip = myAudioClip;
                                int i    = 100;

                                // todo : check this -=---> clip.loadState != UnityEngine.AudioDataLoadState.Loaded
#if !UNITY_2_6 && !UNITY_2_6_1 && !UNITY_3_0 && !UNITY_3_0_0 && !UNITY_3_1 && !UNITY_3_2 && !UNITY_3_3 && !UNITY_3_4 && !UNITY_3_5 && !UNITY_4_0 && !UNITY_4_0_1 && !UNITY_4_1 && !UNITY_4_2 && !UNITY_4_3 && !UNITY_4_5 && !UNITY_4_6 && !UNITY_4_7
                                while (clip.loadState != UnityEngine.AudioDataLoadState.Loaded && i > 0)
#endif
                                {
                                    i--;
                                    System.Threading.Thread.Sleep(10);
                                }
                                myAudioSource.clip = clip;
#else
#if !UNITY_STANDALONE_WIN && !UNITY_EDITOR
                                if (!targetFile.StartsWith("file://"))
                                {
                                    targetFile = "file://" + targetFile;
                                }
#endif


                                long song_sampleSize;
#if uAudio_debug
                                UnityEngine.Debug.LogWarning("%B3%");
#endif
                                AudioClip.PCMReaderCallback SongReadLoop;
                                SongReadLoop = new AudioClip.PCMReaderCallback(Song_Stream_Loop);

                                System.IO.Stream s = System.IO.File.OpenRead(targetFile);

                                if (readFullyStream != null)
                                {
                                    readFullyStream.Dispose();
                                }

                                readFullyStream = new uAudioDemo.Mp3StreamingDemo.ReadFullyStream(s);
                                readFullyStream.stream_CanSeek = true;

                                byte[]          buff = new byte[1024];
                                NLayer.MpegFile c    = new NLayer.MpegFile(readFullyStream, true);
                                //playbackDevice = m;

                                if (startOff == TimeSpan.Zero)
                                {
                                    c.ReadSamples(buff, 0, buff.Length);
                                }

                                playbackDevice = c;
                                //   UAudio.TotalTime
                                //     song_sampleSize = playbackDevice.Length;// * playbackDevice.SampleRate;
                                song_sampleSize = UAudio.SongLength;
                                // hack
                                // song_sampleSize = int.MaxValue;// need to better alocate this

                                int setSongSize;
                                if (song_sampleSize > int.MaxValue)
                                {
                                    UnityEngine.Debug.LogWarning("uAudioPlayer - Song size over size on int #4sgh54h45h45");
                                    setSongSize = int.MaxValue;
                                }
                                else
                                {
                                    setSongSize = (int)song_sampleSize;
                                }

                                myAudioSource.clip =
                                    UnityEngine.AudioClip.Create("uAudio_song", setSongSize,
                                                                 c.WaveFormat.Channels,
                                                                 c.WaveFormat.SampleRate,
                                                                 true, SongReadLoop);

                                if (!startOff.HasValue)
                                {
                                    CurrentTime = TimeSpan.Zero;
                                }
                                else
                                {
                                    CurrentTime = startOff.Value;
                                }
#endif


                                try
                                {
                                    if (sendPlaybackState != null)
                                    {
                                        sendPlaybackState(uAudio_backend.PlayBackState.Playing);
                                    }
                                }
                                catch
                                {
                                    UnityEngine.Debug.LogWarning("theAudioStream_sendStartLoopPump #32fw46hw465h45h");
                                }
#if uAudio_debug
                                UnityEngine.Debug.LogWarning("%B6%");
#endif
                            }
                            else
                            {
                                myAudioSource.clip = null;
                            }
                        }
                        else
                        {
#if uAudio_debug
                            UnityEngine.Debug.LogWarning("%B!%");
#endif
                        }

                        if (myAudioSource.clip != null)
                        {
#if uAudio_debug
                            UnityEngine.Debug.LogWarning("%B7%");
#endif
                            if (!myAudioSource.isPlaying)
                            {
                                myAudioSource.Play();
                            }

#if uAudio_debug
                            UnityEngine.Debug.LogWarning("%B8%");
#endif
                            updateTime = true;
                        }
                        else
                        {
                            State = uAudio_backend.PlayBackState.Stopped;
                        }
                    }
                    catch (System.Exception ex)
                    {
#if uAudio_debug
                        UnityEngine.Debug.LogWarning("%B9%" + System.Environment.NewLine + ex.Message);
#endif
                        State = uAudio_backend.PlayBackState.Stopped;
                        UnityEngine.Debug.LogWarning("uAudioPlayer - Play #j356j536j356j56j");
                        UnityEngine.Debug.LogWarning(ex);
                    }
                }
            }
        }
Beispiel #10
0
        public static AudioClip Create(string name, int lengthSamples, int channels, int frequency, bool stream, AudioClip.PCMReaderCallback pcmreadercallback, AudioClip.PCMSetPositionCallback pcmsetpositioncallback)
        {
            bool flag = name == null;

            if (flag)
            {
                throw new NullReferenceException();
            }
            bool flag2 = lengthSamples <= 0;

            if (flag2)
            {
                throw new ArgumentException("Length of created clip must be larger than 0");
            }
            bool flag3 = channels <= 0;

            if (flag3)
            {
                throw new ArgumentException("Number of channels in created clip must be greater than 0");
            }
            bool flag4 = frequency <= 0;

            if (flag4)
            {
                throw new ArgumentException("Frequency in created clip must be greater than 0");
            }
            AudioClip audioClip = AudioClip.Construct_Internal();
            bool      flag5     = pcmreadercallback != null;

            if (flag5)
            {
                audioClip.m_PCMReaderCallback += pcmreadercallback;
            }
            bool flag6 = pcmsetpositioncallback != null;

            if (flag6)
            {
                audioClip.m_PCMSetPositionCallback += pcmsetpositioncallback;
            }
            audioClip.CreateUserSound(name, lengthSamples, channels, frequency, stream);
            return(audioClip);
        }