/extra/synth/example/example.factor

http://github.com/abeaumont/factor · Factor · 38 lines · 31 code · 5 blank · 2 comment · 0 complexity · 5971ca54c8f2be94e0d90c181bcf4a2c MD5 · raw file

  1. ! Copyright (C) 2008 Alex Chapman
  2. ! See http://factorcode.org/license.txt for BSD license.
  3. USING: accessors arrays kernel namespaces make openal openal.alut sequences
  4. synth synth.buffers ;
  5. IN: synth.example
  6. : play-sine-wave ( freq seconds sample-freq -- )
  7. init-openal
  8. <16bit-mono-buffer> >sine-wave-buffer send-buffer id>>
  9. 1 gen-sources first
  10. [ AL_BUFFER rot set-source-param ] [ source-play ] bi
  11. check-error ;
  12. : test-instrument1 ( -- harmonics )
  13. [
  14. 1 0.5 <harmonic> ,
  15. 2 0.125 <harmonic> ,
  16. 3 0.0625 <harmonic> ,
  17. 4 0.03125 <harmonic> ,
  18. ] { } make ;
  19. : test-instrument2 ( -- harmonics )
  20. [
  21. 1 0.25 <harmonic> ,
  22. 2 0.25 <harmonic> ,
  23. 3 0.25 <harmonic> ,
  24. 4 0.25 <harmonic> ,
  25. ] { } make ;
  26. : sine-instrument ( -- harmonics )
  27. 1 1 <harmonic> 1array ;
  28. : test-note-buffer ( note -- )
  29. init-openal
  30. test-instrument2 swap cd-sample-freq <16bit-mono-buffer>
  31. >note send-buffer id>>
  32. 1 gen-sources first [ swap queue-buffer ] [ source-play ] bi
  33. check-error ;