Пример #1
0
        static void Main(string[] args)
        {
            const long INBUFF = 16384 * 2 * 2;

            int ret;
            mpg123clr.mpg.ErrorCode state;
            long inoffset,inc = 0;
            long outc = 0;
            byte[] buf = new byte[INBUFF];

            if (args.Length < 2)
            {
                Console.WriteLine("Please supply in and out filenames\n");
                Console.WriteLine("Press any key to exit:");
                while (Console.Read() == 0) ;

                return;
            }

            mpg123clr.mpg.ErrorCode err;

            err = mpg123.mpg123_init();

            mpg123 mp = new mpg123();
            err = mp.mpg123_new();

            if (err != mpg123clr.mpg.ErrorCode.ok)
            {
                Console.WriteLine("Unable to create mpg123 handle: " + mpg123error.mpg123_plain_strerror(err));
                Console.WriteLine("Press any key to exit:");
                while (Console.Read() == 0) ;

                return;
            }

            mp.mpg123_param(mpg123clr.mpg.parms.verbose, 4, 0);

            err = mp.mpg123_param(mpg123clr.mpg.parms.flags,
                (int) (mpg123clr.mpg.param_flags.fuzzy |
                mpg123clr.mpg.param_flags.seekbuffer |
                mpg123clr.mpg.param_flags.gapless), 0);

            if (err != mpg123clr.mpg.ErrorCode.ok)
            {
                Console.WriteLine("Unable to set library options: " + mp.mpg123_strerror());
                Console.WriteLine("Press any key to exit:");
                while (Console.Read() == 0) ;

                return;
            }

            // Let the seek index auto-grow and contain an entry for every frame
            err = mp.mpg123_param(mpg123clr.mpg.parms.index_size, -1, 0);

            if (err != mpg123clr.mpg.ErrorCode.ok)
            {
                Console.WriteLine("Unable to set index size: " + mp.mpg123_strerror());
                Console.WriteLine("Press any key to exit:");
                while (Console.Read() == 0) ;

                return;
            }

            // Use float output formats only
            err = mp.mpg123_format_none();

            if (err != mpg123clr.mpg.ErrorCode.ok)
            {
                Console.WriteLine("Unable to disable all output formats: " + mp.mpg123_strerror());
                Console.WriteLine("Press any key to exit:");
                while (Console.Read() == 0) ;

                return;
            }

            int[] rates = mp.mpg123_rates();
            foreach (int rate in rates)
            {
                err = mp.mpg123_format(rate, mpg123clr.mpg.channelcount.both, mpg123clr.mpg.enc.enc_float_32);

                if (err != mpg123clr.mpg.ErrorCode.ok)
                {
                    Console.WriteLine("Unable to set float output formats: " + mp.mpg123_strerror());
                    Console.WriteLine("Press any key to exit:");
                    while (Console.Read() == 0) ;

                    return;
                }
            }

            err = mp.mpg123_open_feed();

            if (err != mpg123clr.mpg.ErrorCode.ok)
            {
                Console.WriteLine("Unable to open feed: " + mp.mpg123_strerror());
                Console.WriteLine("Press any key to exit:");
                while (Console.Read() == 0) ;

                return;
            }

            string filename = args[0];
            BinaryReader _in = new BinaryReader(File.Open(filename, FileMode.Open));

            _out = new BinaryWriter(File.Open(args[1], FileMode.Create));

            while ((ret = (int)(mp.mpg123_feedseek(95000, SeekOrigin.Begin, out inoffset))) == (int)mpg123clr.mpg.ErrorCode.need_more) // equiv to mpg123_feedseek
            {
                buf = _in.ReadBytes((int)INBUFF);

                if (buf.Length <= 0) break;

                inc += buf.Length;

                state = mp.mpg123_feed(buf, (uint)buf.Length);

                if (state == mpg123clr.mpg.ErrorCode.err)
                {
                    Console.WriteLine("Feed error: " + mp.mpg123_strerror());
                    Console.WriteLine("Press any key to exit:");
                    while (Console.Read() == 0) ;

                    return;
                }
            }

            _in.BaseStream.Seek(inoffset, SeekOrigin.Begin);

            while (true)
            {
                buf = _in.ReadBytes((int)INBUFF);
                if (buf.Length <= 0) break;

                inc += buf.Length;

                err = mp.mpg123_feed(buf, (uint)buf.Length);

                int num;
                uint bytes;
                IntPtr audio;

                while (err != mpg123clr.mpg.ErrorCode.err && err != mpg123clr.mpg.ErrorCode.need_more)
                {
                    err = mp.mpg123_decode_frame(out num, out audio, out bytes);

                    if (err == mpg123clr.mpg.ErrorCode.new_format)
                    {
                        mp.mpg123_getformat(out rate, out channels, out enc);

                        initwavformat();
                        initwav();
                    }

                    // (Surprisingly?) even though it does a Marshal.Copy it's as efficient as the pointer example below!!!
                    if (bytes > 0)
                    {
                        byte[] outbuf = new byte[bytes];
                        Marshal.Copy(audio, outbuf, 0, (int)bytes);

                        _out.Write(outbuf, 0, (int)bytes);
                    }

                    // Alternative example of direct usage of audio data via pointers - note it needs "unsafe"
                    //  and I'm fairly sure pointers should be "fixed" first
                    // if (bytes > 0)
                    // unsafe{
                    //        byte* p = (byte*)audio;
                    //        for (int ii = 0; ii < bytes; ii++)
                    //            _out.Write(*p++);
                    // }

                    outc += bytes;
                }

                if (err == mpg123clr.mpg.ErrorCode.err)
                {
                    Console.WriteLine("Error: " + mp.mpg123_strerror());
                    break;
                }

            }

            Console.WriteLine("Finished");

            closewav();

            _out.Close();
            _in.Close();

            mp.mpg123_delete();
            mp.Dispose();

            mpg123.mpg123_exit();
        }
