-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDrawTexture.cs
More file actions
69 lines (54 loc) · 1.51 KB
/
DrawTexture.cs
File metadata and controls
69 lines (54 loc) · 1.51 KB
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
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
[RequireComponent (typeof(AudioSource))]
//[RequireComponent (typeof(GUITexture))]
public class DrawTexture : MonoBehaviour
{
public int width = 500;
// texture width
public int height = 100;
// texture height
public Color backgroundColor = Color.black;
public Color32 waveformColor = Color.white;
public int size = 2048;
// size of sound segment displayed in texture
private Color[] blank;
// blank image array
private Texture2D texture;
private float[] samples;
// audio samples array
public RawImage rawImage;
IEnumerator Start ()
{
// create the samples array
samples = new float[size];
// create the texture and assign to the guiTexture:
texture = new Texture2D (width, height);
rawImage.GetComponent<RawImage> ().texture = texture;
// create a 'blank screen' image
blank = new Color[width * height];
for (int i = 0; i < blank.Length; i++) {
blank [i] = backgroundColor;
}
// refresh the display each 100mS
while (true) {
GetCurWave ();
yield return new WaitForSeconds (0.1f);
}
}
void GetCurWave ()
{
// clear the texture
texture.SetPixels (blank, 0);
// get samples from channel 0 (left)
GetComponent<AudioSource> ().GetOutputData (samples, 0);
// draw the waveform
for (int i = 0; i < size; i++) {
texture.SetPixel ((int)(width * i / size),
(int)(height * (samples [i] + .1f) / .2f),
waveformColor);
} // upload to the graphics card
texture.Apply ();
}
}