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 / PRGS.lha / Sound / play.b next >
Text File  |  1994-01-10  |  4KB  |  207 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.   USAGE 
  8.   -----
  9.   CLI/Shell:     Play [<file> [<sampling rate>]] | ?
  10.  
  11.   Author: David J Benn
  12.     Date: 6th,7th April,16th May,
  13.       30th June,
  14.       1st,3rd,4th,8th,28th July 1992,
  15.       6th,21st,25th January 1993,
  16.       3rd February 1993,
  17.       10th July 1993,
  18.       3rd January 1994
  19. }
  20.  
  21. declare function xRead&    library
  22.  
  23. longint offset&,samples_per_second&
  24. string  path$
  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.  '..skip 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.  
  58.    '..skip rest of Voice8Header structure
  59.    dummy$=input$(6,#1)
  60.  
  61.    offset&=40  '..bytes up to this point
  62.    repeat 
  63.     repeat
  64.       x$=input$(1,#1)
  65.       offset&=offset&+1
  66.     until x$="B" and not eof(1)
  67.     if not eof(1) then
  68.       body$=input$(3,#1)
  69.       offset&=offset&+3
  70.     end if
  71.    until body$="ODY" and not eof(1) 
  72.  
  73.    if not eof(1) then
  74.      x$=input$(4,#1)  '..skip ####   
  75.      offset&=offset&+4
  76.    else
  77.      print "Error in file format!"
  78.      stop
  79.    end if
  80.    close 1
  81.  else
  82.    close 1
  83.    sample_format$="unknown"
  84.    offset&=0
  85.    if argcount<>2 then samples_per_second&=default_rate
  86.  end if
  87.  
  88.  '.. give info about sample
  89.  print "Sound file format is ";sample_format$;"."
  90.  
  91.  if sample_format$="unknown" then
  92.    if argcount<>2 then
  93.      print "Playback rate is";samples_per_second&;"Hz."
  94.    end if
  95.  else 
  96.    print "Sample was recorded at";samples_per_second&;"Hz."
  97.  end if
  98.  
  99. END SUB
  100.  
  101.  
  102. SUB play_sound(f$)
  103. shared offset&,samples_per_second&
  104. const maxsample=131070
  105. const channel=1
  106. const CHIP=0, MAXCHIP=2
  107.  
  108. dim   wave_ptr&(100)
  109.  
  110.  print 
  111.  print "*** ACE sound player ***"
  112.  
  113. '..file sample_size?
  114. open "I",1,f$
  115. sample_size&=lof(1)
  116. close 1
  117.  
  118. if sample_size&<>0 then
  119.   print f$;" contains";sample_size&;"bytes."
  120. else
  121.   print "Can't open ";f$;"."
  122.   stop
  123. end if
  124.  
  125. parse_sample(f$)
  126.  
  127. '..get the sample bytes
  128. buffer&=Alloc(sample_size&,CHIP) '...sample_size& bytes of CHIP RAM
  129. if buffer& = NULL then 
  130.   avail&=fre(MAXCHIP)  '..max. contiguous CHIP RAM
  131.   print "Largest contiguous CHIP RAM is";avail&;"bytes." 
  132.   stop
  133. end if
  134.  
  135. open "I",1,f$  
  136. fh&=handle(1)
  137. if fh&=0 then
  138.   print "Can't open ";f$;"."
  139.   stop 
  140. end if
  141. bytes&=xRead(fh&,buffer&,sample_size&)
  142. close 1
  143.  
  144. '..calculate period
  145. if argcount<>2 then 
  146.   per& = 3579546 \ samples_per_second&  
  147. else
  148.   sample_rate_requested&=clng(val(arg$(2)))
  149.   per& = 3579546 \ sample_rate_requested&
  150.   print "Requested sampling rate is";sample_rate_requested&;"Hz."
  151. end if
  152.   
  153. '...setup waveform table for voice 0
  154. sz&=sample_size&-offset&
  155.  
  156. if sz& <= maxsample then
  157.   '..play it in one go
  158.   wave channel,buffer&+offset&,sz&
  159.   dur&=.279365*per&*bytes&/1e6*18.2
  160.   sound per&,dur&,,channel
  161. else
  162.   segments&=sz&\maxsample
  163.   buf&=buffer&+offset&
  164.  
  165.   '..get the segment pointers
  166.   for i&=0 to segments&
  167.     wave_ptr&(i&)=buf&+maxsample*i&
  168.   next
  169.  
  170.   '..play sample in segments
  171.   for i&=0 to segments&
  172.     if sz& >= maxsample then 
  173.        wave channel,wave_ptr&(i&),maxsample 
  174.        bytes&=maxsample
  175.     else 
  176.        wave channel,wave_ptr&(i&),sz&
  177.        bytes&=sz&
  178.     end if
  179.     dur&=.279365*per&*bytes&/1e6*18.2
  180.     sound per&,dur&,,channel
  181.     sz&=sz&-maxsample
  182.   next   
  183. end if
  184.  
  185. END SUB
  186.  
  187.  
  188. { ** main ** }
  189.  
  190. if argcount=0 then
  191.   path$ = FileBox$("Choose a sound sample")
  192. else
  193.   if arg$(1)="?" then
  194.     print "usage: ";arg$(0);" [<file> [<sampling rate>]] | ?"
  195.     stop
  196.   else
  197.     path$ = arg$(1)
  198.   end if
  199. end if
  200.  
  201. if path$<>"" then 
  202.   play_sound(path$)
  203. else
  204.   dummy = MsgBox("No sound file selected.","OK")
  205. end if
  206.