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