Esempio n. 1
0
        internal MySourceVoice GetVoice(MyCueId cueId, MyInMemoryWave wave, CuePart part = CuePart.Start)
        {
            MyWaveFormat myWaveFormat = new MyWaveFormat()
            {
                Encoding   = wave.WaveFormat.Encoding,
                Channels   = wave.WaveFormat.Channels,
                SampleRate = wave.WaveFormat.SampleRate,
                WaveFormat = wave.WaveFormat
            };

            MySourceVoice voice = m_voicePools[myWaveFormat].NextAvailable();

            if (voice == null)
            {
                return(null);
            }
            voice.Flush();
            voice.SubmitSourceBuffer(cueId, wave, part);

            if (m_applyReverb)
            {
                voice.Voice.SetEffectChain(m_effectDescriptor);
                voice.Voice.EnableEffect(0);
            }
            else
            {
                voice.Voice.SetEffectChain(null);
            }
            return(voice);
        }
 private void SubmitSourceBuffer(MyInMemoryWave wave)
 {
     if (wave == null)
         return;
     m_isLoopable |= (wave.Buffer.LoopCount > 0);
     m_voice.SourceSampleRate = wave.WaveFormat.SampleRate;
     m_voice.SubmitSourceBuffer(wave.Buffer, wave.Stream.DecodedPacketsInfo);
 }
Esempio n. 3
0
 private void SubmitSourceBuffer(MyInMemoryWave wave)
 {
     if (wave == null)
     {
         return;
     }
     m_isLoopable            |= (wave.Buffer.LoopCount > 0);
     m_voice.SourceSampleRate = wave.WaveFormat.SampleRate;
     m_voice.SubmitSourceBuffer(wave.Buffer, wave.Stream.DecodedPacketsInfo);
 }
 internal void SubmitSourceBuffer(MyStringId cue, MyInMemoryWave wave, MyCueBank.CuePart part)
 {
     m_loopBuffers[(int)part] = wave;
     m_cue = cue;
     m_isLoopable |= (wave.Buffer.LoopCount > 0);
 }
Esempio n. 5
0
 internal void SubmitSourceBuffer(MyCueId cueId, MyInMemoryWave wave, MyCueBank.CuePart part)
 {
     m_loopBuffers[(int)part] = wave;
     m_cueId       = cueId;
     m_isLoopable |= (wave.Buffer.LoopCount > 0);
 }
