cyclic_buffer.h 2.51 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
/*****************************************************************************
 * cyclic_buffer.h cyclic buffer helper class for vlc's audio visualizations
 *****************************************************************************
 * Copyright © 2012 Vovoid Media Technologies
 *
 * Authors: Jonatan "jaw" Wallmander
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#ifndef CYCLIC_BUFFER_H
#define CYCLIC_BUFFER_H


class block_holder
{
public:
    float data[512]; // data holder
    mtime_t pts; // machine time when this is to be played
    block_holder()
    {
        pts = 0; // max_int 64-bit
    }
};

#define CYCLIC_BUFFER_SIZE 128
class cyclic_block_queue
{
    block_holder cycl_buffer[CYCLIC_BUFFER_SIZE];
    size_t consumer_pos;
    size_t insertion_pos;
public:
    cyclic_block_queue()
    {
        consumer_pos = 0;
        insertion_pos = 0;
    }

    block_holder* consume()
    {
        mtime_t cur_machine_time = mdate();
        size_t steps = 0;
        while (
               (cycl_buffer[consumer_pos].pts < cur_machine_time)
               &&
               (steps++ < CYCLIC_BUFFER_SIZE)
              )
        {
            consumer_pos++;
            if (consumer_pos == CYCLIC_BUFFER_SIZE)
            {
                consumer_pos = 0;
            }
        }
        return &cycl_buffer[consumer_pos];
    }

  block_holder* get_insertion_object()
  {
      insertion_pos++;
      if ( insertion_pos == CYCLIC_BUFFER_SIZE )
      {
          insertion_pos = 0;
      }
      return &cycl_buffer[insertion_pos];
  }

  void reset()
  {
      for (size_t i = 0; i < CYCLIC_BUFFER_SIZE; i++)
      {
          cycl_buffer[i].pts = 0;
          consumer_pos = 0;
          insertion_pos = 0;
      }
  }
};
#undef CYCLIC_BUFFER_SIZE

#endif // CYCLIC_BUFFER_H