Ejemplo n.º 1
0
        public void Play()
        {
            BasePlayer.Play();
            playing = true;

            provideThread = new Thread(() =>
            {
                try
                {
                    WebClient wc = new WebClient();
                    wc.Headers[HttpRequestHeader.UserAgent] = Globals.USER_AGENT;

                    using (var stream = wc.OpenRead(url))
                    {
                        var readFullyStream = new ReadFullyStream(stream);

                        int packetCounter = 0;
                        while (playing)
                        {
                            byte[][] packets = ogg.GetAudioPackets(readFullyStream);

                            packetCounter++;
                            //Skip first 5 pages (control frames, etc)
                            if (packetCounter <= 5)
                            {
                                continue;
                            }

                            for (int i = 0; i < packets.Length; i++)
                            {
                                var streamBytes = packets[i];
                                try
                                {
                                    int frameSize     = OpusPacketInfo.GetNumSamplesPerFrame(streamBytes, 0, Globals.SAMPLE_RATE); //Get frame size from opus packet
                                    short[] rawBuffer = new short[frameSize * 2];                                                  //2 channels
                                    var buffer        = decoder.Decode(streamBytes, 0, streamBytes.Length, rawBuffer, 0, frameSize, false);
                                    BasePlayer.QueueBuffer(rawBuffer);

                                    if (visualiser != null)
                                    {
                                        visualiser.AddSamples(rawBuffer);
                                    }
                                }
                                catch (Concentus.OpusException)
                                {
                                    //Skip this frame
                                }
                            }
                        }
                    }
                }
                catch (Exception)
                {
                }
            });
            provideThread.Start();
        }
Ejemplo n.º 2
0
        public void Play()
        {
            audioPlayer.Play();
            playing = true;

            provideThread = new Thread(() =>
            {
                try
                {
                    HttpWebRequest req = WebRequest.CreateHttp(url);
                    req.UserAgent      = Globals.USER_AGENT;

                    using (var stream = req.GetResponse().GetResponseStream())
                    {
                        var readFullyStream = new ReadFullyStream(stream);

                        while (playing)
                        {
                            byte[][] packets = ogg.GetAudioPackets(readFullyStream);

                            for (int i = 0; i < packets.Length; i++)
                            {
                                var streamBytes = packets[i];
                                try
                                {
                                    int frameSize     = OpusPacketInfo.GetNumSamplesPerFrame(streamBytes, 0, Globals.SAMPLE_RATE); //Get frame size from opus packet
                                    short[] rawBuffer = new short[frameSize * 2];                                                  //2 channels
                                    var buffer        = decoder.Decode(streamBytes, 0, streamBytes.Length, rawBuffer, 0, frameSize, false);
                                    audioPlayer.QueueBuffer(rawBuffer);

                                    if (visualiser != null)
                                    {
                                        visualiser.AddSamples(rawBuffer);
                                    }
                                }
                                catch (Concentus.OpusException)
                                {
                                    //Skip this frame
                                    //Note: the first 2 frames will hit this exception (I'm pretty sure they're not audio data frames)
                                }
                            }
                        }
                    }
                } catch (Exception)
                {
                }
            });
            provideThread.Start();
        }
Ejemplo n.º 3
0
        public Sound LoadSound(string filepath)
        {
            //Read ogg packets
            FileStream stream = new FileStream(filepath, FileMode.Open);

            byte[][] packets = GetAudioPackets(stream);

            List <byte> pcmBytes = new List <byte>();

            //Decode packets from opus to pcm
            for (int i = 0; i < packets.Length; i++)
            {
                try
                {
                    var     packet    = packets[i];
                    int     frameSize = OpusPacketInfo.GetNumSamplesPerFrame(packet, 0, SAMPLE_RATE); //Get frame size from opus packet
                    short[] rawBuffer = new short[frameSize * 2];                                     //2 channels
                    var     buffer    = decoder.Decode(packet, 0, packet.Length, rawBuffer, 0, frameSize, false);

                    //Convert shorts to bytes
                    byte[] result = new byte[rawBuffer.Length * 2];
                    for (int j = 0; j < rawBuffer.Length; j++)
                    {
                        byte[] val = BitConverter.GetBytes(rawBuffer[j]);
                        Array.Copy(val, 0, result, j * 2, 2);
                    }

                    pcmBytes.AddRange(result);
                }
                catch (Concentus.OpusException e)
                {
                    //Skip this frame
                    //Note: the first 2 frames will hit this exception (they're probably just metadata frames, but i'm too lazy to check)
                }
            }

            decoder.ResetState();
            return(Sound.FromS16LE(pcmBytes.ToArray()));
        }