Exemple #1
0
 /// <summary>
 ///     Creates a new stream instance using the provided stream as a source.
 ///     Will also read the first frame of the MP3 into the internal buffer.
 ///     TODO: allow selecting stereo or mono in the constructor (note that this also requires "implementing" the stereo format).
 ///     UPDATE: (Giperion) I hate that TODO above
 /// </summary>
 public MP3Stream(Stream sourceStream, int chunkSize, bool IsMono)
 {
     IsEOF          = false;
     m_SourceStream = sourceStream;
     pushback       = new PushbackStream(m_SourceStream, chunkSize);
     m_BitStream    = new Bitstream(pushback);
     if (IsMono)
     {
         Decoder.Params ParamsCustom = new Decoder.Params();
         ParamsCustom.OutputChannels = OutputChannels.LEFT;
         m_Decoder         = new Decoder(ParamsCustom);
         m_Buffer          = new Buffer16BitMono();
         FormatRep         = SoundFormat.Pcm16BitMono;
         m_ChannelCountRep = 1;
     }
     else
     {
         m_Buffer          = new Buffer16BitStereo();
         FormatRep         = SoundFormat.Pcm16BitStereo;
         m_ChannelCountRep = 2;
     }
     m_Decoder.OutputBuffer = m_Buffer;
     // read the first frame. This will fill the initial buffer with data, and get our frequency!
     if (!ReadFrame())
     {
         IsEOF = true;
     }
 }
Exemple #2
0
        private static void test_xbuffer(A data)
        {
            var steam = new XSteam(1, 1024 * 1024 * 100);

            // 序列化
            uint offset = 0;

            Timer.beginTime();
            ABuffer.serialize(data, steam);
            Timer.endTime("xbuffer 普通模式序列化");

            var buffer = steam.getBytes();

            // 反序列化
            offset = 0;
            Timer.beginTime();
            ABuffer.deserialize(buffer, ref offset);
            Timer.endTime("xbuffer 普通模式反序列化");

            // 泛型模式序列化
            Timer.beginTime();
            Serializer.cachedSteam = steam;
            Serializer.serialize(data);
            Timer.endTime("xbuffer 泛型模式序列化");

            buffer = steam.getBytes();
            // 泛型模式反序列化
            Timer.beginTime();
            Serializer.deserialize <A>(buffer);
            Timer.endTime("xbuffer 泛型模式反序列化");

            // 内存占用
            Console.WriteLine(string.Format(" xbuffer 普通模式总占用内存 {0} byte.", buffer.Length));
        }
Exemple #3
0
 public virtual void Create(Bitstream stream0, Header header0, SynthesisFilter filtera, SynthesisFilter filterb,
                            ABuffer buffer0, int whichCh0)
 {
     stream         = stream0;
     header         = header0;
     filter1        = filtera;
     filter2        = filterb;
     buffer         = buffer0;
     which_channels = whichCh0;
 }
Exemple #4
0
 public virtual void Create(Bitstream stream0, Header header0, SynthesisFilter filtera, SynthesisFilter filterb,
     ABuffer buffer0, int whichCh0)
 {
     stream = stream0;
     header = header0;
     filter1 = filtera;
     filter2 = filterb;
     buffer = buffer0;
     which_channels = whichCh0;
 }
Exemple #5
0
 internal virtual void Create(Bitstream stream0, Header header0, SynthesisFilter filtera, SynthesisFilter filterb,
                              ABuffer buffer0, int whichCh0)
 {
     Stream        = stream0;
     Header        = header0;
     Filter1       = filtera;
     Filter2       = filterb;
     Buffer        = buffer0;
     WhichChannels = whichCh0;
 }
