Skip to content

Commit

Permalink
Spotify Addon / Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
VRCWizard authored May 29, 2022
1 parent 9823037 commit 76acc10
Show file tree
Hide file tree
Showing 10 changed files with 964 additions and 427 deletions.
21 changes: 10 additions & 11 deletions OSCVRCWiz/AudioSynthesis.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Microsoft.CognitiveServices.Speech;

using Microsoft.CognitiveServices.Speech.Audio;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -13,14 +13,12 @@ public class AudioSynthesis


//TTS
public static async Task SynthesizeAudioAsync(string text, string style, string rate, string pitch, string volume, string voice) //TTS Outputs through speakers //can not change voice style
public static async Task SynthesizeAudioAsync(VoiceWizardWindow MainForm,string text, string style, string rate, string pitch, string volume, string voice) //TTS Outputs through speakers //can not change voice style
{
try
{

var config = SpeechConfig.FromSubscription(VoiceWizardWindow.YourSubscriptionKey, VoiceWizardWindow.YourServiceRegion);


// Note: if only language is set, the default voice of that language is chosen.
// config.SpeechSynthesisLanguage = "<your-synthesis-language>"; // For example, "de-DE"
// The voice setting will overwrite the language setting.
Expand Down Expand Up @@ -172,8 +170,14 @@ public static async Task SynthesizeAudioAsync(string text, string style, string



var audioConfig = AudioConfig.FromSpeakerOutput(MainForm.currentOutputDevice);
if(MainForm.currentOutputDeviceName== "Default")
{
audioConfig = AudioConfig.FromDefaultSpeakerOutput();

}

var synthesizer = new Microsoft.CognitiveServices.Speech.SpeechSynthesizer(config);
var synthesizer = new Microsoft.CognitiveServices.Speech.SpeechSynthesizer(config,audioConfig);

string ssml0 = "<speak version=\"1.0\"";
ssml0 += " xmlns=\"http://www.w3.org/2001/10/synthesis\"";
Expand Down Expand Up @@ -340,11 +344,6 @@ public static async Task SynthesizeAudioAsync(string text, string style, string

System.Diagnostics.Debug.WriteLine(ssml0);
await synthesizer.SpeakSsmlAsync(ssml0);




//using var result = await synthesizer.SpeakSsmlAsync(ssml0);

}
catch (Exception ex)
Expand Down
8 changes: 5 additions & 3 deletions OSCVRCWiz/OSCVRCWiz.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
<ApplicationIcon>WizardPic.ico</ApplicationIcon>
<AssemblyName>TTSVoiceWizard</AssemblyName>
</PropertyGroup>

<ItemGroup>
Expand All @@ -15,9 +16,10 @@

<ItemGroup>
<PackageReference Include="FontAwesome.Sharp" Version="5.15.4" />
<PackageReference Include="MetroModernUI" Version="1.4.0" />
<PackageReference Include="Microsoft.CognitiveServices.Speech" Version="1.20.0" />
<PackageReference Include="ModernUI" Version="2.0.0" />
<PackageReference Include="Microsoft.CognitiveServices.Speech" Version="1.21.0" />
<PackageReference Include="NAudio" Version="2.1.0" />
<PackageReference Include="SpotifyAPI.Web" Version="6.2.2" />
<PackageReference Include="SpotifyAPI.Web.Auth" Version="6.2.2" />
<PackageReference Include="System.Speech" Version="7.0.0-preview.2.22152.2" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion OSCVRCWiz/OutputText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ public async void outputVRChat(VoiceWizardWindow MainForm, string textstring)

}

if (MainForm.rjToggleButtonHideDelay.Checked) //inactive hide
if (MainForm.rjToggleButtonHideDelay2.Checked) //inactive hide
{
System.Diagnostics.Debug.WriteLine("Outputing text to vrchat finished");

Expand Down
36 changes: 36 additions & 0 deletions OSCVRCWiz/Settings1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions OSCVRCWiz/Settings1.settings
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,14 @@
<Setting Name="SyncParaValue" Type="System.Int32" Scope="User">
<Value Profile="(Default)">0</Value>
</Setting>
<Setting Name="MicName" Type="System.String" Scope="User">
<Value Profile="(Default)">Default</Value>
</Setting>
<Setting Name="SpeakerName" Type="System.String" Scope="User">
<Value Profile="(Default)">Default</Value>
</Setting>
<Setting Name="ConnectSpot" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
</Settings>
</SettingsFile>
195 changes: 195 additions & 0 deletions OSCVRCWiz/SpotifyAddon.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SpotifyAPI.Web.Auth;
using SpotifyAPI.Web;

namespace OSCVRCWiz
{

public class SpotifyAddon

{
// private const string CredentialsPath = "credentials.json";
// private static readonly string? clientId = Environment.GetEnvironmentVariable("SPOTIFY_CLIENT_ID");
private static readonly string? clientId = "8ed3657eca864590843f45a659ec2976";
//private static EmbedIOAuthServer _server = new EmbedIOAuthServer(new Uri("http://localhost:5000/callback"), 5000);
private static EmbedIOAuthServer _server;
private static SpotifyClient myClient =null;
private static PKCETokenResponse myPKCEToken = null;
private static string lastSong = "";
private static string globalVerifier = "";







/* private static async Task OnAuthorizationCodeReceived(object sender, AuthorizationCodeResponse response)
{
await _server.Stop();
var config = SpotifyClientConfig.CreateDefault();
var tokenResponse = await new OAuthClient(config).RequestToken(
new AuthorizationCodeTokenRequest(
clientId, clientSecret, response.Code, new Uri("http://localhost:5000/callback")
)
);
var spotify = new SpotifyClient(tokenResponse.AccessToken);
// do calls with Spotify and save token?
myClient = spotify;
myToken = tokenResponse;
}
private static async Task OnImplicitGrantReceived(object sender, ImplictGrantResponse response)
{
await _server.Stop();
var spotify = new SpotifyClient(response.AccessToken);
// do calls with Spotify
myClient = spotify;
}
*/
public static async Task getCurrentSongInfo(VoiceWizardWindow MainForm)
{
// var newResponse = await new OAuthClient().RequestToken(
// new AuthorizationCodeRefreshRequest(clientId, clientSecret, myToken.RefreshToken));

// myClient = new SpotifyClient(newResponse.AccessToken);




var authenticator = new PKCEAuthenticator(clientId, myPKCEToken);

var config = SpotifyClientConfig.CreateDefault()
.WithAuthenticator(authenticator);
var spotify = new SpotifyClient(config);
myClient = spotify;

if (myClient != null)
{


// var spotify = new SpotifyClient(tokenResponse.AccessToken);
FullTrack m_currentTrack;
CurrentlyPlaying m_currentlyPlaying;
m_currentlyPlaying = await myClient.Player.GetCurrentlyPlaying(new PlayerCurrentlyPlayingRequest());
var title = "";
var artist = "";
var duration = "";
if (m_currentlyPlaying != null)
{
IPlayableItem currentlyPlayingItem = m_currentlyPlaying.Item;
m_currentTrack = currentlyPlayingItem as FullTrack;
// System.Diagnostics.Debug.WriteLine(m_currentTrack.Name);

if (m_currentTrack != null)
{

title = m_currentTrack.Name;


string currentArtists = string.Empty;
foreach (SimpleArtist currentArtist in m_currentTrack.Artists)
{
currentArtists += currentArtist.Name + ", ";
}

artist = currentArtists.Remove(currentArtists.Length - 2, 2);
// AlbumLabel.Content = m_currentTrack.Album.Name;
duration = new TimeSpan(0, 0, 0, 0, m_currentTrack.DurationMs).ToString(@"hh\:mm\:ss");
}
}
if (lastSong != title && !string.IsNullOrWhiteSpace(title))
{
lastSong = title;
System.Diagnostics.Debug.WriteLine(title);
System.Diagnostics.Debug.WriteLine(artist);
System.Diagnostics.Debug.WriteLine(duration);

var theString = "Now listening to '" + title + "' by '" + artist + "' on Spotify";
var ot = new OutputText();
ot.outputLog(MainForm, theString);
ot.outputVRChat(MainForm, theString);
}


}

}




private static async Task OnErrorReceived(object sender, string error, string state)
{
Console.WriteLine($"Aborting authorization, error received: {error}");
await _server.Stop();
}


public async Task SpotifyConnect()
{
/*
// Make sure "http://localhost:5000/callback" is in your spotify application as redirect uri!
_server = new EmbedIOAuthServer(new Uri("http://localhost:5000/callback"), 5000);
await _server.Start();
// _server.AuthorizationCodeReceived += OnAuthorizationCodeReceived;
_server.ImplictGrantReceived += OnImplicitGrantReceived;
_server.ErrorReceived += OnErrorReceived;
// var request = new LoginRequest(_server.BaseUri, clientId, LoginRequest.ResponseType.Code)//for OnAuthorizationCodeReceived (do not use 'yet' requires putting secret in code or server)
var request = new LoginRequest(_server.BaseUri, clientId, LoginRequest.ResponseType.Token) //for OnImplicitGrantReceived
{
Scope = new List<string> { Scopes.UserReadCurrentlyPlaying }
};
BrowserUtil.Open(request.ToUri());
*/
_server = new EmbedIOAuthServer(new Uri("http://localhost:5000/callback"), 5000);
await _server.Start();


_server.AuthorizationCodeReceived += GetCallback;// for PKCE aka best method because you dont have to pass a clientSecret
_server.ErrorReceived += OnErrorReceived;


var (verifier, challenge) = PKCEUtil.GenerateCodes();
globalVerifier = verifier;

var loginRequest = new LoginRequest(_server.BaseUri, clientId,LoginRequest.ResponseType.Code)
{
CodeChallengeMethod = "S256",
CodeChallenge = challenge,
Scope = new[] { Scopes.UserReadCurrentlyPlaying }
};
BrowserUtil.Open(loginRequest.ToUri());
}

// This method should be called from your web-server when the user visits "http://localhost:5000/callback"
private static async Task GetCallback(object sender, AuthorizationCodeResponse response)
{
System.Diagnostics.Debug.WriteLine("code: " + response.Code.ToString());
// Note that we use the verifier calculated above!
var initialResponse = await new OAuthClient().RequestToken(new PKCETokenRequest(clientId, response.Code, new Uri("http://localhost:5000/callback"), globalVerifier));

//var spotify = new SpotifyClient(initialResponse.AccessToken);
// Also important for later: response.RefreshToken
myPKCEToken = initialResponse;

}





}
}
Loading

0 comments on commit 76acc10

Please sign in to comment.