The Mudcat Café TM
Thread #91460   Message #2174899
Posted By: GUEST
19-Oct-07 - 07:56 PM
Thread Name: Tech: Digital Tradition Programmer Needed
Subject: RE: Tech: Digital Tradition Programmer Needed
I don't know Goeff. I understand your reasoning but I think I would go the other way and believe abc will be around for a long while.
-------
Dick. I think Mick Pearce has a much better job but, yes, I do use a script, something I hacked together which seems to get most dt files I tried into abc when a page is requested. I doubt it would be of much help but you or anyone else are welcome to do whatever you like with that.

---------

sw2abcdef.php
    //rests
    $pitches["r"] = "z";
    $pitches["R"] = "z";
    $pitches["x"] = "z";
    $pitches["X"] = "z";
    //notes
    $pitches[":"] = "A,,";
    $pitches[";"] = "B,,";
    $pitches["<"] = "C,";
    $pitches["="] = "D,";
    $pitches[">"] = "E,";
    $pitches["?"] = "F,";
    $pitches["@"] = "G,";
    $pitches["A"] = "A,";
    $pitches["B"] = "B,";
    $pitches["C"] = "C";
    $pitches["D"] = "D";
    $pitches["E"] = "E";
    $pitches["F"] = "F";
    $pitches["G"] = "G";
    $pitches["a"] = "A";
    $pitches["b"] = "B";
    $pitches["c"] = "c";
    $pitches["d"] = "d";
    $pitches["e"] = "e";
    $pitches["f"] = "f";
    $pitches["g"] = "g";
    $pitches["h"] = "a";
    $pitches["i"] = "b";
    $pitches["j"] = "c'";
    $pitches["k"] = "d'";
    $pitches["l"] = "e'";
    $pitches["m"] = "f'";
    $pitches["n"] = "g'";
    $pitches["o"] = "a'";
    $pitches["p"] = "b'";

    //accidentals;
    $accidentals["-"] = "";       //none
    $accidentals["&"] = "_";      //flat
    $accidentals["%"] = "=";      //natural
    $accidentals["#"] = "^";      //sharp

    //durations in 16th notes
    $durations["1"] = "16";
    $durations["2"] = "8";
    $durations["3"] = "12";
    $durations["4"] = "4";
    $durations["5"] = "6";
    $durations["6"] = "8/3";      //quarter triplet
    $durations["7"] = "1.33333333333333333333";      //eigth triplet
    $durations["8"] = "2";
    $durations["9"] = "3";
    $durations["0"] = "1";
    $durations["-"] = "3/2";
    $durations["="] = "1/2";
    $durations["/"] = "16/3";   //triplet???
    $durations[")"] = "2/3";    //sixteenth triplet
   
    $codes[" "] = "";
    $codes["_"] = "-";

    //header fields
    $headers["N"] = "T:";
    $headers["C"] = "C:";
    $headers["A"] = "S:";
    $headers["T"] = "Q:";
    $headers["S"] = "I:";
    $headers["K"] = "K:";
    $headers["B"] = "M:";
    $headers["F"] = "N:";

sw2abc.php

require("sw2abcdef.php");
$abclinecount=0;
$beats = 0;
$beatlimit = 0;
$ties="";
$lasttie=false;

function AddHeader($type, $data)
{
        global $beatlimit;
        global $headers;
        global $abcheader;
        if ($data !="")
        $abcheader[$type]=$headers[$type] . substr($data,2);
        if ($type=="B")
                {
                $top=substr($data,2,1);
                $bottom=substr($data,4,1);
                $beatlimit = (16 * $top) / $bottom;
                }
}

function AddLyrics($data)
{
        global $abc;
        global $abclinecount;
        global $ties;
        $replace = "_";
        $data = substr($data, 3);
        $data = trim($data, " -");
        $data = preg_replace('/\s\s+/', ' ', $data);
        $data = str_replace("- ", "-", $data);
        $data = str_replace("-", "-\n", $data);
        $data = str_replace(" ", " \n", $data);
        $words=explode("\n", $data);
        $wordties=explode(",", $ties);
         foreach($wordties as $wordtie)
               {
               if (is_numeric($wordtie))
                       array_splice($words, $wordtie+1, 0, $replace);
               }
        $data=implode("", $words);
        $data=str_replace(" " . $replace, $replace . " " , $data);
        $abc[$abclinecount] = "w:" . $data;
        $abclinecount++;
}
        
