Skip to content

Commit

Permalink
Fixed a bunch of bugs for preview6 release
Browse files Browse the repository at this point in the history
  • Loading branch information
jackyyang09 committed Aug 26, 2023
1 parent 2ab07ae commit 80a63bd
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 46 deletions.
59 changes: 34 additions & 25 deletions Editor/AudioPlaybackToolEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -864,21 +864,27 @@ private void Update()
public static Texture2D RenderStaticPreview(AudioClip clip, Rect rect, float relativeVolume)
{
if (Event.current.type != EventType.Repaint) return null;
//GUI.Box(rect, "");
float[] samples = new float[clip.samples];
float[] waveform = new float[(int)rect.width];
clip.GetData(samples, 0);
int packSize = clip.samples / (int)rect.width + 1;
int s = 0;
float[] samples = new float[0];
float[] waveform = new float[0];
int halfHeight = 0;
if (clip)
{
//GUI.Box(rect, "");
samples = new float[clip.samples];
waveform = new float[(int)rect.width];
clip.GetData(samples, 0);
int packSize = clip.samples / (int)rect.width + 1;
int s = 0;

for (int i = 0; i < clip.samples; i += packSize)
{
waveform[s] = samples[i];
s++;
}

for (int i = 0; i < clip.samples; i += packSize)
{
waveform[s] = samples[i];
s++;
halfHeight = (int)rect.height / 2;
}

int halfHeight = (int)rect.height / 2;


Texture2D tex = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGBA32, false);
for (int x = 0; x < rect.width; x++)
{
Expand All @@ -888,25 +894,28 @@ public static Texture2D RenderStaticPreview(AudioClip clip, Rect rect, float rel
}
}

Color color = new Color(1.0f, 140.0f / 255.0f, 0.0f, 1.0f);

for (int x = 0; x < waveform.Length; x++)
if (clip)
{
// Scale the wave vertically relative to half the rect height and the relative volume
float heightLimit = waveform[x] * halfHeight;
Color color = new Color(1.0f, 140.0f / 255.0f, 0.0f, 1.0f);

for (int y = (int)heightLimit; y >= 0; y--)
for (int x = 0; x < waveform.Length; x++)
{
//Color currentPixelColour = tex.GetPixel(x, halfHeight + y);
//if (currentPixelColour == Color.black) continue;
// Scale the wave vertically relative to half the rect height and the relative volume
float heightLimit = waveform[x] * halfHeight;

for (int y = (int)heightLimit; y >= 0; y--)
{
//Color currentPixelColour = tex.GetPixel(x, halfHeight + y);
//if (currentPixelColour == Color.black) continue;

tex.SetPixel(x, halfHeight + y, color);
tex.SetPixel(x, halfHeight + y, color);

// Get data from upper half offset by 1 unit due to int truncation
tex.SetPixel(x, halfHeight - (y + 1), color);
// Get data from upper half offset by 1 unit due to int truncation
tex.SetPixel(x, halfHeight - (y + 1), color);
}
}
}

tex.Apply();
return tex;
}
Expand Down
2 changes: 1 addition & 1 deletion Editor/JSAMPaths.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public static void TryCreateNewPathAsset()

var asset = CreateInstance<JSAMPaths>();
AssetDatabase.CreateAsset(asset, FULL_PATH);
asset.ResetPresetsPathIfInvalid();
asset.ResetPresetsPath();

paths = asset;
EditorUtility.DisplayDialog("JSAM First Time Setup", "Path asset created successfully!", "Cool.");
Expand Down
35 changes: 21 additions & 14 deletions Editor/MusicFileObjectEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ protected override void DrawPlaybackTool()
else mouseDragging = false;
}
if (!mouseDragging) break;
if (!music) break;
float newProgress = Mathf.InverseLerp(progressRect.xMin, progressRect.xMax, evt.mousePosition.x);
AudioPlaybackToolEditor.helperSource.time = Mathf.Clamp((newProgress * music.length), 0, music.length - AudioManagerInternal.EPSILON);
if (myScript.loopMode == LoopMode.ClampedLoopPoints)
Expand Down Expand Up @@ -329,21 +330,26 @@ protected override void DrawPlaybackTool()
// Reset loop point input mode if not using loop points so the duration shows up as time by default
if (myScript.loopMode != LoopMode.LoopWithLoopPoints && myScript.loopMode != LoopMode.ClampedLoopPoints) loopPointInputMode = 0;

