;¤§¤;¤§¤;¤§¤;¤§¤;¤§¤;¤§¤;
;¤§¤;   Author: Juzzy   ;¤§¤;
;¤§¤;¤§¤;¤§¤;¤§¤;¤§¤;¤§¤;
;
; To load type:     /load -rs mp3.mrc
;
;version 1.5
;

menu channel,status,menubar { 
  &Play an MP3:/mp3 | timer50 off
  &Continuous play:/cplay
  &Skip:/skip
  St&op:/run %player /stop | .timer50 off
  ;St&op:/splay stop | .timer50 off
  &Generate a playlist:/makelist
  &Create a webpage:writemp3
}
alias writemp3 {
  set %ii 1
  set %ttime 0
  write -c mp3.html <html><head><title>Mp3 List</title></head><body bgcolor=#DFD275><table BORDER COLS=4 WIDTH="100%" ><tr><td> Song Name <td width="5%"> Bitrate <td width="8%"> Frequency <td width="15%"> Time </tr>
  write -a mp3.html <center>This page was created by Juzzy's Mp3 Generation script!<br></center>
  :loop
  if (%ii > $lines($shortfn(%mp3list))) { echo 4 -a Done! | goto end }
  set %song $read -l $+ %ii %mp3list
  set %info $mp3details2(%song)
  write -a mp3.html <tr><td> $nopath(%song) <td> $gettok(%info,1,32) <td> $gettok(%info,2,32) <td> $duration($gettok(%info,3,32))</tr>
  set %ttime $calc(%ttime + $gettok(%info,2,32))
  inc %ii
  goto loop
  :end 
  write -a mp3.html </table><br>Total Time:<strong> $duration(%ttime) </strong>      ( $+ %ttime seconds) <br> Total mp3s: $lines($shortfn(%mp3list)) </body></html>
}
alias makelist {
  set %mp3listroot $sdir="Please select the upper most parent mp3 directory"
  :ask
  set %ask $?!="Would you like to add an additional root dir?"
  if (%ask) {
    set %mp3listroot %mp3listroot $sdir="Please select the upper most parent mp3 directory"
    goto ask  
  }
  write -c mp3list.bat @echo off
  write -a mp3list.bat echo **** Please wait while the list is being generated ****
  write -c list.txt
  if ( [ $numtok( [ $+ [ %mp3listroot ] ] ,32) ] > 1) {
    set %i 1
    :loop
    if (%i > $numtok(%mp3listroot,32)) { goto end }
    write -a mp3list.bat dir /b/on/s $gettok( [ %mp3listroot ] ,%i,32) $+ *.mp3 >> list.txt
    inc %i
    goto loop
  }
  else { write -a mp3list.bat dir /b/s %mp3listroot $+ *.mp3 > $mircdirlist.txt }

  :end
  write -a mp3list.bat echo ** ALL DONE! **
  /run mp3list.bat
  set %mp3list list.txt
}
alias mp3details2 {
  bread $shortfn(%song) 0 16 &var
  set %mp3_bits $binary($gettok($bvar(&var,1-16),1,32)) $+ $binary($gettok($bvar(&var,1-16),2,32)) $+ $binary($gettok($bvar(&var,1-16),3,32)) $+ $binary($gettok($bvar(&var,1-16),4,32))
  set %mp3_bits $binary($gettok($bvar(&var,1-16),1,32)) $+ $binary($gettok($bvar(&var,1-16),2,32)) $+ $binary($gettok($bvar(&var,1-16),3,32)) $+ $binary($gettok($bvar(&var,1-16),4,32))
  if ($len(%mp3_bits) != 32) { echo 12 -a ERROR! The bits are the wrong length }
  set %mp3.header $mid(%mp3_bits,1,11)
  set %mp3.version $mid(%mp3_bits,12,2)
  set %mp3.layer $mid(%mp3_bits,14,2)
  set %mp3.crc $mid(%mp3_bits,16,1)
  set %mp3.bitrate $mid(%mp3_bits,17,4)
  set %mp3.mhz $mid(%mp3_bits,21,2)
  set %mp3.pad $mid(%mp3_bits,23,1)
  set %mp3.priv $mid(%mp3_bits,24,1)
  set %mp3.channel $mid(%mp3_bits,25,2)
  set %mp3.steriotype $mid(%mp3_bits,27,2)
  set %mp3.cright $mid(%mp3_bits,29,1)
  set %mp3.orig $mid(%mp3_bits,30,1)
  set %mp3.emphisis $mid(%mp3_bits,31,2)
  ;---version---
  if (%mp3.version == 00) { set %mp3.v v2.5 }
  if (%mp3.version == 10) { set %mp3.v v2 }
  if (%mp3.version == 11) { set %mp3.v v1 }
  else { set %mp3.v v1 }
  ;---layer---
  if (%mp3.layer == 01) { set %mp3.l iii }
  else if (%mp3.layer == 10) { set %mp3.l ii }
  else if (%mp3.layer == 11) { set %mp3.l i }
  else { set %mp3.l iii }
  set %mpeg %mp3.v $+ %mp3.l
  :bitrate
  set %index $findtok( [ $readini -n mp3.ini bitrate bits ] ,%mp3.bitrate,1,46)
  set %mp3.out.bitrate $gettok( [ $readini -n mp3.ini bitrate %mpeg ] ,%index,46)
  :mhz
  set %index2 $findtok( [ $readini -n mp3.ini sample bits ] ,%mp3.mhz,1,46)
  set %mp3.out.sample $gettok( [ $readini -n mp3.ini sample %mp3.v ] ,%index2,46)
  return %mp3.out.bitrate  %mp3.out.sample $calc(($file($shortfn(%song)).size * 8) / (%mp3.out.bitrate * 1000) - 1))
}

