int mpd_put_current_song(char *buffer) { char *cur = buffer; const char *end = buffer + MAX_SIZE; struct mpd_song *song; song = mpd_run_current_song(mpd.conn); if(song == NULL) return 0; cur += json_emit_raw_str(cur, end - cur, "{\"type\": \"song_change\", \"data\":{\"pos\":"); cur += json_emit_int(cur, end - cur, mpd_song_get_pos(song)); cur += json_emit_raw_str(cur, end - cur, ",\"title\":"); cur += json_emit_quoted_str(cur, end - cur, mpd_get_title(song)); if(mpd_song_get_tag(song, MPD_TAG_ARTIST, 0) != NULL) { cur += json_emit_raw_str(cur, end - cur, ",\"artist\":"); cur += json_emit_quoted_str(cur, end - cur, mpd_song_get_tag(song, MPD_TAG_ARTIST, 0)); } if(mpd_song_get_tag(song, MPD_TAG_ALBUM, 0) != NULL) { cur += json_emit_raw_str(cur, end - cur, ",\"album\":"); cur += json_emit_quoted_str(cur, end - cur, mpd_song_get_tag(song, MPD_TAG_ALBUM, 0)); } cur += json_emit_raw_str(cur, end - cur, "}}"); mpd_song_free(song); mpd_response_finish(mpd.conn); return cur - buffer; }
static void song_changed(const struct mpd_song *song) { g_assert(song != NULL); if (mpd_song_get_tag(song, MPD_TAG_ARTIST, 0) == NULL || mpd_song_get_tag(song, MPD_TAG_TITLE, 0) == NULL) { g_message("New song detected with tags missing (%s)", mpd_song_get_uri(song)); g_timer_start(timer); return; } g_timer_start(timer); g_debug("New song detected (%s - %s), id: %u, pos: %u", mpd_song_get_tag(song, MPD_TAG_ARTIST, 0), mpd_song_get_tag(song, MPD_TAG_TITLE, 0), mpd_song_get_id(song), mpd_song_get_pos(song)); as_now_playing(mpd_song_get_tag(song, MPD_TAG_ARTIST, 0), mpd_song_get_tag(song, MPD_TAG_TITLE, 0), mpd_song_get_tag(song, MPD_TAG_ALBUM, 0), mpd_song_get_tag(song, MPD_TAG_MUSICBRAINZ_TRACKID, 0), mpd_song_get_duration(song)); }
static void song_ended(const struct mpd_song *song) { bool long_enough; int elapsed, song_duration, percent_played; GError *error; g_assert(song != NULL); elapsed = g_timer_elapsed(timer, NULL); song_duration = mpd_song_get_duration(song); long_enough = played_long_enough(elapsed, song_duration); percent_played = elapsed * 100 / song_duration; g_debug("Saving old song (%s - %s), id: %u, pos: %u", mpd_song_get_tag(song, MPD_TAG_ARTIST, 0), mpd_song_get_tag(song, MPD_TAG_TITLE, 0), mpd_song_get_id(song), mpd_song_get_pos(song)); error = NULL; if (!db_process(song, long_enough, percent_played, &error)) { g_warning("Saving old song failed: %s", error->message); g_error_free(error); } else if (error != NULL) { g_warning("Skipped saving old song: %s", error->message); g_error_free(error); } }
static void song_changed(const struct mpd_song *song) { g_assert(song != NULL); g_timer_start(timer); g_debug("New song detected (%s - %s), id: %u, pos: %u", mpd_song_get_tag(song, MPD_TAG_ARTIST, 0), mpd_song_get_tag(song, MPD_TAG_TITLE, 0), mpd_song_get_id(song), mpd_song_get_pos(song)); }
static void song_ended(const struct mpd_song *song) { int elapsed; g_assert(song != NULL); elapsed = g_timer_elapsed(timer, NULL); if (mpd_song_get_tag(song, MPD_TAG_ARTIST, 0) == NULL || mpd_song_get_tag(song, MPD_TAG_TITLE, 0) == NULL) { g_message("Song (%s) has missing tags, skipping", mpd_song_get_uri(song)); return; } else if (!played_long_enough(elapsed, mpd_song_get_duration(song))) { g_message("Song (%s - %s), id: %u, pos: %u not played long enough, skipping", mpd_song_get_tag(song, MPD_TAG_ARTIST, 0), mpd_song_get_tag(song, MPD_TAG_TITLE, 0), mpd_song_get_id(song), mpd_song_get_pos(song)); return; } g_debug("Submitting old song (%s - %s), id: %u, pos: %u", mpd_song_get_tag(song, MPD_TAG_ARTIST, 0), mpd_song_get_tag(song, MPD_TAG_TITLE, 0), mpd_song_get_id(song), mpd_song_get_pos(song)); /* FIXME: libmpdclient doesn't have any way to fetch the musicbrainz id. */ as_songchange(mpd_song_get_uri(song), mpd_song_get_tag(song, MPD_TAG_ARTIST, 0), mpd_song_get_tag(song, MPD_TAG_TITLE, 0), mpd_song_get_tag(song, MPD_TAG_ALBUM, 0), mpd_song_get_tag(song, MPD_TAG_MUSICBRAINZ_TRACKID, 0), mpd_song_get_duration(song) > 0 ? mpd_song_get_duration(song) : g_timer_elapsed(timer, NULL), NULL); }
static void song_changed(const struct mpd_song *song) { g_message("new song detected (%s - %s), id: %u, pos: %u\n", mpd_song_get_tag(song, MPD_TAG_ARTIST, 0), mpd_song_get_tag(song, MPD_TAG_TITLE, 0), mpd_song_get_id(song), mpd_song_get_pos(song)); g_timer_start(timer); as_now_playing(mpd_song_get_tag(song, MPD_TAG_ARTIST, 0), mpd_song_get_tag(song, MPD_TAG_TITLE, 0), mpd_song_get_tag(song, MPD_TAG_ALBUM, 0), mpd_song_get_tag(song, MPD_TAG_TRACK, 0), mpd_song_get_tag(song, MPD_TAG_MUSICBRAINZ_TRACKID, 0), mpd_song_get_duration(song)); }
int mpd_put_queue(char *buffer, unsigned int offset) { char *cur = buffer; const char *end = buffer + MAX_SIZE; struct mpd_entity *entity; if (!mpd_send_list_queue_range_meta(mpd.conn, offset, offset+MAX_ELEMENTS_PER_PAGE)) RETURN_ERROR_AND_RECOVER("mpd_send_list_queue_meta"); cur += json_emit_raw_str(cur, end - cur, "{\"type\":\"queue\",\"data\":[ "); while((entity = mpd_recv_entity(mpd.conn)) != NULL) { const struct mpd_song *song; if(mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) { song = mpd_entity_get_song(entity); cur += json_emit_raw_str(cur, end - cur, "{\"id\":"); cur += json_emit_int(cur, end - cur, mpd_song_get_id(song)); cur += json_emit_raw_str(cur, end - cur, ",\"pos\":"); cur += json_emit_int(cur, end - cur, mpd_song_get_pos(song)); cur += json_emit_raw_str(cur, end - cur, ",\"duration\":"); cur += json_emit_int(cur, end - cur, mpd_song_get_duration(song)); cur += json_emit_raw_str(cur, end - cur, ",\"artist\":"); cur += json_emit_quoted_str(cur, end - cur, mpd_get_artist(song)); cur += json_emit_raw_str(cur, end - cur, ",\"album\":"); cur += json_emit_quoted_str(cur, end - cur, mpd_get_album(song)); cur += json_emit_raw_str(cur, end - cur, ",\"title\":"); cur += json_emit_quoted_str(cur, end - cur, mpd_get_title(song)); cur += json_emit_raw_str(cur, end - cur, "},"); } mpd_entity_free(entity); } /* remove last ',' */ cur--; cur += json_emit_raw_str(cur, end - cur, "]}"); return cur - buffer; }
static void env_export_song(struct mpd_song *song) { const char *tag; char *envstr; time_t t; char date[DEFAULT_DATE_FORMAT_SIZE] = { 0 }; g_setenv("MPD_SONG_URI", mpd_song_get_uri(song), 1); t = mpd_song_get_last_modified(song); strftime(date, DEFAULT_DATE_FORMAT_SIZE, DEFAULT_DATE_FORMAT, localtime(&t)); g_setenv("MPD_SONG_LAST_MODIFIED", date, 1); envstr = g_strdup_printf("%u", mpd_song_get_duration(song)); g_setenv("MPD_SONG_DURATION", envstr, 1); g_free(envstr); envstr = g_strdup_printf("%u", mpd_song_get_pos(song)); g_setenv("MPD_SONG_POS", envstr, 1); g_free(envstr); envstr = g_strdup_printf("%u", mpd_song_get_id(song)); g_setenv("MPD_SONG_ID", envstr, 1); g_free(envstr); /* Export tags. FIXME: For now we just export the first tag value to * the environment. */ if ((tag = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0)) != NULL) g_setenv("MPD_SONG_TAG_ARTIST", tag, 1); if ((tag = mpd_song_get_tag(song, MPD_TAG_ALBUM, 0)) != NULL) g_setenv("MPD_SONG_TAG_ALBUM", tag, 1); if ((tag = mpd_song_get_tag(song, MPD_TAG_ALBUM_ARTIST, 0)) != NULL) g_setenv("MPD_SONG_TAG_ALBUM_ARTIST", tag, 1); if ((tag = mpd_song_get_tag(song, MPD_TAG_TITLE, 0)) != NULL) g_setenv("MPD_SONG_TAG_TITLE", tag, 1); if ((tag = mpd_song_get_tag(song, MPD_TAG_TRACK, 0)) != NULL) g_setenv("MPD_SONG_TAG_TRACK", tag, 1); if ((tag = mpd_song_get_tag(song, MPD_TAG_NAME, 0)) != NULL) g_setenv("MPD_SONG_TAG_NAME", tag, 1); if ((tag = mpd_song_get_tag(song, MPD_TAG_GENRE, 0)) != NULL) g_setenv("MPD_SONG_TAG_GENRE", tag, 1); if ((tag = mpd_song_get_tag(song, MPD_TAG_DATE, 0)) != NULL) g_setenv("MPD_SONG_TAG_DATE", tag, 1); if ((tag = mpd_song_get_tag(song, MPD_TAG_COMPOSER, 0)) != NULL) g_setenv("MPD_SONG_TAG_COMPOSER", tag, 1); if ((tag = mpd_song_get_tag(song, MPD_TAG_PERFORMER, 0)) != NULL) g_setenv("MPD_SONG_TAG_PERFORMER", tag, 1); if ((tag = mpd_song_get_tag(song, MPD_TAG_COMMENT, 0)) != NULL) g_setenv("MPD_SONG_TAG_COMMENT", tag, 1); if ((tag = mpd_song_get_tag(song, MPD_TAG_DISC, 0)) != NULL) g_setenv("MPD_SONG_TAG_DISC", tag, 1); if ((tag = mpd_song_get_tag(song, MPD_TAG_MUSICBRAINZ_ARTISTID, 0)) != NULL) g_setenv("MPD_SONG_TAG_MUSICBRAINZ_ARTISTID", tag, 1); if ((tag = mpd_song_get_tag(song, MPD_TAG_MUSICBRAINZ_ALBUMID, 0)) != NULL) g_setenv("MPD_SONG_TAG_MUSICBRAINZ_ALBUMID", tag, 1); if ((tag = mpd_song_get_tag(song, MPD_TAG_MUSICBRAINZ_ALBUMARTISTID, 0)) != NULL) g_setenv("MPD_SONG_TAG_MUSICBRAINZ_ALBUMARTISTID", tag, 1); if ((tag = mpd_song_get_tag(song, MPD_TAG_MUSICBRAINZ_TRACKID, 0)) != NULL) g_setenv("MPD_SONG_TAG_MUSICBRAINZ_TRACKID", tag, 1); }
int main(int argc, char ** argv) { struct mpd_connection *conn; conn = mpd_connection_new(NULL, 0, 30000); if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) return handle_error(conn); { int i; for(i=0;i<3;i++) { printf("version[%i]: %i\n",i, mpd_connection_get_server_version(conn)[i]); } } if(argc==1) { struct mpd_status * status; struct mpd_song *song; const struct mpd_audio_format *audio_format; mpd_command_list_begin(conn, true); mpd_send_status(conn); mpd_send_current_song(conn); mpd_command_list_end(conn); status = mpd_recv_status(conn); if (status == NULL) return handle_error(conn); printf("volume: %i\n", mpd_status_get_volume(status)); printf("repeat: %i\n", mpd_status_get_repeat(status)); printf("queue version: %u\n", mpd_status_get_queue_version(status)); printf("queue length: %i\n", mpd_status_get_queue_length(status)); if (mpd_status_get_error(status) != NULL) printf("error: %s\n", mpd_status_get_error(status)); if (mpd_status_get_state(status) == MPD_STATE_PLAY || mpd_status_get_state(status) == MPD_STATE_PAUSE) { printf("song: %i\n", mpd_status_get_song_pos(status)); printf("elaspedTime: %i\n",mpd_status_get_elapsed_time(status)); printf("elasped_ms: %u\n", mpd_status_get_elapsed_ms(status)); printf("totalTime: %i\n", mpd_status_get_total_time(status)); printf("bitRate: %i\n", mpd_status_get_kbit_rate(status)); } audio_format = mpd_status_get_audio_format(status); if (audio_format != NULL) { printf("sampleRate: %i\n", audio_format->sample_rate); printf("bits: %i\n", audio_format->bits); printf("channels: %i\n", audio_format->channels); } mpd_status_free(status); if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) return handle_error(conn); mpd_response_next(conn); while ((song = mpd_recv_song(conn)) != NULL) { printf("uri: %s\n", mpd_song_get_uri(song)); print_tag(song, MPD_TAG_ARTIST, "artist"); print_tag(song, MPD_TAG_ALBUM, "album"); print_tag(song, MPD_TAG_TITLE, "title"); print_tag(song, MPD_TAG_TRACK, "track"); print_tag(song, MPD_TAG_NAME, "name"); print_tag(song, MPD_TAG_DATE, "date"); if (mpd_song_get_duration(song) > 0) { printf("time: %u\n", mpd_song_get_duration(song)); } printf("pos: %u\n", mpd_song_get_pos(song)); mpd_song_free(song); } if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS || !mpd_response_finish(conn)) return handle_error(conn); } else if(argc==3 && strcmp(argv[1],"lsinfo")==0) { struct mpd_entity * entity; if (!mpd_send_list_meta(conn, argv[2])) return handle_error(conn); while ((entity = mpd_recv_entity(conn)) != NULL) { const struct mpd_song *song; const struct mpd_directory *dir; const struct mpd_playlist *pl; switch (mpd_entity_get_type(entity)) { case MPD_ENTITY_TYPE_UNKNOWN: break; case MPD_ENTITY_TYPE_SONG: song = mpd_entity_get_song(entity); printf("uri: %s\n", mpd_song_get_uri(song)); print_tag(song, MPD_TAG_ARTIST, "artist"); print_tag(song, MPD_TAG_ALBUM, "album"); print_tag(song, MPD_TAG_TITLE, "title"); print_tag(song, MPD_TAG_TRACK, "track"); break; case MPD_ENTITY_TYPE_DIRECTORY: dir = mpd_entity_get_directory(entity); printf("directory: %s\n", mpd_directory_get_path(dir)); break; case MPD_ENTITY_TYPE_PLAYLIST: pl = mpd_entity_get_playlist(entity); printf("playlist: %s\n", mpd_playlist_get_path(pl)); break; } mpd_entity_free(entity); } if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS || !mpd_response_finish(conn)) return handle_error(conn); } else if(argc==2 && strcmp(argv[1],"artists")==0) { struct mpd_pair *pair; if (!mpd_search_db_tags(conn, MPD_TAG_ARTIST) || !mpd_search_commit(conn)) return handle_error(conn); while ((pair = mpd_recv_pair_tag(conn, MPD_TAG_ARTIST)) != NULL) { printf("%s\n", pair->value); mpd_return_pair(conn, pair); } if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS || !mpd_response_finish(conn)) return handle_error(conn); } else if (argc == 2 && strcmp(argv[1], "playlists") == 0) { if (!mpd_send_list_playlists(conn)) return handle_error(conn); struct mpd_playlist *playlist; while ((playlist = mpd_recv_playlist(conn)) != NULL) { printf("%s\n", mpd_playlist_get_path(playlist)); mpd_playlist_free(playlist); } if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS || !mpd_response_finish(conn)) return handle_error(conn); } else if (argc == 2 && strcmp(argv[1], "idle") == 0) { enum mpd_idle idle = mpd_run_idle(conn); if (idle == 0 && mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) return handle_error(conn); for (unsigned j = 0;; ++j) { enum mpd_idle i = 1 << j; const char *name = mpd_idle_name(i); if (name == NULL) break; if (idle & i) printf("%s\n", name); } } else if (argc == 3 && strcmp(argv[1], "subscribe") == 0) { /* subscribe to a channel and print all messages */ if (!mpd_run_subscribe(conn, argv[2])) return handle_error(conn); while (mpd_run_idle_mask(conn, MPD_IDLE_MESSAGE) != 0) { if (!mpd_send_read_messages(conn)) return handle_error(conn); struct mpd_message *msg; while ((msg = mpd_recv_message(conn)) != NULL) { printf("%s\n", mpd_message_get_text(msg)); mpd_message_free(msg); } if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS || !mpd_response_finish(conn)) return handle_error(conn); } return handle_error(conn); } else if (argc == 2 && strcmp(argv[1], "channels") == 0) { /* print a list of channels */ if (!mpd_send_channels(conn)) return handle_error(conn); struct mpd_pair *pair; while ((pair = mpd_recv_channel_pair(conn)) != NULL) { printf("%s\n", pair->value); mpd_return_pair(conn, pair); } if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS || !mpd_response_finish(conn)) return handle_error(conn); } else if (argc == 4 && strcmp(argv[1], "message") == 0) { /* send a message to a channel */ if (!mpd_run_send_message(conn, argv[2], argv[3])) return handle_error(conn); } mpd_connection_free(conn); return 0; }
unsigned Song::getPosition() const { assert(m_song); return mpd_song_get_pos(m_song.get()); }