This repository has been archived by the owner on Feb 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit bfb97c1
Showing
16 changed files
with
836 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
name: goreleaser | ||
|
||
on: | ||
push: | ||
tags: | ||
- '*' | ||
|
||
permissions: | ||
contents: write | ||
|
||
jobs: | ||
goreleaser: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
fetch-depth: 0 | ||
- run: git fetch --force --tags | ||
- uses: actions/setup-go@v4 | ||
with: | ||
go-version: stable | ||
- uses: goreleaser/goreleaser-action@v4 | ||
with: | ||
distribution: goreleaser | ||
version: latest | ||
args: release --clean | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
config.toml | ||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
before: | ||
hooks: | ||
- go mod tidy | ||
builds: | ||
- env: | ||
- CGO_ENABLED=0 | ||
main: ./cmd/vrc-lyrics | ||
goos: | ||
- linux | ||
- windows | ||
- darwin | ||
|
||
archives: | ||
- format: tar.gz | ||
name_template: >- | ||
{{ .ProjectName }}_ | ||
{{- title .Os }}_ | ||
{{- if eq .Arch "amd64" }}x86_64 | ||
{{- else if eq .Arch "386" }}i386 | ||
{{- else }}{{ .Arch }}{{ end }} | ||
{{- if .Arm }}v{{ .Arm }}{{ end }} | ||
format_overrides: | ||
- goos: windows | ||
format: zip | ||
checksum: | ||
name_template: 'checksums.txt' | ||
snapshot: | ||
name_template: "{{ incpatch .Version }}-next" | ||
changelog: | ||
sort: asc | ||
filters: | ||
exclude: | ||
- '^docs:' | ||
- '^test:' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright © 2023 jsopn | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/jsopn/vrc-lyrics/internal/app" | ||
"github.com/jsopn/vrc-lyrics/internal/config" | ||
) | ||
|
||
func main() { | ||
cfg, err := config.ParseConfig("config.toml") | ||
if err != nil { | ||
log.Fatalf("Failed to parse config: %q", err.Error()) | ||
return | ||
} | ||
|
||
if err := app.Run(cfg); err != nil { | ||
log.Fatalf("An error occured while running the app: %q", err) | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[Spotify] | ||
# SP_DC cookie from the open.spotify.com, which will be used to get the token | ||
SPDCCookie = "" | ||
|
||
[VRChat] | ||
# Connection data for VRChat's OSC | ||
OSCHost = "127.0.0.1" | ||
OSCPort = 9000 | ||
|
||
# The formatted string that will be displayed if there **are lyrics** in the current track | ||
# Available fields: {{.artist}}, {{.name}}, {{.trackID}}, {{.line}} | ||
LyricsFormat = "▶️ {{.artist}} - {{.name}} 🎵 {{.line}}" | ||
|
||
# Formatted string that will be displayed if there are **no lyrics** in the track | ||
# Available fields: {{.artist}}, {{.name}}, {{.trackID}} | ||
NoLyricsFormat = "▶️ {{.artist}} - {{.name}}" | ||
|
||
# Formatted string that will be displayed if track is paused | ||
# Available fields: {{.artist}}, {{.name}}, {{.trackID}} | ||
PausedFormat = "⏸️ {{.artist}} - {{.name}}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module github.com/jsopn/vrc-lyrics | ||
|
||
go 1.20 | ||
|
||
require ( | ||
github.com/BurntSushi/toml v1.3.2 | ||
github.com/valyala/fastjson v1.6.4 | ||
) | ||
|
||
require github.com/gorilla/websocket v1.5.0 | ||
|
||
require github.com/hypebeast/go-osc v0.0.0-20220308234300-cec5a8a1e5f5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8= | ||
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= | ||
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/hypebeast/go-osc v0.0.0-20220308234300-cec5a8a1e5f5 h1:fqwINudmUrvGCuw+e3tedZ2UJ0hklSw6t8UPomctKyQ= | ||
github.com/hypebeast/go-osc v0.0.0-20220308234300-cec5a8a1e5f5/go.mod h1:lqMjoCs0y0GoRRujSPZRBaGb4c5ER6TfkFKSClxkMbY= | ||
github.com/valyala/fastjson v1.6.4 h1:uAUNq9Z6ymTgGhcm0UynUAB6tlbakBrz6CQFax3BXVQ= | ||
github.com/valyala/fastjson v1.6.4/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package app | ||
|
||
import ( | ||
"log" | ||
"time" | ||
|
||
"github.com/jsopn/vrc-lyrics/internal/config" | ||
"github.com/jsopn/vrc-lyrics/pkg/osc" | ||
"github.com/jsopn/vrc-lyrics/pkg/spotify" | ||
) | ||
|
||
func reverse[S ~[]E, E any](s S) { | ||
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 { | ||
s[i], s[j] = s[j], s[i] | ||
} | ||
} | ||
|
||
func Run(cfg *config.Config) error { | ||
oscClient := osc.New(cfg.VRChat.OSCHost, cfg.VRChat.OSCPort) | ||
spt := spotify.New(cfg.Spotify.Token, cfg.Spotify.SPDCCookie) | ||
|
||
log.Println("Connecting to Spotify's WebSocket") | ||
playbackChan, err := spt.ConnectWebsocket() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer spt.CloseWebsocket() | ||
|
||
if err := spt.RegisterDevice(); err != nil { | ||
return err | ||
} | ||
|
||
if err := spt.UpdateState(); err != nil { | ||
return err | ||
} | ||
|
||
log.Println("Connected.") | ||
|
||
ticker := time.NewTicker(300 * time.Millisecond) | ||
defer ticker.Stop() | ||
|
||
var playbackState *spotify.PlaybackState | ||
var syncedLyrics []spotify.LyricsLines | ||
var trackMetadata *spotify.TrackMetadata | ||
var syncedLyricsTrackID string | ||
var lastWords string | ||
|
||
for { | ||
select { | ||
case ps := <-playbackChan: | ||
playbackState = ps | ||
log.Printf("[ Updated playback state. | TrackID: %s ]", playbackState.TrackID) | ||
|
||
if syncedLyricsTrackID != playbackState.TrackID { | ||
syncedLyrics, _ = spt.GetLyrics(playbackState.TrackID) | ||
trackMetadata, _ = spt.GetMetadata(playbackState.TrackID) | ||
syncedLyricsTrackID = playbackState.TrackID | ||
|
||
if len(syncedLyrics) > 0 { | ||
log.Println("Lyrics updated.") | ||
} else { | ||
log.Println("No lyrics found for this track.") | ||
} | ||
|
||
reverse(syncedLyrics) | ||
} | ||
|
||
case <-ticker.C: | ||
if playbackState == nil || trackMetadata == nil || !playbackState.IsPlaying { | ||
continue | ||
} | ||
|
||
data := map[string]interface{}{ | ||
"trackID": playbackState.TrackID, | ||
"artist": trackMetadata.Artists, | ||
"album": trackMetadata.Album, | ||
"name": trackMetadata.Name, | ||
} | ||
|
||
if playbackState.IsPaused && cfg.VRChat.PausedFormat != "" { | ||
ticker.Reset(5 * time.Second) | ||
|
||
oscClient.Send(cfg.VRChat.PausedFormat, data) | ||
continue | ||
} | ||
|
||
ticker.Reset(300 * time.Millisecond) | ||
|
||
delta := int(time.Now().UnixMilli()) - playbackState.UpdatedAt | ||
currentMS := playbackState.CurrentMS + delta | ||
line := spotify.GetCurrentWords(syncedLyrics, currentMS) | ||
|
||
if len(syncedLyrics) == 0 || line == "" { | ||
oscClient.Send(cfg.VRChat.NoLyricsFormat, data) | ||
continue | ||
} | ||
|
||
if lastWords == line { | ||
continue | ||
} | ||
|
||
lastWords = line | ||
data["line"] = line | ||
|
||
oscClient.Send(cfg.VRChat.LyricsFormat, data) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package config | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/BurntSushi/toml" | ||
"github.com/jsopn/vrc-lyrics/pkg/spotify" | ||
) | ||
|
||
type SpotifyConfig struct { | ||
// SP_DC cookie from the open.spotify.com, which will be used to get the token | ||
SPDCCookie string | ||
|
||
// A token that will be used to access the spotify API. This is automatically set using the SP_DC cookie. **Do not change**. | ||
Token *spotify.Token | ||
} | ||
|
||
type VRChatConfig struct { | ||
// Connection data for VRChat's OSC | ||
OSCHost string | ||
OSCPort int | ||
|
||
// The formatted string that will be displayed if there **are lyrics** in the current track | ||
// Available fields: {{.artist}}, {{.name}}, {{.line}}, {{.trackID}} | ||
LyricsFormat string | ||
|
||
// Formatted string that will be displayed if there are **no lyrics** in the track | ||
// Available fields: {{.artist}}, {{.name}}, {{.trackID}} | ||
NoLyricsFormat string | ||
|
||
// Formatted string that will be displayed if track is paused | ||
// Available fields: {{.artist}}, {{.name}}, {{.trackID}} | ||
PausedFormat string | ||
} | ||
|
||
type Config struct { | ||
Spotify SpotifyConfig | ||
VRChat VRChatConfig | ||
} | ||
|
||
func ParseConfig(path string) (config *Config, err error) { | ||
if _, err := toml.DecodeFile(path, &config); err != nil { | ||
return nil, err | ||
} | ||
|
||
return config, nil | ||
} | ||
|
||
func WriteConfig(path string, config *Config) error { | ||
f, err := os.OpenFile(path, os.O_RDWR|os.O_TRUNC, os.ModePerm) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return toml.NewEncoder(f).Encode(config) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package osc | ||
|
||
import ( | ||
"bytes" | ||
"log" | ||
"text/template" | ||
"time" | ||
|
||
"github.com/hypebeast/go-osc/osc" | ||
) | ||
|
||
type OSC struct { | ||
client *osc.Client | ||
lastOSCSend time.Time | ||
} | ||
|
||
func New(host string, port int) *OSC { | ||
return &OSC{ | ||
client: osc.NewClient(host, port), | ||
} | ||
} | ||
|
||
func (o *OSC) Send(format string, data map[string]interface{}) error { | ||
// To avoid VRChat's rate-limits | ||
if time.Since(o.lastOSCSend).Milliseconds() < 750 { | ||
return nil | ||
} | ||
|
||
t := template.Must(template.New("chatbox").Parse(format)) | ||
buf := bytes.NewBuffer(nil) | ||
if err := t.Execute(buf, data); err != nil { | ||
return err | ||
} | ||
|
||
s := buf.String() | ||
|
||
msg := osc.NewMessage("/chatbox/input") | ||
msg.Append(s) | ||
msg.Append(true) | ||
msg.Append(false) | ||
|
||
log.Println(s) | ||
|
||
o.lastOSCSend = time.Now() | ||
return o.client.Send(msg) | ||
} |
Oops, something went wrong.