示例#1
0
        private Stream OpenSndFile(LameGlobalFlags gfp, Stream mp3Stream, Enc enc)
        {
            gfp.num_samples = -1;
            musicin         = mp3Stream;

            if (is_mpeg_file_format(parse.input_format))
            {
                if (-1 == lame_decode_initfile(musicin, parse.mp3input_data, enc))
                {
                    throw new Exception(string.Format("Error reading headers in mp3 input file {0}.", mp3Stream));
                }

                gfp.num_channels  = parse.mp3input_data.stereo;
                gfp.in_samplerate = parse.mp3input_data.samplerate;
                gfp.num_samples   = parse.mp3input_data.nsamp;
            }
            else if (parse.input_format == sound_file_format.sf_ogg)
            {
                throw new Exception("sorry, vorbis support in LAME is deprecated.");
            }
            else if (parse.input_format == sound_file_format.sf_raw)
            {
                pcmswapbytes = parse.swapbytes;
            }
            else
            {
                parse.input_format = parse_file_header(gfp, musicin);
            }

            if (parse.input_format == sound_file_format.sf_unknown)
            {
                throw new Exception("Unknown sound format!");
            }

            if (gfp.num_samples == -1)
            {
                double flen = musicin.Length;
                if (flen >= 0)
                {
                    if (is_mpeg_file_format(parse.input_format))
                    {
                        if (parse.mp3input_data.bitrate > 0)
                        {
                            var totalseconds    = flen * 8.0 / (1000.0 * parse.mp3input_data.bitrate);
                            var tmp_num_samples = (int)(totalseconds * gfp.in_samplerate);
                            gfp.num_samples           = tmp_num_samples;
                            parse.mp3input_data.nsamp = tmp_num_samples;
                        }
                    }
                    else
                    {
                        gfp.num_samples = (int)(flen / (2 * gfp.num_channels));
                    }
                }
            }

            return(musicin);
        }
示例#2
0
 internal void init_infile(LameGlobalFlags gfp, Stream mp3Stream, Enc enc)
 {
     count_samples_carefully = false;
     num_samples_read        = 0;
     pcmbitwidth             = parse.in_bitwidth;
     pcmswapbytes            = parse.swapbytes;
     pcm_is_unsigned_8bit    = !parse.in_signed;
     musicin = OpenSndFile(gfp, mp3Stream, enc);
 }
示例#3
0
        private FileStream OpenSndFile(LameGlobalFlags gfp, string inPath, Enc enc)
        {
            gfp.num_samples = -1;
            try
            {
                musicin = new FileStream(inPath, FileMode.Open, FileAccess.Read);
            }
            catch (FileNotFoundException e)
            {
                throw new Exception(string.Format("Could not find \"{0}\".", inPath), e);
            }

            if (is_mpeg_file_format(parse.input_format))
            {
                if (-1 == lame_decode_initfile(musicin, parse.mp3input_data, enc))
                {
                    throw new Exception(string.Format("Error reading headers in mp3 input file {0}.", inPath));
                }

                gfp.num_channels  = parse.mp3input_data.stereo;
                gfp.in_samplerate = parse.mp3input_data.samplerate;
                gfp.num_samples   = parse.mp3input_data.nsamp;
            }
            else if (parse.input_format == sound_file_format.sf_ogg)
            {
                throw new Exception("sorry, vorbis support in LAME is deprecated.");
            }
            else if (parse.input_format == sound_file_format.sf_raw)
            {
                if (parse.silent < 10)
                {
                    Console.WriteLine("Assuming raw pcm input file");
                    if (parse.swapbytes)
                    {
                        Console.Write(" : Forcing byte-swapping\n");
                    }
                    else
                    {
                        Console.Write("\n");
                    }
                }

                pcmswapbytes = parse.swapbytes;
            }
            else
            {
                parse.input_format = parse_file_header(gfp, musicin);
            }

            if (parse.input_format == sound_file_format.sf_unknown)
            {
                throw new Exception("Unknown sound format!");
            }

            if (gfp.num_samples == -1)
            {
                double flen = musicin.Length;
                if (flen >= 0)
                {
                    if (is_mpeg_file_format(parse.input_format))
                    {
                        if (parse.mp3input_data.bitrate > 0)
                        {
                            var totalseconds    = flen * 8.0 / (1000.0 * parse.mp3input_data.bitrate);
                            var tmp_num_samples = (int)(totalseconds * gfp.in_samplerate);
                            gfp.num_samples           = tmp_num_samples;
                            parse.mp3input_data.nsamp = tmp_num_samples;
                        }
                    }
                    else
                    {
                        gfp.num_samples = (int)(flen / (2 * gfp.num_channels));
                    }
                }
            }

            return(musicin);
        }
