Note that this Wiki is a work in progress, items may not be updated or may disappear entirely as the pages are updated.

Script to Fix Song Titles

From SpacialAudio

Jump to: navigation, search

Title repair script for SAM: This handy little utility repairs song titles in your database by applying the appropriate capitalization. I have tested it on about 400 songs and it worked like a champ. I currently have it set up to process one song roughly every 2-4 seconds so as not to interfere with SAM's song play. I did try to run it on my 4000 song library using completely locked execution and it began to hiccup on song playback while it processed the data and wrote to the database.

To alleviate some processing power I added a line that checks to see if the new song title actually ended up different from the original. Only when an actual change is made to the title does the script access and write to the database. This should greatly reduce CPU power usage while it processes by eliminating needless writing to the database.

Another cool feature: if a song title is empty, the script will fill the title with "Title Unavailable"

Some known bugs: 1) Song titles in ALL CAPS will be unaffected. Those you'll have to fix manually. Originally I had this script first convert a title to all lower case and then perform the task of capitalizing the appropriate words. This had a negative effect on songs whose titles were intended to be in all caps, such as "12MXU".

2) Titles with abbreviations are kind of weird. Like "4 A.M." Running this script will give you a title of "4 A.m." because the script considers "A.M." to be one word.

3) The script does not rewrite ID3 tags, only database info (which is all the listener will ever see info from anyway).

These are very minor bugs. If you have a huge library of songs that are titled incorrectly this script can turn hours of searching and correcting into a no-brainer. You may still have to tweak some songs on occassion but, for the most part, ths script will do the work about with about 98% effectiveness.

Thanks to Steve Kunitzer (FesterHead) for posting the grammatical guidelines for capitalization of song titles. His post inspired this script. This script can be amended easily to fix Album Titles and Artist Names as well.

//Keep this script from looping.

PAL.Loop := False;

// Set some global variables to be used throughout the script

  var
   D, D2, D3 : TDataSet;
  var
   songID, letter_position, letter_count : Integer;
   var
   temp_word, temp_title, title, letter, last_word : String ;
       var
   start_date, stop_date, stop_time, start_time : DateTime;
start_date := Date;
start_time := Time;
D:=Query('SELECT ID as songID, title FROM songlist', [], True);
D.First;

// Go through each song

While Not D.EOF Do
Begin
Pal.LockExecution;
temp_word := ;
temp_title := ;
letter_position := 1;

// Grab Song Title length

letter_count := Length(D['title']);

// Iterate through the title and build each word watching for spaces as delimiters

while (letter_position <= letter_count) do
  begin
    while not (D['title'][letter_position] = ' ') and (letter_position <= letter_count) do
    begin
      temp_word := temp_word + (D['title'][letter_position] );
      Inc(letter_position, 1);
    end;

// Capitalize the word

       case temp_word[1] of
         '['    : temp_word[2] := AnsiUpperCase(temp_word[2]);
         '-'    : temp_word[2] := AnsiUpperCase(temp_word[2]);
         '('    : temp_word[2] := AnsiUpperCase(temp_word[2]);
         '*'    : temp_word[2] := AnsiUpperCase(temp_word[2]);
         '/'    : temp_word[2] := AnsiUpperCase(temp_word[2]);
         '~'    : temp_word[2] := AnsiUpperCase(temp_word[2]);
         ']'    : temp_word[2] := AnsiUpperCase(temp_word[2]);
         ')'    : temp_word[2] := AnsiUpperCase(temp_word[2]);
         '+'    : temp_word[2] := AnsiUpperCase(temp_word[2]);
         '|'    : temp_word[2] := AnsiUpperCase(temp_word[2]);
         '='    : temp_word[2] := AnsiUpperCase(temp_word[2]);
         ':'    : temp_word[2] := AnsiUpperCase(temp_word[2]);
         '<'    : temp_word[2] := AnsiUpperCase(temp_word[2]);
         '>'    : temp_word[2] := AnsiUpperCase(temp_word[2]);
         '?'    : temp_word[2] := AnsiUpperCase(temp_word[2]);
         else
           temp_word[1] := AnsiUpperCase(temp_word[1]);
        end;

