tagdb: Find tags for each track
Signed-off-by: Anna Schumaker <anna@ocarinaproject.net>
This commit is contained in:
parent
f207ed94df
commit
72cea29c61
|
@ -75,6 +75,9 @@ public:
|
|||
|
||||
|
||||
class Track : public DatabaseEntry {
|
||||
private:
|
||||
void set_length_str();
|
||||
|
||||
public:
|
||||
Library *library;
|
||||
Artist *artist;
|
||||
|
|
50
lib/tags.cpp
50
lib/tags.cpp
|
@ -6,7 +6,8 @@
|
|||
#include <filter.h>
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include <taglib/tag.h>
|
||||
#include <taglib/fileref.h>
|
||||
|
||||
Database<Artist> artist_db("artist.db", true);
|
||||
Database<Album> album_db("album.db", true);
|
||||
|
@ -151,10 +152,11 @@ void Library :: write(File &f)
|
|||
*
|
||||
*/
|
||||
|
||||
Track :: Track() {}
|
||||
Track :: Track() : library(NULL), artist(NULL), album(NULL), genre(NULL){}
|
||||
|
||||
Track :: Track(const std::string &f, Library *l)
|
||||
: library(l), play_count(0), last_year(0), last_month(0), last_day(0),
|
||||
: library(l), artist(NULL), album(NULL), genre(NULL),
|
||||
play_count(0), last_year(0), last_month(0), last_day(0),
|
||||
filepath(f.substr(l->root_path.size() + 1))
|
||||
{
|
||||
}
|
||||
|
@ -172,8 +174,50 @@ void Track :: write(File &f)
|
|||
{
|
||||
}
|
||||
|
||||
void Track :: set_length_str()
|
||||
{
|
||||
std::stringstream ss;
|
||||
unsigned int minutes = length / 60;
|
||||
unsigned int seconds = length % 60;
|
||||
|
||||
ss << minutes << ":";
|
||||
if (seconds < 10)
|
||||
ss << "0";
|
||||
ss << seconds;
|
||||
length_str = ss.str();
|
||||
}
|
||||
|
||||
static inline const std::string format_tag(const TagLib::String &str)
|
||||
{
|
||||
return str.stripWhiteSpace().to8Bit(true);
|
||||
}
|
||||
|
||||
void Track :: tag()
|
||||
{
|
||||
TagLib :: Tag *tag;
|
||||
TagLib :: AudioProperties *audio;
|
||||
TagLib :: FileRef ref(path().c_str(), true, TagLib::AudioProperties::Fast);
|
||||
|
||||
if (ref.isNull()) {
|
||||
print("ERROR: Could not read tags for file %s\n", path().c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
tag = ref.tag();
|
||||
audio = ref.audioProperties();
|
||||
|
||||
artist = artist_db.insert(Artist(format_tag(tag->artist())));
|
||||
album = album_db.insert(Album(format_tag(tag->album()), tag->year()));
|
||||
genre = genre_db.insert(Genre(format_tag(tag->genre())));
|
||||
track = tag->track();
|
||||
length = audio->length();
|
||||
title = format_tag(tag->title());
|
||||
|
||||
title_lower = filter :: add(title, id);
|
||||
set_length_str();
|
||||
|
||||
filter :: add(artist->name, id);
|
||||
filter :: add(album->name, id);
|
||||
}
|
||||
|
||||
const std::string Track :: path() const
|
||||
|
|
|
@ -3,9 +3,11 @@
|
|||
* Test a DatabaseEntry
|
||||
*/
|
||||
|
||||
#include <tags.h>
|
||||
#include <filter.h>
|
||||
#include <print.h>
|
||||
#include <tags.h>
|
||||
|
||||
#include <set>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
@ -129,12 +131,66 @@ void track_test_tags(Track &track)
|
|||
test_results(track.last_day == 0, __LINE__);
|
||||
}
|
||||
|
||||
void track_test()
|
||||
void track_test_search(const std::string &term, unsigned int expected,
|
||||
unsigned int line)
|
||||
{
|
||||
std::set<unsigned int> s;
|
||||
filter :: search(term, s);
|
||||
test_results(s.size() == 1 && s.find(expected) != s.end(), line);
|
||||
}
|
||||
|
||||
void track_test_basics(Library *library)
|
||||
{
|
||||
Library *library = tagdb :: add_library("Music");
|
||||
Track track("Music/1.ogg", library);
|
||||
|
||||
track_test_tags(track);
|
||||
test_results(track.library == library, __LINE__);
|
||||
test_results(track.artist == NULL, __LINE__);
|
||||
test_results(track.album == NULL, __LINE__);
|
||||
test_results(track.genre == NULL, __LINE__);
|
||||
|
||||
track.tag();
|
||||
|
||||
track_test_tags(track);
|
||||
test_results(track.library == library, __LINE__);
|
||||
test_results(track.artist->name == "Artist", __LINE__);
|
||||
test_results(track.album->name == "Album", __LINE__);
|
||||
test_results(track.album->year == 2014, __LINE__);
|
||||
test_results(track.genre->name == "Silence", __LINE__);
|
||||
test_results(track.track == 1, __LINE__);
|
||||
test_results(track.length == 1, __LINE__);
|
||||
test_results(track.title == "One", __LINE__);
|
||||
test_results(track.title_lower == "one", __LINE__);
|
||||
|
||||
track_test_search("One", track.id, __LINE__);
|
||||
track_test_search("Artist", track.id, __LINE__);
|
||||
track_test_search("Album", track.id, __LINE__);
|
||||
}
|
||||
|
||||
void track_test_lenstrs(const std::string &file, const std::string &expected,
|
||||
Library *library, unsigned int line)
|
||||
{
|
||||
Track track(file, library);
|
||||
track.tag();
|
||||
test_results(track.length_str == expected, line);
|
||||
}
|
||||
|
||||
void track_test_extras(Library *library)
|
||||
{
|
||||
track_test_lenstrs("Music/1.ogg", "0:01", library, __LINE__);
|
||||
track_test_lenstrs("Music/10.ogg", "0:10", library, __LINE__);
|
||||
track_test_lenstrs("Music/15.ogg", "0:15", library, __LINE__);
|
||||
track_test_lenstrs("Music/60.ogg", "1:00", library, __LINE__);
|
||||
track_test_lenstrs("Music/90.ogg", "1:30", library, __LINE__);
|
||||
track_test_lenstrs("Music/600.ogg", "10:00", library, __LINE__);
|
||||
track_test_lenstrs("Music/666.ogg", "11:06", library, __LINE__);
|
||||
}
|
||||
|
||||
void track_test()
|
||||
{
|
||||
Library *library = tagdb :: add_library("Music");
|
||||
track_test_basics(library);
|
||||
track_test_extras(library);
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue