📄 mp3_parse.cpp
字号:
// -*- C++ -*-// $Id: mp3_parse.cpp,v 1.6 2002/11/02 17:48:51 t1mpy Exp $// id3lib: a C++ library for creating and manipulating id3v1/v2 tags// Copyright 2002, Thijmen Klok (thijmen@id3lib.org)// This library is free software; you can redistribute it and/or modify it// under the terms of the GNU Library General Public License as published by// the Free Software Foundation; either version 2 of the License, or (at your// option) any later version.//// This library is distributed in the hope that it will be useful, but WITHOUT// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public// License for more details.//// You should have received a copy of the GNU Library General Public License// along with this library; if not, write to the Free Software Foundation,// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.// The id3lib authors encourage improvements and optimisations to be sent to// the id3lib coordinator. Please see the README file for details on where to// send such submissions. See the AUTHORS file for a list of people who have// contributed to id3lib. See the ChangeLog file for a list of changes to// id3lib. These files are distributed with id3lib at// http://download.sourceforge.net/id3lib/#include "mp3_header.h"#define FRAMES_FLAG 0x0001#define BYTES_FLAG 0x0002#define TOC_FLAG 0x0004#define SCALE_FLAG 0x0008static int ExtractI4(unsigned char *buf){ int x; // big endian extract x = buf[0]; x <<= 8; x |= buf[1]; x <<= 8; x |= buf[2]; x <<= 8; x |= buf[3]; return x;}uint32 fto_nearest_i(float f){ uint32 i; i = (uint32)f; if (i < f) { f -= i; if (f >= 0.5) return i+1; else return i; } else return i;}uint16 calcCRC(char *pFrame, size_t audiodatasize){ size_t icounter; int tmpchar, crcmask, tmpi; uint16 crc = 0xffff; for (icounter = 2; icounter < audiodatasize; ++icounter) { if (icounter != 4 && icounter != 5) //skip the 2 chars of the crc itself { crcmask = 1 << 8; tmpchar = pFrame[icounter]; while (crcmask >>= 1) { tmpi = crc & 0x8000; crc <<= 1; if (!tmpi ^ !(tmpchar & crcmask)) crc ^= 0x8005; } } } crc &= 0xffff; return crc;}void Mp3Info::Clean(){ if (_mp3_header_output != NULL) delete _mp3_header_output; _mp3_header_output = NULL;}using namespace dami;bool Mp3Info::Parse(ID3_Reader& reader, size_t mp3size){ MP3_BitRates _mp3_bitrates[2][3][16] = { { { //MPEG 1, LAYER I MP3BITRATE_NONE, MP3BITRATE_32K, MP3BITRATE_64K, MP3BITRATE_96K, MP3BITRATE_128K, MP3BITRATE_160K, MP3BITRATE_192K, MP3BITRATE_224K, MP3BITRATE_256K, MP3BITRATE_288K, MP3BITRATE_320K, MP3BITRATE_352K, MP3BITRATE_384K, MP3BITRATE_416K, MP3BITRATE_448K, MP3BITRATE_FALSE }, { //MPEG 1, LAYER II MP3BITRATE_NONE, MP3BITRATE_32K, MP3BITRATE_48K, MP3BITRATE_56K, MP3BITRATE_64K, MP3BITRATE_80K, MP3BITRATE_96K, MP3BITRATE_112K, MP3BITRATE_128K, MP3BITRATE_160K, MP3BITRATE_192K, MP3BITRATE_224K, MP3BITRATE_256K, MP3BITRATE_320K, MP3BITRATE_384K, MP3BITRATE_FALSE }, { //MPEG 1, LAYER III MP3BITRATE_NONE, MP3BITRATE_32K, MP3BITRATE_40K, MP3BITRATE_48K, MP3BITRATE_56K, MP3BITRATE_64K, MP3BITRATE_80K, MP3BITRATE_96K, MP3BITRATE_112K, MP3BITRATE_128K, MP3BITRATE_160K, MP3BITRATE_192K, MP3BITRATE_224K, MP3BITRATE_256K, MP3BITRATE_320K, MP3BITRATE_FALSE } }, { { //MPEG 2 or 2.5, LAYER I MP3BITRATE_NONE, MP3BITRATE_32K, MP3BITRATE_48K, MP3BITRATE_56K, MP3BITRATE_64K, MP3BITRATE_80K, MP3BITRATE_96K, MP3BITRATE_112K, MP3BITRATE_128K, MP3BITRATE_144K, MP3BITRATE_160K, MP3BITRATE_176K, MP3BITRATE_192K, MP3BITRATE_224K, MP3BITRATE_256K, MP3BITRATE_FALSE }, { //MPEG 2 or 2.5, LAYER II MP3BITRATE_NONE, MP3BITRATE_8K, MP3BITRATE_16K, MP3BITRATE_24K, MP3BITRATE_32K, MP3BITRATE_40K, MP3BITRATE_48K, MP3BITRATE_56K, MP3BITRATE_64K, MP3BITRATE_80K, MP3BITRATE_96K, MP3BITRATE_112K, MP3BITRATE_128K, MP3BITRATE_144K, MP3BITRATE_160K, MP3BITRATE_FALSE }, { //MPEG 2 or 2.5, LAYER III MP3BITRATE_NONE, MP3BITRATE_8K, MP3BITRATE_16K, MP3BITRATE_24K, MP3BITRATE_32K, MP3BITRATE_40K, MP3BITRATE_48K, MP3BITRATE_56K, MP3BITRATE_64K, MP3BITRATE_80K, MP3BITRATE_96K, MP3BITRATE_112K, MP3BITRATE_128K, MP3BITRATE_144K, MP3BITRATE_160K, MP3BITRATE_FALSE } } }; Mp3_Frequencies _mp3_frequencies[4][4] = { { MP3FREQUENCIES_11025HZ, MP3FREQUENCIES_12000HZ, MP3FREQUENCIES_8000HZ,MP3FREQUENCIES_Reserved }, //MPEGVERSION_2_5 { MP3FREQUENCIES_Reserved, MP3FREQUENCIES_Reserved, MP3FREQUENCIES_Reserved, MP3FREQUENCIES_Reserved}, //MPEGVERSION_Reserved { MP3FREQUENCIES_22050HZ, MP3FREQUENCIES_24000HZ, MP3FREQUENCIES_16000HZ, MP3FREQUENCIES_Reserved }, //MPEGVERSION_2 { MP3FREQUENCIES_44100HZ, MP3FREQUENCIES_48000HZ, MP3FREQUENCIES_32000HZ, MP3FREQUENCIES_Reserved } //MPEGVERSION_1 }; _mp3_header_internal *_tmpheader; const size_t HEADERSIZE = 4;// char buf[HEADERSIZE+1]; //+1 to hold the \0 char ID3_Reader::pos_type beg = reader.getCur() ; ID3_Reader::pos_type end = beg + HEADERSIZE ; reader.setCur(beg); int bitrate_index; _mp3_header_output->layer = MPEGLAYER_FALSE; _mp3_header_output->version = MPEGVERSION_FALSE; _mp3_header_output->bitrate = MP3BITRATE_FALSE; _mp3_header_output->channelmode = MP3CHANNELMODE_FALSE; _mp3_header_output->modeext = MP3MODEEXT_FALSE; _mp3_header_output->emphasis = MP3EMPHASIS_FALSE; _mp3_header_output->crc = MP3CRC_MISMATCH; _mp3_header_output->frequency = 0; _mp3_header_output->framesize = 0; _mp3_header_output->frames = 0; _mp3_header_output->time = 0; _mp3_header_output->vbr_bitrate = 0; reader.readChars(buf, HEADERSIZE); buf[HEADERSIZE]='\0'; // copy the pointer to the struct if (((buf[0] & 0xFF) != 0xFF) || ((buf[1] & 0xE0) != 0xE0)) //first 11 bits should be 1 { this->Clean(); return false; } _tmpheader = reinterpret_cast<_mp3_header_internal *>(buf); bitrate_index = 0; switch (_tmpheader->id) { case 3: _mp3_header_output->version = MPEGVERSION_1; bitrate_index = 0; break; case 2: _mp3_header_output->version = MPEGVERSION_2; bitrate_index = 1; break; case 1: this->Clean(); return false; //wouldn't know how to handle it break; case 0: _mp3_header_output->version = MPEGVERSION_2_5; bitrate_index = 1; break; default: this->Clean(); return false; break; };
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -