home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Programming / ace_gpl_release / src / lib / asm / sound.s < prev    next >
Encoding:
Text File  |  1998-10-04  |  5.2 KB  |  262 lines

  1. ;
  2. ; sound.s - an ACE linked library module: sound routines. 
  3. ; Copyright (C) 1998 David Benn
  4. ; This program is free software; you can redistribute it and/or
  5. ; modify it under the terms of the GNU General Public License
  6. ; as published by the Free Software Foundation; either version 2
  7. ; of the License, or (at your option) any later version.
  8. ;
  9. ; This program is distributed in the hope that it will be useful,
  10. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. ; GNU General Public License for more details.
  13. ;
  14. ; You should have received a copy of the GNU General Public License
  15. ; along with this program; if not, write to the Free Software
  16. ; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17. ;
  18. ; Author: David J Benn
  19. ;   Date: 15th,16th May 1992,
  20. ;         6th,30th June 1992,
  21. ;      4th,5th,6th,7th,19th,22nd July 1992
  22. ;      9th August 1992,
  23. ;      5th December 1992,
  24. ;      1st March 1993,
  25. ;      31st October 1993,
  26. ;      3rd April 1994
  27. ;
  28.  
  29.     ; sound xdefs
  30.  
  31.     xdef    _sound
  32.     xdef    _wave
  33.       xdef    _beep
  34.  
  35.  
  36.     ; * XREFS *
  37.  
  38.     xref    _initialise_waveform_data
  39.     xref    _waveformaddr0
  40.     xref    _waveformaddr1
  41.     xref    _waveformaddr2
  42.     xref    _waveformaddr3
  43.     xref    _wavelen0
  44.     xref    _wavelen1
  45.     xref    _wavelen2
  46.     xref    _wavelen3
  47.     xref    _sinwavedata
  48.  
  49.     ; dos library
  50.     xref    _DOSBase
  51.     xref    _LVODelay
  52.  
  53.     ; math library 
  54.     xref    _MathBase
  55.     xref    _LVOSPFix
  56.     xref    _LVOSPMul
  57.     xref    _LVOSPCmp
  58.     
  59.     SECTION sound_code,CODE
  60.  
  61. ; *** SOUND ROUTINES ***
  62.  
  63. ;
  64. ; SOUND - play a sound. d0=period; d1=duration (ffp); d2=volume; d3=voice.
  65. ;    - the hardware is programmed directly.
  66. ;
  67. _sound:
  68.     ; save parameters
  69.     move.w    d0,_period        ; 124..32767
  70.     move.l  d1,_duration        ; 0..999 (somewhat arbitrary)
  71.     move.w    d2,_volume        ; 0..64
  72.     move.w    d3,_voice        ; 0..3
  73.      
  74.     ; check parameters -> quit if any are out of range
  75.     
  76.     ; period 
  77.     cmpi.w    #124,_period
  78.     blt    _quitsound        ; can't be > 32767 by virtue of
  79.                     ; being a short integer
  80.  
  81.     ; duration    
  82.     move.l    _MathBase,a6    
  83.     move.l    _duration,d0
  84.     move.l    #0,d1
  85.     jsr    _LVOSPCmp(a6)        ; duration < 0?
  86.     blt    _quitsound
  87.     move.l    _duration,d0
  88.     move.l    #$f9c0004a,d1        
  89.     jsr    _LVOSPCmp(a6)        ; duration > 999?
  90.     bgt    _quitsound
  91.     
  92.     ; volume 
  93.     cmpi.w    #0,_volume
  94.     blt    _quitsound
  95.     cmpi.w    #64,_volume
  96.     bgt    _quitsound
  97.  
  98.     ; which audio channel?
  99.     cmpi.w    #0,d3
  100.     bne.s    _aud1
  101.  
  102.     ; audio channel 0
  103.     move.l    _dmactrl,a1
  104.     move.w    #1,(a1)            ; disable DMA
  105.     move.l    _audio0,a0
  106.     move.l    _waveformaddr0,(a0)    ; address of waveform table
  107.     move.w    _wavelen0,4(a0)        ; # of words in table
  108.     move.w    _period,6(a0)        ; period (read in rate)
  109.     move.w    _volume,8(a0)        ; volume
  110.     move.w    #$8201,(a1)        ; enable DMA (start sound)
  111.     jsr    _sound_delay
  112.     move.l    _dmactrl,a1        ; putchar may have corrupted a1
  113.     move.w    #1,(a1)
  114.     rts
  115.  
  116. _aud1:
  117.     cmpi.w    #1,d3
  118.     bne.s    _aud2
  119.     move.l    _dmactrl,a1
  120.     move.w    #2,(a1)
  121.     move.l    _audio1,a0
  122.     move.l    _waveformaddr1,(a0)
  123.     move.w    _wavelen1,4(a0)
  124.     move.w    _period,6(a0)        
  125.     move.w    _volume,8(a0)
  126.     move.w    #$8202,(a1)
  127.     jsr    _sound_delay
  128.     move.l    _dmactrl,a1
  129.     move.w    #2,(a1)
  130.     rts
  131.  
  132. _aud2:
  133.     cmpi.w    #2,d3
  134.     bne.s    _aud3
  135.     move.l    _dmactrl,a1
  136.     move.w    #4,(a1)
  137.     move.l    _audio2,a0
  138.     move.l    _waveformaddr2,(a0)
  139.     move.w    _wavelen2,4(a0)
  140.     move.w    _period,6(a0)        
  141.     move.w    _volume,8(a0)
  142.     move.w    #$8204,(a1)
  143.     jsr    _sound_delay
  144.     move.l    _dmactrl,a1
  145.     move.w    #4,(a1)
  146.     rts
  147.         
  148. _aud3:
  149.     cmpi.w    #3,d3
  150.     bne    _quitsound
  151.     move.l    _dmactrl,a1
  152.     move.w    #8,(a1)
  153.     move.l    _audio3,a0
  154.     move.l    _waveformaddr3,(a0)
  155.     move.w    _wavelen3,4(a0)
  156.     move.w    _period,6(a0)        
  157.     move.w    _volume,8(a0)    
  158.     move.w    #$8208,(a1)
  159.     jsr    _sound_delay
  160.     move.l    _dmactrl,a1
  161.     move.w    #8,(a1)
  162.     rts
  163.     
  164. _sound_delay:
  165.     move.l    _MathBase,a6
  166.     move.l    _duration,d1        ; duration
  167.     move.l    #$afced942,d0        ; 2.747
  168.     jsr    _LVOSPMul(a6)        ; ticks = duration*2.747
  169.     jsr    _LVOSPFix(a6)        ; ticks = (int)ticks
  170.     move.l    _DOSBase,a6
  171.     move.l    d0,d1            ; ticks
  172.     jsr    _LVODelay(a6)
  173.     rts        
  174.  
  175. _quitsound:            
  176.     rts
  177.  
  178. ;
  179. ; WAVE - create a new waveform table for SOUND command.
  180. ;     d0=voice; a0=address of waveform table; d1=length of waveform table.
  181. ;
  182. _wave:
  183.     ; halve # of bytes in waveform table to give # of words
  184.     divu    #2,d1    
  185.  
  186.     ; is it a SIN wave? (address=0)
  187.     cmpa.l    #0,a0
  188.     bne.s    _which_audio        ; -> NO
  189.     lea    _sinwavedata,a0        ; -> YES
  190.     move.w    #16,d1            ; length of sine waveform table
  191.     
  192. _which_audio:
  193.     ; which audio channel?
  194.     cmpi.w    #0,d0
  195.     bne.s    _waud1
  196.     move.l    a0,_waveformaddr0
  197.     move.w    d1,_wavelen0
  198.     rts
  199.  
  200. _waud1:
  201.     cmpi.w    #1,d0
  202.     bne.s    _waud2
  203.     move.l    a0,_waveformaddr1
  204.     move.w    d1,_wavelen1
  205.     rts
  206.  
  207. _waud2:
  208.     cmpi.w    #2,d0
  209.     bne.s    _waud3
  210.     move.l    a0,_waveformaddr2
  211.     move.w    d1,_wavelen2
  212.     rts
  213.  
  214. _waud3:
  215.     cmpi.w    #3,d0
  216.     bne.s    _quitwave
  217.     move.l    a0,_waveformaddr3
  218.     move.w    d1,_wavelen3
  219.     rts
  220.  
  221. _quitwave:    
  222.     rts
  223.  
  224. ;
  225. ; BEEP - issues a 0.12 second pulse through voice 0.
  226. ;      - save and restore all registers since this
  227. ;        routine can be called from C (see Ustringprint).
  228. ;
  229. _beep:
  230.     movem.l    d1-d7/a0-a6,-(sp)
  231.     move.w    #505,d0        
  232.     move.l    #$8ccccd42,d1    ; duration of 0.12 seconds.
  233.     move.w    #64,d2        ; full volume
  234.     move.w    #0,d3        ; voice 0
  235.     jsr    _sound
  236.     movem.l    (sp)+,d1-d7/a0-a6
  237.     rts
  238.  
  239. ; *************************
  240.  
  241.     SECTION sound_data,DATA
  242.  
  243. ; * sound & wave *
  244. _audio0:        dc.l  $dff0a0
  245. _audio1:        dc.l  $dff0b0
  246. _audio2:        dc.l  $dff0c0
  247. _audio3:        dc.l  $dff0d0
  248. _dmactrl:        dc.l  $dff096
  249.  
  250. ; *************************
  251.  
  252.     SECTION sound_mem,BSS
  253.  
  254. ; * sound *
  255. _period:        ds.w 1
  256. _duration:        ds.l 1
  257. _volume:        ds.w 1
  258. _voice:            ds.w 1
  259.  
  260.     END
  261.