switch ((LoopPointTool)loopPointInputMode)
blontent = new GUIContent("-", "The playback time");
if (music)
{
case LoopPointTool.Slider:
case LoopPointTool.TimeInput:
blontent = new GUIContent(AudioPlaybackToolEditor.TimeToString((float)AudioPlaybackToolEditor.helperSource.timeSamples / music.frequency) + " / " + (AudioPlaybackToolEditor.TimeToString(music.length)),
"The playback time in seconds");
break;
case LoopPointTool.TimeSamplesInput:
blontent = new GUIContent(AudioPlaybackToolEditor.helperSource.timeSamples + " / " + music.samples, "The playback time in samples");
break;
case LoopPointTool.BPMInput:
blontent = new GUIContent(string.Format("{0:0}", AudioPlaybackToolEditor.helperSource.time / (60f / myScript.bpm)) + " / " + music.length / (60f / myScript.bpm),
"The playback time in beats");
break;
switch ((LoopPointTool)loopPointInputMode)
{
case LoopPointTool.Slider:
case LoopPointTool.TimeInput:
blontent = new GUIContent(AudioPlaybackToolEditor.TimeToString((float)AudioPlaybackToolEditor.helperSource.timeSamples / music.frequency) + " / " + (AudioPlaybackToolEditor.TimeToString(music.length)),
"The playback time in seconds");
break;
case LoopPointTool.TimeSamplesInput:
blontent = new GUIContent(AudioPlaybackToolEditor.helperSource.timeSamples + " / " + music.samples, "The playback time in samples");
break;
case LoopPointTool.BPMInput:
blontent = new GUIContent(string.Format("{0:0}", AudioPlaybackToolEditor.helperSource.time / (60f / myScript.bpm)) + " / " + music.length / (60f / myScript.bpm),
"The playback time in beats");
break;
}
}

GUIStyle rightJustified = new GUIStyle(EditorStyles.label);
rightJustified.alignment = TextAnchor.UpperRight;
EditorGUILayout.LabelField(blontent, rightJustified);
Expand Down Expand Up @@ -409,7 +415,8 @@ bool ProgressBar(out Rect rect)

AudioClip music = files.GetArrayElementAtIndex(0).objectReferenceValue as AudioClip;
var helperSource = AudioPlaybackToolEditor.helperSource;
var value = (float)helperSource.timeSamples / (float)music.samples;
float value = 0;
if (music) value = (float)helperSource.timeSamples / (float)music.samples;

if (cachedTex == null || AudioPlaybackToolEditor.forceRepaint)
{
Expand Down
3 changes: 2 additions & 1 deletion Editor/SoundFileObjectEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,8 @@ bool ProgressBar()

AudioClip sound = playingClip;
var helperSource = AudioPlaybackToolEditor.helperSource;
var value = helperSource.time / playingClip.length;
float value = 0;
if (playingClip) value = helperSource.time / playingClip.length;

if ((cachedTex == null || AudioPlaybackToolEditor.forceRepaint) && Event.current.type == EventType.Repaint)
{
Expand Down
8 changes: 4 additions & 4 deletions Runtime/Scripts/AudioManagerInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ public MusicChannelHelper PlayMusicInternal(MusicFileObject music, Transform new
{
helper = HandleLimitedInstances(music, helper);
}
helper.Play(music);
helper.SetSpatializationTarget(newTransform);
helper.Play(music);
AudioManager.OnMusicPlayed?.Invoke(music);

return helper;
Expand All @@ -258,8 +258,8 @@ public MusicChannelHelper PlayMusicInternal(MusicFileObject music, Vector3 posit
{
helper = HandleLimitedInstances(music, helper);
}
helper.Play(music);
helper.SetSpatializationTarget(position);
helper.Play(music);
AudioManager.OnMusicPlayed?.Invoke(music);

return helper;
Expand Down Expand Up @@ -452,8 +452,8 @@ public SoundChannelHelper PlaySoundInternal(SoundFileObject sound, Transform new
{
helper = HandleLimitedInstances(sound, helper);
}
helper.Play(sound);
helper.SetSpatializationTarget(newTransform);
helper.Play(sound);
AudioManager.OnSoundPlayed?.Invoke(sound);

return helper;
Expand All @@ -476,8 +476,8 @@ public SoundChannelHelper PlaySoundInternal(SoundFileObject sound, Vector3 posit
{
helper = HandleLimitedInstances(sound, helper);
}
helper.Play(sound);
helper.SetSpatializationTarget(position);
helper.Play(sound);
AudioManager.OnSoundPlayed?.Invoke(sound);

return helper;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.brogrammist.jsam",
"displayName": "Jacky's Simple Audio Manager",
"version": "3.0.0-preview.5",
"version": "3.0.0-preview.6",
"unity": "2022.2",
"description": "A decentralized audio playing system for Unity, designed for simplicity and built to scale!",
"category": "",
Expand Down

0 comments on commit 80a63bd

Please sign in to comment.