Esempio n. 6
0
        public bool Add(MySoundData cue, MyAudioWave cueWave)
        {
            string[] files = { cueWave.Start, cueWave.Loop, cueWave.End };
            SharpDX.Multimedia.WaveFormatEncoding encoding = SharpDX.Multimedia.WaveFormatEncoding.Unknown;
            bool result = true;
            int  i      = 0;

            foreach (var waveFilename in files)
            {
                i++;
                if (string.IsNullOrEmpty(waveFilename))
                {
                    continue;
                }

                var fsPath = Path.IsPathRooted(waveFilename) ? waveFilename : Path.Combine(MyFileSystem.ContentPath, "Audio", waveFilename);
                var exists = MyFileSystem.FileExists(fsPath);
                result |= exists;
                if (exists)
                {
                    try
                    {
                        MyInMemoryWave wave = new MyInMemoryWave(cue, fsPath);
                        if (i != 2)
                        {
                            wave.Buffer.LoopCount = 0;
                        }
                        m_waves[waveFilename] = wave;

                        // check the formats
                        if (encoding == SharpDX.Multimedia.WaveFormatEncoding.Unknown)
                        {
                            encoding = wave.WaveFormat.Encoding;
                        }

                        // check the formats
                        if (wave.WaveFormat.Encoding == SharpDX.Multimedia.WaveFormatEncoding.Unknown)
                        {
                            if (MyAudio.OnSoundError != null)
                            {
                                var msg = string.Format("Unknown audio encoding '{0}', '{1}'", cue.SubtypeId.ToString(), waveFilename);
                                MyAudio.OnSoundError(cue, msg);
                            }
                            result = false;
                        }

                        // 3D sounds must be mono
                        if (cueWave.Type == MySoundDimensions.D3 && wave.WaveFormat.Channels != 1)
                        {
                            if (MyAudio.OnSoundError != null)
                            {
                                var msg = string.Format("3D sound '{0}', '{1}' must be in mono, got {2} channels", cue.SubtypeId.ToString(), waveFilename, wave.WaveFormat.Channels);
                                MyAudio.OnSoundError(cue, msg);
                            }
                            result = false;
                        }

                        // all parts of the sound must have the same encoding
                        if (wave.WaveFormat.Encoding != encoding)
                        {
                            if (MyAudio.OnSoundError != null)
                            {
                                var msg = string.Format("Inconsistent sound encoding in '{0}', '{1}', got '{2}', expected '{3}'", cue.SubtypeId.ToString(), waveFilename, wave.WaveFormat.Encoding, encoding);
                                MyAudio.OnSoundError(cue, msg);
                            }
                            result = false;
                        }
                    }
                    catch (Exception e)
                    {
                        if (MyAudio.OnSoundError != null)
                        {
                            var msg = string.Format("Unable to load audio file: '{0}', '{1}': {2}", cue.SubtypeId.ToString(), waveFilename, e.ToString());
                            MyAudio.OnSoundError(cue, msg);
                        }
                        result = false;
                    }
                    // Second catch shouldn't be needed according to http://stackoverflow.com/questions/5345436/net-exception-catch-block
                    // all non-exceptions will be wrapped as type derived from Exception and caught above.
                    //catch
                    //{
                    //    if (MyAudio.OnSoundError != null)
                    //    {
                    //        var msg = string.Format("Unable to load audio file: '{0}', '{1}': {2}", cue.SubtypeId.ToString(), waveFilename, "Something went horribly wrong");
                    //        MyAudio.OnSoundError(cue, msg);
                    //    }
                    //    result = false;
                    //}
                }
                else
                {
                    if (MyAudio.OnSoundError != null)
                    {
                        var msg = string.Format("Unable to find audio file: '{0}', '{1}'", cue.SubtypeId.ToString(), waveFilename);
                        MyAudio.OnSoundError(cue, msg);
                    }
                    result = false;
                }
            }
            return(result);
        }
Esempio n. 7
0
        internal MySourceVoice GetVoice(MyCueId cueId, out int waveNumber, MySoundDimensions type = MySoundDimensions.D2, int tryIgnoreWaveNumber = -1)
        {
            waveNumber = -1;
            MySoundData cue = GetCue(cueId);

            if ((cue == null) || (cue.Waves == null) || (cue.Waves.Count == 0))
            {
                return(null);
            }

            CuePart        part;
            MyInMemoryWave wave = GetRandomWave(cue, type, out waveNumber, out part, tryIgnoreWaveNumber);

            if (wave == null && type == MySoundDimensions.D2)
            {
                type = MySoundDimensions.D3;
                wave = GetRandomWave(cue, type, out waveNumber, out part, tryIgnoreWaveNumber);
            }
            if (wave == null)
            {
                return(null);
            }

            MySourceVoice voice = GetVoice(cueId, wave, part);

            if (voice == null)
            {
                return(null);
            }

            if (cue.Loopable)
            {
                wave = GetWave(cue, type, waveNumber, CuePart.Loop);
                if (wave != null)
                {
                    Debug.Assert(voice.Owner.WaveFormat.Encoding == wave.WaveFormat.Encoding);
                    if (voice.Owner.WaveFormat.Encoding == wave.WaveFormat.Encoding)
                    {
                        voice.SubmitSourceBuffer(cueId, wave, CuePart.Loop);
                    }
                    else
                    {
                        MyLog.Default.WriteLine(string.Format("Inconsistent encodings: '{0}', got '{1}', expected '{2}', part = '{3}'", cueId, wave.WaveFormat.Encoding, voice.Owner.WaveFormat.Encoding, CuePart.Loop));
                    }
                }
                wave = GetWave(cue, type, waveNumber, CuePart.End);
                if (wave != null)
                {
                    Debug.Assert(voice.Owner.WaveFormat.Encoding == wave.WaveFormat.Encoding);
                    if (voice.Owner.WaveFormat.Encoding == wave.WaveFormat.Encoding)
                    {
                        voice.SubmitSourceBuffer(cueId, wave, CuePart.End);
                    }
                    else
                    {
                        MyLog.Default.WriteLine(string.Format("Inconsistent encodings: '{0}', got '{1}', expected '{2}', part = '{3}'", cueId, wave.WaveFormat.Encoding, voice.Owner.WaveFormat.Encoding, CuePart.End));
                    }
                }
            }

#if DEBUG
            if (voice.CueEnum.IsNull == false)
            {
                AddVoiceForDebug(voice);
            }
#endif

            return(voice);
        }
