14 public static class iOSUtilities
16 const int PARAMETER_CURVE_MAX_SIZE = 16;
17 const double D_EPSILON = 0.00001;
19 private static bool IsEqual(
double _a,
double _b,
double _epsilon = D_EPSILON)
21 return Mathf.Abs((
float)(_a - _b)) <= _epsilon;
25 private static int GetCurrentRamp(
double _a,
double _b)
43 private static List<double> ProcessBuffer(
double [] _buffer,
double _step)
45 List<double> outBuffer =
new List<double>();
47 double currentTimeStamp = 0.0;
50 for (
int i = 0; i < _buffer.Count(); i++)
52 if (i == _buffer.Count() - 1)
54 outBuffer.Add(currentTimeStamp);
55 outBuffer.Add(_buffer[i]);
59 int currentRamp = GetCurrentRamp(_buffer[i], _buffer[i+1]);
62 if (currentRamp != lastRamp)
64 lastRamp = currentRamp;
65 outBuffer.Add(currentTimeStamp);
66 outBuffer.Add(_buffer[i]);
69 currentTimeStamp += _step;
75 public static string BufferToAHAP(
float[] _buffer,
float[] _frequencies,
float[] _transientTimer,
float[] _transientGain,
float _duration,
float _timestep)
77 double[]
transient =
new double[_transientTimer.Length * 4];
79 for (
int j = 0; j <
transient.Length; j += 4)
81 transient[j] = _transientTimer[j / 4];
82 transient[j + 1] = _transientGain[j / 4];
87 double[] buffer =
new double[_buffer.Length];
88 double[] frequencies =
new double[_frequencies.Length];
90 for (
int j = 0; j < _frequencies.Length; j++)
92 frequencies[j] = _frequencies[j];
94 for (
int j = 0; j < _buffer.Length; j++)
96 buffer[j] = _buffer[j];
98 return BufferToAHAP(buffer, frequencies,
transient, _duration, _timestep);
101 public static string BufferToAHAP(
double[] _buffer,
double[] _frequencies,
double[] _transients,
double _duration,
double _timestep)
111 ahap.Pattern =
new List<PatternObject>();
114 if (_transients !=
null)
116 for (
int i = 0; i + 3 < _transients.Length; i += 4)
121 parameterValue = (float)_transients[i + 1]
126 parameterValue = 1.0f
131 Time = (float)_transients[i],
133 eventParameters =
new EventParameter[2] { intensityTrans, sharpnessTrans }
135 ahap.
Pattern.Add(CurrentTransient);
139 float currentTransStart = CurrentTransient.Time;
140 float currentTransEnd = currentTransStart + 0.022f;
142 int startIndex = (int)(currentTransStart / _timestep);
143 int endIndex = Math.Max((
int)(currentTransEnd / _timestep), startIndex + 1);
145 int end = Math.Min(endIndex, _buffer.Length);
146 for (
int index = startIndex; index < end; index++)
148 _buffer[index] = (float)_transients[i + 1];
156 List<double> ampPattern = ProcessBuffer(_buffer, _timestep);
157 List<double> freqPattern = ProcessBuffer(_frequencies, _timestep);
189 eventDuration = (float)_duration,
196 for (
int j = 0; j < ampPattern.Count(); j += PARAMETER_CURVE_MAX_SIZE * 2)
198 List<ParameterCurveControlPoint> controlPoints =
new List<ParameterCurveControlPoint>();
199 for (
int k = 0; k < Math.Min(ampPattern.Count() - j, PARAMETER_CURVE_MAX_SIZE * 2); k+=2)
203 Time = ampPattern[k + j] - ampPattern[j],
204 ParameterValue = Mathf.Max(0, (
float)ampPattern[k + j + 1])
211 Time = ampPattern[j],
212 parameterCurveControlPoints = controlPoints
214 ahap.Pattern.Add(parameterCurve);
219 for (
int j = 0; j < freqPattern.Count(); j += PARAMETER_CURVE_MAX_SIZE * 2)
221 List<ParameterCurveControlPoint> controlPoints =
new List<ParameterCurveControlPoint>();
222 for (
int k = 0; k < Math.Min(freqPattern.Count() - j, PARAMETER_CURVE_MAX_SIZE * 2); k+=2)
226 Time = freqPattern[k + j] - freqPattern[j],
227 ParameterValue = Mathf.Max(0, (Mathf.Clamp((
float)freqPattern[k + j + 1], 65.0f, 300.0f) - 300.0f) / 235.0f + 1) - 1
234 Time = freqPattern[j],
235 parameterCurveControlPoints = controlPoints
237 ahap.Pattern.Add(parameterCurve);
247 public int Version = 1;
254 "\"Version\": " + Version +
","
257 json +=
"\"Pattern\": [";
258 json +=
string.Join(
",", Pattern.Select(e => e.ToJson()));
266 public string Project =
"";
267 public string Created =
"";
271 return "\"Metadata\": {" +
272 "\"Project\": \"" + Project +
"\"," +
273 "\"Created\": \"" + Created +
"\"" +
289 string eventParametersStr =
"\"EventParameters\": [";
291 if (eventParameters.Length > 0)
293 for (
int i = 0; i < eventParameters.Length; i++)
297 eventParametersStr +=
", ";
299 eventParametersStr +=
"{" +
"" +
300 "\"ParameterID\": \"" + eventParameters[i].parameterID.ToString() +
"\"," +
301 "\"ParameterValue\": " + eventParameters[i].parameterValue +
305 eventParametersStr +=
"]";
309 "\"Time\": " + Time +
"," +
310 "\"EventType\": \"" + eventType.ToString() +
"\"," +
311 "\"EventDuration\": " + eventDuration +
"," +
319 public float Time = 0;
321 public float eventDuration = 0;
333 string pccp =
"\"ParameterCurveControlPoints\": [";
335 if (parameterCurveControlPoints.Count > 0)
337 for (
int i = 0; i < parameterCurveControlPoints.Count; i++)
345 "\"Time\": " + parameterCurveControlPoints[i].Time +
"," +
346 "\"ParameterValue\": " + parameterCurveControlPoints[i].ParameterValue +
353 "\"ParameterCurve\": {" +
354 "\"ParameterID\": \"" + parameterID.ToString() +
"\"," +
355 "\"Time\": " + Time +
"," +
364 public double Time = 0;