예제 #1
0
        private SoundEffect LoadSound(Stream stream, int length, string extension)
        {
            switch (extension)
            {
            case ".wav":
                if (!stream.CanSeek)
                {
                    stream = new MemoryStream(stream.ReadBytes(length));
                }
                return(SoundEffect.FromStream(stream));

            case ".mp3":
                using (var mp3Stream = new MP3Stream(stream))
                    using (var ms = new MemoryStream()) {
                        mp3Stream.CopyTo(ms);
                        return(new SoundEffect(ms.ToArray(), mp3Stream.Frequency, (AudioChannels)mp3Stream.ChannelCount));
                    }

            case ".ogg":
                using (var reader = new VorbisReader(stream, true)) {
                    var buffer   = new byte[reader.TotalSamples * 2 * reader.Channels];
                    var floatBuf = new float[buffer.Length / 2];
                    reader.ReadSamples(floatBuf, 0, floatBuf.Length);
                    MusicStreamingOGG.Convert(floatBuf, buffer);
                    return(new SoundEffect(buffer, reader.SampleRate, (AudioChannels)reader.Channels));
                }
            }
            throw new ResourceLoadException("Unknown sound extension " + extension);
        }
예제 #2
0
        private unsafe short[] Decode()
        {
            using (var output = new MemoryStream())
            {
                ushort[] endian_test = new ushort[] { 0x1234 };
                fixed(ushort *ptr1 = endian_test)
                {
                    byte *ptr2 = (byte *)ptr1;

                    if (*ptr2 != 0x34)
                    {
                        throw new NotSupportedException("MP3 decoding is only supported on little-endian processors");
                    }
                }

                using (var input = new MemoryStream(Body, false))
                    using (var mp3 = new MP3Stream(input))
                    {
                        mp3.CopyTo(output);
                    }

                byte[] array = output.ToArray();
                fixed(byte *ptr = array)
                {
                    short *ptr16 = (short *)ptr;

                    short[] array16 = new short[array.Length / sizeof(short)];
                    Marshal.Copy((IntPtr)ptr16, array16, 0, array16.Length);
                    return(array16);
                }
            }
        }
예제 #3
0
        public T FromStream <T>(Stream stream) where T : class
        {
            if (typeof(T) != typeof(SoundEffect))
            {
                throw AssetLoadException.FromInvalidReader <MP3Reader, T>();
            }

            using var mp3Stream = new MP3Stream(stream);
            using var ms        = new MemoryStream();

            mp3Stream.CopyTo(ms);

            return(new SoundEffect(ms.ToArray(), mp3Stream.Frequency, (AudioChannels)mp3Stream.ChannelCount) as T);
        }