alias Ducat[MP3] dialog -md Duc[MP3] Duc[MP3]
on 1:load: { 
  { echo -s Chargement de Ducat[MP3] effectué !! }
  set %MontrerChanMp3 
  set %AfficherToutesInfos 
  set %LectureBoucle
  set %khz 
  set %FichierCurrentJoue 
  set %mp3size
  set %mp3size1 
  set %mp3size2
  set %PauseDuMP3 off
  set %MP3Muet off
  set %Volume 
}

menu channel,menubar,query {
  -   
  [MP3]:/Ducat[MP3]
}

dialog Duc[MP3] {
  title "MP3 By CoCa95"
  size -1 -1 536 465 
  edit "", 1, 25 30 220 20, read autohs 
  scroll "", 2, 25 62 220 8,  disable range 100 horizontal bottom 
  button "<<", 3, 25 75 30 20, 
  button "Play", 4, 60 75 40 20, 
  button "Pause", 5, 105 75 60 20, 
  button "Stop", 6, 170 75 40 20, 
  button ">>", 7, 215 75 30 20, 
  text "Taille:", 8, 20 107 40 15
  edit "", 9, 48 105 40 20, read autohs   
  text "Mb", 10, 90 107 20 15
  edit "", 11, 115 105 40 20, read autohs 
  text "Khz", 12, 157 107 22 15
  text "Temps Total:", 13, 70 137 80 15
  edit "", 14, 140 135 50 20, read autohs 
  edit "", 15, 188 105 40 20, read autohs 
  text "Kbps", 16, 230 107 22 15
  text "Temps Ecoulé:", 17, 70 167 80 15
  edit "", 18, 140 165 50 20, read autohs 
  box "Infos sur la chansons", 19, 15 10 242 345
  text "Titre :", 20, 30 202 40 15
  edit "", 21, 70 200 160 20, read autohs 
  text "Artiste :", 22, 30 227 40 15
  edit "", 23, 70 225 160 20, read autohs 
  text "Album :", 24, 30 252 40 15
  edit "", 25, 70 250 160 20, read autohs 
  text "Année :", 26, 30 277 40 15
  edit "", 27, 70 275 60 20, read autohs 
  text "Genre :", 28, 30 302 40 15
  edit "", 29, 70 300 160 20, read autohs 
  text "Autre :", 30, 30 327 40 15
  edit "", 31, 70 325 160 20, read autohs 
  list 32, 275 25 240 200, result size
  button "Ajouter", 33, 280 230 110 25
  button "Enlever", 34, 400 230 110 25
  button "Repertoire", 35, 285 260 220 25
  button "Sauvegarder la liste", 36, 280 290 110 25
  button "Charger une liste", 37, 400 290 110 25
  button "Effacer la liste", 38, 285 320 220 25
  box "Liste des MP3", 39, 270 10 250 345
  button "+", 40, 35 385 40 20
  button "-", 41, 80 385 40 20
  button "Muet", 42, 135 382 100 25
  box "Volume", 43, 15 360 242 60
  check "Montrer sur le chan", 44, 330 375 120 20
  check "Afficher toutes les infos", 45, 330 395 140 20
  box "Affichage", 46, 270 360 250 60
  check "Lecture continue des chansons", 48, 30 435 170 15
  button "QUITTER", 47, 440 430 80 30, cancel
}

on *:dialog:Duc[MP3]:init:O:{
  var %x = 1
  set %PauseDuMP3 off
  set %MP3Muet off

  while ( %x <= $lines(ListeParDefaut.txt) ) {
    did -ai Duc[MP3] 32 $nopath($read(ListeParDefaut.txt,%x)) 
    inc %x
  }
  if %MontrerChanMp3 == on { did -c Duc[MP3] 44 }
  if %AfficherToutesInfos == on { did -c Duc[MP3] 45 }
  if %LectureBoucle == on { did -c Duc[MP3] 48 }
}

alias mp3len {
  if (!$1-) { return 1 | halt }
  if (!$exists($1-)) { return 2 | halt }
  var %x = $calc($sound($1-).length /1000)
  return $gmt(%x,$iif(%x >= 3600,hh:nn:ss,nn:ss))
}

alias khz { 
  set %khz %mp3file
  set %khz $mid(%khz,2,1)
  if ($asc(%khz) > 249) set %khz 44
  else if ($asc(%khz) == 73) set %khz 44
  else if ($asc(%khz) == 243) set %khz 22
  else if ($asc(%khz) == 227) set %khz 11
  else set %khz 44
}

