home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / assembler / thesource / volume1 / source / modplayers / addsample.lha / AddSample.txt
Encoding:
Text File  |  1993-06-11  |  3.4 KB  |  91 lines

  1. Here is a routine that *SHOULD* sum two sample fragments together.  You
  2. feed it the address of the beginning of each sample fragment, as well as
  3. the address of the destination buffer.  The Playback Lengths are the
  4. Playback Rate of the sample (I.e. how many bytes per second) times the
  5. vertical blank length (1/60 or 1/50).  You should use a lookup table to
  6. calculate the fragment length from the hardware playback period values
  7. that most Trackers use.  Just load the registers, BSR to the routine, and 
  8. when it returns, the address registers will point to the start of the NEXT
  9. sample fragment.  That is, you can just save the registers, and drop them
  10. back in when you call next time.
  11.  
  12. It should be fast enough to do this for four voices within 1/60 second at
  13. maximum playback rate.  I hope.  Well, the worst-possible case is for two
  14. samples at NoiseTracker's highest frequency over a 1/50 second length; the
  15. sample fragment length is 314 bytes AT WORST for realtime.  If you don't 
  16. care about realtime, then samples of length up to 65536 bytes can be summed
  17. at arbitrary frequencies.
  18.  
  19. However, this routine does NOT handle independent hardware volume!!!  This
  20. could be implemented by having a "shiftdown" value for each sample.  each
  21. byte of the sample would then be shifted down by this much.  That's for
  22. next time.  Theoretically, a similar routine could handle the multiplexing
  23. of four voices (not just summing the summed output), but that could get
  24. extremely ugly... ;^)
  25.  
  26. Well, if there's anything wrong with it please let me know and I'll fix it.
  27. Remember, this is *UNTESTED*, so if somebody wants to test it, let me know.
  28.  
  29. -----
  30.  
  31. ; ADDSAMPLES
  32. ; Copyright 1992 Kenneth D. Miller III
  33. ; inputs:    d0 Playback Length of first sample (for 1 VBlank cycle)
  34. ;        d1 Playback Length of second sample (for 1 VBlank cycle)
  35. ;        a0 Address of first sample
  36. ;        a1 Address of second sample
  37. ;        a2 Address of destination buffer (chip RAM!)
  38.  
  39. AddSamp:
  40.     moveq    #0,    d5    ;note: d5 is the Update register
  41.     moveq    #0,    d6
  42.     moveq    #0,    d7
  43.     cmp    d0,    d1
  44.     bpl.s    ASInit
  45.     beq.s    ASFast        ;if playback rates are the same, use fast
  46.     exg    d0,    d1    ;make sure Sample 1 is the fast one
  47.     exg    a0,    a1
  48. ASInit:    move    d0,    d4    ;load counter
  49.     move.b    (a1)+,    d7    ;pre-load first byte of sample 2
  50. ASLoop:    move.b    (a0)+,    d6    ;load byte from sample 1
  51.     add.w    d7,    d6    ;add 'em, preserving byte from sample2
  52.     asr.w    #1,    d6    ;divide by 2
  53.     move.b    d6,    (a2)+    ;jam the summed byte to the output buffer
  54.     add    d1,    d5    ;increment Update register
  55.     cmp    d5,    d0
  56.     bmi    ASLp2        ;if it's greater than playback length 1
  57.     sub    d0,    d5    ;   then drop the Update register back
  58.     move.b    (a1)+,    d7    ;   and load another byte from Sample 2
  59. ASLp2:    dbra    d4,    ASLoop
  60.     rts
  61. ASFast:    move.l    #$FEFEFEFE,d5    ;load mask into d5
  62.     move.l    d0,    d4    ;load loop counter
  63.     btst    #1,    d0
  64.     beq    ASFas2
  65.     move.w    (a0)+,    d6    ;if length mod 4 != 0, pick off first word
  66.     move.w    (a1)+,    d7
  67.     and.w    d5,    d6
  68.     and.w    d5,    d7
  69.     lsr.w    #1,    d6
  70.     lsr.w    #1,    d7
  71.     add.w    d6,    d7
  72.     move.w    d7,    (a2)+
  73.     subq    #2,    d4
  74. ASFas2:    move.l    (a0)+,    d6    ;sum 4 bytes worth of samples
  75.     move.l    (a1)+,    d7
  76.     and.l    d5,    d6
  77.     and.l    d5,    d7
  78.     lsr.l    #1,    d6
  79.     lsr.l    #1,    d7
  80.     add.l    d6,    d7
  81.     move.l    d7,    (a2)+
  82.     subq    #4,    d4
  83.     bpl.s    ASFas2
  84.     rts
  85.  
  86. --
  87. kdmiller@athena.mit.edu | Kenny Miller, sole member of Digital Horizons USA
  88.     ///                 | UPCOMING COOL STUFF: KMUS music format, BrotBusters
  89. \\\///  Amiga Makes     | Demo, Scrolly Roller Demo, Columns, Psycho Ball,
  90.  \XX/   It Possible!    | AmigAtaxx, etc. (don't hold your breath ;^)
  91.