Пример #2
0
        static unsafe void RunFrameByFrameTest(string filename)
        {
            mpg123clr.mpg.ErrorCode err;

            mpg123 mp = new mpg123();

            err = mp.mpg123_new();

            err = mp.mpg123_open(filename);

            if (err != mpg123clr.mpg.ErrorCode.ok)
            {
                Console.WriteLine("Error: " + mp.mpg123_strerror());
            }
            else
            {
                Console.WriteLine("Open:");

                // Show available decoders
                string[] Decoders = mp.mpg123_decoders();

                if (Decoders.Length > 0)
                {
                    Console.WriteLine("\nDecoders:");
                    foreach (string str in Decoders)
                    {
                        Console.WriteLine(str);
                    }
                }

                // Show supported decoders
                string[] supDecoders = mp.mpg123_supported_decoders();

                if (supDecoders.Length > 0)
                {
                    Console.WriteLine("\nSupported Decoders:");
                    foreach (string str in supDecoders)
                    {
                        Console.WriteLine(str);
                    }
                }

                // Show actual decoder
                Console.WriteLine("\nDecoder: " + mp.mpg123_current_decoder());

                // Show estimated file length
                Console.WriteLine("\nLength Estimate: " + mp.mpg123_length().ToString());

                // Scan - gets actual details including ID3v2 and Frame offsets
                err = mp.mpg123_scan();

                // Show actual file length
                if (err == mpg123clr.mpg.ErrorCode.ok)
                {
                    Console.WriteLine("Length Actual  : " + mp.mpg123_length().ToString());
                }

                // Get ID3 data
                mpg123clr.id3.mpg123id3v1 iv1;
                mpg123clr.id3.mpg123id3v2 iv2;
                err = mp.mpg123_id3(out iv1, out iv2);

                // Show ID3v2 data
                Console.WriteLine("\nTitle  : " + iv2.title);
                Console.WriteLine("Artist : " + iv2.artist);
                Console.WriteLine("Album  : " + iv2.album);
                Console.WriteLine("Comment: " + iv2.comment);
                Console.WriteLine("Year   : " + iv2.year);

                // Demo seek (back to start of file - note: scan should already have done this)
                long pos = mp.mpg123_seek(0, System.IO.SeekOrigin.Begin);

                long[] frameindex;
                long   step;
                err = mp.mpg123_index(out frameindex, out step);

                if (err == mpg123clr.mpg.ErrorCode.ok)
                {
                    Console.WriteLine("\nFrameIndex:");
                    foreach (long idx in frameindex)
                    {
                        // Console.WriteLine(idx.ToString());
                    }
                }

                int    num;
                uint   cnt;
                IntPtr audio;

                // Walk the file - effectively decode the data without using it...
                Console.WriteLine("\nFrame Walking  : " + iv2.title);
                DateTime dte, dts = DateTime.Now;

                while (err == mpg123clr.mpg.ErrorCode.ok || err == mpg123clr.mpg.ErrorCode.new_format)
                {
                    err = mp.mpg123_framebyframe_decode(out num, out audio, out cnt);
                    err = mp.mpg123_framebyframe_next();

                    // do something with "audio" here....
                }

                dte = DateTime.Now;

                TimeSpan ts = dte - dts;
                Console.WriteLine("Duration:  " + ts.ToString());

                mp.mpg123_close();
            }

            mp.Dispose();
        }
