Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit efdf244

Browse files
committed
A few cleanups and optimizations
1 parent 6ad45ac commit efdf244

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/UI/Widgets/UnityObjects/AudioClipWidget.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class AudioClipWidget : UnityObjectWidget
2222
static AudioSource Source;
2323
static AudioClipWidget CurrentlyPlaying;
2424
static Coroutine CurrentlyPlayingCoroutine;
25+
static readonly string zeroLengthString = GetLengthString(0f);
2526

2627
public AudioClip RefAudioClip;
2728
private string fullLengthText;
@@ -112,7 +113,6 @@ static string GetLengthString(float seconds)
112113
sb.Append($"{ts.Hours}:");
113114

114115
sb.Append($"{ts.Minutes:00}:");
115-
116116
sb.Append($"{ts.Seconds:00}:");
117117
sb.Append($"{ts.Milliseconds:000}");
118118

@@ -121,7 +121,7 @@ static string GetLengthString(float seconds)
121121

122122
private void ResetProgressLabel()
123123
{
124-
this.progressLabel.text = $"{GetLengthString(0f)} / {fullLengthText}";
124+
this.progressLabel.text = $"{zeroLengthString} / {fullLengthText}";
125125
}
126126

127127
private void OnPlayStopClicked()
@@ -152,8 +152,7 @@ static void SetupAudioPlayer()
152152
AudioPlayerObject = new GameObject("UnityExplorer.AudioPlayer");
153153
UnityEngine.Object.DontDestroyOnLoad(AudioPlayerObject);
154154
AudioPlayerObject.hideFlags = HideFlags.HideAndDontSave;
155-
AudioPlayerObject.transform.position = new(int.MinValue, int.MinValue);
156-
155+
AudioPlayerObject.transform.position = new(int.MinValue, int.MinValue); // move it as far away as possible
157156
#if CPP
158157
Source = AudioPlayerObject.AddComponent(UnhollowerRuntimeLib.Il2CppType.Of<AudioSource>()).TryCast<AudioSource>();
159158
#else
@@ -305,6 +304,7 @@ public override GameObject CreateContent(GameObject uiRoot)
305304
public static class SavWav
306305
{
307306
public const int HEADER_SIZE = 44;
307+
public const float RESCALE_FACTOR = 32767; // to convert float to Int16
308308

309309
public static void Save(AudioClip clip, string filepath)
310310
{
@@ -335,17 +335,17 @@ static void ConvertAndWrite(FileStream fileStream, AudioClip clip)
335335
clip.GetData(samples, 0);
336336
#endif
337337

338+
int len = samples.Length;
339+
338340
// converting in 2 float[] steps to Int16[], then Int16[] to Byte[]
339-
short[] intData = new short[samples.Length];
341+
short[] intData = new short[len];
340342

341343
// bytesData array is twice the size of dataSource array because a float converted in Int16 is 2 bytes.
342-
byte[] bytesData = new byte[samples.Length * 2];
343-
344-
float rescaleFactor = 32767; // to convert float to Int16
344+
byte[] bytesData = new byte[len * 2];
345345

346-
for (int i = 0; i < samples.Length; i++)
346+
for (int i = 0; i < len; i++)
347347
{
348-
intData[i] = (short)(samples[i] * rescaleFactor);
348+
intData[i] = (short)(samples[i] * RESCALE_FACTOR);
349349
byte[] byteArr = BitConverter.GetBytes(intData[i]);
350350
byteArr.CopyTo(bytesData, i * 2);
351351
}

0 commit comments

Comments
 (0)