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