Esempio n. 8
0
        public MyInMemoryWave GetStreamedWave(string filename, MySoundData cue, MySoundDimensions dim = MySoundDimensions.D2)
        {
            if (string.IsNullOrEmpty(filename))
            {
                return(null);
            }

            SharpDX.Multimedia.WaveFormatEncoding encoding = SharpDX.Multimedia.WaveFormatEncoding.Unknown;
            var fsPath = Path.IsPathRooted(filename) ? filename : Path.Combine(MyFileSystem.ContentPath, "Audio", filename);
            var exists = MyFileSystem.FileExists(fsPath);

            if (exists)
            {
                try
                {
                    MyInMemoryWave wave;
                    if (!LoadedStreamedWaves.TryGetValue(fsPath, out wave))
                    {
                        wave = LoadedStreamedWaves[fsPath] = new MyInMemoryWave(cue, fsPath, this, true);
                    }
                    else
                    {
                        wave.Reference();
                    }

                    // check the formats
                    if (encoding == SharpDX.Multimedia.WaveFormatEncoding.Unknown)
                    {
                        encoding = wave.WaveFormat.Encoding;
                    }

                    // check the formats
                    if (wave.WaveFormat.Encoding == SharpDX.Multimedia.WaveFormatEncoding.Unknown)
                    {
                        if (MyAudio.OnSoundError != null)
                        {
                            var msg = string.Format("Unknown audio encoding '{0}', '{1}'", cue.SubtypeId.ToString(), filename);
                            MyAudio.OnSoundError(cue, msg);
                        }
                        return(null);
                    }

                    // 3D sounds must be mono
                    if (dim == MySoundDimensions.D3 && wave.WaveFormat.Channels != 1)
                    {
                        if (MyAudio.OnSoundError != null)
                        {
                            var msg = string.Format("3D sound '{0}', '{1}' must be in mono, got {2} channels", cue.SubtypeId.ToString(), filename, wave.WaveFormat.Channels);
                            MyAudio.OnSoundError(cue, msg);
                        }
                        return(null);
                    }

                    // all parts of the sound must have the same encoding
                    if (wave.WaveFormat.Encoding != encoding)
                    {
                        if (MyAudio.OnSoundError != null)
                        {
                            var msg = string.Format("Inconsistent sound encoding in '{0}', '{1}', got '{2}', expected '{3}'", cue.SubtypeId.ToString(), filename, wave.WaveFormat.Encoding, encoding);
                            MyAudio.OnSoundError(cue, msg);
                        }
                        return(null);
                    }

                    return(wave);
                }
                catch (Exception e)
                {
                    if (MyAudio.OnSoundError != null)
                    {
                        var msg = string.Format("Unable to load audio file: '{0}', '{1}': {2}", cue.SubtypeId.ToString(), filename, e.ToString());
                        MyAudio.OnSoundError(cue, msg);
                    }
                    return(null);
                }
            }
            else
            {
                if (MyAudio.OnSoundError != null)
                {
                    var msg = string.Format("Unable to find audio file: '{0}', '{1}'", cue.SubtypeId.ToString(), filename);
                    MyAudio.OnSoundError(cue, msg);
                }
            }
            return(null);
        }
