Ejemplo n.º 1
0
        /// <summary>
        /// Helper function to retrieve a WaveFormat structure from a pointer
        /// </summary>
        /// <param name="pointer">Pointer to the WaveFormat rawdata</param>
        /// <returns>WaveFormat structure</returns>
        public unsafe static WaveFormat MarshalFrom(IntPtr pointer)
        {
            if (pointer == IntPtr.Zero)
            {
                return(null);
            }

            var pcmWaveFormat = *(__PcmNative *)pointer;
            var encoding      = pcmWaveFormat.waveFormatTag;

            // Load simple PcmWaveFormat if channels <= 2 and encoding is Pcm, IeeFloat, Wmaudio2, Wmaudio3
            // See http://msdn.microsoft.com/en-us/library/microsoft.directx_sdk.xaudio2.waveformatex%28v=vs.85%29.aspx
            if (pcmWaveFormat.channels <= 2 && (encoding == WaveFormatEncoding.Pcm || encoding == WaveFormatEncoding.IeeeFloat || encoding == WaveFormatEncoding.Wmaudio2 || encoding == WaveFormatEncoding.Wmaudio3))
            {
                var waveFormat = new WaveFormat();
                waveFormat.__MarshalFrom(ref pcmWaveFormat);
                return(waveFormat);
            }

            if (encoding == WaveFormatEncoding.Extensible)
            {
                var waveFormat = new WaveFormatExtensible();
                waveFormat.__MarshalFrom(ref *(WaveFormatExtensible.__Native *)pointer);
                return(waveFormat);
            }

            if (encoding == WaveFormatEncoding.Adpcm)
            {
                var waveFormat = new WaveFormatAdpcm();
                waveFormat.__MarshalFrom(ref *(WaveFormatAdpcm.__Native *)pointer);
                return(waveFormat);
            }

            throw new InvalidOperationException(string.Format("Unsupported WaveFormat [{0}]", encoding));
        }
Ejemplo n.º 2
0
 internal void __MarshalTo(ref WaveFormatExtensible.__Native @ref)
 {
     base.__MarshalTo(ref @ref.waveFormat);
       @ref.wValidBitsPerSample = this.wValidBitsPerSample;
       @ref.dwChannelMask = this.ChannelMask;
       @ref.subFormat = this.GuidSubFormat;
 }
Ejemplo n.º 3
0
        public static unsafe WaveFormat MarshalFrom(IntPtr pointer)
        {
            if (pointer == IntPtr.Zero)
            {
                return((WaveFormat)null);
            }
            WaveFormat.__PcmNative @ref = *(WaveFormat.__PcmNative *)(void *) pointer;
            WaveFormatEncoding     waveFormatEncoding = @ref.waveFormatTag;

            if ((int)@ref.channels <= 2 && (waveFormatEncoding == WaveFormatEncoding.Pcm || waveFormatEncoding == WaveFormatEncoding.IeeeFloat || (waveFormatEncoding == WaveFormatEncoding.Wmaudio2 || waveFormatEncoding == WaveFormatEncoding.Wmaudio3)))
            {
                WaveFormat waveFormat = new WaveFormat();
                waveFormat.__MarshalFrom(ref @ref);
                return(waveFormat);
            }
            else if (waveFormatEncoding == WaveFormatEncoding.Extensible)
            {
                WaveFormatExtensible formatExtensible = new WaveFormatExtensible();
                formatExtensible.__MarshalFrom(ref *(WaveFormatExtensible.__Native *)(void *) pointer);
                return((WaveFormat)formatExtensible);
            }
            else
            {
                if (waveFormatEncoding != WaveFormatEncoding.Adpcm)
                {
                    throw new InvalidOperationException(string.Format("Unsupported WaveFormat [{0}]", (object)waveFormatEncoding));
                }
                WaveFormatAdpcm waveFormatAdpcm = new WaveFormatAdpcm();
                waveFormatAdpcm.__MarshalFrom(ref *(WaveFormatAdpcm.__Native *)(void *) pointer);
                return((WaveFormat)waveFormatAdpcm);
            }
        }
