-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWaveFileLooper.cs
80 lines (66 loc) · 2.18 KB
/
WaveFileLooper.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
using WaveFile;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
// http://www.blitter.com/~russtopia/MIDI/~jglatt/tech/wave.htm
public class WaveFileLooper : EditorWindow
{
[MenuItem("Window/WaveFileLooper")]
static void Init()
{
var window = (WaveFileLooper)GetWindow(typeof(WaveFileLooper));
window.Show();
}
string previousPath;
RiffChunk riffChunk;
void OnGUI()
{
var selectedClip = Selection.activeObject as AudioClip;
if (selectedClip == null) return;
var path = AssetDatabase.GetAssetPath(selectedClip);
if (string.IsNullOrEmpty(path)) return;
if (path != previousPath || riffChunk == null)
{
riffChunk = ReadWaveFile(path);
previousPath = path;
}
var samplerChunk = riffChunk.Chunks.OfType<SamplerChunk>().FirstOrDefault();
if (samplerChunk == null)
{
samplerChunk = new SamplerChunk();
riffChunk.Chunks.Add(samplerChunk);
}
var sampleLoop = samplerChunk.SampleLoops.FirstOrDefault();
if (sampleLoop == null)
{
sampleLoop = new SamplerChunk.SampleLoop();
samplerChunk.SampleLoops.Add(sampleLoop);
}
sampleLoop.LoopStart = EditorGUILayout.IntField("loop start (samples)", sampleLoop.LoopStart);
sampleLoop.LoopEnd = EditorGUILayout.IntField("loop end (samples)", sampleLoop.LoopEnd);
if (GUILayout.Button("Save"))
{
using (var fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
using (var writer = new BinaryWriter(fs))
{
riffChunk.Write(writer);
}
AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
}
}
void OnSelectionChange()
{
Repaint();
}
RiffChunk ReadWaveFile(string path)
{
RiffChunk riffChunk;
using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read))
using (var reader = new BinaryReader(fs))
{
riffChunk = (RiffChunk)WaveFileChunkReader.Read(reader);
}
return riffChunk;
}
}