-
Notifications
You must be signed in to change notification settings - Fork 5
/
AudioClipTrimmer.cs
127 lines (102 loc) · 3.64 KB
/
AudioClipTrimmer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
using System.Reflection;
using System;
#endif
#if UNITY_EDITOR
public class AudioClipTrimmer : EditorWindow {
float Left = 0;
float Right = 1;
[MenuItem ("Window/Audio Clip Trimmer")]
public static void ShowWindow () {
EditorWindow.GetWindow(typeof(AudioClipTrimmer));
}
AudioClip GetAudioClip()
{
var Clips = UnityEditor.Selection.GetFiltered<AudioClip>( SelectionMode.Assets );
return Clips[0];
}
// from http://answers.unity3d.com/questions/993241/how-to-play-specific-part-of-the-audio.html
public static AudioClip MakeSubclip(AudioClip clip, float start, float stop)
{
/* Create a new audio clip */
int frequency = clip.frequency;
float timeLength = stop - start;
int samplesLength = (int)(frequency * timeLength);
var NewName = clip.name + " " + start.ToString("0.00") + "..." + stop.ToString("0.00");
AudioClip newClip = AudioClip.Create(NewName, samplesLength, clip.channels, frequency, false);
// Create a temporary buffer for the samples
float[] data = new float[samplesLength];
// Get the data from the original clip
if ( !clip.GetData(data, (int)(frequency* start)) )
throw new System.Exception("Error getting clip data");
// Transfer the data to the new clip
if ( !newClip.SetData(data, 0) )
throw new System.Exception("Error setting clip data");
return newClip;
}
public static void PlayClip(AudioClip clip, float startTime, bool loop)
{
//int startSample = (int)(startTime * clip.frequency);
Assembly assembly = typeof(AudioImporter).Assembly;
Type audioUtilType = assembly.GetType("UnityEditor.AudioUtil");
//Type[] typeParams = { typeof(AudioClip), typeof(int), typeof(bool) };
//object[] objParams = { clip, startSample, loop };
//MethodInfo method = audioUtilType.GetMethod("PlayClip", typeParams);
Type[] typeParams = { typeof(AudioClip) };
object[] objParams = { clip };
MethodInfo method = audioUtilType.GetMethod("PlayClip", typeParams);
/*var Result = */method.Invoke(null, BindingFlags.Static | BindingFlags.Public, null, objParams, null);
}
void ShowInspector(AudioClip Clip)
{
var Preview = AssetPreview.GetAssetPreview(Clip);
Left = GUILayout.HorizontalSlider( Left, 0, 1, GUILayout.ExpandWidth(true) );
Left = Mathf.Min( Left, Right );
Right = GUILayout.HorizontalSlider( Right, 0, 1, GUILayout.ExpandWidth(true) );
Right = Mathf.Max( Left, Right );
if ( GUILayout.Button("Preview", GUILayout.ExpandWidth(false) ) )
{
var SubClip = MakeSubclip( Clip, Left*Clip.length, Right * Clip.length );
PlayClip( SubClip, 0, false );
}
if ( GUILayout.Button("Save As...", GUILayout.ExpandWidth(false) ) )
{
var SubClip = MakeSubclip( Clip, Left*Clip.length, Right * Clip.length );
AssetWriter.SaveAsset( SubClip );
}
{
GUIStyle style = new GUIStyle();
style.fixedHeight = 100.0f;
var Options = new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true) };
var GroupRect = GUILayoutUtility.GetRect(new GUIContent(),style, Options );
GUI.BeginGroup( GroupRect );
GroupRect.x = 0;
GroupRect.y = 0;
EditorGUILayout.BeginVertical ();
GUI.DrawTextureWithTexCoords( GroupRect, Preview, new Rect( Left, 0, Right-Left, 1 ) );
EditorGUILayout.EndVertical ();
GUI.EndGroup();
}
}
void ShowHelpInspector(string Message)
{
EditorGUILayout.HelpBox("Select an audio clip",MessageType.Info);
}
public void OnGUI()
{
try
{
var Clip = GetAudioClip();
ShowInspector(Clip);
}
catch (System.Exception e)
{
ShowHelpInspector(e.Message);
}
}
}
#endif