Esempio n. 9
0
        public bool Add(MySoundData cue, MyAudioWave cueWave)
        {
            string[] files = { cueWave.Start, cueWave.Loop, cueWave.End };
            SharpDX.Multimedia.WaveFormatEncoding encoding = SharpDX.Multimedia.WaveFormatEncoding.Unknown;
            bool result = true;
            int i = 0;
            foreach (var waveFilename in files)
            {
                i++;
                if (string.IsNullOrEmpty(waveFilename) || m_waves.ContainsKey(waveFilename))
                    continue;

                var fsPath = Path.IsPathRooted(waveFilename) ? waveFilename : Path.Combine(MyFileSystem.ContentPath, "Audio", waveFilename);
                var exists = MyFileSystem.FileExists(fsPath);
                result |= exists;
                if (exists)
                {
                    if (cue.StreamSound)
                    {
                        return true;
                    }
                    try
                    {
                        MyInMemoryWave wave = new MyInMemoryWave(cue, fsPath, this);
                        if (i != 2)
                            wave.Buffer.LoopCount = 0;
                        m_waves[waveFilename] = wave;

                        // check the formats
                        if (encoding == SharpDX.Multimedia.WaveFormatEncoding.Unknown)
                        {
                            encoding = wave.WaveFormat.Encoding;
                        }

                        // check the formats
                        if (wave.WaveFormat.Encoding == SharpDX.Multimedia.WaveFormatEncoding.Unknown)
                        {
                            if (MyAudio.OnSoundError != null)
                            {
                                var msg = string.Format("Unknown audio encoding '{0}', '{1}'", cue.SubtypeId.ToString(), waveFilename);
                                MyAudio.OnSoundError(cue, msg);
                            }
                            result = false;
                        }

                        // 3D sounds must be mono
                        if (cueWave.Type == MySoundDimensions.D3 && wave.WaveFormat.Channels != 1)
                        {
                            if (MyAudio.OnSoundError != null)
                            {
                                var msg = string.Format("3D sound '{0}', '{1}' must be in mono, got {2} channels", cue.SubtypeId.ToString(), waveFilename, wave.WaveFormat.Channels);
                                MyAudio.OnSoundError(cue, msg);
                            }
                            result = false;
                        }

                        // all parts of the sound must have the same encoding
                        if (wave.WaveFormat.Encoding != encoding)
                        {
                            if (MyAudio.OnSoundError != null)
                            {
                                var msg = string.Format("Inconsistent sound encoding in '{0}', '{1}', got '{2}', expected '{3}'", cue.SubtypeId.ToString(), waveFilename, wave.WaveFormat.Encoding, encoding);
                                MyAudio.OnSoundError(cue, msg);
                            }
                            result = false;
                        }
                    }
                    catch (Exception e)
                    {
                        if (MyAudio.OnSoundError != null)
                        {
                            var msg = string.Format("Unable to load audio file: '{0}', '{1}': {2}", cue.SubtypeId.ToString(), waveFilename, e.ToString());
                            MyAudio.OnSoundError(cue, msg);
                        }
                        result = false;
                    }
                    // Second catch shouldn't be needed according to http://stackoverflow.com/questions/5345436/net-exception-catch-block
                    // all non-exceptions will be wrapped as type derived from Exception and caught above.
                    //catch
                    //{
                    //    if (MyAudio.OnSoundError != null)
                    //    {
                    //        var msg = string.Format("Unable to load audio file: '{0}', '{1}': {2}", cue.SubtypeId.ToString(), waveFilename, "Something went horribly wrong");
                    //        MyAudio.OnSoundError(cue, msg);
                    //    }
                    //    result = false;
                    //}
                }
                else
                {
                    if (MyAudio.OnSoundError != null)
                    {
                        var msg = string.Format("Unable to find audio file: '{0}', '{1}'", cue.SubtypeId.ToString(), waveFilename);
                        MyAudio.OnSoundError(cue, msg);
                    }
                    result = false;
                }
            }
            return result;
        }
