Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1060261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:18:00+00:00 2026-05-16T18:18:00+00:00

I’m implementing an audio track class and I’m in need of a good circular

  • 0

I’m implementing an audio track class and I’m in need of a good circular buffer implementation. I’m using shorts for my audio samples, so I would prefer to use a ShortBuffer class for the actual buffer. This track will need to be thread-safe but I can guarantee that only one thread will read and another will write on the track.
My current implementation looks like this (it doesn’t handle wrapping).

public class Track {
    //sample rate 44100, 2 channels with room for 4 seconds
    private volatile ShortBuffer buffer = ShortBuffer.allocate((44100 * 2) * 4);
    //keep count of the samples in the buffer
    private AtomicInteger count = new AtomicInteger(0);
    private ReentrantLock lock = new ReentrantLock(true);
    private int readPosition = 0;

    public int getSampleCount() {
        int i = count.get();
        return i > 0 ? i / 2 : 0;
    }

    public short[] getSamples(int sampleCount) {
        short[] samples = new short[sampleCount];
        try {
            lock.tryLock(10, TimeUnit.MILLISECONDS);
            int writePosition = buffer.position();
            buffer.position(readPosition);
            buffer.get(samples);
            //set new read position
            readPosition = buffer.position();
            // set back to write position
            buffer.position(writePosition);
            count.addAndGet(-sampleCount);
        } catch (InterruptedException e) {
            System.err.println("Exception getting samples" + e);
        } finally {
            if (lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }
        return samples;
    }

    public void pushSamples(short[] samples) {
        try {
            lock.tryLock(10, TimeUnit.MILLISECONDS);
            buffer.put(samples);
            count.addAndGet(samples.length);
        } catch (InterruptedException e) {
            System.err.println("Exception getting samples" + e);
        } finally {
            if (lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:18:00+00:00Added an answer on May 16, 2026 at 6:18 pm

    Here’s the solution that I have come up with http://pastebin.com/2St01Wzf I decided it was easier to use a head and tail property with a short array, instead of just the read position with a ShortBuffer. I also took an idea from the Java collections classes to detect when the buffer is full. Here is the source, just in case the pastebin disappears:

    public class Track {
    
    private static Logger log = LoggerFactory.getLogger(Track.class);
    
    private final long id = System.nanoTime();
    
    // number of channels
    private int channelCount;
    
    // maximum seconds to buffer
    private int bufferedSeconds = 5;
    
    private AtomicInteger count = new AtomicInteger(0);
    
    private ReentrantLock lock;
    
    private volatile short[] buffer;
    
    private int capacity = 0;
    
    private int head = 0;
    
    private int tail = 0;
    
    public Track(int samplingRate, int channelCount) {
        // set the number of channels
        this.channelCount = channelCount;
        // size the buffer
        capacity = (samplingRate * channelCount) * bufferedSeconds;
        buffer = new short[capacity];
        // use a "fair" lock
        lock = new ReentrantLock(true);
    }
    
    /**
     * Returns the number of samples currently in the buffer.
     * 
     * @return
     */
    public int getSamplesCount() {
        int i = count.get();
        return i > 0 ? i / channelCount : 0;
    }
    
    /**
     * Removes and returns the next sample in the buffer.
     * 
     * @return single sample or null if a buffer underflow occurs
     */
    public Short remove() {
        Short sample = null;
        if (count.get() > 0) {
            // decrement sample counter
            count.addAndGet(-1);
            // reposition the head
            head = (head + 1) % capacity;
            // get the sample at the head
            sample = buffer[head];
        } else {
            log.debug("Buffer underflow");
        }
        return sample;
    }
    
    /**
     * Adds a sample to the buffer.
     * 
     * @param sample
     * @return true if added successfully and false otherwise
     */
    public boolean add(short sample) {
        boolean result = false;
        if ((count.get() + 1) < capacity) {
            // increment sample counter
            count.addAndGet(1);
            // reposition the tail
            tail = (tail + 1) % capacity;
            // add the sample to the tail
            buffer[tail] = sample;
            // added!
            result = true;
        } else {
            log.debug("Buffer overflow");
        }
        return result;
    }
    
    /**
     * Offers the samples for addition to the buffer, if there is enough capacity to 
     * contain them they will be added.
     * 
     * @param samples
     * @return true if the samples can be added and false otherwise
     */
    public boolean offer(short[] samples) {
        boolean result = false;
        if ((count.get() + samples.length) <= capacity) {
            pushSamples(samples);
            result = true;
        }
        return result;
    }
    
    /**
     * Adds an array of samples to the buffer.
     * 
     * @param samples
     */
    public void pushSamples(short[] samples) {
        log.trace("[{}] pushSamples - count: {}", id, samples.length);
        try {
            lock.tryLock(10, TimeUnit.MILLISECONDS);
            for (short sample : samples) {
                log.trace("Position at write: {}", tail);
                if (!add(sample)) {
                    log.warn("Sample could not be added");
                    break;
                }
            }
        } catch (InterruptedException e) {
            log.warn("Exception getting samples", e);
        } finally {
            if (lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }
    }
    
    /**
     * Returns a single from the buffer.
     * 
     * @return
     */
    public Short popSample(int channel) {
        log.trace("[{}] popSample - channel: {}", id, channel);
        Short sample = null;
        if (channel < channelCount) {
            log.trace("Position at read: {}", head);
            try {
                lock.tryLock(10, TimeUnit.MILLISECONDS);
                sample = remove();
            } catch (InterruptedException e) {
                log.warn("Exception getting sample", e);
            } finally {
                if (lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        }
        return sample;
    }
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
In my XML file chapters tag has more chapter tag.i need to display chapters
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.