Skip to content

Commit

Permalink
grab song title from online radio
Browse files Browse the repository at this point in the history
  • Loading branch information
artemanufrij committed Dec 31, 2017
1 parent 6f5d775 commit 5c62e8a
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 53 deletions.
92 changes: 54 additions & 38 deletions src/Interfaces/SoundIndicator.vala
Original file line number Diff line number Diff line change
Expand Up @@ -91,51 +91,18 @@ namespace PlayMyMusic.Interfaces {
[DBus(name = "org.mpris.MediaPlayer2.Player")]
public class SoundIndicatorPlayer : GLib.Object {
PlayMyMusic.Services.Player player;
PlayMyMusic.Services.TagManager tg_manager;
DBusConnection connection;
PlayMyMusic.PlayMyMusicApp app;

public SoundIndicatorPlayer (DBusConnection connection) {
this.app = PlayMyMusic.PlayMyMusicApp.instance;
this.connection = connection;
player = PlayMyMusic.Services.Player.instance;
player.state_changed.connect_after ((state) => {
Variant property;
switch (state) {
case Gst.State.PLAYING:
property = "Playing";
var metadata = new HashTable<string, Variant> (null, null);
if (player.current_track != null) {
if (player.play_mode == PlayMyMusic.Services.PlayMode.AUDIO_CD) {
metadata.insert("xesam:title", player.current_track.title);
metadata.insert("xesam:artist", get_simple_string_array (player.current_track.audio_cd.title));
} else {
var file = File.new_for_path (player.current_track.album.cover_path);
metadata.insert("mpris:artUrl", file.get_uri ());
metadata.insert("xesam:title", player.current_track.title);
metadata.insert("xesam:artist", get_simple_string_array (player.current_track.album.artist.name));
}
} else if (player.current_radio != null) {
var file = File.new_for_path (player.current_radio.cover_path);
metadata.insert("mpris:artUrl", file.get_uri ());
metadata.insert("xesam:title", player.current_radio.title);
metadata.insert("xesam:artist", get_simple_string_array (player.current_radio.url));
}
send_properties ("Metadata", metadata);
break;
case Gst.State.PAUSED:
property = "Paused";
break;
default:
property = "Stopped";
var metadata = new HashTable<string, Variant> (null, null);
metadata.insert("mpris:artUrl", "");
metadata.insert("xesam:title", "");
metadata.insert("xesam:artist", new string [0]);
send_properties ("Metadata", metadata);
break;
}
send_properties ("PlaybackStatus", property);
});
player.state_changed.connect_after (player_state_changed);

tg_manager = PlayMyMusic.Services.TagManager.instance;
tg_manager.discovered_new_radio_content.connect (radio_content);
}

private static string[] get_simple_string_array (string text) {
Expand Down Expand Up @@ -188,5 +155,54 @@ namespace PlayMyMusic.Interfaces {
app.mainwindow.prev ();
}

private void player_state_changed (Gst.State state) {
Variant property;
switch (state) {
case Gst.State.PLAYING:
property = "Playing";
var metadata = new HashTable<string, Variant> (null, null);
if (player.current_track != null) {
if (player.play_mode == PlayMyMusic.Services.PlayMode.AUDIO_CD) {
metadata.insert("xesam:title", player.current_track.title);
metadata.insert("xesam:artist", get_simple_string_array (player.current_track.audio_cd.title));
} else {
var file = File.new_for_path (player.current_track.album.cover_path);
metadata.insert("mpris:artUrl", file.get_uri ());
metadata.insert("xesam:title", player.current_track.title);
metadata.insert("xesam:artist", get_simple_string_array (player.current_track.album.artist.name));
}
} else if (player.current_radio != null) {
var file = File.new_for_path (player.current_radio.cover_path);
metadata.insert("mpris:artUrl", file.get_uri ());
metadata.insert("xesam:title", player.current_radio.title);
metadata.insert("xesam:artist", get_simple_string_array (player.current_radio.url));
}
send_properties ("Metadata", metadata);
break;
case Gst.State.PAUSED:
property = "Paused";
break;
default:
property = "Stopped";
var metadata = new HashTable<string, Variant> (null, null);
metadata.insert("mpris:artUrl", "");
metadata.insert("xesam:title", "");
metadata.insert("xesam:artist", new string [0]);
send_properties ("Metadata", metadata);
break;
}
send_properties ("PlaybackStatus", property);
}

private void radio_content (string uri, string content) {
if (player.current_radio != null && player.current_radio.file == uri) {
var metadata = new HashTable<string, Variant> (null, null);
var file = File.new_for_path (player.current_radio.cover_path);
metadata.insert("mpris:artUrl", file.get_uri ());
metadata.insert("xesam:title", player.current_radio.title);
metadata.insert("xesam:artist", get_simple_string_array (content));
send_properties ("Metadata", metadata);
}
}
}
}
5 changes: 3 additions & 2 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ namespace PlayMyMusic {
});

