1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
6 ;;;; Purpose: Common Lisp DTML tone generator
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: June 2007
10 ;;;; $Id: cluck.lisp 11571 2007-03-09 14:53:51Z kevin $
12 ;;;; Copyright (c) 2007 Kevin M. Rosenberg
14 ;;;; Redistribution and use in source and binary forms, with or without
15 ;;;; modification, are permitted provided that the following conditions
17 ;;;; 1. Redistributions of source code must retain the above copyright
18 ;;;; notice, this list of conditions and the following disclaimer.
19 ;;;; 2. Redistributions in binary form must reproduce the above copyright
20 ;;;; notice, this list of conditions and the following disclaimer in the
21 ;;;; documentation and/or other materials provided with the distribution.
22 ;;;; 3. Neither the name of the author nor the names of the contributors
23 ;;;; may be used to endorse or promote products derived from this software
24 ;;;; without specific prior written permission.
26 ;;;; THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
27 ;;;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 ;;;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 ;;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
30 ;;;; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 ;;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 ;;;; OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 ;;;; HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 ;;;; LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 ;;;; OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 ;;;; *************************************************************************
41 (defconstant* +dtmf-tones+
63 (dial-tone . (350 . 440))
64 (ringback . (440 . 480))
69 (dolist (record +dtmf-tones+)
70 (push (list (car record) (cddr record) (/ (cadr record) (cddr record)))
72 (sort unsorted (lambda (a b) (< (third a) (third b))))))
74 (defun print-dtmf-ratios (&optional (stream *standard-output*))
75 (dolist (v (dtmf-ratios))
76 (format stream "~A ~D ~6,4F~%" (first v) (second v) (third v))))
78 ;; DTMF formula: f(t) = A(high)*sin(2*pi*f(high)*t) + A(low)*sin(2*pi*f(low)*t)
79 ;; A(low) / A(high) between 0.7 and 0.9
81 (defun dtmf-waveform (key duration sample-freq
82 &key (min -1d0) (max 1d0)
83 (element-type 'double-float)
85 "Returns the DTMF waveform of a key code for duration seconds
86 at a sample frequency of sample-freq. Waveform normalized to -1 to 1 output."
87 (setq dtmf-record (get-alist key +dtmf-tones+))
88 (unless dtmf-record (return-from dtmf-waveform nil))
90 (let* ((period (/ 1D0 sample-freq))
91 (samples (ceiling (* duration sample-freq)))
92 (wave (make-array (list samples) :element-type 'double-float))
93 (out-wave (make-array (list samples) :element-type element-type))
95 (amplitude-ratio 0.8D0)
99 (range (coerce (- max min) 'double-float))
102 (declare (double-float raw-min raw-max raw-range range
103 time amplitude-ratio f-high f-low))
104 (setq f-high (* 2 pi (car dtmf-record)))
105 (setq f-low (* 2 pi (cdr dtmf-record)))
110 (let ((a (+ (sin (* time f-high))
111 (* amplitude-ratio (sin (* time f-low))))))
118 (setf (aref wave i) a))
121 (setq raw-range (- raw-max raw-min))
124 (let ((scaled (+ (* range (/ (- (aref wave i) raw-min) raw-range)) min)))
125 (when (subtypep element-type 'integer)
126 (setq scaled (round scaled)))
127 (setf (aref out-wave i) scaled)))
131 (defun write-dtmf (file key duration sample-freq &key (delimiter #\tab)
133 (setq wave (dtmf-waveform key duration sample-freq))
134 (unless wave (return-from write-dtmf nil))
136 (with-open-file (os file :direction :output)
137 (let ((period (/ 1D0 sample-freq))
139 (declare (double-float time period))
141 (dotimes (i (length wave))
143 (format os "~F~A~F~%" time delimiter (aref wave i))
144 (incf time period)))))
146 (defun write-dtmf-wav (file key duration &key (sample-freq 8000)
147 (n-bits-per-sample 8)
149 (setq wave (dtmf-waveform key duration sample-freq
150 :min (ecase n-bits-per-sample
153 :max (ecase n-bits-per-sample
156 :element-type (list 'signed-byte n-bits-per-sample)))
157 (unless wave (return-from write-dtmf-wav nil))
159 (let ((sample (make-instance 'cl-wav-synth:sample
161 :n-bits-per-sample n-bits-per-sample
162 :n-samples-per-sec sample-freq
164 (cl-wav-synth::set-sample-info sample)
165 (cl-wav-synth::set-total-byte-from-data sample)
166 (cl-wav-synth::set-last-sample sample)
167 (cl-wav-synth:write-sample file sample)
171 (defun plot-dtmf (key duration sample-freq &aux wave)
172 (setq wave (dtmf-waveform key duration sample-freq))
173 (unless wave (return-from plot-dtmf nil))
175 (let ((period (/ 1D0 sample-freq))
176 (x (make-list (length wave)))
177 (y (make-list (length wave)))
179 (declare (double-float time period)
182 (dotimes (i (length wave))
184 (setf (nth i x) time)
185 (setf (nth i y) (aref wave i))
188 (cgn:with-gnuplot ('linux)
189 (cgn:set-range 'x 0 duration)
190 (cgn:set-range 'y -1 1)
191 (cgn:plot-points x y)