赞
踩
该音频剪辑工具允许用户在Unity编辑器中加载音频片段,并通过图形化界面进行剪辑、预览和保存操作。以下是该工具的主要功能:
[MenuItem("Tools/音频剪辑工具")]
public static void ShowWindow()
{
GetWindow<AudioClipEditor>("音频编辑器");
}
private void OnEnable()
{
audioSource = EditorUtility.CreateGameObjectWithHideFlags("AudioSource" , HideFlags.HideAndDontSave , typeof(AudioSource)).GetComponent<AudioSource>();
}
private void OnGUI() { EditorGUILayout.BeginVertical(); audioClip = EditorGUILayout.ObjectField("声音片段" , audioClip , typeof(AudioClip) , false) as AudioClip; if (audioClipTemp != audioClip) { audioClipTemp = audioClip; Init(); } if (audioClip) { DisplayAudioClipInfo(); AdjustClipSection(); DisplayVolumeControl(); GUILayout.Space(10); if (GUILayout.Button("听" , GUILayout.Height(40))) { PlayTestAudio(); } GUILayout.Space(10); if (GUILayout.Button("保存片段" , GUILayout.Height(40))) { ClipAndSave(); } } EditorGUILayout.EndVertical(); }
private void Init() { if (audioClip != null) { endTime = audioClip.length; samples = new float[audioClip.samples * audioClip.channels]; audioClip.GetData(samples , 0); UpdateWaveformTexture(); } } private void DisplayAudioClipInfo() { EditorGUILayout.LabelField($"开始时间: {startTime:F2}s"); EditorGUILayout.LabelField($"结束时间: {endTime:F2}s"); GUILayout.Space(10); }
private void AdjustClipSection() { GUILayout.Label("调整剪辑片段"); Rect waveformRect = GUILayoutUtility.GetRect(position.width , 64); if (waveformTexture == null || waveformTexture.width != (int)waveformRect.width) { UpdateWaveformTexture(); } if (waveformTexture != null) { EditorGUI.DrawPreviewTexture(waveformRect , waveformTexture); } EditorGUILayout.MinMaxSlider("" , ref startTime , ref endTime , 0f , audioClip != null ? audioClip.length : 10f); } private void PlayTestAudio() { if (audioClip != null) { AudioClip clippedAudioClip = ClipAudio(audioClip , startTime , endTime); audioSource.clip = clippedAudioClip; audioSource.volume = volume; audioSource.Play(); } else { Debug.LogError("请指定一段音频"); } }
private void ClipAndSave() { if (audioClip != null) { AudioClip clippedAudioClip = ClipAudio(audioClip , startTime , endTime); string savePath = EditorUtility.SaveFilePanel("保存剪辑的音频" , "Assets" , audioClipTemp.name + "_New" , "wav"); if (!string.IsNullOrEmpty(savePath)) { SaveAudioClipToFile(clippedAudioClip , savePath); } } else { Debug.LogError("请指定一段音频"); } }
private void UpdateWaveformTexture() { if (samples != null) { waveformTexture = GenerateWaveformTexture(samples , audioClip.channels , (int)position.width , 64); } } private Texture2D GenerateWaveformTexture(float[] samples , int channels , int width , int height) { Texture2D texture = new Texture2D(width , height , TextureFormat.RGBA32 , false); Color[] colors = new Color[width * height]; for (int i = 0; i < colors.Length; i++) { colors[i] = Color.black; } texture.SetPixels(colors); int packSize = ( samples.Length / channels ) / width; for (int x = 0; x < width; x++) { float max = 0; for (int i = 0; i < packSize; i++) { float sampleValue = samples[( x * packSize + i ) * channels] * volume; if (sampleValue > max) max = sampleValue; } int heightValue = (int)( max * height ); for (int y = 0; y < heightValue; y++) { texture.SetPixel(x , y , Color.yellow); } } texture.Apply(); return texture; }
private AudioClip ClipAudio(AudioClip originalClip , float start , float end) { float[] data = new float[originalClip.samples * originalClip.channels]; originalClip.GetData(data , 0); int newSampleCount = (int)( ( end - start ) * originalClip.frequency ); float[] newData = new float[newSampleCount * originalClip.channels]; int startIndex = (int)( start * originalClip.frequency * originalClip.channels ); for (int i = 0; i < newSampleCount * originalClip.channels; i++) { newData[i] = data[startIndex + i] * volume; } AudioClip clippedAudioClip = AudioClip.Create("ClippedAudio" , newSampleCount , originalClip.channels , originalClip.frequency , false); clippedAudioClip.SetData(newData , 0); return clippedAudioClip; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。