51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
|
package audio
|
||
|
|
||
|
import (
|
||
|
"os/exec"
|
||
|
"fmt"
|
||
|
"path"
|
||
|
"runtime"
|
||
|
)
|
||
|
|
||
|
|
||
|
type pulsectl struct {
|
||
|
PipeName string
|
||
|
PipeDir string
|
||
|
Encoding Encoding
|
||
|
}
|
||
|
|
||
|
func NewPulsectl() *pulsectl {
|
||
|
if runtime.GOOS != "linux" {
|
||
|
panic(fmt.Errorf("audio.pulsectl is only supported on Linux"))
|
||
|
}
|
||
|
return &pulsectl{
|
||
|
PipeName: "remote-mic",
|
||
|
PipeDir: "/dev/shm",
|
||
|
Encoding: defaultEncoding,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (p *pulsectl) LoadPipeSourceModule() error {
|
||
|
cmd := exec.Command("pactl", "load-module", "module-pipe-source",
|
||
|
fmt.Sprintf("source_name=%s", p.PipeName),
|
||
|
fmt.Sprintf("file=%s", path.Join(p.PipeDir, p.PipeName)),
|
||
|
fmt.Sprintf("format=%s", p.Encoding.Codec),
|
||
|
fmt.Sprintf("rate=%d", p.Encoding.Bitrate),
|
||
|
fmt.Sprintf("channels=%d", p.Encoding.Channels),
|
||
|
)
|
||
|
outBytes, err := cmd.CombinedOutput()
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("failed to load pipe source module:\n%s", string(outBytes))
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (p *pulsectl) UnloadPipeSourceModule() error {
|
||
|
cmd := exec.Command("pactl", "unload-module", "module-pipe-source")
|
||
|
outBytes, err := cmd.CombinedOutput()
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("failed to unload pipe source module:\n%s", string(outBytes))
|
||
|
}
|
||
|
return nil
|
||
|
}
|