alias cplay {
  if (%mp3list == $null) {  set %mp3list $dir="What mp3 playlist would you like to use?" c:\*.* }
  if ($1 == $null) {
    set %mp3i $rand(1, $lines($shortfn(%mp3list)))
    set %song $read -l $+ %mp3i %mp3list
  }
  else {
    set %song $1-
  }
  mp3 %song
  set %mp3i $rand(1, $lines($shortfn(%mp3list)))
  set %nextsong $read -l $+ %mp3i %mp3list
  .timer50 1  $calc(($file($shortfn(%song)).size * 8) / (%mp3.out.bitrate * 1000) - 1)) cplay %nextsong
  echo 14 -a Up Next: %nextsong
}
alias skip { cplay %nextsong }
alias size { if ($1 < 1024) return $1 $+ b
  elseif (($1 > 1024) && ($1 < 1048576)) return $round($calc($1 / 1024),1) $+ k
  elseif (($1 < 1073741824)) return $round($calc($1 / 1048576),2) $+ mb
  elseif (($1 < 1099511627776)) return $round($calc($1 / 1073741824),2) $+ gb
else return $round($calc($1 / 1099511627776),2) $+ tb }
alias mp3 {
  if (%mp3dir == $null) { set %mp3dir $sdir="What is your default mp3 directory?" }
  if (%player == $null) { set %player $dir="What mp3 player would you like to use?" c:\*.exe }
  if ($1- == $null) { set %song $dir="Mp3 to read?" %mp3dir }
  else { set %song $1- } 
  mp3read
}
alias mp3read {
  bread $shortfn(%song) 0 16 &var
  set %mp3_bits $binary($gettok($bvar(&var,1-16),1,32)) $+ $binary($gettok($bvar(&var,1-16),2,32)) $+ $binary($gettok($bvar(&var,1-16),3,32)) $+ $binary($gettok($bvar(&var,1-16),4,32))
  if ($mid(%mp3_bits,1,11) != 11111111111) { 
    ;echo -a Error detected in %song $+ . Header is incorrect. Who in the hell made this mp3? Trying to fix.....
    bread $shortfn(%song) 0 2048 &var
    bwrite mp3debug.wri 0 2048 &var    
    set %i 1
    :read
    if (%i > 4096) { echo -a undefined headerfile | goto end }
    if ($bvar(&var,%i) == 255) {
      ;echo 4 -a Problem fixed by using offset: 0x000 $+ %i 
      set %mp3_bits $binary($gettok($bvar(&var, [ %i ] - [ $calc(%i + 16) ] ),1,32)) $+ $binary($gettok($bvar(&var, [ %i ] - [ $calc(%i + 16) ] ),2,32)) $+ $binary($gettok($bvar(&var, [ %i ] - [ $calc(%i + 16) ] ),3,32)) $+ $binary($gettok($bvar(&var, [ %i ] - [ $calc(%i + 16) ] ),4,32))
    goto end }
    else { inc %i | goto read }
  }
  set %mp3_bits $binary($gettok($bvar(&var,1-16),1,32)) $+ $binary($gettok($bvar(&var,1-16),2,32)) $+ $binary($gettok($bvar(&var,1-16),3,32)) $+ $binary($gettok($bvar(&var,1-16),4,32))
  :end
  msg $chan Playing $chr(32) $+ %mp3i $+ / $+ $lines($shortfn(%mp3list)) [12 $+ $nopath(%song) $+ ] @ $mp3details($shortfn(%song)) $+  $size($file($shortfn(%song)).size) - $duration($calc(($file($shortfn(%song)).size * 8) / (%mp3.out.bitrate * 1000) - 1)))
  run %player $shortfn(%song)
  ;splay $shortfn(%song)
}
alias mp3debug {
  if ($len(%mp3_bits) != 32) { echo 12 -a ERROR! The bits are the wrong length }
  set %mp3.header $mid(%mp3_bits,1,11)
  set %mp3.version $mid(%mp3_bits,12,2)
  set %mp3.layer $mid(%mp3_bits,14,2)
  set %mp3.crc $mid(%mp3_bits,16,1)
  set %mp3.bitrate $mid(%mp3_bits,17,4)
  set %mp3.mhz $mid(%mp3_bits,21,2)
  set %mp3.pad $mid(%mp3_bits,23,1)
  set %mp3.priv $mid(%mp3_bits,24,1)
  set %mp3.channel $mid(%mp3_bits,25,2)
  set %mp3.steriotype $mid(%mp3_bits,27,2)
  set %mp3.cright $mid(%mp3_bits,29,1)
  set %mp3.orig $mid(%mp3_bits,30,1)
  set %mp3.emphisis $mid(%mp3_bits,31,2)
  echo 4 -a  %mp3.header 9 %mp3.version 10 %mp3.layer 11 %mp3.crc 12 %mp3.bitrate 13 %mp3.mhz 8 %mp3.pad 7 %mp3.priv 6 %mp3.channel 5 %mp3.steriotype 3 %mp3.cright 2  %mp3.orig 4 %mp3.emphisis
  echo 4 -a  header 9 ver 10 layer 11 crc 12 bitrate 13 mhz 8 pad 7 priv 6 chan 5 sterio 3 cright 2  orig 4 emph
  echo -a  9 %mp3.v 10 %mp3.l 12 %mp3.out.bitrate 13 %mp3.out.sample
}
alias mp3debug2 {
  if ($len(%mp3_bits) != 32) { echo 12 -a ERROR! The bits are the wrong length }
  set %mp3.header $mid(%mp3_bits,1,11)
  set %mp3.version $mid(%mp3_bits,12,2)
  set %mp3.layer $mid(%mp3_bits,14,2)
  set %mp3.crc $mid(%mp3_bits,16,1)
  set %mp3.bitrate $mid(%mp3_bits,17,4)
  set %mp3.mhz $mid(%mp3_bits,21,2)
  set %mp3.pad $mid(%mp3_bits,23,1)
  set %mp3.priv $mid(%mp3_bits,24,1)
  set %mp3.channel $mid(%mp3_bits,25,2)
  set %mp3.steriotype $mid(%mp3_bits,27,2)
  set %mp3.cright $mid(%mp3_bits,29,1)
  set %mp3.orig $mid(%mp3_bits,30,1)
  set %mp3.emphisis $mid(%mp3_bits,31,2)
  say 4  %mp3.header 9 %mp3.version 10 %mp3.layer 11 %mp3.crc 12 %mp3.bitrate 13 %mp3.mhz 8 %mp3.pad 7 %mp3.priv 6 %mp3.channel 5 %mp3.steriotype 3 %mp3.cright 2  %mp3.orig 4 %mp3.emphisis
  say 4 header 9 ver 10 layer 11 crc 12 bitrate 13 mhz 8 pad 7 priv 6 chan 5 sterio 3 cright 2  orig 4 emph
  say  9 %mp3.v 10 %mp3.l 12 %mp3.out.bitrate 13 %mp3.out.sample
}
alias mp3details {
  if ($len(%mp3_bits) != 32) { echo 12 -a ERROR! The bits are the wrong length }
  set %mp3.header $mid(%mp3_bits,1,11)
  set %mp3.version $mid(%mp3_bits,12,2)
  set %mp3.layer $mid(%mp3_bits,14,2)
  set %mp3.crc $mid(%mp3_bits,16,1)
  set %mp3.bitrate $mid(%mp3_bits,17,4)
  set %mp3.mhz $mid(%mp3_bits,21,2)
  set %mp3.pad $mid(%mp3_bits,23,1)
  set %mp3.priv $mid(%mp3_bits,24,1)
  set %mp3.channel $mid(%mp3_bits,25,2)
  set %mp3.steriotype $mid(%mp3_bits,27,2)
  set %mp3.cright $mid(%mp3_bits,29,1)
  set %mp3.orig $mid(%mp3_bits,30,1)
  set %mp3.emphisis $mid(%mp3_bits,31,2)
  ;---version---
  set %mp3.v $mp3ver(%mp3.version)
  ;if (%mp3.version == 00) { set %mp3.v v2.5 }
  ;if (%mp3.version == 11) { set %mp3.v v1 }
  ;else { set %mp3.v v1 }
  ;---layer---
  if (%mp3.layer == 01) { set %mp3.l iii }
  else if (%mp3.layer == 10) { set %mp3.l ii }
  else if (%mp3.layer == 11) { set %mp3.l i }
  else { set %mp3.l iii | set %mp3.bitrate 1001 }
  set %mpeg %mp3.v $+ %mp3.l
  :bitrate
  set %index $findtok( [ $readini -n mp3.ini bitrate bits ] ,%mp3.bitrate,1,46)
  set %mp3.out.bitrate $gettok( [ $readini -n mp3.ini bitrate %mpeg ] ,%index,46)
  :mhz
  set %index2 $findtok( [ $readini -n mp3.ini sample bits ] ,%mp3.mhz,1,46)
  set %mp3.out.sample $gettok( [ $readini -n mp3.ini sample %mp3.v ] ,%index2,46)
  return [ $+ %mp3.out.bitrate $+ kbps $+ / $+ %mp3.out.sample $+ Hz $+ ]
}
alias mp3ver {
  if ($1 == 10) { return v2 }
  if ($1 == 00) { return v2.5 }
  if ($1 == 11) { return v1 }
}
alias binary {
  if ($1 == $null) { set %input $$?="Enter a number to convert to binary" }
  else { set %input $1 }
  set %i 0
  ;2048 1024 512 256   [= 128 64 32 16 8 4 2 1 = ]
  if (%input > 4095) { echo -a 14the number is too big | halt }
  else if (%input > 255) { halt }
  else { goto 8 }
  :8
  if (%input >= 128) { 
    set %bit1 1
    set %bit_temp $calc(%input - 128)
  goto 7 }
  else { 
    set %bit1 0
    set %bit_temp %input 
  goto 7 }
  :7
  if  (%bit_temp >= 64) {
    set %bit2 1
    set %bit_temp $calc(%bit_temp - 64)
  goto 6 }
  else {
    set %bit2 0
  goto 6 }
  :6
  if  (%bit_temp >= 32) {
    set %bit3 1
    set %bit_temp $calc(%bit_temp - 32)
  goto 5 }
  else {
    set %bit3 0
  goto 5 }
  :5
  if  (%bit_temp >= 16) {
    set %bit4 1
    set %bit_temp $calc(%bit_temp - 16)
  goto 4 }
  else {
    set %bit4 0
  goto 4 }
  :4
  if  (%bit_temp >= 8) {
    set %bit5 1
    set %bit_temp $calc(%bit_temp - 8)
  goto 3 }
  else {
    set %bit5 0
  goto 3 }
  :3
  if  (%bit_temp >= 4) {
    set %bit6 1
    set %bit_temp $calc(%bit_temp - 4)
  goto 2 }
  else {
    set %bit6 0
  goto 2 }
  :2
  if  (%bit_temp >= 2) {
    set %bit7 1
    set %bit_temp $calc(%bit_temp - 2)
  goto 1 }
  else {
    set %bit7 0
  goto 1 }
  :1
  if  (%bit_temp >= 1) {
    set %bit8 1
    set %bit_temp $calc(%bit_temp - 1)
  goto end }
  else {
    set %bit8 0
  goto end }
  :end
  set %byte %bit1 $+ %bit2 $+ %bit3 $+ %bit4 $+ %bit5 $+ %bit6 $+ %bit7 $+ %bit8
  return %byte
  halt 
}
on 1:load:{
  echo -a 3*** Checking script components

  if (%player == $null) { set %player $dir="What mp3 player would you like to use?" c:\*.exe }
  echo -a 3* % $+ player is set to: %player
  else { set %ask $?!="Would you like to change your mp3 player? (currently %player)" 
    if (%ask) { set %player $dir="What mp3 player would you like to use?" c:\*.exe }
  }

  if (%mp3dir == $null) { set %mp3dir $sdir="What is your defaul mp3 directory?" }
  echo -a 3* $chr(37) $+ mp3dir is set to: %mp3dir
  else { set %ask $?!="Would you like to change your default mp3 directory? (currently %mp3dir)"
    if (%ask) { set %mp3dir $sdir="What is your defaul mp3 directory?" }
  }

  if (%mp3list == $null) {  
    set %ask $?!="Do you have an mp3list?" 
    if (%ask == $true) { set %mp3list $dir="What mp3 playlist would you like to use?" c:\*.* }
    else {  set %mp3listroot $sdir="Please select the upper most parent mp3 directory"
      :ask
      set %ask $?!="Would you like to add an additional root dir?"
      if (%ask) {
        set %mp3listroot %mp3listroot $sdir="Please select the upper most parent mp3 directory"
        goto ask  
      }
      write -c mp3list.bat @echo off
      write -a mp3list.bat echo **** Please wait while the list is being generated ****
      write -c list.txt
      if ( [ $numtok( [ $+ [ %mp3listroot ] ] ,32) ] > 1) {
        set %i 1
        :loop
        if (%i > $numtok(%mp3listroot,32)) { goto end }
        write -a mp3list.bat dir /b/s $gettok( [ %mp3listroot ] ,%i,32) $+ *.mp3 >> list.txt
        inc %i
        goto loop
      }
      else { write -a mp3list.bat dir /b/s %mp3listroot $+ *.mp3 > $mircdirlist.txt }

      :end
      write -a mp3list.bat echo ** ALL DONE! **
      /run mp3list.bat
      set %mp3list list.txt
    }
  }
  echo -a 3* $chr(37) $+ mp3list is set to: %mp3list
  echo -a 3* Setup is complete! Enjoy!
}
