remote-mic/main.go

69 lines
1.0 KiB
Go
Raw Normal View History

2020-04-28 21:42:45 +00:00
package main
import (
"fmt"
2020-04-29 12:33:39 +00:00
"io"
2020-04-30 00:14:51 +00:00
"os"
2020-04-29 12:33:39 +00:00
"remote-mic/audio"
2020-04-30 00:14:51 +00:00
"remote-mic/connection"
"time"
2020-04-28 21:42:45 +00:00
)
2020-04-29 12:33:39 +00:00
func socketExample() {
2020-04-30 00:14:51 +00:00
fmt.Print("Listening for connection on port 8080... ")
conn, err := connection.NewListen(":8080")
2020-04-28 21:42:45 +00:00
if err != nil {
panic(err)
}
defer conn.Close()
fmt.Println("Connection accepted!")
2020-04-30 11:03:06 +00:00
recvChan := conn.RecvChan()
2020-04-30 00:14:51 +00:00
go func() {
2020-04-30 11:03:06 +00:00
time.Sleep(10 * time.Second)
2020-04-30 00:14:51 +00:00
conn.Close()
}()
for {
buf, open := <-recvChan
if !open {
break
}
fmt.Printf("Bytes read: %d\n", len(buf))
fmt.Printf("Buf: %v\n", buf)
fmt.Printf("%v\n", string(buf))
}
2020-04-28 21:42:45 +00:00
fmt.Println("Exiting.")
}
2020-04-29 12:33:39 +00:00
func pulsectlExample() {
2020-04-29 18:48:55 +00:00
pulsectl := audio.NewPulsectl()
err := pulsectl.LoadPipeSourceModule()
if err != nil {
panic(err)
}
2020-04-28 21:42:45 +00:00
time.Sleep(10 * time.Second)
2020-04-29 18:48:55 +00:00
pulsectl.UnloadPipeSourceModule()
2020-04-28 21:42:45 +00:00
}
2020-04-29 12:33:39 +00:00
func micStreamExample() {
2020-04-29 18:48:55 +00:00
mic, err := audio.NewMicrophone()
if err != nil {
panic(err)
2020-04-29 12:33:39 +00:00
}
2020-04-29 18:48:55 +00:00
err = mic.Start()
2020-04-29 12:33:39 +00:00
if err != nil {
panic(err)
}
2020-04-29 18:48:55 +00:00
defer mic.Stop()
for {
io.Copy(os.Stdout, mic.AudioStream())
}
2020-04-29 12:33:39 +00:00
}
func main() {
2020-04-30 00:14:51 +00:00
socketExample()
2020-04-29 12:33:39 +00:00
}