Пример #3
0
        static unsafe void RunFrameByFrameTest(string filename)
        {
            mpg123clr.mpg.ErrorCode err;

            mpg123 mp = new mpg123();
            err = mp.mpg123_new();

            err = mp.mpg123_open(filename);

            if (err != mpg123clr.mpg.ErrorCode.ok)
            {
                Console.WriteLine("Error: " + mp.mpg123_strerror());
            }
            else
            {
                Console.WriteLine("Open:");

                // Show available decoders
                string[] Decoders = mp.mpg123_decoders();

                if (Decoders.Length > 0)
                {
                    Console.WriteLine("\nDecoders:");
                    foreach (string str in Decoders) Console.WriteLine(str);
                }

                // Show supported decoders
                string[] supDecoders = mp.mpg123_supported_decoders();

                if (supDecoders.Length > 0)
                {
                    Console.WriteLine("\nSupported Decoders:");
                    foreach (string str in supDecoders) Console.WriteLine(str);
                }

                // Show actual decoder
                Console.WriteLine("\nDecoder: " + mp.mpg123_current_decoder());

                // Show estimated file length
                Console.WriteLine("\nLength Estimate: " + mp.mpg123_length().ToString());

                // Scan - gets actual details including ID3v2 and Frame offsets
                err = mp.mpg123_scan();

                // Show actual file length
                if (err == mpg123clr.mpg.ErrorCode.ok) Console.WriteLine("Length Actual  : " + mp.mpg123_length().ToString());

                // Get ID3 data
                mpg123clr.id3.mpg123id3v1 iv1;
                mpg123clr.id3.mpg123id3v2 iv2;
                err = mp.mpg123_id3(out iv1, out iv2);

                // Show ID3v2 data
                Console.WriteLine("\nTitle  : " + iv2.title);
                Console.WriteLine("Artist : " + iv2.artist);
                Console.WriteLine("Album  : " + iv2.album);
                Console.WriteLine("Comment: " + iv2.comment);
                Console.WriteLine("Year   : " + iv2.year);

                // Demo seek (back to start of file - note: scan should already have done this)
                long pos = mp.mpg123_seek(0, System.IO.SeekOrigin.Begin);

                long[] frameindex;
                long step;
                err = mp.mpg123_index(out frameindex, out step);

                if (err == mpg123clr.mpg.ErrorCode.ok)
                {
                    Console.WriteLine("\nFrameIndex:");
                    foreach (long idx in frameindex)
                    {
                        // Console.WriteLine(idx.ToString());
                    }
                }

                int num;
                uint cnt;
                IntPtr audio;

                // Walk the file - effectively decode the data without using it...
                Console.WriteLine("\nFrame Walking  : " + iv2.title);
                DateTime dte, dts = DateTime.Now;

                while (err == mpg123clr.mpg.ErrorCode.ok || err == mpg123clr.mpg.ErrorCode.new_format)
                {
                    err = mp.mpg123_framebyframe_decode(out num, out audio, out cnt);
                    err = mp.mpg123_framebyframe_next();

                    // do something with "audio" here....
                }

                dte = DateTime.Now;

                TimeSpan ts = dte - dts;
                Console.WriteLine("Duration:  " + ts.ToString());

                mp.mpg123_close();
            }

            mp.Dispose();

        }
