comparison Orchestland/Assets/LeapMotion/Scripts/Utils/LeapRecorder.cs @ 1:f7675884f2a1

Add Orchestland project
author Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
date Fri, 17 Jul 2015 23:09:20 +0900
parents
children
comparison
equal deleted inserted replaced
0:347d21cdfc22 1:f7675884f2a1
1 /******************************************************************************\
2 * Copyright (C) Leap Motion, Inc. 2011-2014. *
3 * Leap Motion proprietary. Licensed under Apache 2.0 *
4 * Available at http://www.apache.org/licenses/LICENSE-2.0.html *
5 \******************************************************************************/
6
7 using UnityEngine;
8 using System;
9 using System.IO;
10 using System.Collections.Generic;
11 using Leap;
12
13 /** The states of the record-playback machine. */
14 public enum RecorderState {
15 Idling = 0,
16 Recording = 1,
17 Playing = 2
18 }
19
20 /**
21 * Maintains a buffer of recorded frames and tracks the state of playback and recording.
22 */
23 public class LeapRecorder {
24
25 /** Playback speed. */
26 public float speed = 1.0f;
27 /** Whether to lop back to the beginning when the last recorded frame is played.*/
28 public bool loop = true;
29 /** The current play state. */
30 public RecorderState state = RecorderState.Playing;
31
32 protected List<byte[]> frames_;
33 protected float frame_index_;
34 protected Frame current_frame_ = new Frame();
35
36 /** Creates a new LeapRecorder object. This doesn't make sense outside the context of a HandController object. */
37 public LeapRecorder() {
38 Reset();
39 }
40
41 /** Sets the play state to idle. */
42 public void Stop() {
43 state = RecorderState.Idling;
44 frame_index_ = 0.0f;
45 }
46
47 /** Sets the play state to idle. */
48 public void Pause() {
49 state = RecorderState.Idling;
50 }
51
52 /** Sets the play state to playing. */
53 public void Play() {
54 state = RecorderState.Playing;
55 }
56
57 /** Sets the play state to recording. */
58 public void Record() {
59 state = RecorderState.Recording;
60 }
61
62 /** Discards any recorded frames. */
63 public void Reset() {
64 frames_ = new List<byte[]>();
65 frame_index_ = 0;
66 }
67
68 /** Restores the default behaviors. */
69 public void SetDefault() {
70 speed = 1.0f;
71 loop = true;
72 }
73
74 /** Returns the ratio of the current playback position to the total recording length. */
75 public float GetProgress() {
76 return frame_index_ / frames_.Count;
77 }
78
79 /** Returns the playback position. */
80 public int GetIndex() {
81 return (int)frame_index_;
82 }
83
84 /**
85 * Sets the playback position to the specified frame count (or the last frame if the
86 * specified index is after the last frame.
87 */
88 public void SetIndex(int new_index) {
89 if (new_index >= frames_.Count) {
90 frame_index_ = frames_.Count - 1;
91 }
92 else {
93 frame_index_ = new_index;
94 }
95 }
96
97 /** Serializes a Leap Frame object and adds it to the end of the recording. */
98 public void AddFrame(Frame frame) {
99 frames_.Add(frame.Serialize);
100 }
101
102 /** Returns the current frame without advancing the playhead. This frame could be invalid. */
103 public Frame GetCurrentFrame() {
104 return current_frame_;
105 }
106
107 /** Advances the playhead, deserializes the frame, and returns it.*/
108 public Frame NextFrame() {
109 current_frame_ = new Frame();
110 if (frames_.Count > 0) {
111 if (frame_index_ >= frames_.Count && loop) {
112 frame_index_ -= frames_.Count;
113 }
114 else if (frame_index_ < 0 && loop) {
115 frame_index_ += frames_.Count;
116 }
117 if (frame_index_ < frames_.Count && frame_index_ >= 0) {
118 current_frame_.Deserialize(frames_[(int)frame_index_]);
119 frame_index_ += speed;
120 }
121 }
122 return current_frame_;
123 }
124
125 /** Deserializes all the recorded frames and returns them in a new list. */
126 public List<Frame> GetFrames() {
127 List<Frame> frames = new List<Frame>();
128 for (int i = 0; i < frames_.Count; ++i) {
129 Frame frame = new Frame();
130 frame.Deserialize(frames_[i]);
131 frames.Add(frame);
132 }
133 return frames;
134 }
135
136 /** The number of recorded frames. */
137 public int GetFramesCount() {
138 return frames_.Count;
139 }
140
141 /** Saves the recorded frames to a file, overwriting an existing file. */
142 public string SaveToNewFile() {
143 string path = Application.persistentDataPath + "/Recording_" +
144 System.DateTime.Now.ToString("yyyyMMdd_hhmmss") + ".bytes";
145
146 if (File.Exists(@path)) {
147 File.Delete(@path);
148 }
149
150 FileStream stream = new FileStream(path, FileMode.Append, FileAccess.Write);
151 for (int i = 0; i < frames_.Count; ++i) {
152 byte[] frame_size = new byte[4];
153 frame_size = System.BitConverter.GetBytes(frames_[i].Length);
154 stream.Write(frame_size, 0, frame_size.Length);
155 stream.Write(frames_[i], 0, frames_[i].Length);
156 }
157
158 stream.Close();
159 return path;
160 }
161
162 /** Loads saved frames from a file. */
163 public void Load(TextAsset text_asset) {
164 byte[] data = text_asset.bytes;
165 frame_index_ = 0;
166 frames_.Clear();
167 int i = 0;
168 while (i < data.Length) {
169 byte[] frame_size = new byte[4];
170 Array.Copy(data, i, frame_size, 0, frame_size.Length);
171 i += frame_size.Length;
172 byte[] frame = new byte[System.BitConverter.ToUInt32(frame_size, 0)];
173 Array.Copy(data, i, frame, 0, frame.Length);
174 i += frame.Length;
175 frames_.Add(frame);
176 }
177 }
178 }