Esempio n. 10
0
        public MyInMemoryWave GetStreamedWave(string filename, MySoundData cue, MySoundDimensions dim = MySoundDimensions.D2)
        {
            if (string.IsNullOrEmpty(filename))
                return null;

            SharpDX.Multimedia.WaveFormatEncoding encoding = SharpDX.Multimedia.WaveFormatEncoding.Unknown;
            var fsPath = Path.IsPathRooted(filename) ? filename : Path.Combine(MyFileSystem.ContentPath, "Audio", filename);
            var exists = MyFileSystem.FileExists(fsPath);
            if (exists)
            {
                try
                {
                    MyInMemoryWave wave;
                    if (!LoadedStreamedWaves.TryGetValue(fsPath, out wave))
                        wave = LoadedStreamedWaves[fsPath] = new MyInMemoryWave(cue, fsPath, this, true);
                    else
                        wave.Reference();

                    // check the formats
                    if (encoding == SharpDX.Multimedia.WaveFormatEncoding.Unknown)
                    {
                        encoding = wave.WaveFormat.Encoding;
                    }

                    // check the formats
                    if (wave.WaveFormat.Encoding == SharpDX.Multimedia.WaveFormatEncoding.Unknown)
                    {
                        if (MyAudio.OnSoundError != null)
                        {
                            var msg = string.Format("Unknown audio encoding '{0}', '{1}'", cue.SubtypeId.ToString(), filename);
                            MyAudio.OnSoundError(cue, msg);
                        }
                        return null;
                    }

                    // 3D sounds must be mono
                    if (dim == MySoundDimensions.D3 && wave.WaveFormat.Channels != 1)
                    {
                        if (MyAudio.OnSoundError != null)
                        {
                            var msg = string.Format("3D sound '{0}', '{1}' must be in mono, got {2} channels", cue.SubtypeId.ToString(), filename, wave.WaveFormat.Channels);
                            MyAudio.OnSoundError(cue, msg);
                        }
                        return null;
                    }

                    // all parts of the sound must have the same encoding
                    if (wave.WaveFormat.Encoding != encoding)
                    {
                        if (MyAudio.OnSoundError != null)
                        {
                            var msg = string.Format("Inconsistent sound encoding in '{0}', '{1}', got '{2}', expected '{3}'", cue.SubtypeId.ToString(), filename, wave.WaveFormat.Encoding, encoding);
                            MyAudio.OnSoundError(cue, msg);
                        }
                        return null;
                    }

                    return wave;
                }
                catch (Exception e)
                {
                    if (MyAudio.OnSoundError != null)
                    {
                        var msg = string.Format("Unable to load audio file: '{0}', '{1}': {2}", cue.SubtypeId.ToString(), filename, e.ToString());
                        MyAudio.OnSoundError(cue, msg);
                    }
                    return null;
                }
            }
            else
            {
                if (MyAudio.OnSoundError != null)
                {
                    var msg = string.Format("Unable to find audio file: '{0}', '{1}'", cue.SubtypeId.ToString(), filename);
                    MyAudio.OnSoundError(cue, msg);
                }
            }
            return null;
        }
Esempio n. 11
0
        internal MySourceVoice GetVoice(MyStringId hashedCue, MySoundDimensions type = MySoundDimensions.D2)
        {
            MySoundData cue = GetCue(hashedCue);

            if ((cue == null) || (cue.Waves == null) || (cue.Waves.Count == 0))
            {
                return(null);
            }

            int            waveNumber;
            CuePart        part;
            MyInMemoryWave wave = GetRandomWave(cue, type, out waveNumber, out part);

            if (wave == null && type == MySoundDimensions.D2)
            {
                type = MySoundDimensions.D3;
                wave = GetRandomWave(cue, type, out waveNumber, out part);
            }
            if (wave == null)
            {
                return(null);
            }

            MySourceVoice voice = GetVoice(hashedCue, wave, part);

            if (voice == null)
            {
                return(null);
            }

            if (cue.Loopable)
            {
                wave = GetWave(cue, type, waveNumber, CuePart.Loop);
                if (wave != null)
                {
                    Debug.Assert(voice.Owner.WaveFormat.Encoding == wave.WaveFormat.Encoding);
                    if (voice.Owner.WaveFormat.Encoding == wave.WaveFormat.Encoding)
                    {
                        voice.SubmitSourceBuffer(hashedCue, wave, CuePart.Loop);
                    }
                    else
                    {
                        MyLog.Default.WriteLine(string.Format("Inconsistent encodings: '{0}', got '{1}', expected '{2}', part = '{3}'", hashedCue, wave.WaveFormat.Encoding, voice.Owner.WaveFormat.Encoding, CuePart.Loop));
                    }
                }
                wave = GetWave(cue, type, waveNumber, CuePart.End);
                if (wave != null)
                {
                    Debug.Assert(voice.Owner.WaveFormat.Encoding == wave.WaveFormat.Encoding);
                    if (voice.Owner.WaveFormat.Encoding == wave.WaveFormat.Encoding)
                    {
                        voice.SubmitSourceBuffer(hashedCue, wave, CuePart.End);
                    }
                    else
                    {
                        MyLog.Default.WriteLine(string.Format("Inconsistent encodings: '{0}', got '{1}', expected '{2}', part = '{3}'", hashedCue, wave.WaveFormat.Encoding, voice.Owner.WaveFormat.Encoding, CuePart.End));
                    }
                }
            }
            return(voice);
        }