Music Information Retrieval Made Simple With LastFM API DLL
A couple of months back I started developing a jukebox-like MP3 player for a radio station in Iași. Given the fact that, just like an audiophile, a radio station requires some serious information about the music it plays, I implemented a method for querying Last FM in search of such data. Just let the Last FM server know what artist and album the track you’re playing is on and it will take care of the rest for you.
Warning: before getting any further, you should know that the DLL was written in Delphi, as it is the only development language I’m fluent in. However, since a DLL practically works with any COM-based programming language, you could also use it in any environment that can interact with DLLs. However, the example and explanation will only rely on Delphi code.
The attached DLL exports contains functions for all the pieces of information related to the album itself. I will be talking about each of them in the lines below.
Exported functions:
function InitializeAPI (const apikey: string) : boolean;
procedure GetAlbumInfo (album: string; artist: string);
function GetArtistMBID : string;
function GetAlbumURL : string;
function GetAlbumReleaseDate : string;
function GetAlbumSmallImg : string;
function GetAlbumMedImg : string;
function GetAlbumLargeImg : string;
function GetAlbumExtraLargeImg : string;
function GetAlbumListeners : string;
function GetPlayCount : string;
function GetAlbumTrackList : TStringList;
More about the DLL
First and foremost, in order to be able to communicate with the web server, you need to sign up for a LastFM API account. You will get an API key which will identify your application and will offer both Last FM and your user a couple of details about it, such as its homepage, its name and its icon.
After you have received the API Key (which looks like this 9b2bdb90bfbaa85bcf30e74ea1a78225
for my application), we’ll pass it to the DLL in the calling application. Remember, you have to do this only once, and should also be done before you get to call any other function in the DLL. Otherwise, the LastFM request will fail.
procedure TMainForm.FormCreate(Sender: TObject);
begin
//don't forget to change the API key with yours
Initialize('9b2bdb90bfbaa85bcf30e74ea1a78225');
end;
OK, now that we have successfully identified ourselves against the LastFM infrastructure, we are ready to perform other queries. The following line gets all the info on the specific album:
procedure TMainForm.Button2Click(Sender: TObject);
begin
GetAlbumInfo (edArtist.Text, edAlbum.Text);
end;
One thing you should pay special attention to is spelling and accuracy. If you misspell the artist’s or album name you might get inaccurate or no results. After you have called the GetAlbumInfo
procedure with the artist’s and album’s names, you will have all the information available in the following variables, as described in the snippet below:
procedure TMainForm.Button2Click(Sender: TObject);
begin
GetAlbumInfo (edArtist.Text, edAlbum.Text);
label9.Caption := 'Number of plays: '+ GetPlayCount;
label8.Caption := 'Album Listeners: ' + GetAlbumListeners;
label1.Caption := 'Artist MBID: ' + GetArtistMBID;
label2.caption := 'Album URL: ' + GetAlbumURL;
label2.Hint := GetAlbumURL;
label3.Caption := 'Release date: ' + GetAlbumReleaseDate;
edSmallImgURL.Text := GetAlbumSmallImg;
edMedImgURL.Text := GetAlbumMedImg;
edLargeImgURL.Text := GetAlbumLargeImg;
edExtraLargeURL.Text := GetAlbumExtraLargeImg;
WebImage.URL := GetAlbumLargeImg;
WebImage.WebPicture.LoadFromURL(GetAlbumLargeImg);
ListView.Clear;
AddTracksToListView(GetAlbumTrackList);
end;
Now that you’ve seen the basics, you might want to take a look at the included demo application. This release only covers the basics of album and artist information retrieval, but the subsequent releases of the DLL will bring extra features.
Subscribe to Lex Talionis
Get the latest posts delivered right to your inbox