/* * 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_console_plotter.cc 749 2007-05-24 22:44:28Z $ #include #include #include #include "config.h" // PLOT_SYMBOL #include "str_utils.h" #include "mp3_console_plotter.h" using std::cout; using std::endl; using std::string; namespace net_outlyer { namespace mp3 { namespace str = str_utils; // static void console_plotter::register_me() { boost::shared_ptr p(new console_plotter()); bitrate_plotter::register_plotter( bitrate_plotter::TEXT, p ); } // Register the console plotter // It's always available void console_plotter::plot(const mp3file & file) const { const bitrate_distribution & dist = file.get_br_distribution(); // Max length of bitrate tags static const size_t LBR = string("invalid").length(); // Max length of number of frames, + padding const size_t LOC = str::to_string(dist.max).length() + 1; // Console width // FIXME: detect const uint16_t CONWIDTH = 80; // 4 = "[]: " + 1 => space between number of frames and dots // using float because a cast would be made anyway const float AVAIL = CONWIDTH - (LBR + LOC + 5); uint32_t sum=0, fs=0; float sumbrs=.0; for (bitrate_distribution::const_iterator b_it = dist.begin(); b_it != dist.end(); ++b_it) { const bitrate_t br = b_it->first; const numframes_t nf = b_it->second; const string & s_br = str::pad( str::to_string(br) + "kbps", LBR, " ", str::PAD_LEFT ); const string & s_nf = str::pad( str::to_string(nf), LOC ); cout << "[" << s_br << "]: " << s_nf; // the scale is relative to the max const int SCALED = int(nf * AVAIL / dist.max); cout << str::pad("", SCALED, PLOT_SYMBOL); cout << "\n"; sum += nf; // number of frames of this bitrate sumbrs += (nf * br); // That's the number of bits per seconds at this bitrate const uint32_t secbits = nf * br * 1000; // Each frame is 0.026 seconds long fs+=int( secbits * 0.026); } { // Finally print the invalid frames cout << "[invalid]: " << dist.bad_frames(); /* const int SCALED = int(br_dist.bad_frames() * AVAIL / br_dist.max); cout << str::pad("", SCALED, PLOT_SYMBOL); */ cout << "\n"; } string abr = str::to_string(sumbrs/sum); const string::size_type dotidx = abr.find_first_of('.'); if (string::npos != dotidx) { if ((dotidx + 3) < abr.length()) { abr.erase(dotidx+3); } } cout << "Average bitrate is " << abr << " kbps\n"; cout << "\twith a total of " << sum << " frames" << endl; cout << "\twhich yields a theoretical (audio data) size of " << (fs / 8) << " bytes" << endl; } } // namespace mp3 } // namespace net_outlyer /* vim:set ts=4 et ai: */