public void SetMenuAmbienceVolume(float volume) { //AmbienceVolume(ref _songPreviewPlayer) = volume / PreviewVolume(ref _songPreviewPlayer); var audioClip = DefaultAudioClip(ref _songPreviewPlayer); _songPreviewPlayer.CrossfadeTo(audioClip, Mathf.Max(Random.Range(0f, audioClip.length - 0.1f), 0f), -1f, true); }
/// <summary> /// Play the preview audio of the provided preview beatmap level. /// </summary> /// <param name="level">The level to play the audio from.</param> public async void CrossfadeAudioToLevelAsync(IPreviewBeatmapLevel level) { if (_songPreviewPlayerCrossfadingLevelID != level.levelID) { try { _songPreviewPlayerCrossfadingLevelID = level.levelID; if (_cancellationTokenSource != null) { _cancellationTokenSource.Cancel(); } _cancellationTokenSource = new CancellationTokenSource(); CancellationToken token = _cancellationTokenSource.Token; AudioClip audio = await level.GetPreviewAudioClipAsync(token); token.ThrowIfCancellationRequested(); _songPreviewPlayer.CrossfadeTo(audio, level.previewStartTime, level.previewDuration); } catch (OperationCanceledException) { if (_songPreviewPlayerCrossfadingLevelID == level.levelID) { _songPreviewPlayerCrossfadingLevelID = null; } } } }
//////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// /// <summary> /// When a cell is selected /// </summary> /// <param name="p_Table">Table instance</param> /// <param name="p_Row">Row index</param> public void DidSelectCellWithIdxEvent(HMUI.TableView p_Table, int p_Row) { if (m_SongPreviewPlayer == null || !m_SongPreviewPlayer || !PlayPreviewAudio || p_Row > Data.Count) { return; } /// Fetch song entry var l_SongRowData = Data[p_Row]; /// Hide if invalid song if (l_SongRowData == null || l_SongRowData.Invalid) { return; } if (l_SongRowData.CustomLevel != null && SongCore.Loader.CustomLevels.ContainsKey(l_SongRowData.CustomLevel.customLevelPath)) { if (AudioClipCache.TryGetValue(l_SongRowData.GetLevelHash(), out var l_AudioClip)) { m_SongPreviewPlayer.CrossfadeTo(l_AudioClip, l_SongRowData.CustomLevel.previewStartTime, l_SongRowData.CustomLevel.previewDuration, PreviewAudioVolume); } else { l_SongRowData.CustomLevel.GetPreviewAudioClipAsync(CancellationToken.None).ContinueWith(x => { if (x.IsCompleted && x.Status == TaskStatus.RanToCompletion) { Unity.MainThreadInvoker.Enqueue(() => { if (!AudioClipCache.ContainsKey(l_SongRowData.GetLevelHash())) { AudioClipCache.Add(l_SongRowData.GetLevelHash(), x.Result); } m_SongPreviewPlayer.CrossfadeTo(x.Result, l_SongRowData.CustomLevel.previewStartTime, l_SongRowData.CustomLevel.previewDuration, PreviewAudioVolume); }); } }); } } else { /// Stop preview music if any m_SongPreviewPlayer.CrossfadeToDefault(); } }
public static void UpdateBGVolume() { if (player != null && SettingsUI.isMenuScene(SceneManager.GetActiveScene())) { float newVolume = normalVolume * Settings.MenuBGVolume; ReflectionUtil.SetPrivateField(player, "_ambientVolumeScale", newVolume); player.CrossfadeTo(ReflectionUtil.GetPrivateField <AudioClip>(player, "_defaultAudioClip"), 0f, -1f, newVolume); } }
/// <summary> /// When a song is selected /// </summary> /// <param name="p_TableView">Source table</param> /// <param name="p_Row">Selected row</param> private void OnSongSelected(TableView p_TableView, int p_Row) { /// Fetch song entry var l_SongEntry = m_SongList.Data[p_Row]; /// Hide if invalid song if (l_SongEntry == null) { /// Hide song info panel UnselectSong(); return; } /// Show UIs m_SongInfoPanel.SetActive(true); FlowCoordinator.DetailView.SetVisible(true); /// Update UIs m_SongInfo_Detail.FromBeatSaver(l_SongEntry.BeatMap, l_SongEntry.Cover); m_SongInfo_Detail.SetFavoriteToggleValue(m_TypeSegmentControl.selectedCellNumber == 2 /* Blacklist */); FlowCoordinator.DetailView.SetDetail(l_SongEntry.BeatMap); /// Set selected song m_SelectedSong = l_SongEntry; /// Launch preview music if local map var l_LocalSong = SongCore.Loader.GetLevelByHash(m_SelectedSong.BeatMap.Hash); if (l_LocalSong != null && SongCore.Loader.CustomLevels.ContainsKey(l_LocalSong.customLevelPath)) { m_SongInfo_Detail.SetPlayButtonText("Play"); /// Load the song clip to get duration if (Config.ChatRequest.PlayPreviewMusic) { l_LocalSong.GetPreviewAudioClipAsync(CancellationToken.None).ContinueWith(x => { if (x.IsCompleted && x.Status == TaskStatus.RanToCompletion) { HMMainThreadDispatcher.instance.Enqueue(() => { /// Start preview song m_SongPreviewPlayer.CrossfadeTo(x.Result, l_LocalSong.previewStartTime, l_LocalSong.previewDuration, 1f); }); } }); } } /// Stop preview music if any else { m_SongInfo_Detail.SetPlayButtonText("Download"); m_SongPreviewPlayer.CrossfadeToDefault(); } }
/// <summary> /// When a song is selected /// </summary> /// <param name="p_TableView">Source table</param> /// <param name="p_Row">Selected row</param> private void OnSongSelected(TableView p_TableView, int p_Row) { /// Fetch song entry BeatSaverCustomSongCellInfo l_SongRow = m_SongList.data[p_Row] as BeatSaverCustomSongCellInfo; /// Get song entry var l_SongEntry = m_SongsProvider.Where(x => x.BeatMap.Hash == l_SongRow.SongHash).FirstOrDefault(); /// Hide if invalid song if (l_SongEntry == null) { /// Hide song info panel UnselectSong(); return; } /// Show UIs m_SongInfoPanel.SetActive(true); /// Update UIs m_SongInfo_Detail.FromBeatSaver(l_SongEntry.BeatMap, l_SongEntry.Cover); /// Set selected song m_SelectedSong = l_SongEntry; /// Launch preview music if local map var l_LocalSong = SongCore.Loader.GetLevelByHash(m_SelectedSong.BeatMap.Hash); if (l_LocalSong != null) { m_SongInfo_Detail.SetPlayButtonText("Play"); /// Load the song clip to get duration if (Config.ChatRequest.PlayPreviewMusic) { l_LocalSong.GetPreviewAudioClipAsync(CancellationToken.None).ContinueWith(x => { if (x.IsCompleted && x.Status == TaskStatus.RanToCompletion) { HMMainThreadDispatcher.instance.Enqueue(() => { /// Start preview song m_SongPreviewPlayer.CrossfadeTo(x.Result, l_LocalSong.previewStartTime, l_LocalSong.previewDuration, 1f); }); } }); } } /// Stop preview music if any else { m_SongInfo_Detail.SetPlayButtonText("Download"); m_SongPreviewPlayer.CrossfadeToDefault(); } }
public async Task PlayRandomAudio(UnityEngine.AudioClip fallback, LevelCompletionResults results, bool highScore) { await SiraUtil.Utilities.AwaitSleep(250); if (results.levelEndStateType == LevelCompletionResults.LevelEndStateType.Failed && _config.ResultFailedSoundsEnabled) { var hasAudio = _config.EnabledResultFailedSounds.Count != 0; if (!hasAudio) { return; } var randomAudioFile = _config.EnabledResultFailedSounds[_random.Next(0, _config.EnabledResultFailedSounds.Count)]; _siraLog.Info($"Loading Audio File: {randomAudioFile.FullName}"); var clip = await _audioClipAsyncLoader.LoadAudioClipAsync(randomAudioFile.FullName, CancellationToken.None); Play(clip); } else if (results.levelEndStateType == LevelCompletionResults.LevelEndStateType.Cleared && _config.ResultSoundsEnabled) { var info = results.fullCombo ? (_config.EnabledResultFCSounds.Count == 0 ? _config.EnabledResultSounds : _config.EnabledResultFCSounds) : _config.EnabledResultSounds; var hasAudio = info.Count != 0; if (!hasAudio) { if (highScore) { _songPreviewPlayer.CrossfadeTo(fallback, 0f, fallback.length); } return; } var randomAudioFile = info[_random.Next(0, info.Count)]; _siraLog.Info($"Loading Audio File: {randomAudioFile.FullName}"); var clip = await _audioClipAsyncLoader.LoadAudioClipAsync(randomAudioFile.FullName, CancellationToken.None); if (highScore) { Play(clip); } } }
IEnumerator ClickTheCircles() { SongPreviewPlayer previewPlayer = null; yield return(new WaitUntil(() => previewPlayer = Resources.FindObjectsOfTypeAll <SongPreviewPlayer>().FirstOrDefault())); yield return(new WaitForSeconds(1)); if (previewPlayer) { previewPlayer.CrossfadeTo(circlesClip, 1, circlesClip.length); } }
void PlayPreview(LevelStaticData _songData) { log.Log("Playing preview for " + _songData.songName); if (_songData.previewAudioClip != null) { if (_songPreviewPlayer != null && _songData != null) { try { _songPreviewPlayer.CrossfadeTo(_songData.previewAudioClip, _songData.previewStartTime, _songData.previewDuration, 1f); } catch (Exception e) { log.Error("Can't play preview! Exception: " + e); } } } else { StartCoroutine(PlayPreviewCoroutine(_songData)); } }
static bool Prefix(MissionNodeVisualController missionNodeVisualController, MissionSelectionMapViewController __instance, SongPreviewPlayer ____songPreviewPlayer) { if (missionNodeVisualController.missionNode.missionData is CustomMissionDataSO) { __instance.SetPrivateField("_selectedMissionNode", missionNodeVisualController.missionNode); CustomPreviewBeatmapLevel level = (missionNodeVisualController.missionNode.missionData as CustomMissionDataSO).customLevel; if (level != null) { ____songPreviewPlayer.CrossfadeTo(level.GetPreviewAudioClipAsync(CancellationToken.None).Result, level.previewStartTime, level.previewDuration); } __instance.GetPrivateField <Action <MissionSelectionMapViewController, MissionNode> >("didSelectMissionLevelEvent")(__instance, missionNodeVisualController.missionNode); return(false); } return(true); }
private void DetailViewPreviewPressed() { if (previewPlaying) { StopPreview(); } else { // start preview ScreenManager.Instance.PlayVideo(); songPreviewPlayer.CrossfadeTo(selectedLevel.GetPreviewAudioClipAsync(new CancellationToken()).Result, 0, selectedLevel.previewDuration, 1f); previewPlaying = true; } _videoDetailViewController.SetPreviewState(previewPlaying); }
private void DetailViewPreviewPressed() { if (previewPlaying) { StopPreview(); } else { // start preview ScreenManager.Instance.PlayVideo(); songPreviewPlayer.CrossfadeTo(selectedLevel.beatmapLevelData.audioClip, 0, selectedLevel.beatmapLevelData.audioClip.length, 1f); previewPlaying = true; } _videoDetailViewController.SetPreviewState(previewPlaying); }
void PlayPreview(LevelStaticData _songData) { Console.WriteLine("Playing preview for " + _songData.songName); if (_songData.previewAudioClip != null) { if (_songPreviewPlayer != null && _songData != null) { try { _songPreviewPlayer.CrossfadeTo(_songData.previewAudioClip, _songData.previewStartTime, _songData.previewDuration, 1f); } catch (Exception e) { Console.WriteLine("Can't play preview! Exception: " + e); } } } }
private void PlayPreview(CustomLevel level) { _songPreviewPlayer.CrossfadeTo(level.audioClip, level.previewStartTime, level.audioClip.length - level.previewStartTime); }
private void DataReceived(string[] _data) { if (_data != null && _data.Length > 0) { foreach (string data in _data) { ServerCommand command = null; try { command = JsonUtility.FromJson <ServerCommand>(data); } catch (Exception e) { Console.WriteLine("Can't parse received JSON! Exception: " + e); } try { if (command != null) { switch (command.commandType) { case ServerCommandType.UpdateRequired: { _selectText.text = "Plugin version mismatch:\nServer: " + command.version + "\nClient: " + BSMultiplayerMain.version; BSMultiplayerMain._instance.DisconnectFromServer(); _loading = false; }; break; case ServerCommandType.SetServerState: { if (command.serverState == ServerState.Playing) { _loading = false; TimeSpan timeRemaining = TimeSpan.FromSeconds(command.selectedSongDuration - command.selectedSongPlayTime); _timerText.text = timeRemaining.Minutes.ToString("00") + ":" + timeRemaining.Seconds.ToString("00"); _selectedSong = SongLoader.CustomSongInfos.First(x => x.levelId == command.selectedLevelID); _selectedSongCell.gameObject.SetActive(true); (_selectedSongCell.transform as RectTransform).anchoredPosition = new Vector2(14f, 39f); _selectedSongCell.songName = _selectedSong.songName + "\n<size=80%>" + _selectedSong.songSubName + "</size>"; _selectedSongCell.author = _selectedSong.authorName; CustomLevelStaticData _songData = SongLoader.CustomLevelStaticDatas.First(x => x.levelId == _selectedSong.levelId); _selectedSongCell.coverImage = _songData.coverImage; _songPreviewPlayer.CrossfadeTo(_songData.previewAudioClip, (float)command.selectedSongPlayTime, (float)(command.selectedSongDuration - command.selectedSongPlayTime - 5f), 1f); } }; break; case ServerCommandType.SetLobbyTimer: { _loading = false; Console.WriteLine("Set timer text to " + command.lobbyTimer); _timerText.text = command.lobbyTimer.ToString(); if (_multiplayerLeaderboard != null && _viewControllers.IndexOf(_multiplayerLeaderboard) >= 0) { _viewControllers.Remove(_multiplayerLeaderboard); Destroy(_multiplayerLeaderboard.gameObject); _songPreviewPlayer.CrossfadeToDefault(); _selectedSongCell.gameObject.SetActive(false); } }; break; case ServerCommandType.SetSelectedSong: { Console.WriteLine("Set selected song " + command.selectedLevelID); _loading = false; if (_selectedSong == null) { try { _selectedSongDifficulty = command.selectedSongDifficlty; _selectedSong = SongLoader.CustomSongInfos.First(x => x.levelId == command.selectedLevelID); _selectText.text = "Next song:"; _selectedSongCell.gameObject.SetActive(true); (_selectedSongCell.transform as RectTransform).anchoredPosition = new Vector2(-25f, 0); _selectedSongCell.songName = _selectedSong.songName + "\n<size=80%>" + _selectedSong.songSubName + "</size>"; _selectedSongCell.author = _selectedSong.authorName; CustomLevelStaticData _songData = SongLoader.CustomLevelStaticDatas.First(x => x.levelId == _selectedSong.levelId); _selectedSongCell.coverImage = _songData.coverImage; PlayPreview(_songData); } catch (Exception e) { Console.WriteLine("EXCEPTION: " + e); } } Console.WriteLine("Done"); }; break; case ServerCommandType.StartSelectedSongLevel: { Console.WriteLine("Starting selected song! Song: " + _selectedSong.songName + ", Diff: " + ((LevelStaticData.Difficulty)_selectedSongDifficulty).ToString()); BSMultiplayerMain._instance.DataReceived -= DataReceived; GameplayOptions gameplayOptions = new GameplayOptions(); gameplayOptions.noEnergy = true; gameplayOptions.mirror = false; if (BSMultiplayerMain._instance._mainGameSceneSetupData != null) { BSMultiplayerMain._instance._mainGameSceneSetupData.SetData(_selectedSong.levelId, (LevelStaticData.Difficulty)_selectedSongDifficulty, null, null, 0f, gameplayOptions, GameplayMode.SoloStandard, null); BSMultiplayerMain._instance._mainGameSceneSetupData.TransitionToScene(0.7f); _selectedSong = null; return; } else { Console.WriteLine("SceneSetupData is null!"); } }; break; case ServerCommandType.DownloadSongs: { if (!AllSongsDownloaded(command.songsToDownload)) { StartCoroutine(DownloadSongs(command.songsToDownload)); BSMultiplayerMain._instance.DisconnectFromServer(); } }; break; case ServerCommandType.SetPlayerInfos: { if (_multiplayerLeaderboard != null) { TimeSpan timeRemaining = TimeSpan.FromSeconds(command.selectedSongDuration - command.selectedSongPlayTime); _timerText.text = timeRemaining.Minutes.ToString("00") + ":" + timeRemaining.Seconds.ToString("00"); try { _multiplayerLeaderboard.SetLeaderboard(command.playerInfos.Select(x => JsonUtility.FromJson <PlayerInfo>(x)).ToArray()); } catch (Exception e) { Console.WriteLine("Leaderboard exception: " + e); } } }; break; } } else { Console.WriteLine("Server Command is null!"); } } catch (Exception e) { Console.WriteLine("Exception (parse switch): " + e); } } } StartCoroutine(BSMultiplayerMain._instance.ReceiveFromServerCoroutine()); }
public void SetMenuAmbienceVolume(float volume) { var audioClip = DefaultAudioClip(ref _songPreviewPlayer); _songPreviewPlayer.CrossfadeTo(audioClip, AudioHelpers.NormalizedVolumeToDB(volume), Mathf.Max(Random.Range(0f, audioClip.length - 0.1f), 0f), -1f, true, null); }
private void DataReceived(string[] _data) { if (_data != null && _data.Length > 0) { foreach (string data in _data) { ServerCommand command = null; try { command = JsonUtility.FromJson <ServerCommand>(data); } catch (Exception e) { Console.WriteLine("Can't parse received JSON! Exception: " + e); } try { if (command != null) { switch (command.commandType) { case ServerCommandType.UpdateRequired: { _selectText.text = "Plugin version mismatch:\nServer: " + command.version + "\nClient: " + BSMultiplayerClient.version; BSMultiplayerClient._instance.DisconnectFromServer(); _loading = false; }; break; case ServerCommandType.SetServerState: { if (command.serverState == ServerState.Playing) { _loading = false; TimeSpan timeRemaining = TimeSpan.FromSeconds(command.selectedSongDuration - command.selectedSongPlayTime); _timerText.text = timeRemaining.Minutes.ToString("00") + ":" + timeRemaining.Seconds.ToString("00"); _selectedSong = SongLoader.CustomSongInfos.First(x => x.levelId == command.selectedLevelID); _selectedSongCell.gameObject.SetActive(true); (_selectedSongCell.transform as RectTransform).anchoredPosition = new Vector2(14f, 39f); _selectedSongCell.songName = _selectedSong.songName + "\n<size=80%>" + _selectedSong.songSubName + "</size>"; _selectedSongCell.author = _selectedSong.authorName; CustomLevelStaticData _songData = SongLoader.CustomLevelStaticDatas.First(x => x.levelId == _selectedSong.levelId); _selectedSongCell.coverImage = _songData.coverImage; _songPreviewPlayer.CrossfadeTo(_songData.previewAudioClip, (float)command.selectedSongPlayTime, (float)(command.selectedSongDuration - command.selectedSongPlayTime - 5f), 1f); } }; break; case ServerCommandType.SetLobbyTimer: { _loading = false; Console.WriteLine("Set timer text to " + command.lobbyTimer); _timerText.text = command.lobbyTimer.ToString(); if (_multiplayerLeaderboard != null && _viewControllers.IndexOf(_multiplayerLeaderboard) >= 0) { _viewControllers.Remove(_multiplayerLeaderboard); Destroy(_multiplayerLeaderboard.gameObject); _songPreviewPlayer.CrossfadeToDefault(); _selectedSongCell.gameObject.SetActive(false); } }; break; case ServerCommandType.SetSelectedSong: { Console.WriteLine("Set selected song " + command.selectedLevelID); _loading = false; if (_selectedSong == null) { try { _selectedSongDifficulty = command.selectedSongDifficlty; _selectedSong = SongLoader.CustomSongInfos.First(x => x.levelId == command.selectedLevelID); _selectText.text = "Next song:"; _selectedSongCell.gameObject.SetActive(true); (_selectedSongCell.transform as RectTransform).anchoredPosition = new Vector2(-25f, 0); _selectedSongCell.songName = _selectedSong.songName + "\n<size=80%>" + _selectedSong.songSubName + "</size>"; _selectedSongCell.author = _selectedSong.authorName; CustomLevelStaticData _songData = SongLoader.CustomLevelStaticDatas.First(x => x.levelId == _selectedSong.levelId); _selectedSongCell.coverImage = _songData.coverImage; PlayPreview(_songData); } catch (Exception e) { Console.WriteLine("EXCEPTION: " + e); } } Console.WriteLine("Done"); }; break; case ServerCommandType.StartSelectedSongLevel: { Console.WriteLine("Starting selected song! Song: " + _selectedSong.songName + ", Diff: " + ((LevelStaticData.Difficulty)_selectedSongDifficulty).ToString()); BSMultiplayerClient._instance.DataReceived -= DataReceived; GameplayOptions gameplayOptions = new GameplayOptions(); gameplayOptions.noEnergy = true; gameplayOptions.mirror = false; if (BSMultiplayerClient._instance._mainGameSceneSetupData != null) { BSMultiplayerClient._instance._mainGameSceneSetupData.SetData(_selectedSong.levelId, (LevelStaticData.Difficulty)_selectedSongDifficulty, null, null, 0f, gameplayOptions, GameplayMode.SoloStandard, null); BSMultiplayerClient._instance._mainGameSceneSetupData.TransitionToScene(0.7f); _selectedSong = null; return; } else { Console.WriteLine("SceneSetupData is null!"); } }; break; case ServerCommandType.DownloadSongs: { if (!AllSongsDownloaded(command.songsToDownload)) { StartCoroutine(DownloadSongs(command.songsToDownload)); BSMultiplayerClient._instance.DisconnectFromServer(); } }; break; case ServerCommandType.SetPlayerInfos: { if (command.serverState == ServerState.Playing) { if (_multiplayerLeaderboard != null) { TimeSpan timeRemaining = TimeSpan.FromSeconds(command.selectedSongDuration - command.selectedSongPlayTime); _timerText.text = timeRemaining.Minutes.ToString("00") + ":" + timeRemaining.Seconds.ToString("00"); try { _multiplayerLeaderboard.SetLeaderboard(command.playerInfos.Select(x => JsonUtility.FromJson <PlayerInfo>(x)).ToArray()); } catch (Exception e) { Console.WriteLine("Leaderboard exception: " + e); } } } else { _playerInfos.Clear(); foreach (string playerStr in command.playerInfos) { PlayerInfo player = JsonUtility.FromJson <PlayerInfo>(playerStr); if (!String.IsNullOrEmpty(player.playerAvatar)) { byte[] avatar = Convert.FromBase64String(player.playerAvatar); player.rightHandPos = Serialization.ToVector3(avatar.Take(12).ToArray()); player.leftHandPos = Serialization.ToVector3(avatar.Skip(12).Take(12).ToArray()); player.headPos = Serialization.ToVector3(avatar.Skip(24).Take(12).ToArray()); player.rightHandRot = Serialization.ToQuaternion(avatar.Skip(36).Take(16).ToArray()); player.leftHandRot = Serialization.ToQuaternion(avatar.Skip(52).Take(16).ToArray()); player.headRot = Serialization.ToQuaternion(avatar.Skip(68).Take(16).ToArray()); } _playerInfos.Add(player); } try { if (_avatars.Count > _playerInfos.Count) { List <AvatarController> avatarsToRemove = new List <AvatarController>(); for (int i = _playerInfos.Count; i < _avatars.Count; i++) { avatarsToRemove.Add(_avatars[i]); } foreach (AvatarController avatar in avatarsToRemove) { _avatars.Remove(avatar); Destroy(avatar.gameObject); } } else if (_avatars.Count < _playerInfos.Count) { for (int i = 0; i < (_playerInfos.Count - _avatars.Count); i++) { _avatars.Add(new GameObject("Avatar").AddComponent <AvatarController>()); } } for (int i = 0; i < _playerInfos.Count; i++) { _avatars[i].SetPlayerInfo(_playerInfos[i], 0f, localPlayerInfo.Equals(_playerInfos[i])); } } catch (Exception e) { Console.WriteLine($"AVATARS EXCEPTION: {e}"); } } }; break; } _serverState = command.serverState; } else { Console.WriteLine("Server Command is null!"); } }catch (Exception e) { Console.WriteLine("Exception (parse switch): " + e); } } } StartCoroutine(BSMultiplayerClient._instance.ReceiveFromServerCoroutine()); }