xref: /haiku/src/apps/cortex/addons/common/AudioBuffer.h (revision d3d8b26997fac34a84981e6d2b649521de2cc45a)
1 // AudioBuffer.h
2 // eamoon@meadgroup.com
3 // 31mar99
4 //
5 // Represents a buffer of audio data.  Derived from RawBuffer;
6 // adds simple audio-specific operations.
7 
8 #ifndef __AudioBuffer_H__
9 #define __AudioBuffer_H__
10 
11 #include "RawBuffer.h"
12 #include <MediaDefs.h>
13 
14 class AudioBuffer :
15 	public		RawBuffer {
16 	typedef	RawBuffer _inherited;
17 
18 public:						// constants
19 	static const media_raw_audio_format 		defaultFormat;
20 
21 public:						// ctor/dtor/accessors
22 	virtual ~AudioBuffer();
23 
24 	// initialize empty buffer
25 	AudioBuffer(
26 		const media_raw_audio_format& format=defaultFormat,
27 		rtm_pool* pFromPool=0);
28 
29 	// allocate buffer of given format & size
30 	AudioBuffer(
31 		const media_raw_audio_format& format,
32 		uint32 frames,
33 		bool bCircular=true,
34 		rtm_pool* pFromPool=0);
35 
36 	// point to given buffer
37 	AudioBuffer(
38 		const media_raw_audio_format& format,
39 		void* pData,
40 		uint32 frames,
41 		bool bCircular=true,
42 		rtm_pool* pFromPool=0);
43 
44 	// +++++ add a flag to disallow adopt()
45 	AudioBuffer(
46 		const media_raw_audio_format& format,
47 		class BBuffer* pBuffer,
48 		bool bCircular=true);
49 
50 	// generate a reference (point) to the target's buffer
51 	AudioBuffer(const AudioBuffer& clone);
52 	AudioBuffer& operator=(const AudioBuffer& clone);
53 
54 	void setFormat(const media_raw_audio_format& format);
55 	const media_raw_audio_format& format() const;
56 
57 	// extra adoption support
58 	void adopt(
59 		const media_raw_audio_format& format,
60 		void* pData,
61 		uint32 frames,
62 		bool bCircular=true,
63 		rtm_pool* pFromPool=0);
64 
65 	// as with RawBuffer::adopt(), returns false if the target
66 	// doesn't own its buffer, but references it anyway
67 	bool adopt(
68 		AudioBuffer& target);
69 
70 public:						// operations
71 
72 	// test for format equivalence against target buffer
73 	// (ie. determine whether any conversion would be necessary
74 	//  for copy/mix operations)
75 
76 	bool formatSameAs(
77 		const AudioBuffer& target) const;
78 
79 	// copy to target audio buffer, applying any necessary
80 	// format conversions.  behaves like rawCopyTo().
81 
82 	uint32 copyTo(
83 		AudioBuffer& target,
84 		uint32* pioFromFrame,
85 		uint32* pioTargetFrame,
86 		uint32 frames) const; //nyi
87 
88 	// as above; copies all frames
89 
90 	uint32 copyTo(
91 		AudioBuffer& target,
92 		uint32* pioFromFrame,
93 		uint32* pioTargetFrame) const;
94 
95 	// mix to target audio buffer, applying any necessary
96 	// format conversions.  behaves like rawCopyTo().
97 
98 	uint32 mixTo(
99 		AudioBuffer& target,
100 		uint32* pioFromFrame,
101 		uint32* pioTargetFrame,
102 		uint32 frames,
103 		float fGain=1.0) const; //nyi
104 
105 	// calculate minimum & maximum peak levels
106 	// (converted/scaled to given type if necessary)
107 	// pMin and pMax must point to arrays with enough room
108 	// for one value per channel.  existing array values aren't
109 	// cleared first.
110 	//
111 	// (if pMin isn't provided, the maximum _absolute_ levels will
112 	// be written to pMax)
113 	//
114 	// if fromFrame and frameCount are given, scans that range.
115 	// if pAt is given, it's expected to point to an array with
116 	// room for one frame value per channel: this stores the
117 	// frame indices at which the peak levels were found, or
118 	// ULONG_MAX if no peak was found for the given channel.
119 
120 	void findMin(float* pMin, uint32* pAt=0) const;
121 	uint32 findMin(uint32 fromFrame, uint32 frameCount,
122 		float* pMin, uint32* pAt=0) const;
123 
124 	void findMax(float* pMax, uint32* pAt=0) const;
125 	uint32 findMax(uint32 fromFrame, uint32 frameCount,
126 		float* pMax, uint32* pAt=0) const;
127 
128 	void findPeaks(float* pPeaks, uint32* pAt=0) const;
129 	uint32 findPeaks(uint32 fromFrame, uint32 frameCount,
130 		float* pPeaks, uint32* pAt=0) const;
131 
132 	// find average level
133 	// (converted/scaled as necessary)
134 	// pAverage must point to an array with enough room
135 	// for one value per channel.
136 
137 	void average(float* pAverage) const; //nyi
138 	uint32 average(uint32 fromFrame, uint32 frameCount,
139 		float* pAverage) const; //nyi
140 
141 protected:					// members
142 	media_raw_audio_format			m_format;
143 };
144 
145 #endif /* __AudioBuffer_H__ */