Compare commits
1 commit
main
...
feature/au
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
60f96104a2 |
4 changed files with 60 additions and 3 deletions
45
audio_controller.go
Normal file
45
audio_controller.go
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"github.com/gordonklaus/portaudio"
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func chk(err error) {
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func audioController(c echo.Context) error {
|
||||||
|
if audioIsInitialized == false {
|
||||||
|
portaudio.Initialize()
|
||||||
|
defer portaudio.Terminate()
|
||||||
|
audioBuffer = make([]float32, audioSampleRate*audioSeconds)
|
||||||
|
stream, err := portaudio.OpenDefaultStream(1, 0, audioSampleRate, len(audioBuffer), func(in []float32) {
|
||||||
|
for i := range audioBuffer {
|
||||||
|
audioBuffer[i] = in[i]
|
||||||
|
}
|
||||||
|
})
|
||||||
|
chk(err)
|
||||||
|
chk(stream.Start())
|
||||||
|
defer stream.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
flusher, _ := c.Response().Writer.(http.Flusher)
|
||||||
|
|
||||||
|
c.Response().Header().Set("Connection", "Keep-Alive")
|
||||||
|
c.Response().Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
c.Response().Header().Set("X-Content-Type-Options", "nosniff")
|
||||||
|
c.Response().Header().Set("Transfer-Encoding", "chunked")
|
||||||
|
c.Response().Header().Set("Content-Type", "audio/wave")
|
||||||
|
|
||||||
|
for true {
|
||||||
|
binary.Write(c.Response().Writer, binary.BigEndian, &audioBuffer)
|
||||||
|
flusher.Flush() // Trigger "chunked" encoding and send a chunk...
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
1
go.mod
1
go.mod
|
|
@ -7,6 +7,7 @@ require (
|
||||||
github.com/daaku/go.zipexe v1.0.2 // indirect
|
github.com/daaku/go.zipexe v1.0.2 // indirect
|
||||||
github.com/gen2brain/shm v0.0.0-20230802011745-f2460f5984f7 // indirect
|
github.com/gen2brain/shm v0.0.0-20230802011745-f2460f5984f7 // indirect
|
||||||
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
|
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
|
||||||
|
github.com/gordonklaus/portaudio v0.0.0-20230709114228-aafa478834f5 // indirect
|
||||||
github.com/gorilla/websocket v1.5.0 // indirect
|
github.com/gorilla/websocket v1.5.0 // indirect
|
||||||
github.com/jezek/xgb v1.1.0 // indirect
|
github.com/jezek/xgb v1.1.0 // indirect
|
||||||
github.com/kbinani/screenshot v0.0.0-20230812210009-b87d31814237 // indirect
|
github.com/kbinani/screenshot v0.0.0-20230812210009-b87d31814237 // indirect
|
||||||
|
|
|
||||||
2
go.sum
2
go.sum
|
|
@ -10,6 +10,8 @@ github.com/gen2brain/shm v0.0.0-20230802011745-f2460f5984f7 h1:VLEKvjGJYAMCXw0/3
|
||||||
github.com/gen2brain/shm v0.0.0-20230802011745-f2460f5984f7/go.mod h1:uF6rMu/1nvu+5DpiRLwusA6xB8zlkNoGzKn8lmYONUo=
|
github.com/gen2brain/shm v0.0.0-20230802011745-f2460f5984f7/go.mod h1:uF6rMu/1nvu+5DpiRLwusA6xB8zlkNoGzKn8lmYONUo=
|
||||||
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
|
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
|
||||||
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
|
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
|
||||||
|
github.com/gordonklaus/portaudio v0.0.0-20230709114228-aafa478834f5 h1:5AlozfqaVjGYGhms2OsdUyfdJME76E6rx5MdGpjzZpc=
|
||||||
|
github.com/gordonklaus/portaudio v0.0.0-20230709114228-aafa478834f5/go.mod h1:WY8R6YKlI2ZI3UyzFk7P6yGSuS+hFwNtEzrexRyD7Es=
|
||||||
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
|
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
|
||||||
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
|
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
|
||||||
|
|
|
||||||
15
main.go
15
main.go
|
|
@ -12,14 +12,21 @@ import (
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
audioSampleRate = 44100
|
||||||
|
audioSeconds = 2
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
templates map[string]*template.Template
|
templates map[string]*template.Template
|
||||||
//go:embed static
|
//go:embed static
|
||||||
staticFiles embed.FS
|
staticFiles embed.FS
|
||||||
//go:embed views/layout views/page
|
//go:embed views/layout views/page
|
||||||
views embed.FS
|
views embed.FS
|
||||||
config Config
|
config Config
|
||||||
actions Actions
|
actions Actions
|
||||||
|
audioBuffer []float32
|
||||||
|
audioIsInitialized bool
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
@ -40,6 +47,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
config = value
|
config = value
|
||||||
|
audioIsInitialized = false
|
||||||
e.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) {
|
e.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) {
|
||||||
if config.Server.Auth.Username == "" && config.Server.Auth.Password == "" {
|
if config.Server.Auth.Username == "" && config.Server.Auth.Password == "" {
|
||||||
return true, nil
|
return true, nil
|
||||||
|
|
@ -61,6 +69,7 @@ func main() {
|
||||||
e.GET("/static/*", echo.WrapHandler(http.StripPrefix("/static/", assetHandler)))
|
e.GET("/static/*", echo.WrapHandler(http.StripPrefix("/static/", assetHandler)))
|
||||||
e.GET("/", homeController)
|
e.GET("/", homeController)
|
||||||
e.GET("/ws", wsController)
|
e.GET("/ws", wsController)
|
||||||
|
e.GET("/audio", audioController)
|
||||||
|
|
||||||
e.Logger.Fatal(e.Start(config.Server.Listen))
|
e.Logger.Fatal(e.Start(config.Server.Listen))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue