프로젝트

일반

사용자정보

통계
| 개정판:

t1 / TFDContents / Assets / KinectDemos / VariousDemos / Scripts / KinectAudioTracker.cs @ 3

이력 | 보기 | 이력해설 | 다운로드 (4.64 KB)

1 3 KTH
#if !(UNITY_WSA_10_0 && NETFX_CORE)
2
using UnityEngine;
3
using System;
4
using System.Collections.Generic;
5
using System.Linq;
6
using System.Text;
7
using Microsoft.Kinect;
8
using Windows.Kinect;
9
10
using KinectAudioSource = Windows.Kinect.AudioSource;
11
12
13
public class KinectAudioTracker : MonoBehaviour
14
{
15
	[Tooltip("GUI-Text to display status messages.")]
16
	public GUIText statusText;
17
18
	[Tooltip("Last observed audio beam angle in radians, in the range [-pi/2, +pi/2]")]
19
	[NonSerialized]
20
    public float beamAngleRad = 0;
21
22
	[Tooltip("Last observed audio beam angle in degrees, in the range [-180, +180]")]
23
	public float beamAngleDeg = 0;
24
25
	[Tooltip("Last observed audio beam angle confidence, in the range [0, 1]")]
26
	public float beamAngleConfidence = 0;
27
28
	private KinectAudioSource audioSource ;
29
	private AudioBeamFrameReader audioReader = null;
30
31
32
	void Start()
33
	{
34
		Kinect2Interface sensorInterface = KinectManager.Instance.GetSensorData().sensorInterface as Kinect2Interface;
35
		Windows.Kinect.KinectSensor kinectSensor = sensorInterface != null ? sensorInterface.kinectSensor : null;
36
37
		if (kinectSensor != null)
38
		{
39
			this.audioSource = kinectSensor.AudioSource;
40
			this.audioReader = audioSource.OpenReader();
41
42
			Debug.Log("AudioSource IsActive: " + audioSource.IsActive);
43
44
			if (audioReader != null)
45
			{
46
				Debug.Log("KinectAudio successfully initialized.");
47
			}
48
			else
49
			{
50
				Debug.Log("KinectAudio initialization failed.");
51
			}
52
		}
53
	}
54
55
	void Update()
56
	{
57
		if (audioReader != null)
58
		{
59
			ProcessAudioFrame();
60
		}
61
	}
62
63
	void OnDestroy()
64
	{
65
		if (audioReader != null)
66
		{
67
			this.audioReader.Dispose();
68
			this.audioReader = null;
69
70
			//Debug.Log("KinectAudio destroyed.");
71
		}
72
	}
73
74
75
	private void ProcessAudioFrame()
76
    {
77
		IList<AudioBeamFrame> frameList = audioReader.AcquireLatestBeamFrames();
78
		//AudioBeamFrameList frameList = (AudioBeamFrameList)reader.AcquireLatestBeamFrames();
79
80
        if (frameList != null)
81
        {
82
            if (frameList[0] != null)
83
            {
84
                if (frameList[0].SubFrames != null && frameList[0].SubFrames.Count > 0)
85
                {
86
                    // Only one audio beam is supported. Get the sub frame list for this beam
87
                    List<AudioBeamSubFrame> subFrameList = frameList[0].SubFrames.ToList();
88
89
                    // Loop over all sub frames, extract audio buffer and beam information
90
                    foreach (AudioBeamSubFrame subFrame in subFrameList)
91
                    {
92
                        // Check if beam angle and/or confidence have changed
93
                        bool updateBeam = false;
94
95
                        if (subFrame.BeamAngle != this.beamAngleRad)
96
                        {
97
                            this.beamAngleRad = subFrame.BeamAngle;
98
							this.beamAngleDeg = this.beamAngleRad * 180.0f / Mathf.PI;
99
                            updateBeam = true;
100
101
							//Debug.Log("beam angle: " + beamAngleDegrees);
102
                        }
103
104
                        if (subFrame.BeamAngleConfidence != this.beamAngleConfidence)
105
                        {
106
                            this.beamAngleConfidence = subFrame.BeamAngleConfidence;
107
                            updateBeam = true;
108
109
							//Debug.Log("beam angle confidence: " + beamAngleRadians);
110
                        }
111
112
                        if (updateBeam)
113
                        {
114
                            // Refresh display of audio beam
115
							if (statusText)
116
							{
117
								statusText.text = string.Format("Audio beam angle: {0:F0} deg., Confidence: {1:F0}%", beamAngleDeg, beamAngleConfidence * 100f);
118
							}
119
                        }
120
                    }
121
                }
122
//                else
123
//                {
124
//                    this.beamAngle = frameList[0].AudioBeam.BeamAngle;
125
//                    Debug.Log("No SubFrame: "+ frameList[0].AudioBeam.BeamAngle);
126
//                }
127
            }
128
//            else
129
//            {
130
//                Debug.Log("Empty Audio Frame: "+ audioSource.AudioBeams.Count());
131
//                if (audioSource.AudioBeams.Count() > 0)
132
//                    Debug.Log(audioSource.AudioBeams[0].BeamAngle);
133
//
134
//            }
135
        }
136
//        else
137
//        {
138
//            Debug.Log("Empty Audio Frame");
139
//        }
140
141
		// clean up
142
		for(int i = frameList.Count - 1; i >= 0; i--)
143
		{
144
			AudioBeamFrame frame = frameList[i];
145
146
			if(frame != null)
147
			{
148
				frame.Dispose();
149
			}
150
		}
151
152
		//frameList.Clear();
153
    }
154
155
    private void Reader_FrameArrived(object sender, AudioBeamFrameArrivedEventArgs e)
156
    {
157
        ProcessAudioFrame();
158
    }
159
160
}
161
#endif