Exemple #6
0
        /// <summary>
        ///     Reads a frame from the MP3 stream.  Returns whether the operation was successful.  If it wasn't,
        ///     the source stream is probably at its end.
        /// </summary>
        private bool ReadFrame(bool first = false)
        {
            // Read a frame from the bitstream.
            Header header = m_BitStream.readFrame();

            if (header == null)
            {
                return(false);
            }

            try
            {
                // Set the channel count and frequency values for the stream.
                if (header.mode() == Header.SINGLE_CHANNEL)
                {
                    m_ChannelCountRep = 1;
                }
                else
                {
                    m_ChannelCountRep = 2;
                }

                m_FrequencyRep = header.frequency();
//		if (first)
//			m_TotalMs = header.total_ms((int) m_SourceStream.Length);

                // Decode the frame.
                ABuffer decoderOutput = m_Decoder.DecodeFrame(header, m_BitStream);

                // Apparently, the way JavaZoom sets the output buffer
                // on the decoder is a bit dodgy. Even though
                // this exception should never happen, we test to be sure.
                if (decoderOutput != m_Buffer)
                {
                    throw new ApplicationException("Output buffers are different.");
                }

                // And we're done.
            }
            finally
            {
                // No resource leaks please!
                m_BitStream.CloseFrame();
            }
            return(true);
        }