Пример #4
0
        static void Main(string[] args)
        {
            const long INBUFF = 16384 * 2 * 2;

            int ret;

            mpg123clr.mpg.ErrorCode state;
            long inoffset, inc = 0;
            long outc = 0;

            byte[] buf = new byte[INBUFF];

            if (args.Length < 2)
            {
                Console.WriteLine("Please supply in and out filenames\n");
                Console.WriteLine("Press any key to exit:");
                while (Console.Read() == 0)
                {
                    ;
                }

                return;
            }

            mpg123clr.mpg.ErrorCode err;

            err = mpg123.mpg123_init();

            mpg123 mp = new mpg123();

            err = mp.mpg123_new();

            if (err != mpg123clr.mpg.ErrorCode.ok)
            {
                Console.WriteLine("Unable to create mpg123 handle: " + mpg123error.mpg123_plain_strerror(err));
                Console.WriteLine("Press any key to exit:");
                while (Console.Read() == 0)
                {
                    ;
                }

                return;
            }

            mp.mpg123_param(mpg123clr.mpg.parms.verbose, 4, 0);

            err = mp.mpg123_param(mpg123clr.mpg.parms.flags,
                                  (int)(mpg123clr.mpg.param_flags.fuzzy |
                                        mpg123clr.mpg.param_flags.seekbuffer |
                                        mpg123clr.mpg.param_flags.gapless), 0);

            if (err != mpg123clr.mpg.ErrorCode.ok)
            {
                Console.WriteLine("Unable to set library options: " + mp.mpg123_strerror());
                Console.WriteLine("Press any key to exit:");
                while (Console.Read() == 0)
                {
                    ;
                }

                return;
            }

            // Let the seek index auto-grow and contain an entry for every frame
            err = mp.mpg123_param(mpg123clr.mpg.parms.index_size, -1, 0);

            if (err != mpg123clr.mpg.ErrorCode.ok)
            {
                Console.WriteLine("Unable to set index size: " + mp.mpg123_strerror());
                Console.WriteLine("Press any key to exit:");
                while (Console.Read() == 0)
                {
                    ;
                }

                return;
            }

            // Use float output formats only
            err = mp.mpg123_format_none();

            if (err != mpg123clr.mpg.ErrorCode.ok)
            {
                Console.WriteLine("Unable to disable all output formats: " + mp.mpg123_strerror());
                Console.WriteLine("Press any key to exit:");
                while (Console.Read() == 0)
                {
                    ;
                }

                return;
            }

            int[] rates = mp.mpg123_rates();
            foreach (int rate in rates)
            {
                err = mp.mpg123_format(rate, mpg123clr.mpg.channelcount.both, mpg123clr.mpg.enc.enc_float_32);

                if (err != mpg123clr.mpg.ErrorCode.ok)
                {
                    Console.WriteLine("Unable to set float output formats: " + mp.mpg123_strerror());
                    Console.WriteLine("Press any key to exit:");
                    while (Console.Read() == 0)
                    {
                        ;
                    }

                    return;
                }
            }

            err = mp.mpg123_open_feed();

            if (err != mpg123clr.mpg.ErrorCode.ok)
            {
                Console.WriteLine("Unable to open feed: " + mp.mpg123_strerror());
                Console.WriteLine("Press any key to exit:");
                while (Console.Read() == 0)
                {
                    ;
                }

                return;
            }

            string       filename = args[0];
            BinaryReader _in      = new BinaryReader(File.Open(filename, FileMode.Open));

            _out = new BinaryWriter(File.Open(args[1], FileMode.Create));

            while ((ret = (int)(mp.mpg123_feedseek(95000, SeekOrigin.Begin, out inoffset))) == (int)mpg123clr.mpg.ErrorCode.need_more) // equiv to mpg123_feedseek
            {
                buf = _in.ReadBytes((int)INBUFF);

                if (buf.Length <= 0)
                {
                    break;
                }

                inc += buf.Length;

                state = mp.mpg123_feed(buf, (uint)buf.Length);

                if (state == mpg123clr.mpg.ErrorCode.err)
                {
                    Console.WriteLine("Feed error: " + mp.mpg123_strerror());
                    Console.WriteLine("Press any key to exit:");
                    while (Console.Read() == 0)
                    {
                        ;
                    }

                    return;
                }
            }

            _in.BaseStream.Seek(inoffset, SeekOrigin.Begin);

            while (true)
            {
                buf = _in.ReadBytes((int)INBUFF);
                if (buf.Length <= 0)
                {
                    break;
                }

                inc += buf.Length;

                err = mp.mpg123_feed(buf, (uint)buf.Length);

                int    num;
                uint   bytes;
                IntPtr audio;

                while (err != mpg123clr.mpg.ErrorCode.err && err != mpg123clr.mpg.ErrorCode.need_more)
                {
                    err = mp.mpg123_decode_frame(out num, out audio, out bytes);

                    if (err == mpg123clr.mpg.ErrorCode.new_format)
                    {
                        mp.mpg123_getformat(out rate, out channels, out enc);

                        initwavformat();
                        initwav();
                    }

                    // (Surprisingly?) even though it does a Marshal.Copy it's as efficient as the pointer example below!!!
                    if (bytes > 0)
                    {
                        byte[] outbuf = new byte[bytes];
                        Marshal.Copy(audio, outbuf, 0, (int)bytes);

                        _out.Write(outbuf, 0, (int)bytes);
                    }

                    // Alternative example of direct usage of audio data via pointers - note it needs "unsafe"
                    //  and I'm fairly sure pointers should be "fixed" first
                    // if (bytes > 0)
                    // unsafe{
                    //        byte* p = (byte*)audio;
                    //        for (int ii = 0; ii < bytes; ii++)
                    //            _out.Write(*p++);
                    // }

                    outc += bytes;
                }

                if (err == mpg123clr.mpg.ErrorCode.err)
                {
                    Console.WriteLine("Error: " + mp.mpg123_strerror());
                    break;
                }
            }

            Console.WriteLine("Finished");

            closewav();

            _out.Close();
            _in.Close();

            mp.mpg123_delete();
            mp.Dispose();

            mpg123.mpg123_exit();
        }