function AddTune($data)
{
        global $abc;
        global $abclinecount;
        global $pitches;
        global $accidentals;
        global $durations;
        global $beats;
        global $codes;
        global $beatlimit;
        global $ties;
        global $lasttie;
        if ($lasttie)
                $ties="-1,";
        else
                $ties = "";
        $lasttie=false;
        $thisnote=0;
        $intriplet=0;
        $I=0;
        $data = $data . " ";
        $abcline = "";
        $item = substr($data, $I, 1);
        while ($item)
                {
                switch ($item):
                    case " ":
                            $I++;
                    break;
                    case "M":
                    case "m":
                    case "+":
                    case "-":
                    case "*":
                            $I=$I+3;
                    break;
                    case "W":
                    case "P":
                            $I=$I+4;
                    break;
                    case "S":
                           if ($data[$I+2]=="6")
                                   {
                                        $abcline = $abcline . " | ";
                                        $beats = 0;
                                        }
                            $I=$I+4;
                    break;
                    default:
                            $space = "";
                            $lasttie = false;
                            $bar = "";
                           //echo "data". $data[$I]; exit;
                           $note = $pitches[$data[$I]];
                           //echo $note;
                           $I++;
                           $accidental = $accidentals[$data[$I]];
                           $I++;
                           $duration = $durations[$data[$I]];
                           $beats = $beats + $duration;
                                   if ((($beatlimit/2) / ($beats)) ==1)
                                   $space = " ";
                           if ($beats >= $beatlimit)
                                {
                                $bar = "|";
                                $beats = 0;
                                }
                           $I++;
                           if ($data[$I-1]=="7") //triplets
                                   {
                                   $intriplet++;
                                   if ($intriplet==1)
                                           $note="(3" . $note;
                                   elseif ($intriplet==3)
                                           $intriplet=0;
                                   $duration=2;
                                   }
                           //need code;
                           $code = $codes[$data[$I]];
                           if ($code=="-")
                                   {
                                   $ties=$ties . $thisnote. ",";
                                   $lasttie=true;
                                   }
                           $I++;
                           if ($note != "z")
                                   $thisnote++;
                           $abcline = $abcline . $accidental . $note . $duration .$code . $space . $bar;
                    endswitch;
                    $item = substr($data, $I, 1);
                    }
        
        //echo $abcline . "
";
        $abc[$abclinecount] = $abcline;
        $abclinecount++;
}


function WriteHeaderLine($field, $fp)
{
        global $abcheader;
        $temp = @$abcheader[$field];
        if ($temp != "")
            //echo $temp . "
";
           fwrite($fp, $temp . "\n");
        
}

function WriteHeader($fp)
{
        global $abcheader;
        fwrite($fp, "X:1\n");
        WriteHeaderLine("N", $fp);
        WriteHeaderLine("C", $fp);
        WriteHeaderLine("A", $fp);
        WriteHeaderLine("F", $fp);
        WriteHeaderLine("B", $fp);
        fwrite($fp, "L:1/16\n");
        //WriteHeaderLine("T", $fp);
        //WriteHeaderLine("S", $fp);
        WriteHeaderLine("K", $fp);
}

function convertsw($ABC)
        {
        global $abclinecount;
        global $abc;

        //start to play with the sw
        //make array;
        $sw = split("
", $ABC);
        //start
        foreach ($sw as $swline)
                    {
                $temp = substr($swline,0, 1);
                    switch ($temp):
               case "N":
               case "C":
               case "A":
               case "T":
               case "S":
               case "K":
               case "B":
               case "F":
               AddHeader($temp, $swline);
               break;
               case "H":
               break;
               case "M":
               AddTune($swline);
               break;
               case "L":
               AddLyrics($swline);
               break;
                    endswitch;
                    }
        $fp = fopen("output.abc", "w");
        WriteHeader($fp);
        for ($I=0; $I<$abclinecount; $I++)
                {
                fwrite($fp,$abc[$I]."\n");
                }
        fclose($fp);
        }
?>