// Check to see if word is one of a select few that should be lower case

    case temp_word of
     'A'    : temp_word := AnsiLowerCase(temp_word);
     'An'    : temp_word := AnsiLowerCase(temp_word);
     'The'    : temp_word := AnsiLowerCase(temp_word);
     'And'    : temp_word := AnsiLowerCase(temp_word);
     'But'    : temp_word := AnsiLowerCase(temp_word);
     'Or'    : temp_word := AnsiLowerCase(temp_word);
     'Nor'    : temp_word := AnsiLowerCase(temp_word);
     'At'    : temp_word := AnsiLowerCase(temp_word);
     'By'    : temp_word := AnsiLowerCase(temp_word);
     'For'    : temp_word := AnsiLowerCase(temp_word);
     'From'    : temp_word := AnsiLowerCase(temp_word);
     'In'    : temp_word := AnsiLowerCase(temp_word);
     'Into'    : temp_word := AnsiLowerCase(temp_word);
     'Of'    : temp_word := AnsiLowerCase(temp_word);
     'To'    : temp_word := AnsiLowerCase(temp_word);
     'With'    : temp_word := AnsiLowerCase(temp_word);
     'Vs'    : temp_word := AnsiLowerCase(temp_word);
     'Vs.'    : temp_word := AnsiLowerCase(temp_word);
     else
    end;
   temp_title := temp_title + temp_word + ' ';
  Inc(letter_position, 1);

// Capitalize Last word

  if (letter_position > letter_count) then
     begin
       last_word := temp_word;
       case last_word[1] of
         '['    : last_word[2] := AnsiUpperCase(last_word[2]);
         '-'    : last_word[2] := AnsiUpperCase(last_word[2]);
         '('    : last_word[2] := AnsiUpperCase(last_word[2]);
         '*'    : last_word[2] := AnsiUpperCase(last_word[2]);
         '/'    : last_word[2] := AnsiUpperCase(last_word[2]);
         '~'    : last_word[2] := AnsiUpperCase(last_word[2]);
         ']'    : last_word[2] := AnsiUpperCase(last_word[2]);
         ')'    : last_word[2] := AnsiUpperCase(last_word[2]);
         '+'    : last_word[2] := AnsiUpperCase(last_word[2]);
         '|'    : last_word[2] := AnsiUpperCase(last_word[2]);
         '='    : last_word[2] := AnsiUpperCase(last_word[2]);
         ':'    : last_word[2] := AnsiUpperCase(last_word[2]);
         '<'    : last_word[2] := AnsiUpperCase(last_word[2]);
         '>'    : last_word[2] := AnsiUpperCase(last_word[2]);
         '?'    : last_word[2] := AnsiUpperCase(last_word[2]);
         else
           last_word[1] := AnsiUpperCase(last_word[1]);
        end;
     end;
  temp_word := ;
  end;

// Capitalize First Word

       case temp_title[1] of
         '['    : temp_title[2] := AnsiUpperCase(temp_title[2]);
         '-'    : temp_title[2] := AnsiUpperCase(temp_title[2]);
         '('    : temp_title[2] := AnsiUpperCase(temp_title[2]);
         '*'    : temp_title[2] := AnsiUpperCase(temp_title[2]);
         '/'    : temp_title[2] := AnsiUpperCase(temp_title[2]);
         '~'    : temp_title[2] := AnsiUpperCase(temp_title[2]);
         ']'    : temp_title[2] := AnsiUpperCase(temp_title[2]);
         ')'    : temp_title[2] := AnsiUpperCase(temp_title[2]);
         '+'    : temp_title[2] := AnsiUpperCase(temp_title[2]);
         '|'    : temp_title[2] := AnsiUpperCase(temp_title[2]);
         '='    : temp_title[2] := AnsiUpperCase(temp_title[2]);
         ':'    : temp_title[2] := AnsiUpperCase(temp_title[2]);
         '<'    : temp_title[2] := AnsiUpperCase(temp_title[2]);
         '>'    : temp_title[2] := AnsiUpperCase(temp_title[2]);
         '?'    : temp_title[2] := AnsiUpperCase(temp_title[2]);
         else
           temp_title[1] := AnsiUpperCase(temp_title[1]);
        end;
    temp_title := TrimRight(temp_title);
  if (temp_title = ) then
      temp_title := 'Title Unavailable';
 WriteLn('Original Title: "' + D['title'] + '"');
 WriteLn('New Title: "' + temp_title + '"');
  if (D['title'] = temp_title) then
       WriteLn('Title unchanged. Nothing saved.')
  else
       begin
          WriteLn('Title changed. Changes saved.');
          ExecSQL('UPDATE songlist SET title = :temp_title WHERE ID = :ID', [temp_title, D['songID']]);
       end;
 WriteLn();
 Pal.UnLockExecution;
 D.Next;
End;
stop_date := Date;
stop_time := Time;
WriteLn();
WriteLn('Script ran from');
WriteLn(DatetoStr(start_date) + ' ' + TimetoStr(start_time));
WriteLn(' - to - ');
WriteLn(DatetoStr(stop_date) + ' ' + TimetoStr(stop_time));
D.Free;
Personal tools