Questions tagged [circular-buffer]

A circular buffer is a data structure that uses a single, fixed-size buffer as if it were connected end-to-end. This structure lends itself easily to buffering data streams.

A circular buffer (AKA cyclic buffer, ring buffer, or circular queue) is a data structure that uses a single fixed-size buffer as if it were connected end-to-end. This structure lends itself easily to buffering data streams.

The circular buffer uses two pointers to indicate the current beginning and ending of the data on the buffer. The old/"First In" data is deleted from the buffer as it is read, moving forward the beginning pointer. New data is added at the end of the buffer (moving forward the corresponding pointer). The buffer physical storage is the same during all the operation if the reading speed is faster than the data acquisition process.

The useful property of a circular buffer is that it does not need to have its elements shuffled around when one is consumed. (If a non-circular buffer were used then it would be necessary to shift all elements when one is consumed.) In other words, the circular buffer is well suited as a FIFO (first in first out) buffer while a standard, non-circular buffer is well suited as a LIFO (last in first out) buffer.

Circular buffering makes a good implementation strategy for a queue that has fixed maximum size. Should a maximum size be adopted for a queue, then a circular buffer is a completely ideal implementation; all queue operations are constant time. However, expanding a circular buffer requires shifting memory, which is comparatively costly. For arbitrarily expanding queues, a Linked list approach may be preferred instead.

Check http://en.wikipedia.org/wiki/Circular_buffer for more information.

417 questions
0
votes
2 answers

Core Audio: Float32 to SInt16 conversion artefacts

I am converting from the following format: const int four_bytes_per_float = 4; const int eight_bits_per_byte = 8; _stereoGraphStreamFormat.mFormatID = kAudioFormatLinearPCM; _stereoGraphStreamFormat.mFormatFlags =…
Anthony Myatt
  • 326
  • 1
  • 12
0
votes
0 answers

Why does this circular buffer design not require any synchronization?

In Wikipedia: CircularBuffer section "Difficulties->Always keep one slot open", the author does not mention any requirement of synchronization between threads: Always keep one slot open This design always keeps one slot unallocated. A full buffer…
0
votes
1 answer

flush() and isEmpty() methods for a ring buffer without using .size() in Java

I am trying to implement a Ring (circular queue) of Chars in Java and I'm having a difficult time thinking through how to detect if the buffer isEmpty without using the .size() method on the array. Because if the Ring Buffer is full read pointer ==…
Nilnoc
  • 101
  • 1
  • 13
0
votes
1 answer

Recursively calling the functions in lua

I am working on a script. The script includes some functions. Every function has one url packet that query the server. The server returns the data in JSON format. To use this data I have included the JSON library. The problem I am facing is each…
Prakash.DTI
  • 913
  • 2
  • 9
  • 19
0
votes
1 answer

Why corrupt sound?

For this code: while (1) { generate_noise(frames, period_size); snd_pcm_writei(dev, frames, period_size); } it works fine.. But for this: generate_noise(frames, period_size); while (1) { snd_pcm_writei(dev, frames,…
Deep
  • 2,472
  • 2
  • 15
  • 25
0
votes
2 answers

Passing reference to deque delete function

I have been given an assignment and I'm struggling to figure out how I'm supposed to implement it. I've pasted the parts of the assignment that puzzled me below Write a deque class to hold a list of integers which is implemented internally with a…
JTK
  • 1,469
  • 2
  • 22
  • 39
0
votes
2 answers

FAST detector with circular array run so slow. Matlab (edit : remove circshift)

hello im working on FAST method using Matlab and it's run so slow in my notebook. this is the theory of my code FAST Detector. I know my code is ineffective and not good enough especially in the array part and loop. this is my code : EDIT : I remove…
0
votes
1 answer

Why is my ring buffer / circular buffer in java breaking?

my addFront method I believe is the reason everythings breaking. The isEmpty() method is built but i'm not sure if that's a reason it's breaking or not. public class ExpandableArrayBuffer implements ExpandableBuffer { private static final int…
Frightlin
  • 161
  • 1
  • 2
  • 14
0
votes
1 answer

Troubleshooting multiple producers single consumer circular buffer

I have been trying to implement a MPSC circular buffer in C for Linux. Here it is the buffer structure: typedef struct mpsc_buffer_s { sem_t semaphore; unsigned char cache_pad_1[CACHE_LINE - sizeof(sem_t)]; uint64_t write_pos; …
ziu
  • 2,634
  • 2
  • 24
  • 39
0
votes
1 answer

AVCaptureOutput to Audio Unit callback via TPCircularBuffer

I'm building an AUGraph, and trying to get audio from the input device via an AVCaptureAudioDataOutput delegate method. The use of an AVCaptureSession is a consequence of the problem explained here. I succesfully managed to build an audio…
Benoît Lahoz
  • 1,270
  • 1
  • 18
  • 43
0
votes
1 answer

Creating a ring/Circular buffer in matlab/simulink

I'm trying to implement a pitch shifting algorithm in Simulink that uses a ring/circular buffer but I don't know how this is done. Im streaming an audio signal using a microphone and i want store the data in a circular buffer in order to change the…
0
votes
1 answer

Circular buffer in VB.NET

How can I create a circular buffer on the heap in VB.NET ? This would be used for audio playback via P/Invoke to winmm.dll waveoutopen and waveoutwrite to support development of a software synth. I currently use the marshall class to build a regular…
acheo
  • 3,106
  • 2
  • 32
  • 57
0
votes
2 answers

Can a linear search be used to find the lowest number in an cyclic (circular) array

I am writing a program that uses a circular array to hold the int values and was just wondering if it was possible to use a linear search to find the lowest number in the circular array. I have used linear search before on 1d and 2d arrays but this…
0
votes
1 answer

Circular Buffer Implementation for FIR Filter in C

I am programming on an embedded microcontroller (TMS320F28069) a 32-bit floating point MCU. I was going over some of the example projects, and one of them implements a simple FIR filter on ADC sampled data. Block diagram here Let's say the ADC…
SPieiga
  • 3
  • 1
  • 2
0
votes
2 answers

How to write a fairness algorithm in Javascript using circular arrays?

My kid's baseball team has 13 kids. Each kid is supposed to rotate through the positions so that they all get equal time at each position. We have 4 outfielders, by the way, as these kids are only 7 years old. I tried to write a little Javascript…
Mreider
  • 83
  • 1
  • 7