C
C   TRANSLATE MUSIC INTO MOTIVE FORM USEFUL FOR TRANSFORMATION
C
      SUBROUTINE TRM(NNT,THM3,THM1,MP1)
      INTEGER RL,SUM,MP1(75,2)
      INTEGER THM3(200,3),THM1(200,3),SDUR(10)
      COMMON/AAA/SDUR,NB,NGB
      RL=NB
      RL=RL/2
C   CALCULATE # OF 64THS IN HALF A BAR
      RL=RL*SDUR(NGB)
      MP1(1,1)=1
      KL=1
C   KL IS COUNTER FOR # OF MOTIVES
      L=2
C   L IS POINTER TO MP1
      NMT=0
C   NMT IS COUNTER FOR # OF NOTES IN MOTIVE
C   INITIALIZE SPECIAL TRIGGER CONTROL NUMBERS
      DO 54 I=1,200
   54 THM1(I,3)=4
      SUM=0
      J=1
C   J IS POINTER TO THM1
C   I IS POINTER TO THM3
      DO 31 I=1,NNT
      NMT=NMT+1
      NNN=SDUR(THM3(I,2))
      SUM=SUM+NNN
      IF(SUM.LE.RL)GO TO 55
C   IF SUM IS LARGER THAN RL THIS NOTE SPANS TWO MOTIVES
C   AND MUST BE DIVIDED
      NLN=SUM-RL
C   FIND 2ND NOTE DURATION
      DO 50 K2=1,10
   50 IF(NLN.EQ.SDUR(K2))GO TO 52
      GO TO 35
C   FIND 1ST NOTE DURATION
   52 NL2=NNN-NLN
      DO 51 K1=1,10
   51 IF(NL2.EQ.SDUR(K1))GO TO 53
      GO TO 35
   53 THM1(J,1)=THM3(I,1)
      THM1(J,2)=K1
C   TWO NOTES SEPARATED FOR TRANS PURPOSES ARE SLURRED TOGETHER
      THM1(J,3)=1
      J=J+1
      THM1(J,1)=THM3(I,1)
      THM1(J,2)=K2
      THM1(J,3)=THM3(I,3)
C   START OF NEW MOTIVE
      MP1(L,1)=J
      KZ=L-1
      MP1(KZ,2)=NMT
      NMT=1
      KL=KL+1
      L=L+1
      J=J+1
      SUM=NLN
      IF(SUM.NE.RL)GO TO 31
      NMT=0
      SUM=0
      MP1(L-1,2)=1
      MP1(L,1)=J
      L=L+1
      KL=KL+1
      GO TO 31
   55 IF(SUM.LT.RL)GO TO 57
C   THIS IS LAST NOTE IN THE MOTIVE
      MP1(L,1)=J+1
      KZ=L-1
      MP1(KZ,2)=NMT
      NMT=0
      KL=KL+1
      L=L+1
      SUM=0
   57 DO 56 K=1,3
   56 THM1(J,K)=THM3(I,K)
      J=J+1
   31 CONTINUE
      IF(SUM.EQ.0)GO TO 34
C   FILL UP REMAINDER OF MOTIVE WITH RESTS
      THM1(J,1)=1
      MP1(KL,2)=1
      NRS=RL-SUM
      DO 32 M=1,10
   32 IF(NRS.EQ.SDUR(M))GO TO 33
      GO TO 35
   33 THM1(J,2)=M
      THM1(J,3)=3
      NTSN=J
      GO TO 80
   34 NTSN=J-1
      GO TO 80
   35 WRITE(4,304)
  304 FORMAT(1X,9HNOT FOUND)
   80 CONTINUE
C   STORE # OF MOTIVES IN 25,1
C   STORE # OF NOTES IN MP1(25,2)
C   ASSUMING THAT # OF MOTIVES IS ALWAYS LESS THAN 25
      MP1(75,1)=KL-1
      MP1(75,2)=NTSN
      RETURN
      END