示例#4
0
        private int lame_decode_initfile(Stream fd, MP3Data mp3data, Enc enc)
        {
            var buf        = new byte[100];
            var pcm_l      = new short[1152];
            var pcm_r      = new short[1152];
            var freeformat = false;

            if (hip != null)
            {
                mpg.hip_decode_exit(hip);
            }

            hip = mpg.hip_decode_init();
            var len = 4;

            if (fd.Position + len > fd.Length)
            {
                return(-1);
            }

            fd.Read(buf, 0, len);

            if (buf[0] == (sbyte)'I' && buf[1] == (sbyte)'D' && buf[2] == (sbyte)'3')
            {
                len = 6;
                try
                {
                    fd.Read(buf, 0, len);
                }
                catch (IOException e)
                {
                    return(-1);
                }

                buf[2] &= 127;
                buf[3] &= 127;
                buf[4] &= 127;
                buf[5] &= 127;
                len     = (((((buf[2] << 7) + buf[3]) << 7) + buf[4]) << 7) + buf[5];
                try
                {
                    fd.Seek(len, SeekOrigin.Current);
                }
                catch (IOException e)
                {
                    return(-1);
                }

                len = 4;
                try
                {
                    fd.Read(buf, 0, len);
                }
                catch (IOException e)
                {
                    return(-1);
                }
            }

            if (check_aid(buf))
            {
                try
                {
                    fd.Read(buf, 0, 2);
                }
                catch (IOException e)
                {
                    return(-1);
                }

                var aid_header = (buf[0] & 0xff) + 256 * (buf[1] & 0xff);

                try
                {
                    fd.Seek(aid_header - 6, SeekOrigin.Current);
                }
                catch (IOException e)
                {
                    return(-1);
                }

                try
                {
                    fd.Read(buf, 0, len);
                }
                catch (IOException e)
                {
                    return(-1);
                }
            }

            len = 4;
            while (!is_syncword_mp123(buf))
            {
                int i;
                for (i = 0; i < len - 1; i++)
                {
                    buf[i] = buf[i + 1];
                }

                try
                {
                    fd.Read(buf, len - 1, 1);
                }
                catch (IOException e)
                {
                    return(-1);
                }
            }

            if ((buf[2] & 0xf0) == 0)
            {
                freeformat = true;
            }

            var ret = mpg.hip_decode1_headersB(hip, buf, len, pcm_l, pcm_r, mp3data, enc, false);

            if (-1 == ret)
            {
                return(-1);
            }

            while (!mp3data.header_parsed)
            {
                try
                {
                    fd.Read(buf, 0, buf.Length);
                }
                catch (IOException e)
                {
                    return(-1);
                }

                ret = mpg.hip_decode1_headersB(hip, buf, buf.Length, pcm_l, pcm_r, mp3data, enc, false);
                if (-1 == ret)
                {
                    return(-1);
                }
            }

            if (mp3data.bitrate == 0 && !freeformat)
            {
                return(lame_decode_initfile(fd, mp3data, enc));
            }

            if (mp3data.totalframes > 0)
            {
            }
            else
            {
                mp3data.nsamp = -1;
            }

            return(0);
        }
