xref: /haiku/src/apps/cortex/addons/common/AudioBuffer.h (revision 220d04022750f40f8bac8f01fa551211e28d04f2)
1 /*
2  * Copyright (c) 1999-2000, Eric Moon.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions, and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions, and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
27  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 
32 // AudioBuffer.h
33 // eamoon@meadgroup.com
34 // 31mar99
35 //
36 // Represents a buffer of audio data.  Derived from RawBuffer;
37 // adds simple audio-specific operations.
38 
39 #ifndef __AudioBuffer_H__
40 #define __AudioBuffer_H__
41 
42 #include "RawBuffer.h"
43 #include <MediaDefs.h>
44 
45 class AudioBuffer :
46 	public		RawBuffer {
47 	typedef	RawBuffer _inherited;
48 
49 public:						// constants
50 	static const media_raw_audio_format 		defaultFormat;
51 
52 public:						// ctor/dtor/accessors
53 	virtual ~AudioBuffer();
54 
55 	// initialize empty buffer
56 	AudioBuffer(
57 		const media_raw_audio_format& format=defaultFormat,
58 		rtm_pool* pFromPool=0);
59 
60 	// allocate buffer of given format & size
61 	AudioBuffer(
62 		const media_raw_audio_format& format,
63 		uint32 frames,
64 		bool bCircular=true,
65 		rtm_pool* pFromPool=0);
66 
67 	// point to given buffer
68 	AudioBuffer(
69 		const media_raw_audio_format& format,
70 		void* pData,
71 		uint32 frames,
72 		bool bCircular=true,
73 		rtm_pool* pFromPool=0);
74 
75 	// +++++ add a flag to disallow adopt()
76 	AudioBuffer(
77 		const media_raw_audio_format& format,
78 		class BBuffer* pBuffer,
79 		bool bCircular=true);
80 
81 	// generate a reference (point) to the target's buffer
82 	AudioBuffer(const AudioBuffer& clone);
83 	AudioBuffer& operator=(const AudioBuffer& clone);
84 
85 	void setFormat(const media_raw_audio_format& format);
86 	const media_raw_audio_format& format() const;
87 
88 	// extra adoption support
89 	void adopt(
90 		const media_raw_audio_format& format,
91 		void* pData,
92 		uint32 frames,
93 		bool bCircular=true,
94 		rtm_pool* pFromPool=0);
95 
96 	// as with RawBuffer::adopt(), returns false if the target
97 	// doesn't own its buffer, but references it anyway
98 	bool adopt(
99 		AudioBuffer& target);
100 
101 public:						// operations
102 
103 	// test for format equivalence against target buffer
104 	// (ie. determine whether any conversion would be necessary
105 	//  for copy/mix operations)
106 
107 	bool formatSameAs(
108 		const AudioBuffer& target) const;
109 
110 	// copy to target audio buffer, applying any necessary
111 	// format conversions.  behaves like rawCopyTo().
112 
113 	uint32 copyTo(
114 		AudioBuffer& target,
115 		uint32* pioFromFrame,
116 		uint32* pioTargetFrame,
117 		uint32 frames) const; //nyi
118 
119 	// as above; copies all frames
120 
121 	uint32 copyTo(
122 		AudioBuffer& target,
123 		uint32* pioFromFrame,
124 		uint32* pioTargetFrame) const;
125 
126 	// mix to target audio buffer, applying any necessary
127 	// format conversions.  behaves like rawCopyTo().
128 
129 	uint32 mixTo(
130 		AudioBuffer& target,
131 		uint32* pioFromFrame,
132 		uint32* pioTargetFrame,
133 		uint32 frames,
134 		float fGain=1.0) const; //nyi
135 
136 	// calculate minimum & maximum peak levels
137 	// (converted/scaled to given type if necessary)
138 	// pMin and pMax must point to arrays with enough room
139 	// for one value per channel.  existing array values aren't
140 	// cleared first.
141 	//
142 	// (if pMin isn't provided, the maximum _absolute_ levels will
143 	// be written to pMax)
144 	//
145 	// if fromFrame and frameCount are given, scans that range.
146 	// if pAt is given, it's expected to point to an array with
147 	// room for one frame value per channel: this stores the
148 	// frame indices at which the peak levels were found, or
149 	// ULONG_MAX if no peak was found for the given channel.
150 
151 	void findMin(float* pMin, uint32* pAt=0) const;
152 	uint32 findMin(uint32 fromFrame, uint32 frameCount,
153 		float* pMin, uint32* pAt=0) const;
154 
155 	void findMax(float* pMax, uint32* pAt=0) const;
156 	uint32 findMax(uint32 fromFrame, uint32 frameCount,
157 		float* pMax, uint32* pAt=0) const;
158 
159 	void findPeaks(float* pPeaks, uint32* pAt=0) const;
160 	uint32 findPeaks(uint32 fromFrame, uint32 frameCount,
161 		float* pPeaks, uint32* pAt=0) const;
162 
163 	// find average level
164 	// (converted/scaled as necessary)
165 	// pAverage must point to an array with enough room
166 	// for one value per channel.
167 
168 	void average(float* pAverage) const; //nyi
169 	uint32 average(uint32 fromFrame, uint32 frameCount,
170 		float* pAverage) const; //nyi
171 
172 protected:					// members
173 	media_raw_audio_format			m_format;
174 };
175 
176 #endif /* __AudioBuffer_H__ */
177