Search Unity

Circular Buffers

Discussion in 'Scripting' started by Nevulus, Jan 12, 2014.

  1. Nevulus

    Nevulus

    Joined:
    Dec 7, 2012
    Posts:
    29
    Recently I've implemented a Circular buffer, or circular array, in my script. I was wondering if my implementation using queue would run more efficiently than just extending the List class as described in this article: http://www.blackwasp.co.uk/CircularBuffer.aspx

    Below is my code:
    Code (csharp):
    1. public class CircularBuffer<T>
    2.   {
    3.     Queue<T> _queue;
    4.     int _size;
    5.  
    6.     public CircularBuffer(int size)
    7.     {
    8.       _queue = new Queue<T>(size);
    9.       _size = size;
    10.     }
    11.  
    12.     public void Add(T obj)
    13.     {
    14.       if (_queue.Count == _size)
    15.       {
    16.         _queue.Dequeue();
    17.         _queue.Enqueue(obj);
    18.       }
    19.       else
    20.         _queue.Enqueue(obj);
    21.     }
    22.     public T Read()
    23.     {
    24.       return _queue.Dequeue();
    25.     }
    26.  
    27.     public T Peek()
    28.     {
    29.       return _queue.Peek();
    30.     }
    31.   }
    I am going to test both methods myself now, but figured I'd ask on the forums first to see if anyone has already encountered this circumstance before.
     
  2. Nevulus

    Nevulus

    Joined:
    Dec 7, 2012
    Posts:
    29
    looks like my method is about 43% more efficient in unity 3d . I ran 100 tests and avg'd the ms of each method. the tests included loading up the array, peeking, and releasing all the data up to 1 million ints.

    quick n dirty.
     
  3. TheVarsek

    TheVarsek

    Joined:
    Feb 5, 2017
    Posts:
    1
    Hey Nevulus thanks so much for this. I was wondering if you could take a look at my code and tell me if I'm using it correctly. I'm kinda new to C#, and I'm not sure if it makes sense:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Audio; // required for dealing with audiomixers
    5.  
    6.  
    7. [RequireComponent (typeof(AudioSource))]
    8.  
    9.  
    10. public class MicrophoneOn : MonoBehaviour {
    11.  
    12.     void Start (){
    13.  
    14.         AudioSource audio = GetComponent<AudioSource>();
    15.         audio.clip = Microphone.Start("Built-in Microphone", true, 100, 44100);
    16.         audio.loop = true;
    17.  
    18.         CircularBuffer<float> Buffer = new CircularBuffer<float> (1024);
    19.  
    20.  
    21.         //Getting Sample Data from Microphone
    22.         float[] samples = new float[audio.clip.samples * audio.clip.channels];
    23.        
    24.         audio.clip.GetData(samples, 0);
    25.    
    26.         int i = 0;
    27.         float sample = 0;
    28.         float Sample = 0;
    29.  
    30.         //Setting each value in the array and adding them to the Circular Buffer
    31.         //Reading the next Sample from the Circular Buffer
    32.         while (i < samples.Length) {
    33.            
    34.             samples [i] = sample;
    35.             Buffer.Add (sample);
    36.             Sample = Buffer.Peek();
    37.             ++i;
    38.         }
    39.  
    40.         float newsample = 0;
    41.  
    42.         //Deriving the new sample value
    43.         foreach (float _sample in samples) {
    44.  
    45.             newsample = (sample * Sample);
    46.        
    47.         }
    48.  
    49.         //Putting the new sample values in different array
    50.         float[] newsamples = new float[audio.clip.samples * audio.clip.channels];
    51.  
    52.         for (int r=0; r < samples.Length; ++r)
    53.         {
    54.             newsamples [r] = newsample;
    55.         }
    56.  
    57.         //Setting new sample value to be read by DAC
    58.         audio.clip.SetData(newsamples, 0);
    59.         audio.Play();
    60.     }
    61.        
    62. }
     
    ROBYER1 likes this.