Lisp HUG Maillist Archive

SOLVED: Playing a generated sound

Hello,

A while back I asked about playing generated sounds, and with some help from Martin, I’ve managed to get it working on a basic level.  It involves loading a 44 KHz 2 channel .WAV sound file and then generating the samples directly into the raw data returned by read-sound-file.  

Here’s a function for generating a sine wave for 1 second:

(defun generate-sine  (&key (frequency 220) (volume 10000) (samples 44100))
  (let ((amplitude 0)
	(data (capi:read-sound-file "~/BusFactor1/Data/Audio/1-second-of-silence.wav")))
    (dotimes (i (1- samples))
      (setf amplitude (floor (* volume (sin (/ (* i pi 2 frequency) 44100)))))
      
      (when (>= amplitude (ash 1 15))
	  (decf amplitude (ash 1 16)))

      (setf (aref data (+ 44 (* i 2))) (ldb (byte 8 0) amplitude))
      (setf (aref data (+ 45 (* i 2))) (ldb (byte 8 8) amplitude))
      (setf (aref data (+ 46 (* i 2))) (ldb (byte 8 0) amplitude))
      (setf (aref data (+ 47 (* i 2))) (ldb (byte 8 8) amplitude)))

    (capi:load-sound data)))

This returns something that can be played directly with capi:play-sound.

—
Burton Samograd

_______________________________________________
Lisp Hug - the mailing list for LispWorks users
lisp-hug@lispworks.com
http://www.lispworks.com/support/lisp-hug.html

Updated at: 2020-12-10 08:31 UTC