Initial commit
This commit is contained in:
commit
262601edb8
2025 files changed
+218754
No files matched your search
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class AudioManager : MonoBehaviour
|
||||
{
|
||||
[HideInInspector]
|
||||
public AudioClip Clip { get; set; }
|
||||
public AudioSource source;
|
||||
|
||||
[Range(0f, 1f)]
|
||||
public float volume;
|
||||
|
||||
[Range(0f, 1f)]
|
||||
public float pitch;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
source = GetComponent<AudioSource>();
|
||||
source.clip = Clip;
|
||||
}
|
||||
|
||||
public void UpdateClip(AudioClip clip)
|
||||
{
|
||||
source.clip = clip;
|
||||
}
|
||||
|
||||
public void Play()
|
||||
{
|
||||
source.Play();
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
source.Stop();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 61669be1370e61be299d7421ad39b205
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,97 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
public class BassEffect
|
||||
{
|
||||
public AudioSource source;
|
||||
|
||||
public int range = 128;
|
||||
public int sensitivity = 9;
|
||||
public float bassDesensitivity = 0.4f;
|
||||
|
||||
public float[] SpectrumData { get; set; }
|
||||
|
||||
public float bassThreshhold = 0.1f;
|
||||
|
||||
public Queue<float> bassHistory = new(5);
|
||||
|
||||
List<float> bassData = new();
|
||||
|
||||
|
||||
// No sensitivity and bass reduction
|
||||
public float[] UnprocessedSpectrumData { get; set; }
|
||||
|
||||
// Raw data from GetSpectrumData
|
||||
private float[] RawSpectrumData;
|
||||
|
||||
public BassEffect(AudioSource source)
|
||||
{
|
||||
this.source = source;
|
||||
|
||||
SpectrumData = new float[range];
|
||||
RawSpectrumData = new float[4 * range];
|
||||
}
|
||||
|
||||
public float[] UpdateSpectrumData()
|
||||
{
|
||||
source.GetSpectrumData(RawSpectrumData, 0, FFTWindow.Blackman);
|
||||
|
||||
float[] res = new float[range];
|
||||
|
||||
// Smooth the spectrum data using a moving average
|
||||
for (int i = 0; i < range; i++)
|
||||
{
|
||||
float result = Mathf.Lerp(SpectrumData[i], RawSpectrumData[i], 0.1f);
|
||||
SpectrumData[i] = result;
|
||||
|
||||
if (i < (range / 14))
|
||||
{
|
||||
result *= bassDesensitivity;
|
||||
}
|
||||
|
||||
res[i] = sensitivity * result;
|
||||
}
|
||||
IsBassOn();
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
public float[] GetBass()
|
||||
{
|
||||
return SpectrumData.Take(3).ToArray();
|
||||
}
|
||||
|
||||
public float GetBassStrength()
|
||||
{
|
||||
float[] bass = GetBass();
|
||||
|
||||
float sum = bass.Sum() * 5f;
|
||||
|
||||
return (float) Math.Pow(sum, 2) / 10f;
|
||||
}
|
||||
|
||||
public bool IsBassOn()
|
||||
{
|
||||
float[] bass = GetBass();
|
||||
|
||||
float level = bass.Sum() / bass.Length;
|
||||
|
||||
bassData.Add(level);
|
||||
|
||||
return level > bassThreshhold;
|
||||
}
|
||||
|
||||
public void WriteData()
|
||||
{
|
||||
string res = "";
|
||||
|
||||
foreach (var bass in bassData)
|
||||
{
|
||||
res += bass + ", ";
|
||||
}
|
||||
|
||||
System.IO.File.WriteAllText("/home/thastertyn/bass-data.txt", res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 439cd1a6fdb271b12bbba95d1273c881
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,82 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
using UITypes;
|
||||
|
||||
public class Game : MonoBehaviour
|
||||
{
|
||||
const float DELAY = 0.005f; // 200 TPS
|
||||
|
||||
public AudioManager manager;
|
||||
|
||||
private MainMenu menu;
|
||||
private BassEffect effect;
|
||||
|
||||
string totalDuration;
|
||||
bool repeatRoutine = true;
|
||||
|
||||
|
||||
void Awake()
|
||||
{
|
||||
menu = GetComponent<MainMenu>();
|
||||
|
||||
effect = new(manager.source);
|
||||
}
|
||||
|
||||
public void GameUpdate()
|
||||
{
|
||||
float[] specData = effect.UpdateSpectrumData();
|
||||
float bassStrength = effect.GetBassStrength();
|
||||
|
||||
menu.GameElem.UpdateVisualizers(specData, bassStrength);
|
||||
|
||||
UpdateTimestamp();
|
||||
}
|
||||
|
||||
IEnumerator GameLoop()
|
||||
{
|
||||
while (repeatRoutine)
|
||||
{
|
||||
GameUpdate();
|
||||
yield return new WaitForSeconds(DELAY);
|
||||
}
|
||||
}
|
||||
|
||||
public void LoadSong(SongEntry song)
|
||||
{
|
||||
totalDuration = song.Song.Duration;
|
||||
|
||||
manager.source.clip = song.Song.Clip;
|
||||
|
||||
menu.GameElem.SetColumnColor(song.Song.AverageColor);
|
||||
}
|
||||
|
||||
void UpdateTimestamp()
|
||||
{
|
||||
int totalSeconds = (int)manager.source.time;
|
||||
int h = totalSeconds / 3600;
|
||||
int m = (totalSeconds % 3600) / 60;
|
||||
int s = totalSeconds % 60;
|
||||
|
||||
string result = $"{h:D2}:{m:D2}:{s:D2} / {totalDuration}";
|
||||
menu.GameElem.UpdateSongTime(result);
|
||||
}
|
||||
|
||||
public void EscapeClicked()
|
||||
{
|
||||
repeatRoutine = false;
|
||||
manager.Stop();
|
||||
menu.Escape();
|
||||
}
|
||||
|
||||
public void Play()
|
||||
{
|
||||
manager.Play();
|
||||
repeatRoutine = true;
|
||||
StartCoroutine(GameLoop());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b1cb1154a484d1bf18dda4f1c4f16322
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,20 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
[System.Serializable]
|
||||
public class Sound : MonoBehaviour
|
||||
{
|
||||
public AudioClip clip;
|
||||
|
||||
[Range(0f, 1f)]
|
||||
public float volume;
|
||||
|
||||
[Range(0f, 1f)]
|
||||
public float pitch;
|
||||
|
||||
public string soundName;
|
||||
|
||||
[HideInInspector]
|
||||
public AudioSource source;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 33f413aa8eabb985da07d956df7f198e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in new issue
Block a user