public async Task <ServerAudioInfo> GrabFrameInfoAsync(ShoutcastStreamProcessor processor, ServerAudioInfo serverSentInfo)
        {
            ServerAudioInfo audioInfo = new ServerAudioInfo();

            audioInfo.AudioFormat = StreamAudioFormat.AAC_ADTS;

            //load the first byte
            byte lastByte = await processor.ReadByteFromSocketAsync();

            while (true) //wait for frame sync
            {
                var curByte = await processor.ReadByteFromSocketAsync();

                if (AAC_ADTSParser.IsFrameSync(lastByte, curByte)) //check if we're at the frame sync. if we are, parse some of the audio data
                {
                    byte[] header = new byte[AAC_ADTSParser.HeaderLength];
                    header[0] = lastByte;
                    header[1] = curByte;


                    Array.Copy(await processor.ReadBytesFromSocketAsync(5), 0, header, 2, 5);

                    //todo deal with CRC

                    try
                    {
                        audioInfo.SampleRate = (uint)AAC_ADTSParser.GetSampleRate(header);

                        audioInfo.ChannelCount = (uint)AAC_ADTSParser.GetChannelCount(header);

                        //bitrate gets sent by the server.
                        audioInfo.BitRate = serverSentInfo.BitRate;
                        //audioInfo.BitRate = (uint)AAC_ADTSParser.GetBitRate(header);

                        //if (audioInfo.BitRate == 0) throw new Exception("Unknown bitrate.");
                    }
                    catch (IndexOutOfRangeException)
                    {
                        //probably not the header. continue.
                        lastByte = curByte;
                        continue;
                    }
                    break;
                }
                else
                {
                    lastByte = curByte;
                }
            }

            return(audioInfo);
        }
        public async Task <ServerAudioInfo> GrabFrameInfoAsync(ShoutcastStreamProcessor processor, ServerAudioInfo serverSentInfo)
        {
            ServerAudioInfo audioInfo = new ServerAudioInfo();

            audioInfo.AudioFormat = StreamAudioFormat.MP3;

            //load the first byte
            byte lastByte = await processor.ReadByteFromSocketAsync();

            while (true) //wait for frame sync
            {
                var curByte = await processor.ReadByteFromSocketAsync();

                if (MP3Parser.IsFrameSync(lastByte, curByte)) //check if we're at the frame sync. if we are, parse some of the audio data
                {
                    byte[] header = new byte[MP3Parser.HeaderLength];
                    header[0] = lastByte;
                    header[1] = curByte;

                    Array.Copy(await processor.ReadBytesFromSocketAsync(2), 0, header, 2, 2);

                    if (!MP3Parser.IsValidHeader(header))
                    {
                        lastByte = header[3];
                        continue;
                    }
                    else
                    {
                        audioInfo.HeaderData   = header;
                        audioInfo.SampleRate   = (uint)MP3Parser.GetSampleRate(header);
                        audioInfo.ChannelCount = (uint)MP3Parser.GetChannelCount(header);
                        audioInfo.BitRate      = (uint)MP3Parser.GetBitRate(header);
                        break;
                    }
                }
                else
                {
                    lastByte = curByte;
                }
            }

            if (audioInfo.BitRate == 4294967294)
            {
                //if this gets hit, abort mission immediately.
                throw new ArithmeticException();
            }


            return(audioInfo);
        }
        public async Task <Tuple <MediaStreamSample, uint> > ParseSampleAsync(ShoutcastStreamProcessor processor, DataReader socketReader, bool partial = false, byte[] partialBytes = null)
        {
            //http://www.mpgedit.org/mpgedit/mpeg_format/MP3Format.html

            IBuffer           buffer = null;
            MediaStreamSample sample = null;
            uint sampleLength        = 0;

            if (partial)
            {
                buffer       = partialBytes.AsBuffer();
                sampleLength = MP3Parser.mp3_sampleSize - (uint)partialBytes.Length;
                //processor.byteOffset += sampleLength;
            }
            else
            {
                var read = await socketReader.LoadAsync(MP3Parser.mp3_sampleSize);

                if (read == 0 || read < MP3Parser.mp3_sampleSize)
                {
                    //disconnected.
                    throw new ShoutcastDisconnectionException();
                }

                buffer = socketReader.ReadBuffer(MP3Parser.mp3_sampleSize);

                //processor.byteOffset += MP3Parser.mp3_sampleSize;

                sampleLength = MP3Parser.mp3_sampleSize;
            }

            sample          = MediaStreamSample.CreateFromBuffer(buffer, processor.timeOffSet);
            sample.Duration = MP3Parser.mp3_sampleDuration;
            sample.KeyFrame = true;

            processor.timeOffSet = processor.timeOffSet.Add(MP3Parser.mp3_sampleDuration);


            return(new Tuple <MediaStreamSample, uint>(sample, sampleLength));
        }
        public async Task <Tuple <MediaStreamSample, uint> > ParseSampleAsync(ShoutcastStreamProcessor processor,
                                                                              SocketWrapper socket, bool partial = false, byte[] partialBytes = null)
        {
            IBuffer           buffer = null;
            MediaStreamSample sample = null;
            uint sampleLength        = 0;

            if (partial)
            {
                buffer       = partialBytes.AsBuffer();
                sampleLength = AAC_ADTSParser.aac_adts_sampleSize - (uint)partialBytes.Length;
                //processor.byteOffset += sampleLength;
            }
            else
            {
                var read = await socket.LoadAsync(AAC_ADTSParser.aac_adts_sampleSize);

                if (read == 0 || read < AAC_ADTSParser.aac_adts_sampleSize)
                {
                    //disconnected.
                    throw new ShoutcastDisconnectionException();
                }

                buffer = await socket.ReadBufferAsync(AAC_ADTSParser.aac_adts_sampleSize);

                //processor.byteOffset += AAC_ADTSParser.aac_adts_sampleSize;
                sampleLength = AAC_ADTSParser.aac_adts_sampleSize;
            }

            sample          = MediaStreamSample.CreateFromBuffer(buffer, processor.timeOffSet);
            sample.Duration = AAC_ADTSParser.aac_adts_sampleDuration;
            sample.KeyFrame = true;

            processor.timeOffSet = processor.timeOffSet.Add(AAC_ADTSParser.aac_adts_sampleDuration);


            return(new Tuple <MediaStreamSample, uint>(sample, sampleLength));
        }