Ejemplo n.º 4
0
        public X3DAudioEngine()
        {
            _xaudio2 = new XAudio2();
            _masteringVoice = new MasteringVoice(_xaudio2);

            _deviceFormat = _xaudio2.GetDeviceDetails(0).OutputFormat;
            _x3dAudio = new X3DAudio(_deviceFormat.ChannelMask);

            Position = new Vector3D(0, 0, 0);
            Rotation = System.Windows.Media.Media3D.Quaternion.Identity;
        }
Ejemplo n.º 5
0
 public override void Setup(WaveFormatEx format)
 {
     _format = new WaveFormatExtensible(format.nSamplesPerSec, format.wBitsPerSample, format.nChannels);
     _voice = new SourceVoice(_xaudio2, _format);
     _voice.StreamEnd += _voice_StreamEnd;
     _voice.VoiceError += _voice_VoiceError;
     _emitter = new Emitter
     {
         ChannelAzimuths = GetAzimuths(_format.Channels),
         ChannelCount = _format.Channels,
         ChannelRadius = 10,
         CurveDistanceScaler = float.MinValue,
         OrientFront = new Vector3(0, 0, 1),
         OrientTop = new Vector3(0, 1, 0),
         Position = new Vector3(0, 0, 0),
         Velocity = new Vector3(0, 0, 0)
     };
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Helper function to retrieve a WaveFormat structure from a pointer
        /// </summary>
        /// <param name="pointer">Pointer to the WaveFormat rawdata</param>
        /// <returns>WaveFormat structure</returns>
        public static unsafe WaveFormat MarshalFrom(IntPtr pointer)
        {
            if (pointer == IntPtr.Zero) return null;

            var pcmWaveFormat = *(__PcmNative*)pointer;
            var encoding = pcmWaveFormat.waveFormatTag;

            // Load simple PcmWaveFormat if channels <= 2 and encoding is Pcm, IeeFloat, Wmaudio2, Wmaudio3
            // See http://msdn.microsoft.com/en-us/library/microsoft.directx_sdk.xaudio2.waveformatex%28v=vs.85%29.aspx
            if (pcmWaveFormat.channels <= 2 && (encoding == WaveFormatEncoding.Pcm || encoding == WaveFormatEncoding.IeeeFloat || encoding == WaveFormatEncoding.Wmaudio2 || encoding == WaveFormatEncoding.Wmaudio3))
            {
                var waveFormat = new WaveFormat();
                waveFormat.__MarshalFrom(ref pcmWaveFormat);
                return waveFormat;
            }

            if (encoding == WaveFormatEncoding.Extensible)
            {
                var waveFormat = new WaveFormatExtensible();
                waveFormat.__MarshalFrom(ref *(WaveFormatExtensible.__Native*)pointer);
                return waveFormat;
            }

            if (encoding == WaveFormatEncoding.Adpcm)
            {
                var waveFormat = new WaveFormatAdpcm();
                waveFormat.__MarshalFrom(ref *(WaveFormatAdpcm.__Native*)pointer);
                return waveFormat;
            }

            throw new InvalidOperationException(string.Format("Unsupported WaveFormat [{0}]", encoding));
        }
Ejemplo n.º 7
0
 public MyX3DAudio(WaveFormatExtensible format)
 {
     m_x3dAudio = new X3DAudio(format.ChannelMask);
     m_dsp = new DspSettings(1, format.Channels);
 }
Ejemplo n.º 8
0
 internal unsafe void __MarshalFrom(ref DeviceDetails.__Native @ref)
 {
     fixed (char* ptr = &@ref.DeviceID)
     {
         this.DeviceID = Utilities.PtrToStringUni((IntPtr)((void*)ptr), 256);
     }
     fixed (char* ptr2 = &@ref.DisplayName)
     {
         this.DisplayName = Utilities.PtrToStringUni((IntPtr)((void*)ptr2), 256);
     }
     this.Role = @ref.Role;
     this.OutputFormat = new WaveFormatExtensible();
     this.OutputFormat.__MarshalFrom(ref @ref.OutputFormat);
 }
Ejemplo n.º 9
0
 public static unsafe WaveFormat MarshalFrom(IntPtr pointer)
 {
     if (pointer == IntPtr.Zero)
     return (WaveFormat) null;
       WaveFormat.__PcmNative @ref = *(WaveFormat.__PcmNative*) (void*) pointer;
       WaveFormatEncoding waveFormatEncoding = @ref.waveFormatTag;
       if ((int) @ref.channels <= 2 && (waveFormatEncoding == WaveFormatEncoding.Pcm || waveFormatEncoding == WaveFormatEncoding.IeeeFloat || (waveFormatEncoding == WaveFormatEncoding.Wmaudio2 || waveFormatEncoding == WaveFormatEncoding.Wmaudio3)))
       {
     WaveFormat waveFormat = new WaveFormat();
     waveFormat.__MarshalFrom(ref @ref);
     return waveFormat;
       }
       else if (waveFormatEncoding == WaveFormatEncoding.Extensible)
       {
     WaveFormatExtensible formatExtensible = new WaveFormatExtensible();
     formatExtensible.__MarshalFrom(ref *(WaveFormatExtensible.__Native*) (void*) pointer);
     return (WaveFormat) formatExtensible;
       }
       else
       {
     if (waveFormatEncoding != WaveFormatEncoding.Adpcm)
       throw new InvalidOperationException(string.Format("Unsupported WaveFormat [{0}]", (object) waveFormatEncoding));
     WaveFormatAdpcm waveFormatAdpcm = new WaveFormatAdpcm();
     waveFormatAdpcm.__MarshalFrom(ref *(WaveFormatAdpcm.__Native*) (void*) pointer);
     return (WaveFormat) waveFormatAdpcm;
       }
 }
Ejemplo n.º 10
0
        private void SetUpAudioDevice()
        {
            if (AudioDevice == null)
            {
                AudioDevice = new DirectSound();
                AudioDevice.SetCooperativeLevel(new WindowInteropHelper(Owner).Handle, CooperativeLevel.Normal);
            }

            if (AudioBuffer == null)
            {
                BufferDesc = new SoundBufferDescription();
                BufferDesc.Flags = BufferFlags.GlobalFocus | BufferFlags.GetCurrentPosition2 | BufferFlags.ControlVolume;
                BufferDesc.AlgorithmFor3D = Guid.Empty;

                //Вывод звука в Ависинте (через DirectShow\VFW)
                //v2.57   |   global OPT_AllowFloatAudio = True (по дефолту FLOAT преобразуется в 16бит, а 32бит и 24бит - выводятся как есть)
                //v2.58   |   global OPT_UseWaveExtensible = True (по дефолту WaveFormatExtensible не используется, даже для многоканального и многобитного звука)
                //v2.60   |   global OPT_dwChannelMask(int v) (переназначение дефолтной конфигурации каналов при использовании WaveFormatExtensible)
                //FFCHANNEL_LAYOUT в FFMS2

                if (reader.GetVarBoolean("OPT_UseWaveExtensible", true)) //У нас свой дефолт
                {
                    #region WaveFormatExtensible
                    WaveFormatExtensible format = new WaveFormatExtensible(reader.Samplerate, reader.BitsPerSample, reader.Channels);

                    //SharpDX считает, что весь 32-битный звук - FLOAT
                    if (reader.Clip.SampleType == AudioSampleType.INT32)
                        format.GuidSubFormat = new Guid("00000001-0000-0010-8000-00aa00389b71"); //PCM

                    #region channels
                    //AviSynth (дефолт)
                    //Chan. Mask MS channels
                    //----- ------ -----------------------
                    //1   0x0004 FC                      4
                    //2   0x0003 FL FR                   3
                    //3   0x0007 FL FR FC                7
                    //4   0x0033 FL FR BL BR             51
                    //5   0x0037 FL FR FC BL BR          55
                    //6   0x003F FL FR FC LF BL BR       63
                    //7   0x013F FL FR FC LF BL BR BC    319
                    //8   0x063F FL FR FC LF BL BR SL SR 1599

                    int mask = reader.GetVarInteger("OPT_dwChannelMask", -1);
                    if (mask != -1) format.ChannelMask = (Speakers)mask;
                    else if (reader.Channels == 1) format.ChannelMask = Speakers.Mono; //4
                    //else if (reader.Channels == 2) format.ChannelMask = Speakers.Stereo; //3
                    else if (reader.Channels == 3) format.ChannelMask = Speakers.Stereo | Speakers.FrontCenter; //7 //TwoPointOne; //11
                    else if (reader.Channels == 4) format.ChannelMask = Speakers.Quad; //51
                    else if (reader.Channels == 5) format.ChannelMask = Speakers.Quad | Speakers.FrontCenter; //55  //FourPointOne; //59
                    //else if (reader.Channels == 6) format.ChannelMask = Speakers.FivePointOne; //63
                    else if (reader.Channels == 7) format.ChannelMask = Speakers.FivePointOne | Speakers.BackCenter; //319
                    else if (reader.Channels == 8) format.ChannelMask = Speakers.SevenPointOneSurround; //1599  //SevenPointOne; //255
                    /*else //Этот способ уже был использован при вызове конструктора, хз насколько он корректный и насколько корректно всё то, что выше..
                    {
                        //NAudio\SharpDX
                        int dwChannelMask = 0;
                        for (int n = 0; n < 1; n++) dwChannelMask |= (1 << n);
                        format.ChannelMask = (Speakers)dwChannelMask;

                        //ch mask (SlimDX) [SharpDX]
                        //1    1 (FrontLeft) [FrontLeft]
                        //2    3 (Stereo) [FrontLeft | FrontRight]
                        //3    7 (Mono) [FrontLeft | FrontRight | FrontCenter]
                        //4   15 (Mono) [FrontLeft | FrontRight | FrontCenter | LowFrequency]
                        //5   31 (TwoPointOne | Mono | BackLeft) [FrontLeft | FrontRight | FrontCenter | LowFrequency | BackLeft]
                        //6   63 (FivePointOne) [FrontLeft | FrontRight | FrontCenter | LowFrequency | BackLeft | BackRight]
                        //7  127 (FivePointOne | FrontLeftOfCenter) [FrontLeft | FrontRight | FrontCenter | LowFrequency | BackLeft | BackRight | FrontLeftOfCenter]
                        //8  255 (SevenPointOne) [FrontLeft | FrontRight | FrontCenter | LowFrequency | BackLeft | BackRight | FrontLeftOfCenter | FrontRightOfCenter]
                    }*/
                    #endregion

                    samplesPerHalfBuff = (int)((format.SampleRate / 2) * k); //Кол-во сэмплов на половину буфера
                    bytesPerSample = format.BlockAlign;

                    BufferDesc.BufferBytes = samplesPerHalfBuff * format.BlockAlign * 2; //Кол-во байт на полный буфер
                    BufferDesc.Format = format;
                    #endregion
                }
                else
                {
                    #region WaveFormat
                    WaveFormatEncoding tag = (reader.Clip.SampleType == AudioSampleType.FLOAT) ? WaveFormatEncoding.IeeeFloat : WaveFormatEncoding.Pcm;
                    WaveFormat format = WaveFormat.CreateCustomFormat(tag, reader.Samplerate, reader.Channels, reader.Clip.AvgBytesPerSec, reader.Channels * reader.Clip.BytesPerSample, reader.BitsPerSample);

                    samplesPerHalfBuff = (int)((format.SampleRate / 2) * k); //Кол-во сэмплов на половину буфера
                    bytesPerSample = format.BlockAlign;

                    BufferDesc.BufferBytes = samplesPerHalfBuff * format.BlockAlign * 2; //Кол-во байт на полный буфер
                    BufferDesc.Format = format;
                    #endregion
                }

                AudioBuffer = new SecondarySoundBuffer(AudioDevice, BufferDesc);

                if (ABuffer == null)
                    ABuffer = new byte[BufferDesc.BufferBytes / 2];

                if (!h.IsAllocated)
                    h = GCHandle.Alloc(ABuffer, GCHandleType.Pinned);
            }
        }