Skip to content

Instantly share code, notes, and snippets.

@vgmoose
Last active September 7, 2022 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vgmoose/93d4c63ee78270ce0ff2649f957f8e17 to your computer and use it in GitHub Desktop.
Save vgmoose/93d4c63ee78270ce0ff2649f957f8e17 to your computer and use it in GitHub Desktop.
Parsing id3 title, album, artist info using mpg123
// returns a size-3 vector of: (title, artist, album)
std::vector<std::string> CST_GetMusicInfo(Mix_Music* music) {
std::vector<std::string> info;
// these three methods require a recent (late 2019) sdl_mixer verison
// info.push_back(Mix_GetMusicTitle(music));
// info.push_back(Mix_GetMusicArtistTag(music));
// info.push_back(Mix_GetMusicAlbumTag(music));
// adapted from https://gist.github.com/deepakjois/988032/640b7a41b0e62a394515697c142777ad3a1b8905
auto m = mpg123_new(NULL, NULL);
mpg123_open(m, "./background.mp3");
mpg123_scan(m);
auto meta = mpg123_meta_check(m);
mpg123_id3v1* v1;
mpg123_id3v2* v2;
if (meta == MPG123_ID3 && mpg123_id3(m, &v1, &v2) == MPG123_OK) {
if (v2 != NULL) {
// fmt.Println("ID3V2 tag found")
info.push_back(v2->title && v2->title->p ? v2->title->p : "");
info.push_back(v2->artist && v2->artist->p ? v2->artist->p : "");
info.push_back(v2->album && v2->album->p ? v2->album->p : "");
return info;
}
if (v1 != NULL) {
// fmt.Println("ID3V1 tag found")
info.push_back(v1->title[0] ? v1->title : "");
info.push_back(v1->artist[0] ? v1->artist : "");
info.push_back(v1->album[0] ? v1->album : "");
return info;
}
}
info.push_back("background.mp3");
info.push_back("");
info.push_back("");
return info;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment