SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Member
    Join Date
    Jan 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Array => String => File Name, XML => TXT and more

    Hi, I'm trying to rewrite the code below to extract data from the melody.xml file and create a text file with the name "Artist - Title.txt".

    Ultimately I would like to put the php-file in my root-dir and process all folders, since each folder contains a melody.xml file and a mp3 file. Then the script could rename the mp3 file to "Artist - Title.mp3" as well.

    But first things first.
    As you can see I've put some own code in there in order to just create a dummy file with just the artist name, problem is, I can't create the file.

    If I however replace the value in $pfile2 with "ArtistName - Title" it creates a file named "ArtistName - Title.txt".

    Any ideas or suggestions?

    Included is the melody.xml-file (renamed melody.txt)


    PHP Code:
    <?php
    //error_reporting(0);
    $freestyle=false;
    $golden=false;
    $dat=file("melody.xml");
    $start_pause=0;      //GAP?
    $found=false;      //First length found?
    $fertig=false;      //Header found?
    $first_note=true;   //for parsing the gap
    $real_word=true;
    for (
    $i=0;$i<sizeof($dat);$i++) {
       
       if (
    eregi('<MELODY',$dat[$i])){      //New sentence / parsing bpm
          
    $dat[$i]=ereg_replace("<--","",$dat[$i]);
          
    $dat[$i]=ereg_replace("-->","",$dat[$i]);
          
    $regs=explode("\"",$dat[$i]);
          
    $bpm=$regs[7]*2//maybe the offset needs to be change depending on the disc
       
    }
       if (
    eregi('<!-- Artist',$dat[$i])){      //parsing artist
          
    $dat[$i]=ereg_replace("<","",$dat[$i]);
          
    $dat[$i]=ereg_replace("-->","",$dat[$i]);
          
    $pfile $dat[$i]; //Get the Artist so I can create the file name 
          
    $regs=explode(":",$dat[$i]);
          
    $artist=$regs[1];


    $break Explode(':'$pfile); //Breaks the  $pfile
    $pfile2 $break[count($break) - 1]; //Take the last value in the array
    echo $pfile2;
    $suffix=".txt";
    $ourFileHandle fopen($pfile2.$suffix,'W');
    fclose($ourFileHandle);

       }
       if (
    eregi('<!-- Title',$dat[$i])){      //parsing title
          
    $dat[$i]=ereg_replace("<","",$dat[$i]);
          
    $dat[$i]=ereg_replace("-->","",$dat[$i]);
          
    $regs=explode(":",$dat[$i]);
          
    $title=$regs[1];
          
    //$title=substr($title, 0, -1);
          
    $fertig=true;
       }   
       if (
    $fertig){               //writing header
          
    $start_pause=0;
          
    $x=0;
          
    $found=false;
          echo 
    "#TITLE:".$title."\n<br>#ARTIST:".$artist."\n<br>#EDITION:Singstar Rocks\n<br>#LANGUAGE:English\n<br>#COVER:".$artist." - ".$title." [CO].jpg\n<br>#BACKGROUND:".$artist." - ".$title." [BG].jpg\n<br>#MP3:".$artist." - ".$title.".mp3\n<br>#BPM:".$bpm."\n<br>";
          
    $fertig=false;
       }
       if (
    eregi('NOTE',$dat[$i])){         //parsing tones/lenght
          
    $dat[$i]=ereg_replace("<","",$dat[$i]);
          
    $dat[$i]=ereg_replace(">","",$dat[$i]);
          
    $regs=explode("\"",$dat[$i]);
          
    $note=$regs[1];
          
    $regs[5].="&nbsp;";//Added this so you don't need to add spaces manually
          
    $word=ereg_replace(" -&nbsp;","",trim($regs[5]));
          
    $length=$regs[3];
          
          if (
    $word != "-&nbsp;"){//added so it doesn't look like this: "Do-", instead "Do -".
             
    $real_word=true;
          }
          if (
    $word == "-&nbsp;" && $real_word==true){
             
    $word="&nbsp;-&nbsp;";
             
    $real_word=false;
          }
          
        
         if (
    eregi('bonus'$dat[$i])){
             
    $golden=true;
          }
         if (
    eregi('freestyle'$dat[$i])){
             
    $freestyle=true;
          }
        
          if (!
    $found){
             
    $start_pause=$length;
             
    $found=true;
          }
           
          if (
    $note==&& $first_note == true){//Added this, it writes the GAP
             
    $bpm_temp=$bpm/2;
             
    $gap=($length/($bpm_temp/7.5))*1000;
             echo 
    "#GAP:".round($gap2)."\n<br>";
             
    $first_note false;
          }
          if (
    $note>0){
          if (!
    $freestyle && !$golden){
             echo 
    ": ".($x-$start_pause)." ".$length." ".$note." ".$word."<br>\n";
             }
          if (
    $golden){
             echo 
    "* ".($x-$start_pause)." ".$length." ".$note." ".$word."<br>\n";
             
    $golden=false;
             }
          if (
    $freestyle){
             echo 
    "F ".($x-$start_pause)." ".$length." ".$note." ".$word."<br>\n";
             
    $freestyle=false;
             }      
          }
          
    $x+=$length;
       }
       if (
    eregi('</SENTENCE>',$dat[$i])){   //End of sentence
          //$start_pause=0;
          //$x=0;
          
    echo "- ".($x-$start_pause);
          echo 
    "<br>\n";
       }
          if (
    eregi('</MELODY>',$dat[$i])){   //End of Song
          
    echo "E<br>\n";
          
    $fertig=false;
       }
    }
    ?>
    Attached Files Attached Files

  2. #2
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    your code doesn't work because it's supposed to be a lowercase 'w'

  3. #3
    SitePoint Member
    Join Date
    Jan 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm... did change the 'W' to 'w+'. Still no go... when I echo the $pfile2 I get "Dusty Springfield" which is the artist in the song.xml file...

    why doesn't it create the file when it can do that when I change the value to $pfile2="Dusty Springfield" manually?

  4. #4
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you don't need w+, just w, the + means it can read as well as write.

    it's not creating the file because there's a space after it

    PHP Code:
    $pfile2 trim($break[count($break) - 1]); //Take the last value in the array 

    that code needs to be replaced with simplexml

  5. #5
    SitePoint Member
    Join Date
    Jan 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by galen View Post
    you don't need w+, just w, the + means it can read as well as write.

    it's not creating the file because there's a space after it

    PHP Code:
    $pfile2 trim($break[count($break) - 1]); //Take the last value in the array 

    that code needs to be replaced with simplexml
    Hmm okay, any suggestions?
    I'm an absolute beginner ya' know

  6. #6
    SitePoint Member
    Join Date
    Jan 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, now I can create the file. Forgot that I had to remove the line breaks.

    Now to the fun part...

    Trying to actually write what's now echoed into a file.

    Guess there has to be a way to do this using simplexml. or should I stick to the regular expressions used in the script from the beginning?

  7. #7
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by magnus_78 View Post
    Ok, now I can create the file. Forgot that I had to remove the line breaks.

    Now to the fun part...

    Trying to actually write what's now echoed into a file.

    Guess there has to be a way to do this using simplexml. or should I stick to the regular expressions used in the script from the beginning?
    just change anywhere it echos from

    PHP Code:
    echo EXPRESSION
    to

    PHP Code:
    $theData .= EXPRESSION
    and at the end of the code write $theData to the file.

  8. #8
    SitePoint Member
    Join Date
    Jan 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Galen,
    it actually works )

    one problem though, I need to get rid of the last two lines that are written to the text file. I can write the "E" which comes last, after the iteration, so that one is no problem.

    does anyone have any ideas on how to skip the last two iterations?


    PHP Code:
    <?php
    error_reporting
    (0);
    $freestyle=false;
    $golden=false;
    $dat=file("melody.xml");
    $start_pause=0;      //GAP?
    $found=false;      //First length found?
    $fertig=false;      //Header found?
    $first_note=true;   //for parsing the gap
    $real_word=true;
    for (
    $i=0;$i<sizeof($dat);$i++) {
       
       if (
    eregi('<MELODY',$dat[$i])){      //New sentence / parsing bpm
          
    $dat[$i]=ereg_replace("<--","",$dat[$i]);
          
    $dat[$i]=ereg_replace("-->","",$dat[$i]);
          
    $regs=explode("\"",$dat[$i]);
          
    $bpm=$regs[7]*2//maybe the offset needs to be change depending on the disc
          
               
          
       
    }
       if (
    eregi('<!-- Artist',$dat[$i])){      //parsing artist
          
    $dat[$i]=ereg_replace("<","",$dat[$i]);
          
    $dat[$i]=ereg_replace("-->","",$dat[$i]);
          
    $pfile1 $dat[$i];
          
    $regs=explode(":",$dat[$i]);
          
    $artist2=$regs[1];
          
    $artist=$regs[1];

    $break Explode(':'$pfile1);
    $pfile1 $break[count($break) - 1];

    $pfile1 str_replace("\r"""$pfile1);
    $pfile1 str_replace("\n"""$pfile1);
    $pfile1 substr($pfile12, -1);
    echo 
    $pfile1;
    //$suffix=".txt";
    //$ourFileHandle = fopen($pfile2.$suffix,'w+');
    //fclose($ourFileHandle);

       
    }
       if (
    eregi('<!-- Title',$dat[$i])){      //parsing title
          
    $dat[$i]=ereg_replace("<","",$dat[$i]);
          
    $dat[$i]=ereg_replace("-->","",$dat[$i]);
          
    $regs=explode(":",$dat[$i]);
          
    $title=$regs[1];
          
    //$title=substr($title, 0, -1);
         
    $pfile2 $dat[$i]; 
         
    $pfile2 str_replace("\r"""$pfile2);
         
    $pfile2 str_replace("\n"""$pfile2);
         
    $pfile2 substr($pfile214, -1);
    echo 
    $pfile2
          
    $fertig=true;
          
          
          
       }

    $artist str_replace("\r"""$artist);
    $artist str_replace("\n"""$artist);
    $title str_replace("\r"""$title);
    $title str_replace("\n"""$title);
    $bpm str_replace("\r"""$bpm);
    $bpm str_replace("\n"""$bpm);

    //$pfile2 = str_replace("\n", "", $pfile2);
    //$pfile2 = str_replace("\r", "", $pfile2);
    //$pfile2 = str_replace("\n", "", $pfile2);

       
    if ($fertig){               //writing header
          
    $start_pause=0;
          
    $x=0;
          
    $found=false;
          echo 
    "#TITLE:".$title."\n<br>#ARTIST:".$pfile1."\n<br>#EDITION:Singstar Legends\n<br>#LANGUAGE:English\n<br>#COVER:".$artist." - ".$title." [CO].jpg\n<br>#BACKGROUND:".$artist." - ".$title." [BG].jpg\n<br>#MP3:".$artist." - ".$title.".mp3\n<br>#BPM:".$bpm."\n<br>";
          
    $ourData .= "#TITLE:".$pfile2."\r\n#ARTIST:".$pfile1."\r\n#EDITION:Singstar Legends\r\n#LANGUAGE:English\r\n#COVER:".$pfile1." - ".$pfile2."[CO].jpg\r\n#BACKGROUND:".$pfile1." - ".$pfile2." [BG].jpg\r\n#MP3:".$pfile1." - ".$pfile2.".mp3\r\n#BPM:".$bpm."\r\n";
         
    $fertig=false;
       }
       if (
    eregi('NOTE',$dat[$i])){         //parsing tones/lenght
          
    $dat[$i]=ereg_replace("<","",$dat[$i]);
          
    $dat[$i]=ereg_replace(">","",$dat[$i]);
          
    $regs=explode("\"",$dat[$i]);
          
    $note=$regs[1];
          
    $regs[5].=" ";//Added this so you don't need to add spaces manually
          
    $word=ereg_replace(" -","",trim($regs[5]));
          
    $length=$regs[3];
          
          if (
    $word != "- "){//added so it doesn't look like this: "Do-", instead "Do -".
             
    $real_word=true;
          }
          if (
    $word == "- " && $real_word==true){
             
    $word=" - ";
             
    $real_word=false;
          }
          
        
         if (
    eregi('bonus'$dat[$i])){
             
    $golden=true;
          }
         if (
    eregi('freestyle'$dat[$i])){
             
    $freestyle=true;
          }
        
          if (!
    $found){
             
    $start_pause=$length;
             
    $found=true;
          }
           
          if (
    $note==&& $first_note == true){//Added this, it writes the GAP
             
    $bpm_temp=$bpm/2;
             
    $gap=($length/($bpm_temp/7.5))*1000;
             echo 
    "#GAP:".round($gap2)."\n<br>";
             
    $ourData .="#GAP:".round($gap2)."\r\n";
         
    $first_note false;
          }
          if (
    $note>0){
          if (!
    $freestyle && !$golden){
             echo 
    ": ".($x-$start_pause)." ".$length." ".$note." ".$word."<br>\n";
             
    $ourData .=": ".($x-$start_pause)." ".$length." ".$note." ".$word" \r\n";
         
    //echo $word."<br>\n";
             
    }
          if (
    $golden){
             echo 
    "* ".($x-$start_pause)." ".$length." ".$note." ".$word."<br>\n";
              
    $ourData .="* ".($x-$start_pause)." ".$length." ".$note." ".$word." \r\n";
         
    $golden=false;
             }
          if (
    $freestyle){
             echo 
    "F ".($x-$start_pause)." ".$length." ".$note." ".$word."<br>\n";
              
    $ourData .="F ".($x-$start_pause)." ".$length." ".$note." ".$word" \r\n";
         
    $freestyle=false;
             }      
          }
          
    $x+=$length;
       }
       if (
    eregi('</SENTENCE>',$dat[$i])){   //End of sentence
          //$start_pause=0;
          //$x=0;
          
    echo "- ".($x-$start_pause);
          
    $ourData .="- ".($x-$start_pause);
          echo 
    "<br>\n";
          
    $ourData .="\r\n";
       }
          if (
    eregi('</MELODY>',$dat[$i])){   //End of Song
          
    echo "E<br>\n";
          
          
    $fertig=false;
       }
    }
    //echo $ourData;
    $ourData .="E\r\n";
    $myFile $pfile1." - ".$pfile2.".txt";
    $fh fopen($myFile'w+') or die("can't open file");
    fwrite($fh$ourData);
    fclose($fh);
    rename('2channel.1.wav.mp3'$pfile1." - ".$pfile2.".mp3");

    ?>

  9. #9
    SitePoint Member
    Join Date
    Jan 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll correct myself on the last one

    just had to add
    PHP Code:
    for ($i=0;$i<sizeof($dat)-8;$i++) 
    I will now try to put the file in my root folder and go through the folders one by one.
    Guess I'll have to list all the folder names into an array and then iterate through them one by one.

    Now I need some serious help

  10. #10
    SitePoint Member
    Join Date
    Jan 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can successfully iterate through all folders.
    Problem is that my script writes to "." and ".." as well.

    How do I prevent writing to those two folders?


    PHP Code:
    <?php
       $mydir 
    dir('/test/song/1/');
       while((
    $file $mydir->read()) !== false) {
          if(
    is_dir($mydir->path.$file)) {
             
    //echo "Directory: $file\r\n<BR>";
    .
    .
    .
    .
    $myFile $file."/".$pfile1." - ".$pfile2.".txt";
    $fh fopen($myFile'w+') or die("can't open file");
    fwrite($fh$ourData);
    fclose($fh);
    rename($file.'/2channel.1.wav.mp3'$pfile1." - ".$pfile2.".mp3");         
    $ourData "";
          } else {
             echo 
    "";
          }
       }
       
    $mydir->close();
    ?>

  11. #11
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check whether the file is ./.. or not:
    PHP Code:
    $mydir dir('/test/song/1/');
    while((
    $file $mydir->read()) !== false) {
        if(
    $file != "." && $file != ".."){
            if(
    is_dir($mydir->path.$file)) {
                
    //echo "Directory: $file\r\n<BR>";
                
    .
                .
                .
                .
                
    $myFile $file."/".$pfile1." - ".$pfile2.".txt";
                
    $fh fopen($myFile'w+') or die("can't open file");
                
    fwrite($fh$ourData);
                
    fclose($fh);
                
    rename($file.'/2channel.1.wav.mp3'$pfile1." - ".$pfile2.".mp3");         
                
    $ourData "";
            }
            else{
                echo 
    "";
            }
        }
    }
    $mydir->close(); 
    Does this work for you?

    Edit:
    And while renaming on this line:
    PHP Code:
    rename($file '/2channel.1.wav.mp3'$pfile1 " - " $pfile2.".mp3"); 
    There is i can see spaces before and after - in the destination directory/folder name. Avoid using spaces on the directory/file names.
    PHP Code:
    rename($file '/2channel.1.wav.mp3'$pfile1 "-" $pfile2.".mp3"); 
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  12. #12
    SitePoint Member
    Join Date
    Jan 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rajug View Post
    Does this work for you?
    Yeah Thanks a lot


    There is i can see spaces before and after - in the destination directory/folder name. Avoid using spaces on the directory/file names.
    I'll keep that in mind. It has been my standard convention when naming mp3-files.

  13. #13
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by magnus_78 View Post
    I'll keep that in mind. It has been my standard convention when naming mp3-files.
    Having spaces in directory/file names will not be standard convention
    Code:
    " - " is not good
    "-" is good
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •