//*************************************************************************** QString Kwave::MP3Decoder::parseId3Frame2String(const ID3_Frame *frame) { QString s; char *text = ID3_GetString(frame, ID3FN_TEXT); if (text && strlen(text)) { s = _(text); ID3_FreeString(text); } return s; }
//*************************************************************************** bool Kwave::MP3Decoder::parseID3Tags(ID3_Tag &tag) { if (tag.NumFrames() < 1) return true; // no tags, nothing to do QDate creation_date; QTime creation_time; int year = -1; int month = -1; int day = -1; ID3_Tag::Iterator *it = tag.CreateIterator(); ID3_Frame *frame = 0; Kwave::FileInfo info(metaData()); while (it && (frame = it->GetNext())) { const ID3_FrameID id = frame->GetID(); const Kwave::FileProperty property = m_property_map.property(id); const ID3_PropertyMap::Encoding encoding = m_property_map.encoding(id); switch (encoding) { case ID3_PropertyMap::ENC_TEXT_PARTINSET: { QString s = parseId3Frame2String(frame); int cd = 0; int cds = 0; if (s.contains(QLatin1Char('/'))) { int i = s.indexOf(QLatin1Char('/')); cd = s.left(i).toInt(); cds = s.mid(i + 1).toInt(); } else { cd = s.toInt(); } if (cd > 0) info.set(Kwave::INF_CD , QVariant(cd)); if (cds > 0) info.set(Kwave::INF_CDS, QVariant(cds)); break; } case ID3_PropertyMap::ENC_TRACK_NUM: { QString s = parseId3Frame2String(frame); int track = 0; int tracks = 0; if (s.contains(QLatin1Char('/'))) { int i = s.indexOf(QLatin1Char('/')); track = s.left(i).toInt(); tracks = s.mid(i + 1).toInt(); } else { track = s.toInt(); } if (track > 0) info.set(Kwave::INF_TRACK , QVariant(track)); if (tracks > 0) info.set(Kwave::INF_TRACKS, QVariant(tracks)); break; } case ID3_PropertyMap::ENC_TERMS_OF_USE: // the same as ENC_COMMENT, but without "Description" /* FALLTHROUGH */ case ID3_PropertyMap::ENC_COMMENT: { QString s = parseId3Frame2String(frame); // optionally prepend language char *lang = ID3_GetString(frame, ID3FN_LANGUAGE); if (lang) { s = _("[") + _(lang) + _("] ") + s; ID3_FreeString(lang); } // append to already existing tag, separated by a slash if (info.contains(property)) s = info.get(property).toString() + _(" / ") + s; info.set(property, QVariant(s)); break; } case ID3_PropertyMap::ENC_GENRE_TYPE: { QString s = parseId3Frame2String(frame); int genre = Kwave::GenreType::fromID3(s); if (genre >= 0) s = Kwave::GenreType::name(genre, false); info.set(property, QVariant(s)); break; } case ID3_PropertyMap::ENC_LENGTH: { // length in ms -> convert this to samples QString s = parseId3Frame2String(frame); const double rate = info.rate(); bool ok = false; const double ms = s.toDouble(&ok) + 0.5; if (ok && (rate > 0)) { // NOTE: this overwrites the length found in the header! sample_index_t length = static_cast<sample_index_t>( (rate * ms) / 1000.0); info.setLength(length); } break; } case ID3_PropertyMap::ENC_TEXT_TIMESTAMP: { if (!creation_date.isValid()) { QString s = parseId3Frame2String(frame); switch (id) { case ID3FID_RECORDINGDATES: // should be a ISO 8601 timestamp or similar s = Kwave::string2date(s); if (s.length()) creation_date = QDate::fromString(s, Qt::ISODate); break; case ID3FID_DATE: { // DDMM unsigned int ddmm = s.toUInt(); day = ddmm / 100; month = ddmm % 100; break; } case ID3FID_YEAR: /* FALLTHROUGH */ case ID3FID_ORIGYEAR: // YYYY year = s.toUInt(); break; default: break; } } if (creation_time.isValid()) { switch (id) { case ID3FID_TIME: creation_time = QTime::fromString(_("hhmm")); break; default: break; } } break; } case ID3_PropertyMap::ENC_TEXT_SLASH: { // append to already existing tag, separated by a slash QString s = parseId3Frame2String(frame); if (info.contains(property)) s = info.get(property).toString() + _(" / ") + s; info.set(property, QVariant(s)); break; } case ID3_PropertyMap::ENC_TEXT_URL: /* FALLTHROUGH */ case ID3_PropertyMap::ENC_TEXT: info.set(property, QVariant(parseId3Frame2String(frame))); break; case ID3_PropertyMap::ENC_NONE: /* FALLTHROUGH */ default: { QString s = parseId3Frame2String(frame); qWarning("unsupported ID3 tag: %d, descr: '%s', text: '%s'", id, frame->GetDescription(), DBG(s)); break; } } } /* * try to build a valid creation date/time */ if (!creation_date.isValid()) { // no complete creation date - try to reassemble from found y/m/d creation_date = QDate(year, month, day); } if (creation_date.isValid() && creation_time.isValid()) { // full date + time QDateTime dt(creation_date, creation_time); info.set(Kwave::INF_CREATION_DATE, dt.toString( _("yyyy-MM-ddTHH:mm:ss"))); } else if (creation_date.isValid()) { // date without time info.set(Kwave::INF_CREATION_DATE, creation_date.toString( _("yyyy-MM-dd"))); } else if (year > 0) { // only year creation_date = QDate(year, 1, 1); info.set(Kwave::INF_CREATION_DATE, creation_date.toString(_("yyyy"))); } metaData().replace(Kwave::MetaDataList(info)); return true; }
int main(int argc, char* argv[]) { // Parse any command-line options. namespace po = boost::program_options; po::options_description desc("Allowed options"); desc.add_options() ("help", "show help") ("debug-httpd", po::value<bool>(&mp3d_debug_httpd), "show httpd debug output") ("root", po::value<std::string>(&mp3d_music_root), "root of file system mp3 tree") ("port", po::value<int>(&mp3d_port), "httpd port number") ; po::variables_map args; po::store(po::parse_command_line(argc, argv, desc), args); po::notify(args); if (args.count("help")) { std::cout << desc << std::endl; return 1; } // Index all the mp3s. Paths paths; find_mp3_files(mp3d_music_root, paths); std::cerr << ".mp3 files found: " << paths.size() << std::endl; int old_percentage = -1; size_t id = 0; for (Paths::const_iterator it = paths.begin(); it != paths.end(); ++it) { Mp3Info mp3; mp3.filename = (*it).string(); const ID3_Tag tag(mp3.filename.c_str()); ID3_Tag::ConstIterator* it = tag.CreateIterator(); for (size_t i = 0; i < tag.NumFrames(); ++i) { const ID3_Frame* frame = it->GetNext(); if (frame != 0) { std::string* dst; switch (frame->GetID()) { case ID3FID_ALBUM: dst = &mp3.album; break; case ID3FID_LEADARTIST: dst = &mp3.artist; break; case ID3FID_TITLE: dst = &mp3.title; break; default: continue; } char* text = ID3_GetString(frame, ID3FN_TEXT); dst->assign(text); ID3_FreeString(text); } } // FIXME: maybe a hash, to enable bookmarks? mp3.id = id++; all_mp3s.push_back(mp3); // Show progress. Not really useful when we're not debugging. // FIXME: start the web server straight away, and say "Indexing..." there. const int new_percentage = (100*all_mp3s.size())/paths.size(); if (new_percentage != old_percentage) { std::cout << "\rScanned: " << new_percentage << "%" << std::flush; old_percentage = new_percentage; } } std::cout << "\r.mp3 files scanned: " << all_mp3s.size() << std::endl; // Set up the static files we need to serve. read_static_file("/static/add.png", "/usr/share/icons/gnome/16x16/actions/gtk-add.png"); read_static_file("/static/play.png", "/usr/share/icons/gnome/16x16/actions/gtk-media-play-ltr.png"); read_static_file("/static/remove.png", "/usr/share/icons/gnome/16x16/actions/gtk-remove.png"); static_file_map["/static/site.css"] = make_css(); // Start the mp3 player thread. boost::thread mp3_player_thread(mp3_play_loop); // Start the HTTP server. std::cerr << "Starting HTTP server on port " << mp3d_port << "..." << std::endl; const int mhd_flags = MHD_USE_SELECT_INTERNALLY; MHD_Daemon* daemon = MHD_start_daemon(mhd_flags, mp3d_port, 0, 0, &handle_request, 0, MHD_OPTION_END); if (daemon == 0) { fail("MHD_start_daemon failed!"); } getchar(); // Wait for the user to hit enter. MHD_stop_daemon(daemon); //mp3_player_thread.join(); return 0; }