private void LowLevelInit(OggVorbisEncoderSetup setup)
        {
            unsafe
            {
                lls = (LowLevelState*)Marshal.AllocHGlobal(sizeof(LowLevelState));
                if (lls == null)
                    throw new InvalidOperationException("Could not allocate unmanaged memory");
                *lls = new LowLevelState();
                xm.vorbis_info_init(ref lls->vi);
                // choose an encoding mode.
                setup.BitRate(setup, ref lls->vi);

                /* add a comments */
                xm.vorbis_comment_init(ref lls->vc);
                foreach (OggCommentEntry entry in setup.Comments)
                {
                    if (entry.Tag != null)
                        xm.vorbis_comment_add_tag(ref lls->vc, entry.Tag, entry.Text);
                    else
                        xm.vorbis_comment_add(ref lls->vc, entry.Text);
                }

                /* set up the analysis state and auxiliary encoding storage */
                xm.vorbis_analysis_init(ref lls->vd, ref lls->vi);
                xm.vorbis_block_init(ref lls->vd, ref lls->vb);

                /* set up our packet->stream encoder */
                /* pick a random serial number; that way we can more likely build
                   chained streams just by concatenation */
                Random random = new Random();
                xm.ogg_stream_init(ref lls->os, random.Next());

                /* Vorbis streams begin with three headers; the initial header (with
                   most of the codec setup parameters) which is mandated by the Ogg
                   bitstream spec.  The second header holds any comment fields.  The
                   third header holds the bitstream codebook.  We merely need to
                   make the headers, then pass them to libvorbis one at a time;
                   libvorbis handles the additional Ogg bitstream constraints */

                xm.vorbis_analysis_headerout(ref lls->vd, ref lls->vc, ref lls->header, ref lls->header_comm, ref lls->header_code);
                xm.ogg_stream_packetin(ref lls->os, ref lls->header); /* automatically placed in its own
                                         page */
                xm.ogg_stream_packetin(ref lls->os, ref lls->header_comm);
                xm.ogg_stream_packetin(ref lls->os, ref lls->header_code);

                /* This ensures the actual
                 * audio data will start on a new page, as per spec
                 */
                while (true)
                {
                    int result = xm.ogg_stream_flush(ref lls->os, ref lls->og);
                    if (result == 0) break;
                    WritePage(ref lls->og, Stream);
                }
            }
        }
Example #2
0
        private void LowLevelInit(OggVorbisEncoderSetup setup)
        {
            unsafe
            {
                lls = (LowLevelState *)Marshal.AllocHGlobal(sizeof(LowLevelState));
                if (lls == null)
                {
                    throw new InvalidOperationException("Could not allocate unmanaged memory");
                }
                *lls = new LowLevelState();
                xm.vorbis_info_init(ref lls->vi);
                // choose an encoding mode.
                setup.BitRate(setup, ref lls->vi);

                /* add a comments */
                xm.vorbis_comment_init(ref lls->vc);
                foreach (OggCommentEntry entry in setup.Comments)
                {
                    if (entry.Tag != null)
                    {
                        xm.vorbis_comment_add_tag(ref lls->vc, entry.Tag, entry.Text);
                    }
                    else
                    {
                        xm.vorbis_comment_add(ref lls->vc, entry.Text);
                    }
                }

                /* set up the analysis state and auxiliary encoding storage */
                xm.vorbis_analysis_init(ref lls->vd, ref lls->vi);
                xm.vorbis_block_init(ref lls->vd, ref lls->vb);

                /* set up our packet->stream encoder */

                /* pick a random serial number; that way we can more likely build
                 * chained streams just by concatenation */
                Random random = new Random();
                xm.ogg_stream_init(ref lls->os, random.Next());

                /* Vorbis streams begin with three headers; the initial header (with
                 * most of the codec setup parameters) which is mandated by the Ogg
                 * bitstream spec.  The second header holds any comment fields.  The
                 * third header holds the bitstream codebook.  We merely need to
                 * make the headers, then pass them to libvorbis one at a time;
                 * libvorbis handles the additional Ogg bitstream constraints */



                xm.vorbis_analysis_headerout(ref lls->vd, ref lls->vc, ref lls->header, ref lls->header_comm, ref lls->header_code);
                xm.ogg_stream_packetin(ref lls->os, ref lls->header);                 /* automatically placed in its own
                                                                                       * page */
                xm.ogg_stream_packetin(ref lls->os, ref lls->header_comm);
                xm.ogg_stream_packetin(ref lls->os, ref lls->header_code);

                /* This ensures the actual
                 * audio data will start on a new page, as per spec
                 */
                while (true)
                {
                    int result = xm.ogg_stream_flush(ref lls->os, ref lls->og);
                    if (result == 0)
                    {
                        break;
                    }
                    WritePage(ref lls->og, Stream);
                }
            }
        }