home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / jËzyki_programowania / ace_basic / ace / include / play_sound.h < prev    next >
Text File  |  1977-12-31  |  3KB  |  176 lines

  1.   << play a sound file! >>
  2.  
  3.   Currently handles IFF 8SVX format.
  4.   If format is unknown, a default 
  5.   sampling rate is assumed. 
  6.   
  7.   Author: David J Benn
  8.     Date: 6th,7th April,16th May,
  9.       30th June,
  10.       1st,3rd,4th,8th July,
  11.       22nd November 1992,
  12.       7th January 1993
  13. }
  14.  
  15. library exec
  16.  
  17. declare function AllocMem& library exec
  18. declare function FreeMem   library exec
  19. declare function xRead&       library dos
  20.  
  21. const NOTOK=-1
  22. const OK=0
  23.  
  24. longint offset&,samples_per_second&
  25.  
  26. SUB parse_sample(f$)
  27. shared offset&,samples_per_second&
  28. const default_rate=10000&
  29.  
  30.  { if IFF 8SVX sample, return
  31.    offset from start of file to
  32.    sample data and sampling rate in
  33.    samples per second. }
  34.  
  35.  open "I",1,f$
  36.  
  37.  '..FORM#### ?
  38.  dummy$ = input$(8,#1)
  39.  
  40.  '..8SVX ?
  41.  x$ = input$(4,#1)
  42.  if x$="8SVX" then
  43.    sample_format$="IFF 8SVX"
  44.  
  45.    '..skip VHDR###
  46.    dummy$ = input$(8,#1)
  47.  
  48.    '..skip ULONGs x 3 
  49.    dummy$ = input$(12,#1)
  50.  
  51.    '..get sampling rate bytes
  52.    hi%=asc(input$(1,#1))  '..high byte
  53.    lo%=asc(input$(1,#1))  '..low byte
  54.    samples_per_second&=hi%*256 + lo%
  55.  
  56.    '..find BODY
  57.    '..skip rest of Voice8Header structure
  58.    dummy$ = input$(6,#1)
  59.  
  60.    offset&=40  '..bytes up to this point
  61.    repeat 
  62.     repeat
  63.       x$=input$(1,#1)
  64.       offset&=offset&+1
  65.     until x$="B" and not eof(1)
  66.     if not eof(1) then
  67.       body$=input$(3,#1)
  68.       offset&=offset&+3
  69.     end if
  70.    until body$="ODY" and not eof(1) 
  71.  
  72.    if not eof(1) then
  73.      x$=input$(4,#1)  '..skip ####   
  74.      offset&=offset&+4
  75.    else
  76.      close 1
  77.      parse_sample=NOTOK
  78.      exit sub
  79.    end if
  80.    close 1
  81.  else
  82.    close 1
  83.    sample_format$="unknown"
  84.    offset&=0
  85.    samples_per_second&=default_rate
  86.    parse_sample=OK
  87.  end if
  88.  
  89. END SUB
  90.  
  91.  
  92. SUB play_sound(f$)
  93. shared offset&,samples_per_second&
  94. const maxsample=131070
  95. const channel=1
  96.  
  97. dim   wave_ptr&(100)
  98.  
  99. '..file size?
  100. open "I",1,f$
  101. f_size&=lof(1)
  102. close 1
  103.  
  104. if f_size&=0 then 
  105.   play_sound=NOTOK
  106.   library close exec
  107.   exit sub
  108. end if
  109.  
  110. '..parse the sample
  111. if parse_sample(f$) = NOTOK then
  112.   play_sound=NOTOK
  113.   library close exec
  114.   exit sub
  115. end if
  116.  
  117. '..get the sample bytes
  118. buffer&=AllocMem(f_size&,MEMF_CHIP) '...f_size& bytes of CHIP RAM
  119. if buffer& = NULL then 
  120.   avail&=fre(MEMF_CHIP)  '..max. contiguous CHIP RAM
  121.   play_sound=NOTOK
  122.   library close exec
  123.   exit sub
  124. end if
  125.  
  126. '..read whole sample
  127. open "I",1,f$  
  128. fh&=handle(1)
  129. if fh&=0 then clean.up
  130. bytes&=xRead(fh&,buffer&,f_size&)
  131. close 1
  132.  
  133. '..calculate period
  134. per& = 3579546 \ samples_per_second&  
  135.   
  136. '...setup waveform table for voice 0
  137. sz&=f_size&-offset&
  138.  
  139. if sz& <= maxsample then
  140.   '..play it in one go
  141.   wave channel,buffer&+offset&,sz&
  142.   dur&=.279365*per&*bytes&/1e6*18.2
  143.   sound per&,dur&,,channel
  144. else
  145.   segments&=sz&\maxsample
  146.   buf&=buffer&+offset&
  147.  
  148.   '..get the segment pointers
  149.   for i&=0 to segments&
  150.     wave_ptr&(i&)=buf&+maxsample*i&
  151.   next
  152.  
  153.   '..play sample in segments
  154.   for i&=0 to segments&
  155.     if sz& >= maxsample then 
  156.        wave channel,wave_ptr&(i&),maxsample 
  157.        bytes&=maxsample
  158.     else 
  159.        wave channel,wave_ptr&(i&),sz&
  160.        bytes&=sz&
  161.     end if
  162.     dur&=.279365*per&*bytes&/1e6*18.2
  163.     sound per&,dur&,,channel
  164.     sz&=sz&-maxsample
  165.   next   
  166. end if
  167.   
  168. clean.up:
  169.  FreeMem(buffer&,f_size&)
  170.  library close exec
  171.  
  172.  play_sound=OK
  173.  
  174. END SUB
  175.