Created
September 14, 2023 13:19
-
-
Save Yamayamada0924/0c74ec48dfe8ca6eadf0fb5cecc7bf23 to your computer and use it in GitHub Desktop.
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
using UnityEditor; | |
using UnityEngine; | |
/// <summary> | |
/// Tools - CalcSampleWindow に追加されるウィンドウ | |
/// サンプリングレート、BPM、拍子からサンプル数を計算する | |
/// 大抵の場合サンプリングレートは44100Hzで良い | |
/// </summary> | |
public class CalcSampleWindow : EditorWindow | |
{ | |
private readonly GUIContent[] _popupDisplayOptions = new[] | |
{ | |
new GUIContent("44100 Hz"), | |
new GUIContent("48000 Hz"), | |
}; | |
private int _popupIndex = 0; | |
private string _bpmString = "120"; | |
private int _bpm = 120; | |
private string _beatString = "4"; | |
private int _beat = 4; | |
private int _barSamples = 0; | |
private int _beatSamples = 0; | |
private const double OneMinute = 60.0; | |
private const int LabelWidth = 130; | |
[MenuItem("Tools/CalcSampleWindow")] | |
static void Init() | |
{ | |
var window = (CalcSampleWindow)EditorWindow.GetWindow(t:typeof(CalcSampleWindow), utility:false, title:"CalcSampleWindow"); | |
window.CalcSamples(); | |
window.Show(); | |
} | |
void OnGUI() | |
{ | |
EditorGUILayout.BeginVertical(); | |
var popupIndex = UnityEditor.EditorGUILayout.Popup( | |
label: new UnityEngine.GUIContent("サンプリングレート"), | |
selectedIndex: _popupIndex, | |
displayedOptions: _popupDisplayOptions | |
); | |
if (_popupIndex != popupIndex) | |
{ | |
_popupIndex = popupIndex; | |
CalcSamples(); | |
} | |
EditorGUILayout.BeginHorizontal(); | |
{ | |
GUILayout.Label("BPM",GUILayout.Width(LabelWidth)); | |
_bpmString = GUILayout.TextField(_bpmString); | |
if(int.TryParse(_bpmString, out var bpm)) | |
{ | |
if(_bpm != bpm) | |
{ | |
_bpm = bpm; | |
CalcSamples(); | |
} | |
} | |
} | |
EditorGUILayout.EndHorizontal(); | |
EditorGUILayout.BeginHorizontal(); | |
{ | |
GUILayout.Label("拍子(Beats)", GUILayout.Width(LabelWidth)); | |
_beatString = GUILayout.TextField(_beatString); | |
if(int.TryParse(_beatString, out var beat)) | |
{ | |
if(_beat != beat) | |
{ | |
_beat = beat; | |
CalcSamples(); | |
} | |
} | |
} | |
EditorGUILayout.EndHorizontal(); | |
EditorGUILayout.Space(8); | |
EditorGUILayout.BeginHorizontal(); | |
{ | |
GUILayout.Label("1小節(Bar)", GUILayout.Width(LabelWidth)); | |
GUILayout.Label(_barSamples.ToString()); | |
} | |
EditorGUILayout.EndHorizontal(); | |
EditorGUILayout.BeginHorizontal(); | |
{ | |
GUILayout.Label("4分音符(Beat)", GUILayout.Width(LabelWidth)); | |
GUILayout.Label(_beatSamples.ToString()); | |
} | |
EditorGUILayout.EndHorizontal(); | |
EditorGUILayout.EndVertical(); | |
} | |
private void CalcSamples() | |
{ | |
var sampleRate = 44100; | |
if (_popupIndex == 1) | |
{ | |
sampleRate = 48000; | |
} | |
var barSeconds = OneMinute / _bpm * _beat; | |
_barSamples = (int)(barSeconds * sampleRate); | |
_beatSamples = _barSamples / _beat; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment