Example #1
0
        public Result Load()
        {
            if (Loaded)
            {
                Unload();
            }

            if (Buffer == null)
            {
                if (Filepath != null && Filepath.Contains(".wav"))
                {
                    var buf = File.ReadAllBytes(Filepath);
                    using (var ms = new MemoryStream(buf))
                    {
                        int ch;
                        int bit;
                        int r;

                        try
                        {
                            Buffer     = LoadWave(ms, out ch, out bit, out r);
                            Channels   = ch;
                            Bits       = bit;
                            SampleRate = r;
                        }
                        catch { }
                    }
                }
                else if (Filepath != null && Filepath.Contains(".mp3"))
                {
                    var buf = File.ReadAllBytes(Filepath);
                    using (var ms = new MemoryStream(buf))
                    {
                        int ch;
                        int bit;
                        int r;

                        try
                        {
                            Buffer     = LoadWave(ms, out ch, out bit, out r);
                            Channels   = ch;
                            Bits       = bit;
                            SampleRate = r;
                        }
                        catch { }
                    }
                }
                else if (Stream != null)
                {
                    int ch  = 0;
                    int bit = 0;
                    int r   = 0;

                    try
                    {
                        if (Format == "WAV")
                        {
                            Buffer = LoadWave(Stream, out ch, out bit, out r);
                        }
                        else if (Format == "MP3")
                        {
                            Buffer = LoadMp3(Stream, out ch, out bit, out r);
                        }
                        Channels   = ch;
                        Bits       = bit;
                        SampleRate = r;
                    }
                    catch { }
                }
            }
            if (Buffer == null)
            {
                return(Result.Failed);
            }

            Source   = AL.GenSource();
            ALBuffer = AL.GenBuffer();
            var format = ALFormat.Stereo16;

            if (Channels == 2 && Bits == 8)
            {
                format = ALFormat.Stereo8;
            }
            else if (Channels == 1 && Bits == 16)
            {
                format = ALFormat.Mono16;
            }
            else if (Channels == 1 && Bits == 8)
            {
                format = ALFormat.Mono8;
            }
            AL.BufferData(ALBuffer, format, Buffer, Buffer.Length, SampleRate);
            AL.BindBufferToSource(Source, ALBuffer);

            Loaded = true;
            return(Result.Success);
        }