Beispiel #1
0
        /// <summary>
        /// Decode stream and get PCM waveform.
        /// MP3ストリームをデコードして、PCMを得ます。
        /// </summary>
        /// <param name="mp3data">mp3 stream</param>
        /// <returns>pcm waveform</returns>
        public byte[] Decode(byte[] mp3data)
        {
            if (deviceHandle == IntPtr.Zero)
            {
                throw new InvalidOperationException("Stream is not open.");
            }

            byte[] input = new byte[buffer.Length + mp3data.Length];
            Array.Copy(buffer, input, buffer.Length);
            Array.Copy(mp3data, 0, input, buffer.Length, mp3data.Length);

            uint size;

            Win32.acmStreamSize(deviceHandle, (uint)input.Length, out size, 1);
            byte[] output = new byte[maxBufferSize];

            GCHandle hIn  = GCHandle.Alloc(input, GCHandleType.Pinned);
            GCHandle hOut = GCHandle.Alloc(output, GCHandleType.Pinned);

            IntPtr pHeader = Marshal.AllocHGlobal(Win32.SizeOfAcmStreamHeader);

            Win32.AcmStreamHeader ash = new Win32.AcmStreamHeader();
            ash.cbStruct        = (uint)Win32.SizeOfAcmStreamHeader;
            ash.pbSrc           = hIn.AddrOfPinnedObject();
            ash.cbSrcLength     = (uint)input.Length;
            ash.cbSrcLengthUsed = 0;
            ash.pbDst           = hOut.AddrOfPinnedObject();
            ash.cbDstLength     = (uint)output.Length;
            ash.cbDstLengthUsed = 0;
            Marshal.StructureToPtr(ash, pHeader, true);

            Win32.acmStreamPrepareHeader(deviceHandle, pHeader, 0);
            Win32.acmStreamConvert(deviceHandle, pHeader, 0);
            Win32.acmStreamUnprepareHeader(deviceHandle, pHeader, 0);

            ash = (Win32.AcmStreamHeader)Marshal.PtrToStructure(pHeader, typeof(Win32.AcmStreamHeader));
            hIn.Free();
            hOut.Free();

            Array.Resize <byte>(ref buffer, (int)(ash.cbSrcLength - ash.cbSrcLengthUsed));
            Array.Copy(input, ash.cbSrcLengthUsed, buffer, 0, ash.cbSrcLength - ash.cbSrcLengthUsed);

            byte[] decodedSamples = new byte[ash.cbDstLengthUsed];
            Array.Copy(output, decodedSamples, decodedSamples.Length);

            return(decodedSamples);
        }
Beispiel #2
0
        /// <summary>
        /// Decode stream and get PCM waveform.
        /// MP3�X�g���[����f�R�[�h���āAPCM�𓾂܂��B
        /// </summary>
        /// <param name="mp3data">mp3 stream</param>
        /// <returns>pcm waveform</returns>
        public byte[] Decode(byte[] mp3data)
        {
            if (deviceHandle == IntPtr.Zero)
                throw new InvalidOperationException("Stream is not open.");

            byte[] input = new byte[buffer.Length + mp3data.Length];
            Array.Copy(buffer, input, buffer.Length);
            Array.Copy(mp3data, 0, input, buffer.Length, mp3data.Length);

            uint size;

            Win32.acmStreamSize(deviceHandle, (uint)input.Length, out size, 1);
            byte[] output = new byte[maxBufferSize];

            GCHandle hIn = GCHandle.Alloc(input, GCHandleType.Pinned);
            GCHandle hOut = GCHandle.Alloc(output, GCHandleType.Pinned);

            IntPtr pHeader = Marshal.AllocHGlobal(Win32.SizeOfAcmStreamHeader);

            Win32.AcmStreamHeader ash = new Win32.AcmStreamHeader();
            ash.cbStruct = (uint)Win32.SizeOfAcmStreamHeader;
            ash.pbSrc = hIn.AddrOfPinnedObject();
            ash.cbSrcLength = (uint)input.Length;
            ash.cbSrcLengthUsed = 0;
            ash.pbDst = hOut.AddrOfPinnedObject();
            ash.cbDstLength = (uint)output.Length;
            ash.cbDstLengthUsed = 0;
            Marshal.StructureToPtr(ash, pHeader, true);

            Win32.acmStreamPrepareHeader(deviceHandle, pHeader, 0);
            Win32.acmStreamConvert(deviceHandle, pHeader, 0);
            Win32.acmStreamUnprepareHeader(deviceHandle, pHeader, 0);

            ash = (Win32.AcmStreamHeader)Marshal.PtrToStructure(pHeader, typeof(Win32.AcmStreamHeader));
            hIn.Free();
            hOut.Free();

            Array.Resize<byte>(ref buffer, (int)(ash.cbSrcLength - ash.cbSrcLengthUsed));
            Array.Copy(input, ash.cbSrcLengthUsed, buffer, 0, ash.cbSrcLength - ash.cbSrcLengthUsed);

            byte[] decodedSamples = new byte[ash.cbDstLengthUsed];
            Array.Copy(output, decodedSamples, decodedSamples.Length);

            return decodedSamples;
        }