1 ! Copyright (C) 2007, 2008 Alex Chapman
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs combinators hashtables kernel lists math
4 namespaces make openal parser-combinators promises sequences
5 strings symbols synth synth.buffers unicode.case ;
9 : morse-codes ( -- array )
68 : ch>morse-assoc ( -- assoc )
69 morse-codes >hashtable ;
71 : morse>ch-assoc ( -- assoc )
72 morse-codes [ reverse ] map >hashtable ;
76 : ch>morse ( ch -- str )
77 ch>lower ch>morse-assoc at* swap "" ? ;
79 : morse>ch ( str -- ch )
80 morse>ch-assoc at* swap f ? ;
82 : >morse ( str -- str )
84 [ CHAR: \s , ] [ ch>morse % ] interleave
89 : dot-char ( -- ch ) CHAR: . ;
90 : dash-char ( -- ch ) CHAR: - ;
91 : char-gap-char ( -- ch ) CHAR: \s ;
92 : word-gap-char ( -- ch ) CHAR: / ;
94 : =parser ( obj -- parser )
97 LAZY: 'dot' ( -- parser )
100 LAZY: 'dash' ( -- parser )
103 LAZY: 'char-gap' ( -- parser )
104 char-gap-char =parser ;
106 LAZY: 'word-gap' ( -- parser )
107 word-gap-char =parser ;
109 LAZY: 'morse-char' ( -- parser )
110 'dot' 'dash' <|> <+> ;
112 LAZY: 'morse-word' ( -- parser )
113 'morse-char' 'char-gap' list-of ;
115 LAZY: 'morse-words' ( -- parser )
116 'morse-word' 'word-gap' list-of ;
120 : morse> ( str -- str )
121 'morse-words' parse car parsed>> [
125 ] map [ [ CHAR: \s , ] [ % ] interleave ] "" make ;
128 SYMBOLS: source dot-buffer dash-buffer intra-char-gap-buffer letter-gap-buffer ;
130 : queue ( symbol -- )
131 get source get swap queue-buffer ;
133 : dot ( -- ) dot-buffer queue ;
134 : dash ( -- ) dash-buffer queue ;
135 : intra-char-gap ( -- ) intra-char-gap-buffer queue ;
136 : letter-gap ( -- ) letter-gap-buffer queue ;
140 : <morse-buffer> ( -- buffer )
141 half-sample-freq <8bit-mono-buffer> ;
143 : sine-buffer ( seconds -- id )
144 beep-freq swap <morse-buffer> >sine-wave-buffer
147 : silent-buffer ( seconds -- id )
148 <morse-buffer> >silent-buffer send-buffer id>> ;
150 : make-buffers ( unit-length -- )
152 [ sine-buffer dot-buffer set ]
153 [ 3 * sine-buffer dash-buffer set ]
154 [ silent-buffer intra-char-gap-buffer set ]
155 [ 3 * silent-buffer letter-gap-buffer set ]
158 : playing-morse ( quot unit-length -- )
160 init-openal 1 gen-sources first source set make-buffers
162 source get source-play
165 : play-char ( ch -- )
169 { dash-char [ dash ] }
170 { word-gap-char [ intra-char-gap ] }
176 : play-as-morse* ( str unit-length -- )
178 [ letter-gap ] [ ch>morse play-char ] interleave
179 ] swap playing-morse ;
181 : play-as-morse ( str -- )
182 0.05 play-as-morse* ;