示例#5
0
        private int lame_decode_initfile(FileStream fd, MP3Data mp3data, Enc enc)
        {
            var buf        = new byte[100];
            var pcm_l      = new short[1152];
            var pcm_r      = new short[1152];
            var freeformat = false;

            if (hip != null)
            {
                mpg.hip_decode_exit(hip);
            }

            hip = mpg.hip_decode_init();
            var len = 4;

            try
            {
                fd.Read(buf, 0, len);
            }
            catch (IOException e)
            {
                Console.WriteLine(e.ToString());
                Console.Write(e.StackTrace);
                return(-1);
            }

            if (buf[0] == (sbyte)'I' && buf[1] == (sbyte)'D' && buf[2] == (sbyte)'3')
            {
                if (parse.silent < 10)
                {
                    Console.WriteLine(
                        "ID3v2 found. " + "Be aware that the ID3 tag is currently lost when transcoding.");
                }

                len = 6;
                try
                {
                    fd.Read(buf, 0, len);
                }
                catch (IOException e)
                {
                    Console.WriteLine(e.ToString());
                    Console.Write(e.StackTrace);
                    return(-1);
                }

                buf[2] &= 127;
                buf[3] &= 127;
                buf[4] &= 127;
                buf[5] &= 127;
                len     = (((((buf[2] << 7) + buf[3]) << 7) + buf[4]) << 7) + buf[5];
                try
                {
                    fd.Seek(len, SeekOrigin.Current);
                }
                catch (IOException e)
                {
                    Console.WriteLine(e.ToString());
                    Console.Write(e.StackTrace);
                    return(-1);
                }

                len = 4;
                try
                {
                    fd.Read(buf, 0, len);
                }
                catch (IOException e)
                {
                    Console.WriteLine(e.ToString());
                    Console.Write(e.StackTrace);
                    return(-1);
                }
            }

            if (check_aid(buf))
            {
                try
                {
                    fd.Read(buf, 0, 2);
                }
                catch (IOException e)
                {
                    Console.WriteLine(e.ToString());
                    Console.Write(e.StackTrace);
                    return(-1);
                }

                var aid_header = (buf[0] & 0xff) + 256 * (buf[1] & 0xff);
                if (parse.silent < 10)
                {
                    Console.Write("Album ID found.  length={0:D} \n", aid_header);
                }

                try
                {
                    fd.Seek(aid_header - 6, SeekOrigin.Current);
                }
                catch (IOException e)
                {
                    Console.WriteLine(e.ToString());
                    Console.Write(e.StackTrace);
                    return(-1);
                }

                try
                {
                    fd.Read(buf, 0, len);
                }
                catch (IOException e)
                {
                    Console.WriteLine(e.ToString());
                    Console.Write(e.StackTrace);
                    return(-1);
                }
            }

            len = 4;
            while (!is_syncword_mp123(buf))
            {
                int i;
                for (i = 0; i < len - 1; i++)
                {
                    buf[i] = buf[i + 1];
                }

                try
                {
                    fd.Read(buf, len - 1, 1);
                }
                catch (IOException e)
                {
                    Console.WriteLine(e.ToString());
                    Console.Write(e.StackTrace);
                    return(-1);
                }
            }

            if ((buf[2] & 0xf0) == 0)
            {
                if (parse.silent < 10)
                {
                    Console.WriteLine("Input file is freeformat.");
                }

                freeformat = true;
            }

            var ret = mpg.hip_decode1_headersB(hip, buf, len, pcm_l, pcm_r, mp3data, enc);

            if (-1 == ret)
            {
                return(-1);
            }

            while (!mp3data.header_parsed)
            {
                try
                {
                    fd.Read(buf, 0, 1024);
                }
                catch (IOException e)
                {
                    Console.WriteLine(e.ToString());
                    Console.Write(e.StackTrace);
                    return(-1);
                }

                ret = mpg.hip_decode1_headersB(hip, buf, buf.Length, pcm_l, pcm_r, mp3data, enc);
                if (-1 == ret)
                {
                    return(-1);
                }
            }

            if (mp3data.bitrate == 0 && !freeformat)
            {
                if (parse.silent < 10)
                {
                    Console.Error.WriteLine("fail to sync...");
                }

                return(lame_decode_initfile(fd, mp3data, enc));
            }

            if (mp3data.totalframes > 0)
            {
            }
            else
            {
                mp3data.nsamp = -1;
            }

            return(0);
        }