Ejemplo n.º 1
0
        public void Play()
        {
#if !NO_FMOD
            if (Playing && mChannel != null)
            {
                bool paused = false;
                mChannel.getPaused(ref paused);
                if (paused)
                {
                    mChannel.setPaused(false);
                    return;
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }
            else
            {
                if (data._streamIndex >= 0)
                {
                    if (mStreamBytes != null)
                    {
                        mAbortStream = false;
                        return;
                    }
                    // if this isn't true, we may have 2 async requests to play, which would be super bad
                    GSGE.Debug.assert(mStreamBytes == null);
                    GSGE.Debug.assert(mAbortStream == false);
                }
                if (mSound != null)
                    Dispose();

                FMOD.RESULT result;
                FMOD.CREATESOUNDEXINFO exinfo = SoundData.exinfo;
                exinfo.cbsize = Marshal.SizeOf(exinfo);

                // allocate streaming?
                if (data._size > 0)
                {
                    mStreamRequested = true;
                    mStreamBytes = new byte[data._size];

                    string file = "Content/Audio/";
                    if (data._persist)
                        file += "Deferred/";
                    else
                        file += "Streaming/";
                    file += data._streamIndex;
                    file += ".mp3";


                    //GSGE.Debug.logMessage("streaming audio " + file);
                    NaCl.AsyncFS.FileStream fs = new NaCl.AsyncFS.FileStream(file, System.IO.FileMode.Open);

                    fs.BeginRead(mStreamBytes,
                        0,
                        mStreamBytes.Length,
                        delegate(IAsyncResult aresult)
                        {
                            int bytesRead;
                            try
                            {
                                bytesRead = fs.EndRead(aresult);
                            }
                            catch (System.IO.FileNotFoundException)
                            {
                                bytesRead = -1;
                            }
                            fs.Close();

                            if (bytesRead != data._size)
                            {
                                GSGE.Debug.logMessage("Error streaming in sound " + data._streamIndex);
                                mStreamStarted = true;
                                Dispose();
                                return;
                            }

                            if (data._persist)
                            {
                                byte[] audiodata = mStreamBytes;

                                mStreamBytes = null;
                                mStreamRequested = false;
                                mStreamStarted = false;

                                // reconfigure parent Sound to be persistent now.
                                data.setPersistent(audiodata);

                                if (mAbortStream)
                                {
                                    Dispose();
                                }
                                else
                                {
                                    Play();
                                }
                            }
                            else
                            {
                                mStreamStarted = true;
                                if (mAbortStream)
                                {
                                    Dispose();
                                }
                                else
                                {
                                    exinfo.length = (uint)mStreamBytes.Length;
                                    FMOD.MODE mode = FMOD.MODE.OPENMEMORY_POINT | FMOD.MODE.CREATECOMPRESSEDSAMPLE | FMOD.MODE.SOFTWARE;
                                    if (data._looping)
                                        mode |= FMOD.MODE.LOOP_NORMAL;
                                    result = FMOD.Framework.system.createSound(mStreamBytes, mode, ref exinfo, ref mSound);
                                    mOwnSound = true;
                                    ERRCHECK(result);

                                    AudioEngine audioengine = GSGE.AudioManager.GetAudioManager().GetAudioEngine();
                                    audioengine.FmodSounds.Add(this);

                                    result = FMOD.Framework.system.playSound(FMOD.CHANNELINDEX.FREE, mSound, false, ref mChannel);
                                    if (mSound == null)
                                    {
                                        Dispose();
                                    }
                                    else
                                    {
                                        GSGE.Debug.assert(mSound != null);
                                        ERRCHECK(result);
                                        mChannel.getFrequency(ref mBaseFrequency);
                                    }
                                }
                            }

                            // all streaming sounds need to assign these asap
                            Volume = _volume;
                            Pitch = _pitch;
                            LowPassCutoff = _lowPassCutoff;
                            Reverb = _reverb;

                            mAbortStream = false;
                        }, null);
                }
                else
                {
                    if (data.mSound == null)
                    {
                        data.createSound();
                    }

                    //exinfo.length = (uint)data.data.Length;
                    //FMOD.MODE mode = FMOD.MODE.OPENMEMORY_POINT | FMOD.MODE.CREATECOMPRESSEDSAMPLE | FMOD.MODE.SOFTWARE;
                    //if (data._looping)
                    //    mode |= FMOD.MODE.LOOP_NORMAL;
                    //result = FMOD.Framework.system.createSound(data.data, mode, ref exinfo, ref mSound);

                    //ERRCHECK(result);

                    mSound = data.mSound;
                    if (mSound == null)
                    {
                        Dispose();
                        return;
                    }
                    GSGE.Debug.assert(mSound != null);

                    AudioEngine audioengine = GSGE.AudioManager.GetAudioManager().GetAudioEngine();
                    audioengine.FmodSounds.Add(this);

                    result = FMOD.Framework.system.playSound(FMOD.CHANNELINDEX.FREE, mSound, false, ref mChannel);
                    GSGE.Debug.assert(mSound != null);
                    ERRCHECK(result);
                    mChannel.getFrequency(ref mBaseFrequency);
                }
            }
#endif
        }
Ejemplo n.º 2
0
        public void Play()
        {
#if !NO_FMOD
            if (Playing && mChannel != null)
            {
                bool paused = false;
                mChannel.getPaused(ref paused);
                if (paused)
                {
                    mChannel.setPaused(false);
                    return;
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }
            else
            {
                if (data._streamIndex >= 0)
                {
                    if (mStreamBytes != null)
                    {
                        mAbortStream = false;
                        return;
                    }
                    // if this isn't true, we may have 2 async requests to play, which would be super bad
                    GSGE.Debug.assert(mStreamBytes == null);
                    GSGE.Debug.assert(mAbortStream == false);
                }
                if (mSound != null)
                {
                    Dispose();
                }

                FMOD.RESULT            result;
                FMOD.CREATESOUNDEXINFO exinfo = SoundData.exinfo;
                exinfo.cbsize = Marshal.SizeOf(exinfo);

                // allocate streaming?
                if (data._size > 0)
                {
                    mStreamRequested = true;
                    mStreamBytes     = new byte[data._size];

                    string file = "Content/Audio/";
                    if (data._persist)
                    {
                        file += "Deferred/";
                    }
                    else
                    {
                        file += "Streaming/";
                    }
                    file += data._streamIndex;
                    file += ".mp3";


                    //GSGE.Debug.logMessage("streaming audio " + file);
                    NaCl.AsyncFS.FileStream fs = new NaCl.AsyncFS.FileStream(file, System.IO.FileMode.Open);

                    fs.BeginRead(mStreamBytes,
                                 0,
                                 mStreamBytes.Length,
                                 delegate(IAsyncResult aresult)
                    {
                        int bytesRead;
                        try
                        {
                            bytesRead = fs.EndRead(aresult);
                        }
                        catch (System.IO.FileNotFoundException)
                        {
                            bytesRead = -1;
                        }
                        fs.Close();

                        if (bytesRead != data._size)
                        {
                            GSGE.Debug.logMessage("Error streaming in sound " + data._streamIndex);
                            mStreamStarted = true;
                            Dispose();
                            return;
                        }

                        if (data._persist)
                        {
                            byte[] audiodata = mStreamBytes;

                            mStreamBytes     = null;
                            mStreamRequested = false;
                            mStreamStarted   = false;

                            // reconfigure parent Sound to be persistent now.
                            data.setPersistent(audiodata);

                            if (mAbortStream)
                            {
                                Dispose();
                            }
                            else
                            {
                                Play();
                            }
                        }
                        else
                        {
                            mStreamStarted = true;
                            if (mAbortStream)
                            {
                                Dispose();
                            }
                            else
                            {
                                exinfo.length  = (uint)mStreamBytes.Length;
                                FMOD.MODE mode = FMOD.MODE.OPENMEMORY_POINT | FMOD.MODE.CREATECOMPRESSEDSAMPLE | FMOD.MODE.SOFTWARE;
                                if (data._looping)
                                {
                                    mode |= FMOD.MODE.LOOP_NORMAL;
                                }
                                result    = FMOD.Framework.system.createSound(mStreamBytes, mode, ref exinfo, ref mSound);
                                mOwnSound = true;
                                ERRCHECK(result);

                                AudioEngine audioengine = GSGE.AudioManager.GetAudioManager().GetAudioEngine();
                                audioengine.FmodSounds.Add(this);

                                result = FMOD.Framework.system.playSound(FMOD.CHANNELINDEX.FREE, mSound, false, ref mChannel);
                                if (mSound == null)
                                {
                                    Dispose();
                                }
                                else
                                {
                                    GSGE.Debug.assert(mSound != null);
                                    ERRCHECK(result);
                                    mChannel.getFrequency(ref mBaseFrequency);
                                }
                            }
                        }

                        // all streaming sounds need to assign these asap
                        Volume        = _volume;
                        Pitch         = _pitch;
                        LowPassCutoff = _lowPassCutoff;
                        Reverb        = _reverb;

                        mAbortStream = false;
                    }, null);
                }
                else
                {
                    if (data.mSound == null)
                    {
                        data.createSound();
                    }

                    //exinfo.length = (uint)data.data.Length;
                    //FMOD.MODE mode = FMOD.MODE.OPENMEMORY_POINT | FMOD.MODE.CREATECOMPRESSEDSAMPLE | FMOD.MODE.SOFTWARE;
                    //if (data._looping)
                    //    mode |= FMOD.MODE.LOOP_NORMAL;
                    //result = FMOD.Framework.system.createSound(data.data, mode, ref exinfo, ref mSound);

                    //ERRCHECK(result);

                    mSound = data.mSound;
                    if (mSound == null)
                    {
                        Dispose();
                        return;
                    }
                    GSGE.Debug.assert(mSound != null);

                    AudioEngine audioengine = GSGE.AudioManager.GetAudioManager().GetAudioEngine();
                    audioengine.FmodSounds.Add(this);

                    result = FMOD.Framework.system.playSound(FMOD.CHANNELINDEX.FREE, mSound, false, ref mChannel);
                    GSGE.Debug.assert(mSound != null);
                    ERRCHECK(result);
                    mChannel.getFrequency(ref mBaseFrequency);
                }
            }
#endif
        }