summaryrefslogtreecommitdiff
path: root/libs/audiographer/audiographer/utils.h
blob: 2f9c51918b3781e3b522443fd554dd2d6b0df14d (plain)
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
#ifndef AUDIOGRAPHER_UTILS_H
#define AUDIOGRAPHER_UTILS_H

#include "types.h"
#include "exception.h"

#include <cstring>

namespace AudioGrapher
{

class Utils
{
  public:
	
	static void free_resources();
	
	/// Initialize zero buffer, if buffer is != 0, it will be used as the zero buffer
	template <typename T>
	static void init_zeros (nframes_t frames, T const * buffer = 0)
	{
		if (frames == 0) {
			throw Exception (Utils(), "init_zeros must be called with an argument greater than zero.");
		}
		unsigned long n_zeros = frames * sizeof (T);
		if (n_zeros <= num_zeros) { return; }
		delete [] zeros;
		if (buffer) {
			zeros = reinterpret_cast<char const *>(buffer);
		} else {
			zeros = new char[n_zeros];
			memset (const_cast<char *>(zeros), 0, n_zeros);
		}
		num_zeros = n_zeros;
	}
	  
	template <typename T>
	static T const * get_zeros (nframes_t frames)
	{
		if (frames * sizeof (T) > num_zeros) {
			throw Exception (Utils(), "init_zeros has not been called with a large enough frame count");
		}
		return reinterpret_cast<T const *> (zeros);
	}
	
	template <typename T>
	static nframes_t get_zero_buffer_size ()
	{
		return num_zeros / sizeof (T);
	}

  private:
	static char const *  zeros;
	static unsigned long num_zeros;
};

} // namespace

#endif // AUDIOGRAPHER_ROUTINES_H