Guest User

j602\media\wav\wav.ijs

a guest
Dec 8th, 2014
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
J 6.25 KB | None | 0 0
  1. NB. media/wav - Windows WAV file creation and play
  2. NB.
  3. NB. 12/03/2006 Oleg Kobchenko
  4.  
  5. require 'dll files'
  6.  
  7. coclass 'pwav'
  8.  
  9. NB. =========================================================
  10. NB. playing wave files
  11. PlaySound=: 'winmm PlaySoundA > i *c i i'&cd
  12.  
  13. 'SND_SYNC SND_ASYNC SND_MEMORY SND_LOOP SND_PURGE SND_ALIAS'=: 0 1 4 8 64 65536
  14.  
  15. NB.*wavplay v plays wav from file or memory
  16. NB.   y  wav file or char vector of wav format
  17. NB.  [x] SND_* flags, SND_SYNC default, see wav.ijs for details
  18. wavplay=: SND_SYNC&$: : (4 : 0)
  19.   PlaySound y;0;x
  20. )
  21.  
  22. NB. =========================================================
  23. NB. making wave files
  24. BIGEND=. ({.a.)={.1(3!:4)1
  25. '`i2 i4'=: (0 1+2*BIGEND){ (1&ic)`(2&ic)`([: , _2 |.\ 1&ic)`([: , _4 |.\ 2&ic)
  26. RATE=: 11000
  27.  
  28. NB.*wavmake v wav format from samples
  29. NB.   y  samples vector or (2,N)=$y matrix for stereo
  30. NB.       range is 0..255 or _32768..32767
  31. NB.  [x] sample rate in Hz, RATE default of 11000
  32. wavmake=: 3 : 0
  33.   RATE wavmake y               NB. x: sample rate, eg 11 kHz
  34. :
  35.   bs=. 8 16{~255<>./,y         NB. bits per sample
  36.   ba=. (bs*#$y) <.@% 8         NB. block align
  37.   br=. x * ba                  NB. byte rate (bytes/sec)
  38.   d=. 'data',(,~ i4@#) i2`({&a.)@.(bs=8) ,|:y
  39.   f=. 'fmt ',(i4 16),(i2 1),(i2 #$y),(i4 x),(i4 br),(i2 ba),i2 bs
  40.   'RIFF',(,~ i4@#) 'WAVE',f,d
  41. )
  42.  
  43. NB. =========================================================
  44. NB. making notes
  45. freq=: 440*2^12%~-&9
  46. note=: 0.25&$: :(4 : '<.255*(%>./)(-<./) 1 o.y*x*2p1*(i.%<:)1+RATE*x')"0
  47.  
  48. NB.*wavmakenote v make wav format from musical notes
  49. wavmakenote=: [: wavmake ;@(<@note freq)
  50.  
  51. NB.*wavnote v plays musical notes
  52. NB.   y  tones 0=C, 1=C#, 2=D of main octave
  53. NB.  [x] durations in sec, 0.25 default of 1/4 sec
  54. wavnote=: 4 wavplay wavmakenote
  55.  
  56.  
  57. NB. =========================================================
  58. NB. reading PCM wave files
  59.  
  60. ic=: 3!:4             NB. integer conversion
  61. bigEnd=: 2 : 'u`v@.(1=_1 ic 0 1{a.)'
  62. int=: _2&ic  bigEnd  ( _2 ic [: , _4 |.\ ] )
  63. i24=: (_2 ic [: , (0{a.) ,.~ _3 ]\ ])  bigEnd  ( _2 ic [: , (0{a.) ,. _3 |.\ ] )
  64. sht=: _1&ic  bigEnd  ( _1 ic [: , _4 |.\ ] )
  65. byt=: a.&i.
  66. substr=: ,."1@[ ];.0 ]
  67.  
  68. oibeg=: 3 : 'OFFS=: 0'  NB. offset iterator
  69. oimov=: 3 : 'y,~y-~OFFS=: OFFS+y'
  70.  
  71. wavhead=: 3 : 0
  72.   oibeg''
  73.          chunk         =:        y substr~ oimov 4
  74.   assert chunk         -: 'RIFF'
  75.          chunkSize     =: {. int y substr~ oimov 4
  76.          format        =:        y substr~ oimov 4
  77.   assert format        -: 'WAVE'
  78.  
  79.          subChunk1     =:        y substr~ oimov 4
  80.          chunkSize1    =: {. int y substr~ oimov 4
  81.          format1       =: {. sht y substr~ oimov 2
  82.   assert format1       -: 1
  83.  
  84.          chanels       =: {. sht y substr~ oimov 2
  85.          sampleRate    =: {. int y substr~ oimov 4
  86.          byteRate      =: {. int y substr~ oimov 4
  87.          blockAlign    =: {. sht y substr~ oimov 2
  88.          bitsPerSample =: {. sht y substr~ oimov 2
  89.  
  90.          aux=: ''
  91.   while. OFFS<#y do.
  92.          subChunk2     =:        y substr~ oimov 4
  93.          chunkSize2    =: {. int y substr~ oimov 4
  94.     if. 'data'-:subChunk2 do. break. end.
  95.          aux=: aux,<             y substr~ oimov chunkSize2
  96.   end.
  97.          sampleSize    =: (bitsPerSample <.@% 8) { 1 1 2 3 4
  98.          sampleCount   =: chunkSize2 <.@% sampleSize
  99. )
  100.  
  101. NB.*wavdata v WAVE data from noun
  102. NB.   y  wave format
  103. NB.  [x] samples: (index,count)[, .. ,:index,count]
  104. wavdata=: 3 : 0
  105.   '' wavdata y
  106. :
  107.   wavhead y
  108.   n=. oimov chunkSize2
  109.   if. #x do. n=. (x*sampleSize) +"1 ({.n),0 end.
  110.   f=. ]`byt`sht`i24`[email protected]
  111.   data=: f , n substr y
  112. )
  113.  
  114. NB.*wavinfo v WAVE info from noun
  115. NB.   y  wave format
  116. wavinfo=: 3 : 0
  117.   wavhead y
  118.   r=.      'File Type       ',chunk
  119.   r=. r,LF,'Total Size      ',":8+chunkSize
  120.   r=. r,LF,'File Format     ',format
  121.   r=. r,LF,'Channels        ',":chanels
  122.   r=. r,LF,'Sample Rate     ',":sampleRate
  123.   r=. r,LF,'Sample Count    ',":sampleCount
  124.   r=. r,LF,'Bits per Sample ',":bitsPerSample
  125. )
  126.  
  127. NB.*wavfile v WAVE info or data from file
  128. NB.   y  wav file
  129. NB.  [x] what[;samples]
  130. NB.      what:    0-info 1-head 2-data
  131. NB.      samples: see wavdata
  132. wavfile=: 3 : 0
  133.   0 wavfile y
  134. :
  135.   require 'jmf'
  136.   r=. ''
  137.   JCHAR map_jmf_ 'WAVE_pwav_';y;'';1  NB. readonly
  138.   'fn nx'=. 2{.boxopen x
  139.   try.
  140.     select. fn
  141.     case. 1 do. r=.    wavhead WAVE_pwav_
  142.     case. 2 do. r=. nx wavdata WAVE_pwav_
  143.     case.   do. r=.    wavinfo WAVE_pwav_
  144.     end.
  145.   catchd. 0 end.
  146.   unmap_jmf_ 'WAVE_pwav_'
  147.   r
  148. )
  149.  
  150. NB. =========================================================
  151. wavplay_z_=: wavplay_pwav_
  152. wavmake_z_=: wavmake_pwav_
  153. wavmakenote_z_=: wavmakenote_pwav_
  154. wavnote_z_=: wavnote_pwav_
  155. wavhead_z_=: wavhead_pwav_
  156. wavdata_z_=: wavdata_pwav_
  157. wavinfo_z_=: wavinfo_pwav_
  158. wavfile_z_=: wavfile_pwav_
  159.  
  160.  
  161. NB. =========================================================
  162. Note 'Test playing and making waves'
  163. scriptdoc 'media/wav'
  164.  
  165. wavplay jpath'~addons/media/wav/test1.wav'
  166. 1 wavplay jpath'~addons/media/wav/test1.wav'      NB. async
  167. (1+8) wavplay jpath'~addons/media/wav/test1.wav'  NB. async loop
  168. 64 wavplay <0                                     NB. stop
  169.  
  170. load 'files'
  171. (wavmake i.100) fwrite jpath'~addons/media/wav/test2.wav'
  172. (wavmake 1000?@$200) fwrite jpath'~addons/media/wav/test3.wav'
  173. (wavmake ,~^:6 i.200) fwrite jpath'~addons/media/wav/test4.wav'
  174.  
  175. 4 wavplay 2000 wavmake 1000?@$200              NB. memory
  176. 4 wavplay wavmake <.255*(%>./)(-<./) 1 o.440*    2p1*(i.%<:) RATE_pwav_
  177. 4 wavplay wavmake <.255*(%>./)(-<./) 1 o.440*1 o.2p1*(i.%<:) RATE_pwav_
  178.  
  179. 65536 wavplay&>({.,&.>}.);:'System Asterisk Exclamation Exit Hand Question'
  180.  
  181. 0.25 0.25 0.5 1 wavnote 0 2 4 5
  182. 0.25 wavnote 0 2 4 5
  183. joy=.     11 11 12 14  14 12 11 9  7 7 9 11  11 9 9 9
  184. joy=. joy,11 11 12 14  14 12 11 9  7 7 9 11  9  7 7 7
  185. 0.35 wavnote joy
  186. (0.35 wavmakenote joy) fwrite jpath'~addons/media/wav/joy.wav'
  187.  
  188. <.0.5+freq_pwav_+i.12  NB. main octave
  189. load 'plot'
  190. plot 1 o.1 2 4*/2p1*(i.%<:)101
  191. )
  192.  
  193. Note 'Test reading waves'
  194.   load 'plot'
  195.   W=. fread jpath '~addons/media/wav/test1.wav'
  196.   wavinfo W
  197.   plot wavdata W
  198.  
  199.   W=. fread jpath '~addons/media/wav/test2.wav'
  200.   wavinfo W
  201.   plot wavdata W
  202.  
  203.   wavfile jpath '~addons/media/wav/test1.wav'
  204.   wavfile jpath '~addons/media/wav/test2.wav'
  205.   plot 2 wavfile jpath '~addons/media/wav/test1.wav'
  206. )
Advertisement
Add Comment
Please, Sign In to add comment