/* * mp3plot - Bitrate analysis tool * * Copyright (C) 2007 Toni Corvera * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ // $Id: mp3_file.cc 749 2007-05-24 22:44:28Z $ #include "mp3_file.h" #include #include #include "str_utils.h" #include "spinner.h" #include "config.h" // File structure based on namespace net_outlyer { namespace mp3 { namespace str=net_outlyer::str_utils; using std::ifstream; using std::cerr; using std::cout; using std::endl; using std::string; using std::ios; // -- MP3 File -- mp3file::mp3file(const string & file_path) : file_type(UNKNOWN), meta_info(), file(file_path.c_str(), ios::binary|ios::in) {} void mp3file::check_file_type() throw (e_error) { file.seekg(0, ios::beg); uint8_t filestart[3]; file.read(CHARR(filestart), 3); if (0 == memcmp(filestart, "ID3", 3)) { file_type = FT_ID3V2; } else if (filestart[0] == 0xff && 0xf0 == (filestart[1] & 0xf0)) { file_type = FT_MP3; } else { try { seek_to_next_frame(); } catch (e_read_failure & e) { cerr << "End of file reached without finding a frame" << endl; throw e_bad_filetype(); } cerr << "WARNING: Junk at the beginning of the file (up to byte #" << file.tellg() << ")\n" "\tFile information might be inaccurate.\n" << endl; } } void mp3file::read_info() throw (e_error) { check_file_type(); file.seekg(0, ios::beg); if (FT_ID3V2 == file_type) { id3v2.read(file); // Jump to tag end: file.seekg(id3v2.tag_size, ios::cur); // ID3v2 total size is header(=10) + tag_size } // Find first frame seek_to_next_frame(); // throws e_read_failure if none found // POST ]-> at first frame std::auto_ptr fframe (new mp3frame_first(meta_info)); fframe->read(file); fframe->frame_idx = 1; frames.push_back( *fframe ); net_outlyer::spinner spinner; try { uint32_t frame_idx = 2; spinner.start(); while (true) { seek_to_next_frame(); mp3frame f; f.frame_idx = frame_idx++; f.read(file); f.skip_data(file); frames.push_back(f); spinner.step(); } } catch (e_read_failure & e) { spinner.stop(); cerr << "No more frames found after byte #" << frames.back().offset() << std::endl; } spinner.stop(); cerr << frames.back().frame_idx << " frames read" << std::endl; } void mp3file::dump_meta_info() const { if (FT_ID3V2 == file_type) { id3v2.dump(); } else { cout << "This file doesn't contain an ID3v2 header" << std::endl; } frames[0].dump(); cout << "VBR: " << (meta_info.is_vbr ? "Yes\n" : "No\n"); if (meta_info.is_vbr) { if (meta_info.has_numframes) { cout << "\tNumber of frames included (" << meta_info.num_frames << ")\n"; } if (meta_info.has_filesize) cout << "\tFile size included (" << meta_info.file_size << ")\n"; if (meta_info.has_toc) cout << "\tTOC included\n"; if (meta_info.has_vbr_scale) cout << "\tVBR scale included\n"; } } void mp3file::fill_bitrate_distribution() { if (br_dist.is_filled) return; br_dist.fill_from(frames, meta_info.is_vbr); assert( br_dist.is_filled ); } void mp3file::plot(bitrate_plotter::plotter_type_t preferred_type) { fill_bitrate_distribution(); try { bitrate_plotter::choose(preferred_type).plot(*this); } catch (bitrate_plotter::e_unregistered_plotter_type & e) { cerr << "There's no support for the chosen plot type available." << endl; } } void mp3file::seek_to_next_frame() throw (e_read_failure) { uint8_t last=0, byte; // Frames start with { 0xFF , 0xFX } while (file.good()) { file.read(CHARR(&byte), 1); if ((last==0xff) && 0xf0 == (byte&0xf0)) { file.seekg( -2 , ios::cur); return; } last = byte; } throw e_read_failure("Couldn't find next frame.", EX_DATAERR); } } // namespace mp3 } // namespace net_outlyer /* vim:set ts=4 et ai: */