Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
shadorki committed Feb 26, 2022
0 parents commit 4bc9f5d
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bin
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build:
GOOS=windows GOARCH=386 go build -o bin/vrc-osc-audio.exe main.go
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# VRC OSC Audio Controls

A go program to control your audio using VRChat OSC Parameters

## Usage

Download the executable from the releases page and double-click!

## Contributing

### Requirements
- Go
- make command

## Getting Started

1. Clone the repository
```bash
git clone [email protected]:uzair-ashraf/vrc-osc-audio-controls.git
```
2. Run the program
```bash
go run main.go
```
1. Compile the executable for windows
```bash
make build
```
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module vrc-ocs-audio-controls

go 1.17

require github.com/hypebeast/go-osc v0.0.0-20210408213458-3287e1838f40 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/hypebeast/go-osc v0.0.0-20210408213458-3287e1838f40 h1:w+HdpjIfD+5Kb8aJ1KK9ROuqS9No/p0mSYTx/0XCASc=
github.com/hypebeast/go-osc v0.0.0-20210408213458-3287e1838f40/go.mod h1:lqMjoCs0y0GoRRujSPZRBaGb4c5ER6TfkFKSClxkMbY=
129 changes: 129 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package main

import (
"fmt"
"os/exec"
"strconv"
"strings"

"github.com/hypebeast/go-osc/osc"
)

func main() {
addr := "127.0.0.1:9001"
d := osc.NewStandardDispatcher()
d.AddMsgHandler("/avatar/parameters/OSC_AUDIO_CONTROLS_PLAY_PAUSE", handlePlayPauseSong)
d.AddMsgHandler("/avatar/parameters/OSC_AUDIO_CONTROLS_NEXT", handleNextSong)
d.AddMsgHandler("/avatar/parameters/OSC_AUDIO_CONTROLS_PREVIOUS", handlePreviousSong)
d.AddMsgHandler("/avatar/parameters/OSC_AUDIO_CONTROLS_MUTE", handleMute)
server := &osc.Server{
Addr: addr,
Dispatcher: d,
}
server.ListenAndServe()
}

func handlePlayPauseSong(msg *osc.Message) {
wasSelected, err := parseVRCBool(msg)
if err != nil {
fmt.Println(err)
return
}
if !wasSelected {
return
}
playPauseSong()
}

func handleNextSong(msg *osc.Message) {
wasSelected, err := parseVRCBool(msg)
if err != nil {
fmt.Println(err)
return
}
if !wasSelected {
return
}
nextSong()
}

func handlePreviousSong(msg *osc.Message) {
wasSelected, err := parseVRCBool(msg)
if err != nil {
fmt.Println(err)
return
}
if !wasSelected {
return
}
previousSong()
}

func handleMute(msg *osc.Message) {
wasSelected, err := parseVRCBool(msg)
if err != nil {
fmt.Println(err)
return
}
if !wasSelected {
return
}
muteVolume()
}

func parseVRCBool(msg *osc.Message) (bool, error) {
message := strings.Trim(msg.String(), " ")
fmt.Println(message)
if strings.HasSuffix(message, ",T true") {
return true, nil
} else if strings.HasSuffix(message, ",F false") {
return false, nil
}

return false, fmt.Errorf("Unexpected value: %v\n", message)
}

func parseVRCFloat(msg *osc.Message) (float64, error) {
message := strings.Trim(msg.String(), " ")
messageSlice := strings.Split(message, " ")
floatStr := messageSlice[len(messageSlice)-1]
return strconv.ParseFloat(floatStr, 64)
}

func executePowershellCommand(command string) (string, error) {
cmd := exec.Command("powershell", "-nologo", "-noprofile", command)
stdin, err := cmd.StdinPipe()
if err != nil {
return "", err
}
defer stdin.Close()
out, err := cmd.CombinedOutput()
if err != nil {
return "", err
}
return string(out), nil
}
func playPauseSong() (string, error) {
return executePowershellCommand(`
$wshell = New-Object -ComObject wscript.shell
$wshell.SendKeys([Char]0xB3)
`)
}
func nextSong() (string, error) {
return executePowershellCommand(`
$wshell = New-Object -ComObject wscript.shell
$wshell.SendKeys([Char]0xB0)
`)
}
func previousSong() (string, error) {
return executePowershellCommand(`
$wshell = New-Object -ComObject wscript.shell
$wshell.SendKeys([Char]0xB1)
`)
}
func muteVolume() (string, error) {
return executePowershellCommand(`
$wshell = New-Object -ComObject wscript.shell
$wshell.SendKeys([Char]0xAD)
`)
}

0 comments on commit 4bc9f5d

Please sign in to comment.