merge with master.
[ardour.git] / libs / appleutility / AUOutputBL.cpp
1 /*      Copyright:      � Copyright 2005 Apple Computer, Inc. All rights reserved.
2
3         Disclaimer:     IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
4                         ("Apple") in consideration of your agreement to the following terms, and your
5                         use, installation, modification or redistribution of this Apple software
6                         constitutes acceptance of these terms.  If you do not agree with these terms,
7                         please do not use, install, modify or redistribute this Apple software.
8
9                         In consideration of your agreement to abide by the following terms, and subject
10                         to these terms, Apple grants you a personal, non-exclusive license, under Apple�s
11                         copyrights in this original Apple software (the "Apple Software"), to use,
12                         reproduce, modify and redistribute the Apple Software, with or without
13                         modifications, in source and/or binary forms; provided that if you redistribute
14                         the Apple Software in its entirety and without modifications, you must retain
15                         this notice and the following text and disclaimers in all such redistributions of
16                         the Apple Software.  Neither the name, trademarks, service marks or logos of
17                         Apple Computer, Inc. may be used to endorse or promote products derived from the
18                         Apple Software without specific prior written permission from Apple.  Except as
19                         expressly stated in this notice, no other rights or licenses, express or implied,
20                         are granted by Apple herein, including but not limited to any patent rights that
21                         may be infringed by your derivative works or by other works in which the Apple
22                         Software may be incorporated.
23
24                         The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
25                         WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
26                         WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27                         PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
28                         COMBINATION WITH YOUR PRODUCTS.
29
30                         IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
31                         CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
32                         GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33                         ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
34                         OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
35                         (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
36                         ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38 /*=============================================================================
39         AUOutputBL.h
40         
41 =============================================================================*/
42 #include "AUOutputBL.h"
43
44 /*
45 struct AudioBufferList
46 {
47         UInt32          mNumberBuffers;
48         AudioBuffer     mBuffers[1];
49 };
50 struct AudioBuffer
51 {
52         UInt32  mNumberChannels;        //      number of interleaved channels in the buffer
53         UInt32  mDataByteSize;          //      the size of the buffer pointed to by mData
54         void*   mData;                          //      the pointer to the buffer
55 };
56 */
57
58 AUOutputBL::AUOutputBL (const CAStreamBasicDescription &inDesc, UInt32 inDefaultNumFrames) 
59                 : mFormat (inDesc),
60                   mBufferMemory(NULL),
61                   mBufferList (NULL),
62                   mNumberBuffers (0), // keep this here, so can ensure integrity of ABL
63                   mBufferSize (0),
64                   mFrames(inDefaultNumFrames)
65 {
66         mNumberBuffers = mFormat.IsInterleaved() ? 1 : mFormat.NumberChannels();
67         mBufferList = reinterpret_cast<AudioBufferList*>(new Byte[sizeof(UInt32) + (mNumberBuffers * sizeof(AudioBuffer))]);
68 }
69
70 AUOutputBL::~AUOutputBL()
71 {
72         if (mBufferMemory)
73                 delete[] mBufferMemory;
74
75         if (mBufferList)
76                 delete [] mBufferList;
77 }
78
79 void    AUOutputBL::Prepare (UInt32 inNumFrames, bool inWantNullBufferIfAllocated) 
80 {
81         UInt32 channelsPerBuffer = mFormat.IsInterleaved() ? mFormat.NumberChannels() : 1;
82         
83         if (mBufferMemory == NULL || inWantNullBufferIfAllocated)
84         {
85                 mBufferList->mNumberBuffers = mNumberBuffers;
86                 AudioBuffer *buf = &mBufferList->mBuffers[0];
87                 for (UInt32 i = 0; i < mNumberBuffers; ++i, ++buf) {
88                         buf->mNumberChannels = channelsPerBuffer;
89                         buf->mDataByteSize = mFormat.FramesToBytes (inNumFrames);
90                         buf->mData = NULL;
91                 }
92         }
93         else
94         {
95                 UInt32 nBytes = mFormat.FramesToBytes (inNumFrames);
96                 if ((nBytes * mNumberBuffers) > AllocatedBytes())
97                         throw OSStatus(-10874);//(kAudioUnitErr_TooManyFramesToProcess);
98                         
99                 mBufferList->mNumberBuffers = mNumberBuffers;
100                 AudioBuffer *buf = &mBufferList->mBuffers[0];
101                 Byte* p = mBufferMemory;
102                 for (UInt32 i = 0; i < mNumberBuffers; ++i, ++buf) {
103                         buf->mNumberChannels = channelsPerBuffer;
104                         buf->mDataByteSize = nBytes;
105                         buf->mData = p;
106                         p += mBufferSize;
107                 }
108         }
109 }
110
111
112 void    AUOutputBL::Allocate (UInt32 inNumFrames)
113 {
114         if (inNumFrames) 
115         {
116                 UInt32 nBytes = mFormat.FramesToBytes (inNumFrames);
117                 
118                 if (nBytes <= AllocatedBytes()) 
119                         return;
120                 
121                         // align successive buffers for Altivec and to take alternating
122                         // cache line hits by spacing them by odd multiples of 16
123                 if (mNumberBuffers > 1)
124                         nBytes = (nBytes + (0x10 - (nBytes & 0xF))) | 0x10;
125                 
126                 mBufferSize = nBytes;
127                 
128                 UInt32 memorySize = mBufferSize * mNumberBuffers;
129                 Byte *newMemory = new Byte[memorySize];
130                 memset(newMemory, 0, memorySize);       // make buffer "hot"
131                 
132                 Byte *oldMemory = mBufferMemory;
133                 mBufferMemory = newMemory;
134                 delete[] oldMemory;
135                 
136                 mFrames = inNumFrames;
137         } 
138         else 
139         {
140                 if (mBufferMemory) {
141                         delete [] mBufferMemory;
142                         mBufferMemory = NULL;
143                 }
144                 mBufferSize = 0;
145                 mFrames = 0;
146         }
147 }
148
149 #if DEBUG
150 void                    AUOutputBL::Print()
151 {
152         printf ("AUOutputBL::Print\n");
153         mFormat.Print();
154         printf ("Num Buffers:%ld, mFrames:%ld, allocatedMemory:%c\n", mBufferList->mNumberBuffers, mFrames, (mBufferMemory != NULL ? 'T' : 'F'));
155         AudioBuffer *buf = &mBufferList->mBuffers[0];
156         for (UInt32 i = 0; i < mBufferList->mNumberBuffers; ++i, ++buf)
157                 printf ("\tBuffer:%ld, Size:%ld, Chans:%ld, Buffer:%X\n", i, buf->mDataByteSize, buf->mNumberChannels, int(buf->mData));
158 }
159 #endif
160