/examples/sampler.scm

http://github.com/digego/extempore · Scheme · 103 lines · 44 code · 20 blank · 39 comment · 0 complexity · ff7f58036783c0b8aa83d944f9ef2943 MD5 · raw file

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;
  3. ;; This example shows how to use the builtin sampler
  4. ;;
  5. ;; You will first need to load and compile dsp_library.scm
  6. ;;
  7. ;; Then you're OK to go
  8. ;;
  9. ;; NOTE [at the moment compiling in a secondary thread is a little
  10. ;; [flakey. I'm working on this so in the mean time you'll
  11. ;; [just have to put up with the audio delays while compiling
  12. ;;
  13. ;; first find a stereo audio file of some kind (not mp3 or aac)
  14. ;; ogg wav or aif should all be fine
  15. ;; then load up a few excerpts from the file
  16. ;; set-sampler-index takes
  17. ;; 1st: the instrument to load into (the default sampler is called 'sampler')
  18. ;; 2nd: the audio file to load from
  19. ;; 3rd: an index (from 0-127) to load into
  20. ;; this should be the base frequency of the sample
  21. ;; in other words a middle C sample should be loaded into 60.
  22. ;; 4th: an offset in samples (frames without channels)
  23. ;; 5th: a duration or length in samples to read
  24. ;; first let's just read in at one index
  25. ;; we'll choose middle C - 60
  26. ;; make sure your audio file is long enough for the params below!!
  27. (set-sampler-index sampler "/tmp/audio.ogg" 60 500000 1000000)
  28. ;; playing back at 60 should playback without pitch shift
  29. (play-note (now) sampler 60 80 100000)
  30. ;; anything else will pitch shift
  31. ;; floating point is OK
  32. (play-note (now) sampler 67.25 80 100000)
  33. ;; a loop
  34. (define loop
  35. (lambda (beat dur)
  36. (play sampler (random 48 72) 80 dur)
  37. (callback (*metro* (+ beat (* .5 dur))) 'loop
  38. (+ beat dur)
  39. dur)))
  40. ;; start loop
  41. (loop (*metro* 'get-beat 4) 1)
  42. ;; read a directory full of samples
  43. ;; samples define a midi root i.e. 60.wav (for middle c)
  44. ;; must be stereo samples of type wav aif or ogg
  45. (define-macro (load-sampler sampler path)
  46. `(let ((files (sys:directory-list ,path)))
  47. (for-each (lambda (f)
  48. (if (regex:match? f "([0-9]*)\.(wav|aif|aiff|ogg)$")
  49. (let ((result (regex:matched f "([0-9]*)\.(wav|aif|aiff|ogg)$")))
  50. (set-sampler-index ,sampler f
  51. (string->number (cadr result)) 0 0))))
  52. files)))
  53. ;; load audio samples
  54. ;; I'm using piano samples
  55. (load-sampler sampler "/home/andrew/Desktop/piano")
  56. (define loop2
  57. (lambda (beat dur root)
  58. (play 3 sampler 36 100 dur)
  59. (for-each (lambda (p offset)
  60. (play (+ offset) sampler p 100 dur))
  61. (pc:make-chord 40 84 7
  62. (pc:chord root (if (member root '(10 8))
  63. '^7
  64. '-7)))
  65. '(1/3 1 3/2 1 2 3 13/3))
  66. (callback (*metro* (+ beat (* .5 dur))) 'loop2 (+ beat dur)
  67. dur
  68. (if (member root '(0 8))
  69. (random '(2 7 10))
  70. (random '(0 8))))))
  71. (loop2 (*metro* 'get-beat 4) 4 0)
  72. ;; make some more samplers
  73. (define-sampler sampler2 sampler-note sampler-fx)
  74. (define-sampler sampler3 sampler-note sampler-fx)
  75. ;; add new samplers to dsp
  76. (definec:dsp dsp
  77. (lambda (in time chan dat)
  78. (cond ((< chan 2.0) (+ (synth in time chan dat)
  79. (sampler in time chan dat)
  80. (sampler2 in time chan dat)
  81. (sampler3 in time chan dat)))
  82. (else 0.0))))
  83. ;; load new samplers