Beispiel #1
0
        private void PlatformInitializeFormat(byte[] header, byte[] buffer, int bufferSize, int loopStart, int loopLength)
        {
            var format         = BitConverter.ToInt16(header, 0);
            var channels       = BitConverter.ToInt16(header, 2);
            var sampleRate     = BitConverter.ToInt32(header, 4);
            var blockAlignment = BitConverter.ToInt16(header, 12);
            var sampleBits     = BitConverter.ToInt16(header, 14);

            WaveFormat waveFormat;

            if (format == 1)
            {
                waveFormat = new WaveFormat(sampleRate, sampleBits, channels);
            }
            else if (format == 2)
            {
                waveFormat = new WaveFormatAdpcm(sampleRate, channels, blockAlignment);
            }
            else if (format == 3)
            {
                waveFormat = WaveFormat.CreateIeeeFloatWaveFormat(sampleRate, channels);
            }
            else
            {
                throw new NotSupportedException("Unsupported wave format!");
            }

            CreateBuffers(waveFormat,
                          ToDataStream(0, buffer, bufferSize),
                          loopStart,
                          loopLength);
        }
    /// <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(void *pointer)
    {
        if (pointer == null)
        {
            throw new InvalidOperationException("Pointer cannot be 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.WindowsMediaAudio || encoding == WaveFormatEncoding.WindowsMediaAudioProfessional))
        {
            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));
    }