Skip to content

Commit 42770c4

Browse files
committed
Preview Audio from Sound 🔊
1 parent 6b0234e commit 42770c4

File tree

2 files changed

+15
-101
lines changed

2 files changed

+15
-101
lines changed

Editor/SoundEditor.cs

+7-100
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using UnityEngine.Audio;
55
using SoundSystem;
66

7+
//https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/Inspector/AudioClipInspector.cs
8+
79
[CustomEditor(typeof(Sound))]
810
public class SoundEditor : Editor {
911
private Sound _targetSound;
@@ -27,7 +29,6 @@ private void OnDisable() {
2729
}
2830

2931
// Passing in clip and importer separately as we're not completely done with the asset setup at the time we're asked to generate the preview.
30-
//https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/Inspector/AudioClipInspector.cs
3132
private void DoRenderPreview(AudioClip clip, Rect wantedRect, float scaleFactor) {
3233
// scaleFactor *= 0.95f; // Reduce amplitude slightly to make highly compressed signals fit.
3334
float[] sampleData = new float[_targetClip.samples * _targetClip.channels];
@@ -62,6 +63,8 @@ private void DoRenderPreview(AudioClip clip, Rect wantedRect, float scaleFactor)
6263
}
6364

6465
private void PlayPreview() {
66+
_targetClip = _targetSound.GetClip();
67+
6568
_previewer.clip = _targetClip;
6669
_previewer.volume = _targetSound.Volume;
6770
_previewer.pitch = _targetSound.Pitch;
@@ -71,9 +74,9 @@ private void PlayPreview() {
7174
_previewer.Play();
7275
}
7376

74-
private void PreviewNextClip() {
77+
private void GetNextClip() {
78+
_targetSound.ManualIncrementIndex();
7579
_targetClip = _targetSound.GetClip();
76-
// PlayPreview();
7780
}
7881

7982
private void StopPreview() {
@@ -94,11 +97,7 @@ public override void OnPreviewGUI(Rect r, GUIStyle background) {
9497
if (Event.current.isMouse) {
9598
return;
9699
}
97-
//TODO replace this line
98100
gameObjectEditor.DrawPreview(r);
99-
// gameObjectEditor.RenderStaticPreview();
100-
// Texture2D temp = AssetPreview.GetAssetPreview(_targetClip);
101-
// DoRenderPreview(_targetClip, r, 1.0f);
102101
if (_previewer.isPlaying) {
103102
float t = _previewer.time;
104103

@@ -115,76 +114,6 @@ public override void OnPreviewGUI(Rect r, GUIStyle background) {
115114
}
116115
}
117116

118-
//TODO figure out how to override click events
119-
// public override void OnPreviewGUI(Rect r, GUIStyle background) {
120-
// if (s_DefaultIcon == null) Init();
121-
122-
// Event evt = Event.current;
123-
// if (evt.type != EventType.Repaint && evt.type != EventType.Layout && evt.type != EventType.Used) {
124-
// switch (evt.type) {
125-
// case EventType.MouseDrag:
126-
// case EventType.MouseDown: {
127-
// if (r.Contains(evt.mousePosition)) {
128-
// var startSample = (int)(evt.mousePosition.x * (_targetClip.samples / (int)r.width));
129-
// if (!AudioUtil.IsPreviewClipPlaying() || _targetClip != m_Clip)
130-
// PlayClip(clip, startSample, s_Loop);
131-
// else
132-
// AudioUtil.SetPreviewClipSamplePosition(clip, startSample);
133-
// evt.Use();
134-
// }
135-
// }
136-
// break;
137-
// }
138-
// return;
139-
// }
140-
141-
// if (Event.current.type == EventType.Repaint)
142-
// background.Draw(r, false, false, false, false);
143-
144-
// int c = _targetClip.channels;
145-
// Rect s_WantedRect = new Rect(r.x, r.y, r.width, r.height);
146-
// float sec2px = ((float)s_WantedRect.width / _targetClip.length);
147-
148-
// bool previewAble = AudioUtil.HasPreview(clip) || !(AudioUtil.IsTrackerFile(clip));
149-
// if (!previewAble) {
150-
// float labelY = (r.height > 150) ? r.y + (r.height / 2) - 10 : r.y + (r.height / 2) - 25;
151-
// if (r.width > 64) {
152-
// if (AudioUtil.IsTrackerFile(clip)) {
153-
// EditorGUI.DropShadowLabel(new Rect(r.x, labelY, r.width, 20), string.Format("Module file with " + AudioUtil.GetMusicChannelCount(clip) + " channels."));
154-
// } else
155-
// EditorGUI.DropShadowLabel(new Rect(r.x, labelY, r.width, 20), "Can not show PCM data for this file");
156-
// }
157-
158-
// if (m_Clip == clip && playing) {
159-
// float t = AudioUtil.GetPreviewClipPosition();
160-
161-
// System.TimeSpan ts = new System.TimeSpan(0, 0, 0, 0, (int)(t * 1000.0f));
162-
163-
// EditorGUI.DropShadowLabel(new Rect(s_WantedRect.x, s_WantedRect.y, s_WantedRect.width, 20), string.Format("Playing - {0:00}:{1:00}.{2:000}", ts.Minutes, ts.Seconds, ts.Milliseconds));
164-
// }
165-
// } else {
166-
// PreviewGUI.BeginScrollView(s_WantedRect, m_Position, s_WantedRect, "PreHorizontalScrollbar", "PreHorizontalScrollbarThumb");
167-
168-
// if (Event.current.type == EventType.Repaint) {
169-
// DoRenderPreview(_targetClip, r, 1.0f);
170-
// }
171-
172-
// for (int i = 0; i < c; ++i) {
173-
// if (c > 1 && r.width > 64) {
174-
// var labelRect = new Rect(s_WantedRect.x + 5, s_WantedRect.y + (s_WantedRect.height / c) * i, 30, 20);
175-
// EditorGUI.DropShadowLabel(labelRect, "ch " + (i + 1));
176-
// }
177-
// }
178-
179-
180-
// PreviewGUI.EndScrollView();
181-
// }
182-
183-
// // force update GUI
184-
// if (playing)
185-
// GUIView.current.Repaint();
186-
// }
187-
188117
public override void OnPreviewSettings() {
189118
if (GUILayout.Button(EditorGUIUtility.IconContent("PlayButton"), EditorStyles.toolbarButton)) {
190119
if (!_previewer.isPlaying) {
@@ -193,28 +122,6 @@ public override void OnPreviewSettings() {
193122
StopPreview();
194123
}
195124
};
196-
if (GUILayout.Button(EditorGUIUtility.IconContent("Animation.NextKey"), EditorStyles.toolbarButton)) PreviewNextClip();
125+
if (GUILayout.Button(EditorGUIUtility.IconContent("Animation.NextKey"), EditorStyles.toolbarButton)) GetNextClip();
197126
}
198-
199-
//TODO
200-
// public override string GetInfoString() {
201-
// AudioClip clip = _targetClip;
202-
// int c = _targetClip.channels;
203-
// string ch = c == 1 ? "Mono" : c == 2 ? "Stereo" : (c - 1) + ".1";
204-
// // AudioCompressionFormat platformFormat = AudioUtil.GetTargetPlatformSoundCompressionFormat(clip);
205-
// // AudioCompressionFormat editorFormat = AudioUtil.GetSoundCompressionFormat(clip);
206-
// string s = "";
207-
// // if (platformFormat != editorFormat)
208-
// // s += " (" + editorFormat + " in editor" + ")";
209-
// s += ", " + _targetClip.frequency + " Hz, " + ch + ", ";
210-
211-
// System.TimeSpan ts = new System.TimeSpan(0, 0, 0, 0, (int)_targetClip.length);
212-
213-
// if ((uint)_targetClip.length == 0xffffffff)
214-
// s += "Unlimited";
215-
// else
216-
// s += String.Format("{0:00}:{1:00}.{2:000}", ts.Minutes, ts.Seconds, ts.Milliseconds);
217-
218-
// return s;
219-
// }
220127
}

Runtime/Sound.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class Sound : ScriptableObject {
2727
/// </summary>
2828
[SerializeField] private AudioClip[] clips;
2929
private int clipIndex = 0;
30+
public int ClipIndex => clipIndex;
3031

3132
/// <summary>
3233
/// Specifies how clips should be retrieved from this Sound
@@ -90,7 +91,7 @@ private void UnloadClips() {
9091
/// Get the next AudioClip for this sound
9192
///</summary>
9293
public AudioClip GetClip() {
93-
if (clips.Length == 0) return null;
94+
if (clips == null || clips.Length == 0) return null;
9495

9596
switch (clipType) {
9697
case ClipType.ordered:
@@ -116,6 +117,12 @@ public void SetClipIndex(int index) {
116117
clipIndex = index;
117118
}
118119

120+
public void ManualIncrementIndex() {
121+
if (clipType != ClipType.manual) return;
122+
123+
clipIndex = (clipIndex + 1) % clips.Length;
124+
}
125+
119126
#endregion //ManageClips
120127

121128
#region AudioCtrl

0 commit comments

Comments
 (0)