core/audio: Add a function to load tracks by filepath

If a matching track isn't found in the track database, then use the new
track_alloc_external() function to allocate an external track with the
correct tags.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2017-04-21 11:08:06 -04:00
parent cc464ed198
commit a8f94e9443
4 changed files with 57 additions and 5 deletions

View File

@ -51,8 +51,11 @@ static struct track *__audio_load(struct track *track, unsigned int flags)
GST_STATE_PLAYING : GST_STATE_PAUSED); GST_STATE_PLAYING : GST_STATE_PAUSED);
playlist_played(prev); playlist_played(prev);
if (prev && TRACK_IS_EXTERNAL(prev))
track_free_external(prev);
playlist_selected(track); playlist_selected(track);
if (flags & LOAD_HISTORY) if (flags & LOAD_HISTORY && !TRACK_IS_EXTERNAL(track))
playlist_add(playlist_lookup(PL_SYSTEM, "History"), track); playlist_add(playlist_lookup(PL_SYSTEM, "History"), track);
if (audio_cb) if (audio_cb)
audio_cb->audio_cb_load(track); audio_cb->audio_cb_load(track);
@ -188,7 +191,7 @@ void audio_deinit()
void audio_save() void audio_save()
{ {
if (audio_track) if (audio_track && !TRACK_IS_EXTERNAL(audio_track))
settings_set(SETTINGS_TRACK, track_index(audio_track)); settings_set(SETTINGS_TRACK, track_index(audio_track));
} }
@ -199,6 +202,18 @@ bool audio_load(struct track *track)
return __audio_load(track, LOAD_DEFAULT) != NULL; return __audio_load(track, LOAD_DEFAULT) != NULL;
} }
bool audio_load_filepath(const gchar *filepath)
{
struct track *track;
if (!filepath)
return false;
track = track_lookup(filepath);
if (!track)
track = track_alloc_external(filepath);
return audio_load(track);
}
struct track *audio_cur_track() struct track *audio_cur_track()
{ {
return audio_track; return audio_track;

View File

@ -66,7 +66,7 @@ void playlist_played(struct track *track)
{ {
unsigned int i; unsigned int i;
if (track) { if (track && !TRACK_IS_EXTERNAL(track)) {
for (i = 0; i < PL_MAX_TYPE; i++) for (i = 0; i < PL_MAX_TYPE; i++)
playlist_types[i]->pl_played(track); playlist_types[i]->pl_played(track);
} }
@ -75,7 +75,7 @@ void playlist_played(struct track *track)
void playlist_selected(struct track *track) void playlist_selected(struct track *track)
{ {
unsigned int i; unsigned int i;
if (track) { if (track && !TRACK_IS_EXTERNAL(track)) {
for (i = 0; i < PL_MAX_TYPE; i++) for (i = 0; i < PL_MAX_TYPE; i++)
playlist_types[i]->pl_selected(track); playlist_types[i]->pl_selected(track);

View File

@ -33,8 +33,9 @@ void audio_deinit();
void audio_save(); void audio_save();
/* Called to load a track for playback. */ /* Called to load either a track or file for playback. */
bool audio_load(struct track *); bool audio_load(struct track *);
bool audio_load_filepath(const gchar *);
/* Called to get the current track. */ /* Called to get the current track. */
struct track *audio_cur_track(); struct track *audio_cur_track();

View File

@ -230,6 +230,41 @@ void test_autopause()
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PAUSED); g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PAUSED);
} }
void test_filepath()
{
struct playlist *history = playlist_lookup(PL_SYSTEM, "History");
const gchar *path = "tests/Music/Hyrule Symphony/01 - Title Theme.ogg";
const gchar *path2 = "tests/Music/Ocarina of Time/01 - Title Theme.ogg";
struct track *track = track_lookup(path);
load_count = 0;
playlist_generic_clear(history);
g_assert_false(audio_load_filepath(NULL));
g_assert_false(audio_load_filepath("tests/Music/00 - No Track.ogg"));
g_assert_cmpuint(playlist_size(history), ==, 0);
g_assert_cmpuint(load_count, ==, 0);
g_assert_true(audio_load_filepath(path));
g_assert_cmpuint(load_count, ==, 1);
g_assert(audio_cur_track() == track);
g_assert_cmpuint(playlist_size(history), ==, 1);
g_assert_true(playlist_has(history, audio_cur_track()));
g_assert_true(audio_load_filepath(path2));
g_assert_cmpuint(load_count, ==, 2);
g_assert(audio_cur_track() != track);
g_assert_null(audio_cur_track()->tr_library);
g_assert_cmpuint(playlist_size(history), ==, 1);
g_assert_false(playlist_has(history, audio_cur_track()));
g_assert_true(audio_load_filepath(path));
g_assert_cmpuint(load_count, ==, 3);
g_assert(audio_cur_track() == track);
g_assert_cmpuint(playlist_size(history), ==, 2);
g_assert_true(playlist_has(history, audio_cur_track()));
}
static void test_deinit() static void test_deinit()
{ {
core_deinit(); core_deinit();
@ -247,6 +282,7 @@ int main(int argc, char **argv)
g_test_add_func("/Core/Audio/Next", test_next); g_test_add_func("/Core/Audio/Next", test_next);
g_test_add_func("/Core/Audio/Previous", test_prev); g_test_add_func("/Core/Audio/Previous", test_prev);
g_test_add_func("/Core/Audio/Automatic Pausing", test_autopause); g_test_add_func("/Core/Audio/Automatic Pausing", test_autopause);
g_test_add_func("/Core/Audio/Filepath", test_filepath);
g_test_add_func("/Core/Audio/Deinitialization", test_deinit); g_test_add_func("/Core/Audio/Deinitialization", test_deinit);
return g_test_run(); return g_test_run();
} }