finalize PROGRAM_NAME change for ardour3
[ardour.git] / libs / audiographer / audiographer / utils.h
1 #ifndef AUDIOGRAPHER_UTILS_H
2 #define AUDIOGRAPHER_UTILS_H
3
4 #include "types.h"
5 #include "exception.h"
6
7 #include <cstring>
8
9 namespace AudioGrapher
10 {
11
12 class Utils
13 {
14   public:
15         
16         static void free_resources();
17         
18         /// Initialize zero buffer, if buffer is != 0, it will be used as the zero buffer
19         template <typename T>
20         static void init_zeros (nframes_t frames, T const * buffer = 0)
21         {
22                 if (frames == 0) {
23                         throw Exception (Utils(), "init_zeros must be called with an argument greater than zero.");
24                 }
25                 unsigned long n_zeros = frames * sizeof (T);
26                 if (n_zeros <= num_zeros) { return; }
27                 delete [] zeros;
28                 if (buffer) {
29                         zeros = reinterpret_cast<char const *>(buffer);
30                 } else {
31                         zeros = new char[n_zeros];
32                         memset (const_cast<char *>(zeros), 0, n_zeros);
33                 }
34                 num_zeros = n_zeros;
35         }
36           
37         template <typename T>
38         static T const * get_zeros (nframes_t frames)
39         {
40                 if (frames * sizeof (T) > num_zeros) {
41                         throw Exception (Utils(), "init_zeros has not been called with a large enough frame count");
42                 }
43                 return reinterpret_cast<T const *> (zeros);
44         }
45         
46         template <typename T>
47         static nframes_t get_zero_buffer_size ()
48         {
49                 return num_zeros / sizeof (T);
50         }
51
52   private:
53         static char const *  zeros;
54         static unsigned long num_zeros;
55 };
56
57 } // namespace
58
59 #endif // AUDIOGRAPHER_ROUTINES_H