public static void UpdateBalls(Vector3 center, ILPoints balls, ILOutArray <float> velocity, bool addBalls) { using (ILScope.Enter()) { ILArray <float> position = balls.Positions.Storage; ILArray <float> colors = balls.Colors.Storage; if (addBalls) { // increase number of balls (this is very inefficient!) position[full, r(end + 1, end + 10)] = tosingle(randn(3, 10)); colors[full, r(end + 1, end + 10)] = tosingle(rand(4, 10)); velocity[full, r(end + 1, end + 10)] = tosingle(randn(3, 10)); } ILArray <float> d = array <float>(center.X, center.Y, center.Z); ILArray <float> off = position * 0.1f; ILArray <float> dist = sqrt(sum(position * position)); ILArray <int> where = find(dist < 0.2f); velocity[full, where] = velocity[full, where] + tosingle(rand(3, where.Length)) * 0.2f; dist.a = position - d; off.a = off + dist * -0.02f / sqrt(sum(dist * dist)); velocity.a = velocity * 0.95f - off; balls.Positions.Update(position + velocity * 0.12f); ILArray <float> Zs = abs(position[end, full]); colors[end, full] = Zs / maxall(Zs) * 0.7f + 0.3f; balls.Colors.Update(colors); } }
/// <summary> /// computes normalized magnitudes out of raw samples /// </summary> /// <param name="buffer">sample buffer from naudio</param> /// <param name="buffLen">number of samples</param> /// <param name="fftLen">number of samples for fft </param> /// <param name="MaxValue">[output] index of maximum magnitude value</param> /// <returns>normalized magnitudes (for Y axis)</returns> public static ILRetArray <float> GetMagnitudes(byte[] buffer, int buffLen, int fftLen, ILOutArray <int> MaxValue) { using (ILScope.Enter()) { // how many samples returned from naudio? int newSampleLen = Math.Min(buffLen / 2, fftLen); // create a temporary array for the samples ILArray <float> tmp = zeros <float>(fftLen, 1); // transfer byte[] buffer to temp array for (int s = 0; s < newSampleLen; s++) { tmp.SetValue((short)(buffer[s * 2 + 1] << 8 | buffer[s * 2]), s); } // transform into frequency domain, we use a simple cosine window here ILArray <float> cosWin = sin(pif * counter <float>(0f, 1f, tmp.Length, 1) / (tmp.Length - 1)); //ILArray<float> hamm = (0.54f - 0.46f * cos(2f * pif * counter<float>(0f,1f,ret.Length,1)/ (ret.Length - 1))); // compute the magnitudes, keep relevant part only tmp.a = abs(fft(tmp * cosWin)[r(0, end / 2 + 1)]); // some poor mans high pass filter if (tmp.Length > 20) { tmp["0:20"] = tmp[20]; } // compute max values ILArray <int> maxTmpId = 0; // -> we do want the indices ILArray <float> maxTmp = sort(tmp, Indices: maxTmpId, descending: true); // assign to output parameter MaxValue.a = maxTmpId["0:4"]; // return magnitudes Y values return(tmp.T); } }
public static void UpdateBalls(Vector3 center, ILPoints balls, ILOutArray<float> velocity, bool addBalls) { if (!balls.IsDisposed) { // <- this obviously is not threadsafe!! TODO using (ILScope.Enter()) { ILArray<float> position = balls.Positions.Storage; ILArray<float> colors = balls.Colors.Storage; if (addBalls) { // increase number of balls (this is very inefficient!) position[full, r(end + 1, end + 10)] = tosingle(randn(3, 10)); colors[full, r(end + 1, end + 10)] = tosingle(rand(4, 10)); velocity[full, r(end + 1, end + 10)] = tosingle(randn(3, 10)); } ILArray<float> d = array<float>(center.X, center.Y, center.Z); ILArray<float> off = position * 0.1f; ILArray<float> dist = sqrt(sum(position * position)); ILArray<int> where = find(dist < 0.2f); velocity[full, where] = velocity[full, where] + tosingle(rand(3, where.Length)) * 0.2f; dist.a = position - d; off.a = off + dist * -0.02f / sqrt(sum(dist * dist)); velocity.a = velocity * 0.95f - off; balls.Positions.Update(position + velocity * 0.12f); ILArray<float> Zs = abs(position[end, full]); colors[end, full] = Zs / maxall(Zs) * 0.7f + 0.3f; balls.Colors.Update(colors); } } }
/// <summary> /// computes normalized magnitudes out of raw samples /// </summary> /// <param name="buffer">sample buffer from naudio</param> /// <param name="buffLen">number of samples</param> /// <param name="fftLen">number of samples for fft </param> /// <param name="MaxValue">[output] index of maximum magnitude value</param> /// <returns>normalized magnitudes (for Y axis)</returns> public static ILRetArray<float> GetMagnitudes(byte[] buffer, int buffLen, int fftLen, ILOutArray<int> MaxValue) { using (ILScope.Enter()) { // how many samples returned from naudio? int newSampleLen = Math.Min(buffLen / 2, fftLen); // create a temporary array for the samples ILArray<float> tmp = zeros<float>(fftLen, 1); // transfer byte[] buffer to temp array for (int s = 0; s < newSampleLen; s++) { tmp.SetValue((short)(buffer[s * 2 + 1] << 8 | buffer[s * 2]), s); } // transform into frequency domain, we use a simple cosine window here ILArray<float> cosWin = sin(pif * counter<float>(0f, 1f, tmp.Length, 1) / (tmp.Length - 1)); //ILArray<float> hamm = (0.54f - 0.46f * cos(2f * pif * counter<float>(0f,1f,ret.Length,1)/ (ret.Length - 1))); // compute the magnitudes, keep relevant part only tmp.a = abs(fft(tmp * cosWin)[r(0, end / 2 + 1)]); // some poor mans high pass filter if (tmp.Length > 20) tmp["0:20"] = tmp[20]; // compute max values ILArray<int> maxTmpId = 0; // -> we do want the indices ILArray<float> maxTmp = sort(tmp, Indices: maxTmpId, descending: true); // assign to output parameter MaxValue.a = maxTmpId["0:4"]; // return magnitudes Y values return tmp.T; } }