source: trunk/source/contrib/krueger/InterfaceProjects/Speech/speech-controller.lisp @ 13390

Last change on this file since 13390 was 13390, checked in by plkrueger, 11 years ago

New contrib from Paul Krueger

File size: 4.4 KB
Line 
1;;; speech-controller.lisp
2
3#|
4The MIT license.
5
6Copyright (c) 2009 Paul L. Krueger
7
8Permission is hereby granted, free of charge, to any person obtaining a copy of this software
9and associated documentation files (the "Software"), to deal in the Software without restriction,
10including without limitation the rights to use, copy, modify, merge, publish, distribute,
11sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all copies or substantial
15portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
18LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23|#
24
25;;; Sample lisp/Cocoa interface that uses a NIB file defined with interface builder;
26;;; A definition is provided for the "SpeechController" class that was specified to interface builder
27;;; as the class of the NIB file owner.
28;;; We manually create an instance of SpeechController and specify it as the owner for the NIB file.
29
30(require :NIB)
31
32(defpackage :speech-controller 
33  (:nicknames :spc)
34  (:use :iu :ccl :common-lisp)
35  (:export test-speech))
36
37(in-package :spc)
38
39(defclass speech-controller (ns:ns-object)
40  ((speech-text :foreign-type :id :accessor speech-text)
41   (button-matrix :foreign-type :id :accessor button-matrix)
42   (speech-synth :accessor speech-synth 
43                 :initform (make-instance ns:ns-speech-synthesizer))
44   (voices :accessor voices 
45           :initform (#/retain (#/availableVoices ns:ns-speech-synthesizer)))
46   (nib-objects :accessor nib-objects :initform nil))
47  (:metaclass ns:+ns-object))
48
49(defmethod initialize-instance :after ((self speech-controller) 
50                                       &key &allow-other-keys)
51  (setf (nib-objects self)
52        (load-nibfile 
53         (truename "ip:Speech;SpeechView.nib") 
54         :nib-owner self
55         :retain-top-objs t))
56  ;; get all of the voice strings and set the names of the radio buttons
57  (dotimes (i (#/count (voices self)))
58    (multiple-value-bind (col row) (floor i 6)
59      (#/setTitle: (#/cellAtRow:column: (button-matrix self) row col)
60                   (#/objectForKey: 
61                    (#/attributesForVoice: ns:ns-speech-synthesizer 
62                                           (#/objectAtIndex: (voices self) i)) 
63                    #&NSVoiceName))))
64  ;; Make sure that the initial voice selected for the speech syntesizer matches
65  ;; the radio button that is selected at startup. To do that we'll just call our
66  ;; own buttonPushed: method.
67  (#/buttonPushed: self (button-matrix self))
68  ;; we do the following so that ccl:terminate will be called before we are
69  ;; garbage collected and we can release the top-level objects from the NIB
70  ;; that we retained when loaded
71  (ccl:terminate-when-unreachable self))
72
73(defmethod ccl:terminate ((self speech-controller))
74  (when (speech-synth self)
75    (#/release (speech-synth self)))
76  (when (voices self)
77    (#/release (voices self)))
78  (dolist (top-obj (nib-objects self))
79    (unless (eql top-obj (%null-ptr))
80      (#/release top-obj))))
81
82;; Action methods called as a result of pushing some button
83
84(objc:defmethod (#/startSpeaking: :void) 
85                ((self speech-controller) (s :id))
86  (declare (ignore s))
87  (with-slots (speech-text speech-synth) self
88    (let ((stxt (#/stringValue speech-text)))
89      (when (zerop (#/length stxt))
90        (setf stxt #@"I have nothing to say"))
91      (#/startSpeakingString: speech-synth stxt))))
92
93(objc:defmethod (#/stopSpeaking: :void) 
94                ((self speech-controller) (s :id))
95  (declare (ignore s))
96  (with-slots (speech-synth) self
97    (#/stopSpeaking speech-synth)))
98
99(objc:defmethod (#/buttonPushed: :void) 
100                ((self speech-controller) (button-matrix :id))
101  (let ((row (#/selectedRow button-matrix))
102        (col (#/selectedColumn button-matrix)))
103    (#/setVoice: (speech-synth self) 
104                 (#/objectAtIndex: (voices self) (+ row (* col 6))))))
105 
106;; test funtion
107
108(defun test-speech ()
109  (make-instance 'speech-controller))
110
111(provide :speech-controller)
Note: See TracBrowser for help on using the repository browser.