Skip to content

Commit 78328e5

Browse files
committed
Working demo without interruptions
1 parent 2029f21 commit 78328e5

File tree

4 files changed

+29
-32
lines changed

4 files changed

+29
-32
lines changed

TODO.org

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ CLOSED: [2025-01-20 Lun 07:43]
106106

107107
* TODO add core.async.flow support
108108
:LOGBOOK:
109+
CLOCK: [2025-01-25 Sat 16:35]
109110
CLOCK: [2025-01-25 Sat 15:18]--[2025-01-25 Sat 15:43] => 0:25
110111
CLOCK: [2025-01-25 Sat 11:14]--[2025-01-25 Sat 11:39] => 0:25
111112
CLOCK: [2025-01-25 Sat 09:50]--[2025-01-25 Sat 10:15] => 0:25

core/src/voice_fn/experiments/flow.clj

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,8 @@
327327
"Processor that streams audio out in real time so we can account for
328328
interruptions."
329329
(flow/process
330-
{:describe (fn [] {:ins {:in "Channel for audio output frames "}
331-
:outs {:out "Channel on which serialized buffered output is put"}})
330+
{:describe (fn [] {:ins {:in "Channel for audio output frames "
331+
:sys-in "Channel for system messages"}})
332332
:params {:transport/out-chan "Channel on which to put buffered serialized audio"
333333
:audio.out/duration-ms "Duration of each audio chunk. Defaults to 20ms"
334334
:transport/supports-interrupt? "Whether the processor supports interrupt or not"}
@@ -369,9 +369,11 @@
369369

370370
:else [state]))}))
371371

372-
(def gdef
372+
(defn make-twilio-flow
373+
[in out]
373374
{:procs
374-
{:transport-in {:proc twilio-transport-in}
375+
{:transport-in {:proc twilio-transport-in
376+
:args {:transport/in-ch in}}
375377
:deepgram-transcriptor {:proc deepgram-processor
376378
:args {:transcription/api-key (secret [:deepgram :api-key])
377379
:transcription/interim-results? true
@@ -418,13 +420,7 @@
418420
:audio.out/channels 1
419421
:audio.out/duration-ms 20}}
420422
:realtime-out {:proc realtime-transport-out-processor
421-
:args {:transport/out-chan (a/chan 1024)}}
422-
423-
:print-sink {:proc (flow/process
424-
{:describe (fn [] {:ins {:in "Channel for receiving transcriptions"}})
425-
:transform (fn [_ _ frame]
426-
(when (frame/audio-output-raw? frame)
427-
(t/log! {:id :print-sink :level :info} ["RESULT: " (:frame/data frame)])))})}}
423+
:args {:transport/out-chan out}}}
428424

429425
:conns [[[:transport-in :sys-out] [:deepgram-transcriptor :sys-in]]
430426
[[:transport-in :out] [:deepgram-transcriptor :in]]
@@ -440,6 +436,7 @@
440436
[[:llm-sentence-assembler :out] [:tts :in]]
441437

442438
[[:tts :out] [:audio-splitter :in]]
439+
[[:transport-in :sys-out] [:realtime-out :sys-in]]
443440
[[:audio-splitter :out] [:realtime-out :in]]]})
444441

445442
(comment

core/src/voice_fn/processors/elevenlabs.clj

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,13 @@
3939
:output_format (encoding->elevenlabs encoding sample-rate)})))
4040

4141
(comment
42-
(make-elevenlabs-url {:audio-in/sample-rate 8000
43-
:audio-in/encoding :ulaw
44-
:audio-in/channels 1
45-
:audio-in/sample-size-bits 8
46-
:audio-out/sample-rate 8000
47-
:audio-out/encoding :ulaw
48-
:audio-out/bitrate 64000
49-
:audio-out/sample-size-bits 8
50-
:audio-out/channels 1
51-
:pipeline/language :ro}
52-
{:elevenlabs/api-key (secrets/secret [:elevenlabs :api-key])
53-
:elevenlabs/model-id "eleven_flash_v2_5"
54-
:elevenlabs/voice-id "7sJPxFeMXAVWZloGIqg2"
55-
:voice/stability 0.5
56-
:voice/similarity-boost 0.8
57-
:voice/use-speaker-boost? true}))
42+
(make-elevenlabs-ws-url
43+
{:elevenlabs/api-key (secrets/secret [:elevenlabs :api-key])
44+
:elevenlabs/model-id "eleven_flash_v2_5"
45+
:elevenlabs/voice-id "7sJPxFeMXAVWZloGIqg2"
46+
:voice/stability 0.5
47+
:voice/similarity-boost 0.8
48+
:voice/use-speaker-boost? true}))
5849

5950
(defn begin-stream-message
6051
[{:voice/keys [stability similarity-boost use-speaker-boost?]
@@ -123,7 +114,7 @@
123114
type
124115
pipeline
125116
processor-config)
126-
new-conn @(ws/websocket (make-elevenlabs-url (:pipeline/config @pipeline) processor-config)
117+
new-conn @(ws/websocket (make-elevenlabs-ws-url processor-config)
127118
conn-config)]
128119
(swap! pipeline assoc-in [type :websocket/conn] new-conn)
129120
(t/log! :debug "Elevenlabs connection ready"))))))

examples/src/voice_fn_examples/twilio_websocket.clj

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
[ring.websocket :as ws]
2020
[taoensso.telemere :as t]
2121
[voice-fn.core]
22-
[voice-fn.experiments.flow :refer [gdef]]
22+
[voice-fn.experiments.flow :refer [make-twilio-flow]]
2323
[voice-fn.pipeline :as pipeline]
2424
[voice-fn.processors.elevenlabs]
2525
[voice-fn.processors.groq]
@@ -187,7 +187,9 @@
187187
(defn twilio-ws-handler-flow
188188
[req]
189189
(assert (ws/upgrade-request? req) "Must be a websocket request")
190-
(let [fl (flow/create-flow gdef)]
190+
(let [in (a/chan 1024)
191+
out (a/chan 1024)
192+
fl (flow/create-flow (make-twilio-flow in out))]
191193
(reset! dbg-flow fl)
192194
{::ws/listener
193195
{:on-open (fn on-open [socket]
@@ -196,14 +198,20 @@
196198
(when-let [[msg c] (a/alts! [report-chan error-chan])]
197199
(when (map? msg)
198200
(t/log! {:level :debug :id (if (= c error-chan) :error :report)} msg))
201+
(recur)))
202+
(a/go-loop []
203+
(when-let [output (a/<! out)]
204+
(ws/send socket output)
199205
(recur))))
200206
(flow/resume fl)
201207
nil)
202208
:on-message (fn on-text [_ws payload]
203-
(flow/inject fl [:transport-in :in] [payload]))
209+
(a/put! in payload))
204210
:on-close (fn on-close [_ws _status-code _reason]
205211
(t/log! "Call closed")
206-
(flow/stop fl))
212+
(flow/stop fl)
213+
(a/close! in)
214+
(a/close! out))
207215
:on-error (fn on-error [ws error]
208216
(prn error)
209217
(t/log! :debug error))

0 commit comments

Comments
 (0)