(asdf:oos 'asdf:load-op :cl-alsa)

(defpackage :my-test
  (:use :cl :cl-alsa))

(in-package :my-test)

;;;; Sample Generators ;;;;

(defconstant *MAX-VOLUME* 65535)
(defun noise (n) (random *MAX-VOLUME*))
(defun slowpitch (n) (truncate (* .5 *MAX-VOLUME* (+ (sin (* .00001 n)) 1))))
(defun highpitch (n) (truncate (* .5 *MAX-VOLUME* (+ (sin n) 1))))
(defun tone (n) (truncate (* .5 *MAX-VOLUME* (+ (sin (* .005 n)) 1))))
(defun multitone (n) (truncate (* .5 (+ (highpitch n) (tone n)))))

;;;; Audio Play Function ;;;;

(defun test-audio (samp-func duration)
  (declare (fixnum duration))
  (let ((data (make-array duration :element-type '(unsigned-byte 16))))
    (dotimes (n duration)
      (setf (elt data n) (funcall samp-func n)))
    (with-pcm-stream (pcm :data-format :snd-pcm-format-u16-le)
      (stream-write-sequence pcm data 0 duration))))

;;;; Audio Tests ;;;;

(format t "NOISE...")
(test-audio #'noise 65535)
  
(format t "ANNOY...")
(test-audio #'highpitch 32768)

(format t "DIAL...")
(test-audio #'tone 98304)

(format t "CLICK...")
(test-audio #'multitone 1270)

(format t "COMBO...")
(test-audio #'multitone 50000)

(format t "slow...")
(test-audio #'slowpitch 200000)