]> git.sesse.net Git - vlc/blob - modules/visualization/cyclic_buffer.h
Upnp: remove trailing space
[vlc] / modules / visualization / cyclic_buffer.h
1 /*****************************************************************************
2  * cyclic_buffer.h cyclic buffer helper class for vlc's audio visualizations
3  *****************************************************************************
4  * Copyright © 2012 Vovoid Media Technologies
5  *
6  * Authors: Jonatan "jaw" Wallmander
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 #ifndef CYCLIC_BUFFER_H
24 #define CYCLIC_BUFFER_H
25
26
27 class block_holder
28 {
29 public:
30     float data[512]; // data holder
31     mtime_t pts; // machine time when this is to be played
32     block_holder()
33     {
34         pts = 0; // max_int 64-bit
35     }
36 };
37
38 #define CYCLIC_BUFFER_SIZE 128
39 class cyclic_block_queue
40 {
41     block_holder cycl_buffer[CYCLIC_BUFFER_SIZE];
42     size_t consumer_pos;
43     size_t insertion_pos;
44 public:
45     cyclic_block_queue()
46     {
47         consumer_pos = 0;
48         insertion_pos = 0;
49     }
50
51     block_holder* consume()
52     {
53         mtime_t cur_machine_time = mdate();
54         size_t steps = 0;
55         while (
56                (cycl_buffer[consumer_pos].pts < cur_machine_time)
57                &&
58                (steps++ < CYCLIC_BUFFER_SIZE)
59               )
60         {
61             consumer_pos++;
62             if (consumer_pos == CYCLIC_BUFFER_SIZE)
63             {
64                 consumer_pos = 0;
65             }
66         }
67         return &cycl_buffer[consumer_pos];
68     }
69
70   block_holder* get_insertion_object()
71   {
72       insertion_pos++;
73       if ( insertion_pos == CYCLIC_BUFFER_SIZE )
74       {
75           insertion_pos = 0;
76       }
77       return &cycl_buffer[insertion_pos];
78   }
79
80   void reset()
81   {
82       for (size_t i = 0; i < CYCLIC_BUFFER_SIZE; i++)
83       {
84           cycl_buffer[i].pts = 0;
85           consumer_pos = 0;
86           insertion_pos = 0;
87       }
88   }
89 };
90 #undef CYCLIC_BUFFER_SIZE
91
92 #endif // CYCLIC_BUFFER_H