m
oBc           @   s  d  Z  d Z d Z y e Wn d0 \ Z Z n Xy| d k Z d k Z d k Z d k Z d k	 Z	 e i
 i e i i e	 i e i    d d d g   d	 k l Z l Z Wn1 y e i i d
 e  Wn e GHn X  n Xe i d i e i  d Z e i d  d Z e i   d Z d   Z d   Z d   Z d k Z d k Z e e  d e" d d  d g d  e  d e" d d  d g d d  d1 g d2 g d3 g d4 g d5 g  Z# d   Z$ d   Z% d    Z& d!   Z' d"   Z( d#   Z) d$   Z* d%   Z+ d&   Z, d'   Z- d(   Z. d)   Z/ d*   Z0 d+   Z1 d,   Z2 d-   Z3 d.   Z4 e5 d/ j o e6 e  e i d  n d S(6   s  read ensoniq disk images

   you can get an image of an ensoniq disk by booting up Damn Small Linux
   or other Knoppix live CDROM on a PC, fetching epsread, and dumping the
   diskette in /dev/fd0 to a file:
    wget http://yceran.org/eps/epsread
    chmod +x epsread
    ./epsread > /tmp/workdisk.dat
   then copy it to someplace permanent:
    scp /tmp/workdisk.dat luser@tty.freeshell.org:

   don't try:
    dd if=/dev/fd0 of=/tmp/epsdisk.dat
   it'll seem to work, but you'll be missing a bunch of sectors

   disk format info is from:
   http://youngmonkey.ca/nose/audio_tech/synth/Ensoniq-DiskFormats.html
   and http://www.thoralt.de/download/EPSSongSeqInfo.pdf; the former
   resource seems to match the disk structure better, while the latter
   has more goodies on what's in the trackss#  
    eps -- read ensoniq EPS disk images
    Copyright (C) 2005  John Comeau <jc.jcomeau.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    s5   Not all needed libraries found, upgrade or check pathi   i    Ni   t   libt   python(   s   gpls	   jclicenses   %s
it   .c          G   s   t  S(   N(   t   False(   t   whatever(    (    t   ./eps.pyt
   DebugPrintB   s    c          G   s0   x) t  |  d  t i j o |  d }  q W|  S(   sA   reduces args to lists or scalars, helps with command-line testingi    N(   t   typet   argst   typest	   TupleType(   R   (    (    R   t   noTuplesE   s
       c          G   s<   t  |   }  x) t |  d  t i j o |  d }  q W|  S(   s8   reduces args to scalars, helps with command-line testingi    N(   R   R   R   R	   t   ListType(   R   (    (    R   t   scalarsJ   s       iX   t   notei   t
   aftertouchi   t   modwheeli   t   volumepedali   t	   timedelayi   t
   endoftracki   t   wtfc          G   s   yn t  |   }  |  d |  d |  d } } }  | t t i d  j o t |  | |   n t d |  Wn t i	 d t
 IJ  n Xd S(   s}   get command and process it

    example: eps /tmp/epsdisk.dat ls
    or: eps /tmp/epsdisk.dat cp "MY SONG" /tmp/my_song.dat
 i    i   i   t   __main__s   No such command %ss+   Usage: %s ENSONIQ_DISK_IMAGE COMMAND [ARGS]N(   R   R   t   imaget   commandt   dirt   syst   modulest   evalt	   Exceptiont   stderrt   self(   R   R   R   (    (    R   t   eps\   s      c    
      G   s3  t  |   }  |  d |  d } }  g  } t |  } | i d  } | i   | d d !d j o | i	 d  p t
 d  n x t d	 d
  D] }	 d |	 d } } | d j o
 d } n xm t | d d d  D]U } | | | | | d !} t |  d j  o" | d d j o | i |  q q Wq W| S(   s"   return list of files on disk imagei    i   i 
  i  i  t   OSt   DRs8   Not a VFX-SD image, and no other EPS formats implementedi   i   i   i   i   i   t    N(   R   R   R   t	   directoryt   opent	   imagedatat   readt   epsdatat   closet   endswithR   t   ranget   blockt
   blockstartt   skipt   offsett   entryt   lent   append(
   R   R/   R%   R-   R   R.   R#   R'   R,   R+   (    (    R   t   rawdirectorym   s(     
$ 
 %c          G   s   g  } x t |   D] } | i t | d  t | d  | d d !t i d | d d ! d t i d | d d ! d t i d	 | d d
 ! d t | d
  t i d	 | d  d d @g  q W| S(   Ni    i   i   i   s   >Hi   i   i   s   >Li   ii (   R   R2   R   R/   R1   t   ordt   structt   unpack(   R   R/   R   (    (    R   R#      s
     c       
   G   s   t  |   }  |  d |  d } }  g  } x t d d  D]y } t | |  } | i d  p q7 n xJ t d d d d  D]2 } | i
 t i d d	 | | | d ! d  qz Wq7 W| S(   s   file allocation blocksi    i   i   i   t   FBi   i   s   >LR"   N(   R   R   R   t   fabsR*   t   blocknumberR+   t	   blockdataR)   t   fabR1   R4   R5   (   R   R9   R7   R   R8   R:   (    (    R   R7      s       4c          G   sx   t  |   }  |  \ } } t |  t i j o t |  } n t |  } | i
 | d  | i d  } | i   | S(   Ni   (   R   R   R   R+   R   R	   t
   StringTypeR   R$   R%   t   seekR&   R'   R(   (   R   R%   R   R+   R'   (    (    R   R+      s    
c    	      G   s  t  |   }  |  \ } }  |  d } t | |  t i i |  p t d  n x t	 |  D] } | d i   | d } } | g } x7 t |  | D]% } | d j o Pq | i |  q Wt t i i | d |  d  } x$ | D] } | i t | |   q Wq\ Wd S(	   s0   dump all sequence/song files to target directoryi    s%   2nd arg must specify target directoryi   i   i   s   %s.efvt   wbN(   R   R   R   t	   targetdirR   t   ost   patht   isdirR   R#   R/   t   stript   namet   inodet
   fileblocksR7   R8   R1   R$   t   joint   outfilet   writeR+   (	   R   RC   RG   RD   R   R8   R>   R/   RE   (    (    R   t	   dumpfiles   s(     
 	  "  c          G   s   t  |   }  x3 |  D]+ } t | d  } | i   } | i   q Wd g  } } x| t
 ot t i d | | | d ! d d } | d j  o Pn t d |  | d 7} | i | | | | ! | | 7} qR Wt t |   GHd  S(   Nt   rbi@  s   >Li   i    t   tracklength(   R   R   t   rawfilenameR$   t   rawfileR&   t   dataR(   t   pointert   trackst   TrueR4   R5   RK   R   R1   t   reprt
   midievents(   R   RK   RP   RM   RL   RN   RO   (    (    R   t   vfxsequence   s"      % 
c    
      G   sM  t  |   }  d d d t |   d g g } x|  D]} | i d d g  d \ }	 } x |	 t |  j  o t i	 d | |	 |	 d ! d } |	 d 7}	 t d	 |  | d
 @| d @d ?| d @} } } | d j o t d  qY t | } t d |  t |  | |	 | | |  \ }	 } | o t d |  | | 7} qY qY Wq1 W| S(   Nt   MThdi   i   i`   t   MTrki    s   >Hi   s   commandword: %xi   i   i   i   s(   found non-command when expecting commands   command found: s   delay to next event: (   i    i    (   R   R   R0   t   mididatat   trackR1   RO   t   ticksR4   R5   t   commandwordR   t   command_signifiert   delayR   R   t   vfx_commandst   vfx_commandR   (
   R   R\   RY   R^   R[   RX   R   RZ   RW   RO   (    (    R   RS      s,      !
$
c          G   s   t  |   }  |  \ } }	 } } } | d @} t |  d }
 t
 i d | |	 |	 d ! d } |	 d 7}	 | d @d ?| d @} } | d j o9 t d  t
 i d | |	 |	 d ! d } |	 d 7}	 n t d	 | d
 |  t | d |
 | | g |  t | | d |
 | d g |  |	 | f S(   Ni   i   s   >Hi    i   i   i  s#   processing double-length note eventt   velocityt   durationi   (   R   R   R   RO   RY   RX   RW   R   R0   t   channelR4   R5   t   notedataR_   R`   R   t   insertevent(   R   RY   Rb   R   RX   R   R`   R_   RW   RO   Ra   (    (    R   R      s    
!

!!c          G   sc   t  |   }  |  \ } } } } } t i d | | | d ! d } t
 d |  | d 7} | | f S(   sC   found this in one sequence just before end-of-track, maybe a no-op?s   >Hi   i    s   effing dataN(   R   R   R   RO   RY   RX   RW   R4   R5   RN   R   (   R   RY   RN   RX   R   RW   RO   (    (    R   R      s     !
c          G   sc   t  |   }  |  \ } } } } } t i d | | | d ! d } t
 d |  | d 7} | | f S(   Ns   >Hi   i    s   volume pedal data(   R   R   R   RO   RY   RX   RW   R4   R5   RN   R   (   R   RY   RN   RX   R   RW   RO   (    (    R   R      s    !
c          G   sc   t  |   }  |  \ } } } } } t i d | | | d ! d } t
 d |  | d 7} | | f S(   Ns   >Hi   i    s   modwheel data(   R   R   R   RO   RY   RX   RW   R4   R5   RN   R   (   R   RY   RN   RX   R   RW   RO   (    (    R   R      s    !
c          G   sc   t  |   }  |  \ } } } } } t i d | | | d ! d } t
 d |  | d 7} | | f S(   Ns   >Hi   i    s   aftertouch data(   R   R   R   RO   RY   RX   RW   R4   R5   RN   R   (   R   RY   RN   RX   R   RW   RO   (    (    R   R     s    !
c          G   sg   t  |   }  |  \ } } } } } t i d | | | d ! d } t
 d |  | d 7} | | | f S(   Ns   >Hi   i    s   timedelay data(   R   R   R   RO   RY   RX   RW   R4   R5   RN   R   (   R   RY   RN   RX   R   RW   RO   (    (    R   R     s    !
c          G   sy   t  |   }  |  \ } } } } } | t |  j o t d | |  n t d  t	 | d d d d g |  | | f S(   Ns   garbage at end of tracks   end of tracki   i/   i    t    (
   R   R   R   RO   RY   RX   RW   R0   R   Rc   (   R   RY   RX   R   RW   RO   (    (    R   R     s    
c          G   s   t  |   }  |  \ } } } t d | d |  t d   | d d  } xu t d t	 |   D]^ } | d d | d } | | d j  o' | d i d | | g |  d Sq^ | | 8} q^ W| d i | g |  d S(   s   insert MIDI event into track list at correct time offset

    not by any means optimized, going for "simple" rather than "fast"
 RY   t   eventc         C   s   |  d S(   Ni    (   t   element(   Rf   (    (    R   t   <lambda>*  s    ii   i    N(   R   R   RY   Re   RP   R   t   mapt
   deltatimesR*   R0   t   indext   deltat   insertR1   (   R   RY   Rj   RP   Ri   Rk   Re   (    (    R   Rc   "  s      c          G   s?  t  |   }  |  d |  d } }  t i |  } | d d @| d d } } } t i	   d t i	 |  d j o
 d } n d | t i
 | t i	 |   f GHx t |  D] } | d | d | d	 | d
 f \ }	 } } }
 |	 d j o d | |
 f GHq |	 d j o d | GHq |	 d j o q d |	 GHq Wd S(   s   list files on disk imagei    i   i  i   s   %b %e %Ys   %b %e %H:%Ms   %o %si   i   i   i   s	   "%s" %10ss   "%s"s%   Unimplemented directory entry type %dN(   R   R   R   R?   t   statt   modbitst   mtimet
   timeformatt   timet	   localtimet   strftimeR#   R/   t	   entrytypeRC   RD   t   size(   R   Rm   Rn   RC   RD   R   Ro   Rp   R/   Rt   Ru   (    (    R   t   ls5  s$      $
$ .R   (   i   i    (   i   s   modwheel(   i   s   volumepedal(   i   s	   timedelay(   i   s
   endoftrack(   i   s   wtf(7   t   __doc__t	   Copyrightt   errormessageRQ   R   R   R?   R	   t   ret   pwdR@   R1   t   sepRF   t   getpwuidt   geteuidt   com.jcomeaut   gplt	   jclicenseR   RH   t   argvt   splitR   R   t   originalselfR   R   R   Rq   R4   t   dictRh   t   NoneR*   R]   R   R2   R#   R7   R+   RI   RT   RS   R   R   R   R   R   R   R   Rc   Rv   t   __name__R   (%   Rx   R   Rc   R   R4   R7   R   R   Rz   Rv   R]   RI   RQ   R   R   R   R   R   R{   R   R	   R   R   R2   R   R   RS   Ry   R   RT   R   R   R   Rq   R#   R?   R+   (    (    R   t   ?   sV   -8				o
																	
		