Example #1
0
        public void Next()
        {
            It("should return a promise for an IteratorResult object", () => {
                EcmaValue g = new GeneratorFunction(EmptyGenerator);
                IEnumerable <EcmaValue> g1_()
                {
                    yield return(Yield.Many(g.Call()));
                }
                EcmaValue g1 = new AsyncGeneratorFunction(g1_);
                EcmaValue p  = g1.Call().Invoke("next");
                p.Invoke("then", Intercept(result => {
                    VerifyIteratorResult(result, true);
                }));
                That(p, Is.InstanceOf(Global.Promise));
                VerifyPromiseSettled();
            });

            It("should reject promise if getter `done` abrupt completes", () => {
                EcmaValue thrownError = Error.Construct();
                EcmaValue obj         = CreateObject((Symbol.Iterator, FunctionLiteral(() => {
                    return(CreateObject(new {
                        next = FunctionLiteral(() => {
                            return CreateObject(
                                ("done", get: () => Keywords.Throw(thrownError), set: null),
                                ("value", get: () => 1, set: null)
                                );
                        })
                    }));
                })));
        /// <summary>
        /// Метод, подготавливающий массив сэмплов.
        /// </summary>
        /// <param name="generator">Функция-генератор.</param>
        /// <param name="startFrequency">Частота, на которой генерируется этот массив сэмплов.</param>
        /// <param name="endFrequency">Максимальная частота, на которой планируется генерировать звук.</param>
        /// <returns>Массив сэмплов.</returns>
        private static float[] PrepareSamples(GeneratorFunction generator, double freq, double maxFreq)
        {
            var length = (int)DefaultSampleRate;
            var result = new float[length];

            for (int i = 0; i < result.Length; ++i)
            {
                result[i] = (float)generator((double)i / result.Length, freq, maxFreq);
            }

            return(result);
        }
 /// <summary>
 /// Инициализирует новый объект типа WaveTableOscillator с заданными параметрами.
 /// </summary>
 /// <param name="generator">Функция-генератор.</param>
 /// <param name="startFrequency">Минимальная частота, на которой планируется генерировать звук.</param>
 /// <param name="endFrequency">Максимальная частота, на которой планируется генерировать звук.</param>
 public WaveTableOscillator(
     GeneratorFunction generator,
     double startFrequency,
     double endFrequency)
 {
     waveTablesAmount = (int)(Math.Floor(Math.Log(endFrequency / startFrequency, 2)) + 1);
     waveTables       = new WaveTable[waveTablesAmount];
     for (int i = 0; i < waveTables.Length; ++i)
     {
         var frequency = startFrequency * (1 << i);
         var samples   = PrepareSamples(generator, frequency, DefaultSampleRate / 2);
         waveTables[i] = new WaveTable
         {
             Length         = samples.Length,
             PhaseIncrement = (float)(frequency / DefaultSampleRate),
             Samples        = samples,
         };
     }
     Normalize();
 }
Example #4
0
 public static unsafe void Generate(this AudioFrame frame, uint channels, Int64 frameOffset, GeneratorFunction generator)
 {
     using (var buffer = frame.LockBuffer(AudioBufferAccessMode.ReadWrite))
     {
         using (var bufferReference = buffer.CreateReference())
         {
             byte *pByteData;
             uint  capacity;
             ((IMemoryBufferByteAccess)bufferReference).GetBuffer(out pByteData, out capacity);
             uint   bufferLength = capacity / sizeof(float) / channels;
             float *pSamples     = (float *)pByteData;
             int    sampleIndex  = 0;
             for (Int64 frameIndex = frameOffset; frameIndex < frameOffset + bufferLength; frameIndex++)
             {
                 for (uint channelIndex = 0; channelIndex < channels; channelIndex++, sampleIndex++)
                 {
                     pSamples[sampleIndex] = generator(frameIndex, channelIndex);
                 }
             }
         }
     }
 }
Example #5
0
 /**
  * Creates the cache and sets the pointer to a generator function.
  *
  * @param fn a pointer to a generator function or static class method
  */
 public Cache(GeneratorFunction fn)
 {
     generator = new GeneratorFunction(fn);
 }
Example #6
0
        void CreateNewStamp()
        {
            while (stamp.transform.childCount > 0)
            {
                DestroyImmediate(stamp.transform.GetChild(0).gameObject);
            }

            GeneratorFunction generate = functions[(int)ip.algorithm];

            stamp = generate(ip, stamp);

            var count = Mathf.Min(1000, (Mathf.PI * Mathf.Pow(ip.brushRadius, 2)) / (1f / ip.roomDensity));

            for (var i = 0; i < 0 /*count*/; i++)
            {
                var child = new GameObject("Dummy");
                child.transform.parent = stamp.transform;
                var p = Random.insideUnitCircle;
                child.transform.localPosition = new Vector3(p.x, 0, p.y) * ip.brushRadius;
                var eulerAngles = Vector3.zero;
                // set random rotation between 0 and 360
                eulerAngles.y = Random.value * 360f;
                // round rotation to 90 degree angles
                eulerAngles.y = Mathf.Round(eulerAngles.y / 90f) * 90f;
                child.transform.localEulerAngles = eulerAngles;
                GameObject dummy = PrefabUtility.InstantiatePrefab(ip.SelectedPrefab) as GameObject;
                foreach (var c in dummy.GetComponentsInChildren <Collider>())
                {
                    c.enabled = false;
                }
                dummy.transform.parent        = child.transform;
                dummy.transform.localPosition = Vector3.zero;
                dummy.transform.localRotation = Quaternion.identity;
            }

            var toDestroy = new HashSet <GameObject>();

            /*for (var i = 0; i < stamp.transform.childCount; i++)
             * {
             *  var child = stamp.transform.GetChild(i);
             *  if (toDestroy.Contains(child.gameObject)) continue;
             *
             *
             *  var bounds = child.gameObject.GetRendererBounds();
             *  var childVolume = bounds.size.x * bounds.size.y * bounds.size.z;
             *  for (var x = 0; x < stamp.transform.childCount; x++)
             *  {
             *      var check = stamp.transform.GetChild(x);
             *      if (check.gameObject == child.gameObject) continue;
             *      if (toDestroy.Contains(check.gameObject)) continue;
             *      var b = check.gameObject.GetRendererBounds();
             *      if (b.Intersects(bounds))
             *      {
             *          var overlapVolume = b.size.x * b.size.y * b.size.z;
             *          var intersection = Intersection(b, bounds);
             *          var intersectionVolume = intersection.size.x * intersection.size.y * intersection.size.z;
             *          // Handles.DrawWireCube(intersection.center, intersection.size);
             *          var maxIntersection = Mathf.Max(intersectionVolume / overlapVolume, intersectionVolume / childVolume);
             *          // Handles.Label(intersection.center, maxIntersection.ToString());
             *          if (maxIntersection > ip.maxIntersectionVolume)
             *          {
             *              toDestroy.Add(child.gameObject);
             *              break;
             *          }
             *      }
             *  }
             * }*/
            foreach (var i in toDestroy)
            {
                DestroyImmediate(i);
            }
        }