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