library_manager = PlayMyMusic.Services.LibraryManager.instance;
library_manager.tag_discover_started.connect (() => {
library_manager.sync_started.connect (() => {
Idle.add (() => {
spinner.active = true;
menu_item_resync.sensitive = false;
menu_item_reset.sensitive = false;
return false;
});
});
library_manager.tag_discover_finished.connect (() => {
library_manager.sync_started.connect (() => {
Idle.add (() => {
spinner.active = false;
menu_item_resync.sensitive = true;
Expand Down Expand Up @@ -144,6 +144,7 @@ namespace PlayMyMusic {
previous_button.sensitive = false;
next_button.sensitive = false;
} else if (library_manager.player.current_radio != null) {

headerbar.title = library_manager.player.current_radio.title;
previous_button.sensitive = false;
next_button.sensitive = false;
Expand Down
8 changes: 4 additions & 4 deletions src/Services/LibraryManager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ namespace PlayMyMusic.Services {
}
}

public signal void tag_discover_started ();
public signal void tag_discover_finished ();
public signal void sync_started ();
public signal void sync_finished ();
public signal void added_new_artist (PlayMyMusic.Objects.Artist artist);
public signal void added_new_album (PlayMyMusic.Objects.Album album);
public signal void added_new_playlist (PlayMyMusic.Objects.Playlist playlist);
Expand Down Expand Up @@ -83,8 +83,7 @@ namespace PlayMyMusic.Services {

tg_manager = PlayMyMusic.Services.TagManager.instance;
tg_manager.discovered_new_item.connect (discovered_new_local_item);
tg_manager.discover_started.connect ( () => { tag_discover_started (); });
tg_manager.discover_finished.connect ( () => { tag_discover_finished (); });
tg_manager.discover_finished.connect (() => { sync_finished (); });

db_manager = PlayMyMusic.Services.DataBaseManager.instance;
db_manager.added_new_artist.connect ((artist) => { added_new_artist (artist); });
Expand Down Expand Up @@ -151,6 +150,7 @@ namespace PlayMyMusic.Services {
// LOCAL FILES REGION
public async void sync_library_content () {
new Thread <void*> (null, () => {
sync_started ();
remove_non_existent_items ();
scan_local_library_for_new_files (settings.library_location);
return null;
Expand Down
30 changes: 27 additions & 3 deletions src/Services/Player.vala
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ namespace PlayMyMusic.Services {
}
}

public signal void state_changed (Gst.State state);
public signal void current_progress_changed (double percent);
public signal void current_duration_changed (int64 duration);
uint progress_timer = 0;
uint radio_tag_grabber_timer = 0;

PlayMyMusic.Settings settings;

Expand Down Expand Up @@ -88,8 +90,6 @@ namespace PlayMyMusic.Services {

public double target_progress { get; set; default = 0; }

public signal void state_changed (Gst.State state);

private Player () {
settings = PlayMyMusic.Settings.get_default ();
playbin = Gst.ElementFactory.make ("playbin", "play");
Expand All @@ -103,11 +103,16 @@ namespace PlayMyMusic.Services {
playbin.set_state (state);
} else {
stop_progress_signal ();
stop_radio_grabber ();
Interfaces.Inhibitor.instance.uninhibit ();
}
switch (state) {
case Gst.State.PLAYING:
start_progress_signal ();
if (play_mode == PlayMode.RADIO) {
start_radio_grabber ();
} else {
start_progress_signal ();
}
Interfaces.Inhibitor.instance.inhibit ();
break;
case Gst.State.READY:
Expand Down Expand Up @@ -142,6 +147,25 @@ namespace PlayMyMusic.Services {
});
}

public void stop_radio_grabber () {
if (radio_tag_grabber_timer != 0) {
Source.remove (radio_tag_grabber_timer);
radio_tag_grabber_timer = 0;
}
}

public void start_radio_grabber () {
PlayMyMusic.Services.LibraryManager.instance.tg_manager.add_discover_uri (current_radio.file);
radio_tag_grabber_timer = GLib.Timeout.add (3000, () => {
if (current_radio == null) {
stop_radio_grabber ();
return false;
}
PlayMyMusic.Services.LibraryManager.instance.tg_manager.add_discover_uri (current_radio.file);
return true;
});
}

public void set_radio (PlayMyMusic.Objects.Radio? radio) {
if (radio == current_radio || radio == null || radio.file == null) {
return;
Expand Down
36 changes: 30 additions & 6 deletions src/Services/TagManager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace PlayMyMusic.Services {
}

public signal void discovered_new_item (PlayMyMusic.Objects.Artist artist, PlayMyMusic.Objects.Album album, PlayMyMusic.Objects.Track track);
public signal void discovered_new_radio_content (string radio_uri, string content);
public signal void discover_started ();
public signal void discover_finished ();

Expand All @@ -59,16 +60,38 @@ namespace PlayMyMusic.Services {

private void discovered (Gst.PbUtils.DiscovererInfo info, Error err) {
new Thread<void*> (null, () => {
string uri = info.get_uri ();
if (info.get_result () != Gst.PbUtils.DiscovererResult.OK) {
warning ("DISCOVER ERROR: '%d' %s %s\n(%s)", err.code, err.message, info.get_result ().to_string (), info.get_uri ());

} else if (uri.has_prefix ("http")) {
var tags = info.get_tags ();
string o;
if (tags.get_string (Gst.Tags.TITLE, out o)) {
discovered_new_radio_content (uri, o);
return null;
}

if (tags.get_string (Gst.Tags.ARTIST, out o)) {
discovered_new_radio_content (uri, o);
return null;
}

if (tags.get_string (Gst.Tags.ALBUM_ARTIST, out o)) {
discovered_new_radio_content (uri, o);
return null;
}

if (tags.get_string (Gst.Tags.ALBUM, out o)) {
discovered_new_radio_content (uri, o);
return null;
}
} else {
var tags = info.get_tags ();
if (tags != null) {
string uri = info.get_uri ();

uint64 duration = info.get_duration ();
File f = File.new_for_uri (uri);

string o;
GLib.Date? d;
Gst.DateTime? dt;
Expand All @@ -85,7 +108,6 @@ namespace PlayMyMusic.Services {
if (track.title.strip () == "") {
track.title = f.get_basename ();
}

if (tags.get_uint (Gst.Tags.TRACK_NUMBER, out u)) {
track.track = (int)u;
}
Expand Down Expand Up @@ -155,10 +177,12 @@ namespace PlayMyMusic.Services {
}

public void add_discover_uri (string uri) {
if (discover_counter == 0) {
discover_started ();
if (!uri.has_prefix ("http")) {
if (discover_counter == 0) {
discover_started ();
}
discover_counter++;
}
discover_counter++;
discoverer.discover_uri_async (uri);
}
}
Expand Down

0 comments on commit 5c62e8a

Please sign in to comment.