From 70b4bf779e4ad9e337d752b59d595be11712e16a Mon Sep 17 00:00:00 2001 From: Alex X Date: Tue, 22 Apr 2025 16:35:44 +0300 Subject: [PATCH] Change wyoming Event.Data type to string --- pkg/wyoming/api.go | 7 ++++--- pkg/wyoming/backchannel.go | 2 +- pkg/wyoming/mic.go | 2 +- pkg/wyoming/satellite.go | 8 ++++---- pkg/wyoming/wakeword.go | 8 ++++---- 5 files changed, 14 insertions(+), 13 deletions(-) diff --git a/pkg/wyoming/api.go b/pkg/wyoming/api.go index 59de747c..ce297a22 100644 --- a/pkg/wyoming/api.go +++ b/pkg/wyoming/api.go @@ -64,10 +64,11 @@ func (w *API) ReadEvent() (*Event, error) { evt := Event{Type: hdr.Type} if hdr.DataLength > 0 { - evt.Data = make([]byte, hdr.DataLength) - if _, err = io.ReadFull(w.rd, evt.Data); err != nil { + data = make([]byte, hdr.DataLength) + if _, err = io.ReadFull(w.rd, data); err != nil { return nil, err } + evt.Data = string(data) } if hdr.PayloadLength > 0 { @@ -86,7 +87,7 @@ func (w *API) Close() error { type Event struct { Type string - Data []byte + Data string Payload []byte } diff --git a/pkg/wyoming/backchannel.go b/pkg/wyoming/backchannel.go index b4167ff1..e0569fe1 100644 --- a/pkg/wyoming/backchannel.go +++ b/pkg/wyoming/backchannel.go @@ -44,7 +44,7 @@ func (b *Backchannel) AddTrack(media *core.Media, codec *core.Codec, track *core ts := time.Now().Nanosecond() evt := &Event{ Type: "audio-chunk", - Data: []byte(fmt.Sprintf(`{"rate":22050,"width":2,"channels":1,"timestamp":%d}`, ts)), + Data: fmt.Sprintf(`{"rate":22050,"width":2,"channels":1,"timestamp":%d}`, ts), Payload: pkt.Payload, } _ = b.api.WriteEvent(evt) diff --git a/pkg/wyoming/mic.go b/pkg/wyoming/mic.go index 014ba4ea..325b0c36 100644 --- a/pkg/wyoming/mic.go +++ b/pkg/wyoming/mic.go @@ -16,7 +16,7 @@ func (s *Server) HandleMic(conn net.Conn) error { api := NewAPI(conn) mic := newMicConsumer(func(chunk []byte) { data := fmt.Sprintf(`{"rate":16000,"width":2,"channels":1,"timestamp":%d}`, timestamp) - evt := &Event{Type: "audio-chunk", Data: []byte(data), Payload: chunk} + evt := &Event{Type: "audio-chunk", Data: data, Payload: chunk} if err := api.WriteEvent(evt); err != nil { closed.Done(nil) } diff --git a/pkg/wyoming/satellite.go b/pkg/wyoming/satellite.go index c542e3a9..0d1ea3e8 100644 --- a/pkg/wyoming/satellite.go +++ b/pkg/wyoming/satellite.go @@ -57,7 +57,7 @@ func (s *Server) Handle(conn net.Conn) error { case "describe": // {"asr": [], "tts": [], "handle": [], "intent": [], "wake": [], "satellite": {"name": "my satellite", "attribution": {"name": "", "url": ""}, "installed": true, "description": "my satellite", "version": "1.4.1", "area": null, "snd_format": null}} data := fmt.Sprintf(`{"satellite":{"name":%q,"attribution":{"name":"go2rtc","url":"https://github.com/AlexxIT/go2rtc"},"installed":true}}`, s.Name) - _ = api.WriteEvent(&Event{Type: "info", Data: []byte(data)}) + _ = api.WriteEvent(&Event{Type: "info", Data: data}) case "run-satellite": if err = sat.run(); err != nil { return err @@ -193,7 +193,7 @@ func (s *satellite) onMicChunk(chunk []byte) { // some problems with wake word - redirect to HA evt := &Event{ Type: "run-pipeline", - Data: []byte(`{"start_stage":"wake","end_stage":"tts","restart_on_end":false}`), + Data: `{"start_stage":"wake","end_stage":"tts","restart_on_end":false}`, } if err := s.api.WriteEvent(evt); err != nil { return @@ -211,7 +211,7 @@ func (s *satellite) onMicChunk(chunk []byte) { // check if wake word detected evt := &Event{ Type: "run-pipeline", - Data: []byte(`{"start_stage":"asr","end_stage":"tts","restart_on_end":false}`), + Data: `{"start_stage":"asr","end_stage":"tts","restart_on_end":false}`, } _ = s.api.WriteEvent(evt) s.state = stateStreaming @@ -232,7 +232,7 @@ func (s *satellite) onMicChunk(chunk []byte) { if s.state == stateStreaming { data := fmt.Sprintf(`{"rate":16000,"width":2,"channels":1,"timestamp":%d}`, s.timestamp) - evt := &Event{Type: "audio-chunk", Data: []byte(data), Payload: chunk} + evt := &Event{Type: "audio-chunk", Data: data, Payload: chunk} _ = s.api.WriteEvent(evt) } diff --git a/pkg/wyoming/wakeword.go b/pkg/wyoming/wakeword.go index 3603e22a..4c728f20 100644 --- a/pkg/wyoming/wakeword.go +++ b/pkg/wyoming/wakeword.go @@ -53,7 +53,7 @@ func (w *WakeWord) handle() { var data struct { Name string `json:"name"` } - if err = json.Unmarshal(evt.Data, &data); err != nil { + if err = json.Unmarshal([]byte(evt.Data), &data); err != nil { return } w.Detection = data.Name @@ -95,7 +95,7 @@ func (w *WakeWord) Start() error { if err != nil { return err } - evt := &Event{Type: "detect", Data: data} + evt := &Event{Type: "detect", Data: string(data)} if err := w.WriteEvent(evt); err != nil { return err } @@ -114,7 +114,7 @@ func (w *WakeWord) WriteChunk(payload []byte) error { return w.WriteEvent(evt) } -func audioData(send int) []byte { +func audioData(send int) string { // timestamp in ms = send / 2 * 1000 / 16000 = send / 32 - return []byte(fmt.Sprintf(`{"rate":16000,"width":2,"channels":1,"timestamp":%d}`, send/32)) + return fmt.Sprintf(`{"rate":16000,"width":2,"channels":1,"timestamp":%d}`, send/32) }