Exemple #7
0
        /// <summary>
        ///     Constructor.
        ///     REVIEW: these constructor arguments should be moved to the decodeFrame() method.
        /// </summary>
        public LayerIIIDecoder(Bitstream stream, Header header, SynthesisFilter filtera, SynthesisFilter filterb, ABuffer buffer, int whichCh)
        {
            Huffman.Initialize();

            InitBlock();
            is_1d = new int[SBLIMIT*SSLIMIT + 4];
            ro = new float[2][][];
            for (int i = 0; i < 2; i++)
            {
                ro[i] = new float[SBLIMIT][];
                for (int i2 = 0; i2 < SBLIMIT; i2++)
                {
                    ro[i][i2] = new float[SSLIMIT];
                }
            }
            lr = new float[2][][];
            for (int i3 = 0; i3 < 2; i3++)
            {
                lr[i3] = new float[SBLIMIT][];
                for (int i4 = 0; i4 < SBLIMIT; i4++)
                {
                    lr[i3][i4] = new float[SSLIMIT];
                }
            }
            out_1d = new float[SBLIMIT*SSLIMIT];
            prevblck = new float[2][];
            for (int i5 = 0; i5 < 2; i5++)
            {
                prevblck[i5] = new float[SBLIMIT*SSLIMIT];
            }
            k = new float[2][];
            for (int i6 = 0; i6 < 2; i6++)
            {
                k[i6] = new float[SBLIMIT*SSLIMIT];
            }
            nonzero = new int[2];

            //III_scalefact_t
            III_scalefac_t = new ScaleFactorData[2];
            III_scalefac_t[0] = new ScaleFactorData();
            III_scalefac_t[1] = new ScaleFactorData();
            scalefac = III_scalefac_t;
            // L3TABLE INIT

            sfBandIndex = new SBI[9]; // SZD: MPEG2.5 +3 indices
            int[] l0 =
            {
                0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464, 522, 576
            };
            int[] s0 = {0, 4, 8, 12, 18, 24, 32, 42, 56, 74, 100, 132, 174, 192};
            int[] l1 =
            {
                0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 114, 136, 162, 194, 232, 278, 330, 394, 464, 540, 576
            };
            int[] s1 = {0, 4, 8, 12, 18, 26, 36, 48, 62, 80, 104, 136, 180, 192};
            int[] l2 =
            {
                0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464, 522, 576
            };
            int[] s2 = {0, 4, 8, 12, 18, 26, 36, 48, 62, 80, 104, 134, 174, 192};

            int[] l3 =
            {
                0, 4, 8, 12, 16, 20, 24, 30, 36, 44, 52, 62, 74, 90, 110, 134, 162, 196, 238, 288, 342, 418, 576
            };
            int[] s3 = {0, 4, 8, 12, 16, 22, 30, 40, 52, 66, 84, 106, 136, 192};
            int[] l4 =
            {
                0, 4, 8, 12, 16, 20, 24, 30, 36, 42, 50, 60, 72, 88, 106, 128, 156, 190, 230, 276, 330, 384, 576
            };
            int[] s4 = {0, 4, 8, 12, 16, 22, 28, 38, 50, 64, 80, 100, 126, 192};
            int[] l5 =
            {
                0, 4, 8, 12, 16, 20, 24, 30, 36, 44, 54, 66, 82, 102, 126, 156, 194, 240, 296, 364, 448, 550,
                576
            };
            int[] s5 = {0, 4, 8, 12, 16, 22, 30, 42, 58, 78, 104, 138, 180, 192};
            // SZD: MPEG2.5
            int[] l6 =
            {
                0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464, 522,
                576
            };
            int[] s6 = {0, 4, 8, 12, 18, 26, 36, 48, 62, 80, 104, 134, 174, 192};
            int[] l7 =
            {
                0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464, 522,
                576
            };
            int[] s7 = {0, 4, 8, 12, 18, 26, 36, 48, 62, 80, 104, 134, 174, 192};
            int[] l8 =
            {
                0, 12, 24, 36, 48, 60, 72, 88, 108, 132, 160, 192, 232, 280, 336, 400, 476, 566, 568, 570, 572,
                574, 576
            };
            int[] s8 = {0, 8, 16, 24, 36, 52, 72, 96, 124, 160, 162, 164, 166, 192};

            sfBandIndex[0] = new SBI(l0, s0);
            sfBandIndex[1] = new SBI(l1, s1);
            sfBandIndex[2] = new SBI(l2, s2);

            sfBandIndex[3] = new SBI(l3, s3);
            sfBandIndex[4] = new SBI(l4, s4);
            sfBandIndex[5] = new SBI(l5, s5);
            //SZD: MPEG2.5
            sfBandIndex[6] = new SBI(l6, s6);
            sfBandIndex[7] = new SBI(l7, s7);
            sfBandIndex[8] = new SBI(l8, s8);
            // END OF L3TABLE INIT

            if (reorder_table == null)
            {
                // SZD: generate LUT
                reorder_table = new int[9][];
                for (int i = 0; i < 9; i++)
                    reorder_table[i] = Reorder(sfBandIndex[i].s);
            }

            // Sftable
            int[] ll0 = {0, 6, 11, 16, 21};
            int[] ss0 = {0, 6, 12};
            sftable = new ScaleFactorTable(this, ll0, ss0);
            // END OF Sftable

            // scalefac_buffer
            scalefac_buffer = new int[54];
            // END OF scalefac_buffer

            this.stream = stream;
            this.header = header;
            filter1 = filtera;
            filter2 = filterb;
            this.buffer = buffer;
            which_channels = whichCh;

            frame_start = 0;
            channels = (this.header.mode() == Header.SINGLE_CHANNEL) ? 1 : 2;
            max_gr = (this.header.version() == Header.MPEG1) ? 2 : 1;

            sfreq = this.header.sample_frequency() +
                    ((this.header.version() == Header.MPEG1) ? 3 : (this.header.version() == Header.MPEG25_LSF) ? 6 : 0); // SZD

            if (channels == 2)
            {
                switch (which_channels)
                {
                    case (int) OutputChannelsEnum.LEFT_CHANNEL:
                    case (int) OutputChannelsEnum.DOWNMIX_CHANNELS:
                        first_channel = last_channel = 0;
                        break;

                    case (int) OutputChannelsEnum.RIGHT_CHANNEL:
                        first_channel = last_channel = 1;
                        break;

                    case (int) OutputChannelsEnum.BOTH_CHANNELS:
                    default:
                        first_channel = 0;
                        last_channel = 1;
                        break;
                }
            }
            else
            {
                first_channel = last_channel = 0;
            }

            for (int ch = 0; ch < 2; ch++)
                for (int j = 0; j < 576; j++)
                    prevblck[ch][j] = 0.0f;

            nonzero[0] = nonzero[1] = 576;

            m_BitReserve = new BitReserve();
            m_SideInfo = new Layer3SideInfo();
        }