alias mp3.pos { 
  did -ra Duc[MP3] 18 $mp3position 
  if ( $mp3position >= $mp3len(%FichierCurrentJoue) && ( %LectureBoucle == off    ) ) { .timerMP3POSITION off | splay stop } 
  else if ( $mp3position >= $mp3len(%FichierCurrentJoue) && ( %LectureBoucle == on ) ) { .timerMP3POSITION off | splay stop | AVANCE |    .timerMP3POSITION 0 1 mp3.pos } 
}

alias mp3position {
  if (!$insong) { return FIN | halt }
  var %x = $calc($insong.pos /1000)
  return $gmt(%x,$iif(%x >= 3600,hh:nn:ss,nn:ss))
}

alias mp3.scroll { if ($dialog(Duc[MP3])) { did -c Duc[MP3] 2 $round($calc(($insong.pos / $insong.length) * 100),0) } | else .timermp3.scroll off }

alias MP3INFO {
  set %mp3size1 $file(%FichierCurrentJoue).size
  set %mp3size2 $calc(%mp3size1 / 1000000)
  set %mp3size $round(%mp3size2,2)
  if ( %AfficherToutesInfos == on && %MontrerChanMp3 == on ) { /msg $active 7,1J'ecoute 7[4,1 $nopath(%FichierCurrentJoue) 7] 9,1Durée 11,1[4,1 $mp3len(%FichierCurrentJoue) 11,1] 9,1Taille 11,1[4,1 %mp3size $+ Mb11,1] 9,1kHz 11,1[4,1 %khz 11,1]   9,1Kbps 11,1[4,1 $mp3(%FichierCurrentJoue).bitrate 11,1] 10,1 | goto end }
  if %MontrerChanMp3 == on { /msg $active 7J'ecoute 8[4 $nopath(%FichierCurrentJoue) 8] 8,1[11,12Merci A qui A qui  A dj A djba A djbass }
  :end
  mp3.scroll 
  { did -ra Duc[MP3] 1 $nopath(%FichierCurrentJoue) }
  { did -ra Duc[MP3] 9 %mp3size }
  { did -ra Duc[MP3] 11 $mp3(%FichierCurrentJoue).bitrate }
  { did -ra Duc[MP3] 14 $mp3len(%FichierCurrentJoue) }
  { did -ra Duc[MP3] 15 %khz }
  { did -ra Duc[MP3] 21 $sound($insong.fname).title }
  { did -ra Duc[MP3] 23 $sound($insong.fname).artist }
  { did -ra Duc[MP3] 25 $sound($insong.fname).album }
  { did -ra Duc[MP3] 27 $sound($insong.fname).year }
  { did -ra Duc[MP3] 29 $sound($insong.fname).genre }
  { did -ra Duc[MP3] 31 $sound($insong.fname).comment }
}

alias AVANCE {
  if ( %LigneDeLaListe == $lines(ListeParDefaut.txt) ) { set %LigneDeLaListe 0 }
  set %LigneDeLaListe $calc( %LigneDeLaListe + 1 )
  set %FichierCurrentJoue $read(ListeParDefaut.txt,%LigneDeLaListe)
  splay %FichierCurrentJoue
  set %DernierMp3 %FichierCurrentJoue
  MP3INFO 
}

on 1:dialog:Duc[MP3]:sclick:*: {
  if ( $did == 4 ) { 
    set %LigneDeLaListe $did($dname,32).sel
    set %FichierCurrentJoue $read(ListeParDefaut.txt,%LigneDeLaListe)
    splay %FichierCurrentJoue
    khz
    mp3.pos
    .timerMP3POSITION 0 1 mp3.pos
    MP3INFO
  }
  if ( $did == 6 ) { 
    splay stop | .timerMP3POSITION off
  }
  if ( $did == 5 ) { 
    if ( %PauseDuMP3 == off ) { splay pause | set %PauseDuMP3 on } 
    else if ( %PauseDuMP3 == on ) { splay resume | set %PauseDuMP3 off } 
  }
  if ( $did == 47 ) { 
  splay stop | .timerMP3POSITION off }
  if ( $did == 38 ) { write -c ListeParDefaut.txt | did -r $dname 32 }
  if ( $did == 35 ) { 
    if ( %RepTemp == $null ) { set %RepTemp dir }
    set %RepTemp $sdir(%RepTemp,Repertoire des Mp3 à ajouter)
    var %x $findfile(%RepTemp,*.mp3,0,1, write ListeParDefaut.txt $1- )
    did -r Duc[MP3] 32
    var %x = 1
    while ( %x <= $lines(ListeParDefaut.txt) ) {
      did -ai Duc[MP3] 32 $nopath($read(ListeParDefaut.txt,%x)) 
      inc %x
    }
  }
  if ( $did == 37 ) { 
    set %CheminLoadMp3 $dir="Choisi ta liste de MP3" $mircdir*.txt 
    var %x = 1 
    while ( %x <= $lines(%CheminLoadMp3) ) {
      did -ai $dname 32 $nopath($read(%CheminLoadMp3,%x))
      write ListeParDefaut.txt $read(%CheminLoadMp3,%x)
      inc %x
    }
  }
  if ( $did == 36 ) { if ($dialog(SauveList) == $null) { dialog -m SauveList SauveList } }
  if ( $did == 34 ) {
    set %LigneTempMp3 $did(Duc[MP3],32).sel
    write -dl [ $+ [ %LigneTempMp3 ] ] ListeParDefaut.txt
    did -d $dname 32 %LigneTempMp3
    unset %LigneTempMp3
  }
  if ( $did == 33 ) { 
    set %Mp3TempCHemin $dir="Choisi une chanson a ajouter" $mircdir*.mp3
    set %NomChansonTemp $gettok($nopath(%Mp3TempCHemin),1,46)
    write defaultplaylist.txt %Mp3TempCHemin
    did -a $dname 32 %NomChansonTemp
    unset %NomChansonTemp
    unset %Mp3TempCHemin
  }
  if ($did(Duc[MP3],44).state == 1) { set %MontrerChanMp3 on }
  else { set %MontrerChanMp3 off }
  if ($did(Duc[MP3],45).state == 1) { set %AfficherToutesInfos on }
  else { set %AfficherToutesInfos off }
  if ( $did == 42 ) {
    if ( %MP3Muet == off ) { set %MP3Muet on | vol -vu1 }
    else if ( %MP3Muet == on ) { set %MP3Muet off | vol -vu2 }
  }
  if ($did(Duc[MP3],48).state == 1) { set %LectureBoucle  on }
  else { set %LectureBoucle   off }
  if ( $did == 40 ) {
    Var %Volume = $Calc( $Int($Calc( $Vol(Wave) / 1300 )) * 1300 + 2600 ) 
    If ( %Volume > 65000 ) { Var %Volume = 65000 }
    Vol -w %Volume
  }
  if ( $did == 41 ) {
    Var %Volume = $Calc( $Int($Calc( $Vol(Wave) / 1300 )) * 1300 - 2600 ) 
    if ( %Volume < 1 ) { var %Volume = 0 }
    vol -w %Volume 
  }
  if ( $did == 7 ) { 
    AVANCE 
    .timerMP3POSITION 0 1 mp3.pos
  }
  if ( $did == 3 ) {
    if ( %LigneDeLaListe == $lines(ListeParDefaut.txt) ) { set %LigneDeLaListe 0 }
    set %LigneDeLaListe $calc( %LigneDeLaListe - 1 )
    set %FichierCurrentJoue $read(ListeParDefaut.txt,%LigneDeLaListe)
    splay %FichierCurrentJoue
    set %DernierMp3 %FichierCurrentJoue
    MP3INFO 
    .timerMP3POSITION 0 1 mp3.pos
  }
}
on 1:dialog:Duc[MP3]:dclick:32:{
  set %LigneDeLaListe $did($dname,32).sel
  set %FichierCurrentJoue $read(ListeParDefaut.txt,%LigneDeLaListe)
  splay %FichierCurrentJoue
  khz
  mp3.pos
  .timerMP3POSITION 0 1 mp3.pos
  set %mp3size1 $file(%FichierCurrentJoue).size
  set %mp3size2 $calc(%mp3size1 / 1000000)
  set %mp3size $round(%mp3size2,2)
  mp3.scroll 
  MP3INFO
} 
on 1:dialog:Duc[MP3]:scroll:*:{ 
  if ( $did == 2 ) {
    splay seek $round($calc(($did(Duc[MP3],2).sel / 100) * $insong.length),0)
  }
}

dialog SauveList {
  title "Sauvegarde de la liste de MP3"
  size -1 -1 155 52 
  option dbu
  edit "", 2, 55 9 90 10, autohs
  text " Par exemple C:\MaListe.txt ", 3, 35 21 90 8
  button "Sauvegarder", 4, 36 38 40 10, flat ok
  button "Annuler", 5, 80 38 40 10, flat cancel
  box "", 7, 3 2 148 30
  text "Sauvegarder sous", 8, 7 10 45 8
}
on 1:dialog:SauveList:init:0: { did -ra $dname 2 C:\ }
on 1:dialog:SauveList:sclick:4: {
  set %CheminSaveMp3 $did(SauveList,2).text
  write -c %CheminSaveMp3
  var %a = 1
  while ( %a <= $lines(ListeParDefaut.txt) ) {
    write -il [ $+ [ %a ] ] %CheminSaveMp3 $read(ListeParDefaut.txt,%a)
    inc %a
  }
}
