Jamoma API  0.6.0.a19
AUBuffer.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 "AUBuffer.h"
42 #include <stdlib.h>
43 
44 AUBufferList::~AUBufferList()
45 {
46  Deallocate();
47  if (mPtrs)
48  free(mPtrs);
49 }
50 
51 void AUBufferList::Allocate(const CAStreamBasicDescription &format, UInt32 nFrames)
52 {
53  UInt32 nStreams;
54  UInt32 channelsPerStream;
55  if (format.IsInterleaved()) {
56  nStreams = 1;
57  channelsPerStream = format.mChannelsPerFrame;
58  } else {
59  nStreams = format.mChannelsPerFrame;
60  channelsPerStream = 1;
61  }
62 
63  // careful -- the I/O thread could be running!
64  if (nStreams > mAllocatedStreams) {
65  mPtrs = (AudioBufferList *)CA_realloc(mPtrs, offsetof(AudioBufferList, mBuffers) + nStreams * sizeof(AudioBuffer));
66  mAllocatedStreams = nStreams;
67  }
68  UInt32 bytesPerStream = (nFrames * format.mBytesPerFrame + 0xF) & ~0xF;
69  UInt32 nBytes = nStreams * bytesPerStream;
70  if (nBytes > mAllocatedBytes) {
71  if (mExternalMemory) {
72  mExternalMemory = false;
73  mMemory = NULL;
74  }
75  mMemory = (Byte *)CA_realloc(mMemory, nBytes);
76  mAllocatedBytes = nBytes;
77  }
78  mAllocatedFrames = nFrames;
79  mPtrState = kPtrsInvalid;
80 }
81 
82 void AUBufferList::Deallocate()
83 {
84  mAllocatedStreams = 0;
85  mAllocatedFrames = 0;
86  mAllocatedBytes = 0;
87 // this causes a world of hurt if someone upstream disconnects during I/O (SysSoundGraph)
88 /* if (mPtrs) {
89  printf("deallocating bufferlist %08X\n", int(mPtrs));
90  free(mPtrs);
91  mPtrs = NULL;
92  } */
93  if (mMemory) {
94  if (mExternalMemory)
95  mExternalMemory = false;
96  else
97  free(mMemory);
98  mMemory = NULL;
99  }
100  mPtrState = kPtrsInvalid;
101 }
102 
103 AudioBufferList & AUBufferList::PrepareBuffer(const CAStreamBasicDescription &format, UInt32 nFrames)
104 {
105  if (nFrames > mAllocatedFrames)
106  COMPONENT_THROW(kAudioUnitErr_TooManyFramesToProcess);
107 
108  UInt32 nStreams;
109  UInt32 channelsPerStream;
110  if (format.IsInterleaved()) {
111  nStreams = 1;
112  channelsPerStream = format.mChannelsPerFrame;
113  } else {
114  nStreams = format.mChannelsPerFrame;
115  channelsPerStream = 1;
116  if (nStreams > mAllocatedStreams)
117  COMPONENT_THROW(kAudioUnitErr_FormatNotSupported);
118  }
119 
120  AudioBufferList *abl = mPtrs;
121  abl->mNumberBuffers = nStreams;
122  AudioBuffer *buf = abl->mBuffers;
123  Byte *mem = mMemory;
124  UInt32 streamInterval = (mAllocatedFrames * format.mBytesPerFrame + 0xF) & ~0xF;
125  UInt32 bytesPerBuffer = nFrames * format.mBytesPerFrame;
126  for ( ; nStreams--; ++buf) {
127  buf->mNumberChannels = channelsPerStream;
128  buf->mData = mem;
129  buf->mDataByteSize = bytesPerBuffer;
130  mem += streamInterval;
131  }
132  if (UInt32(mem - mMemory) > mAllocatedBytes)
133  COMPONENT_THROW(kAudioUnitErr_TooManyFramesToProcess);
134  mPtrState = kPtrsToMyMemory;
135  return *mPtrs;
136 }
137 
138 AudioBufferList & AUBufferList::PrepareNullBuffer(const CAStreamBasicDescription &format, UInt32 nFrames)
139 {
140  UInt32 nStreams;
141  UInt32 channelsPerStream;
142  if (format.IsInterleaved()) {
143  nStreams = 1;
144  channelsPerStream = format.mChannelsPerFrame;
145  } else {
146  nStreams = format.mChannelsPerFrame;
147  channelsPerStream = 1;
148  if (nStreams > mAllocatedStreams)
149  COMPONENT_THROW(kAudioUnitErr_FormatNotSupported);
150  }
151  AudioBufferList *abl = mPtrs;
152  abl->mNumberBuffers = nStreams;
153  AudioBuffer *buf = abl->mBuffers;
154  UInt32 bytesPerBuffer = nFrames * format.mBytesPerFrame;
155  for ( ; nStreams--; ++buf) {
156  buf->mNumberChannels = channelsPerStream;
157  buf->mData = NULL;
158  buf->mDataByteSize = bytesPerBuffer;
159  }
160  mPtrState = kPtrsToExternalMemory;
161  return *mPtrs;
162 }
163 
164 // this should NOT be called while I/O is in process
165 void AUBufferList::UseExternalBuffer(const CAStreamBasicDescription &format, const AudioUnitExternalBuffer &buf)
166 {
167  UInt32 alignedSize = buf.size & ~0xF;
168  if (mMemory != NULL && alignedSize >= mAllocatedBytes) {
169  // don't accept the buffer if we already have one and it's big enough
170  // if we don't already have one, we don't need one
171  Byte *oldMemory = mMemory;
172  mMemory = buf.buffer;
173  mAllocatedBytes = alignedSize;
174  // from Allocate(): nBytes = nStreams * nFrames * format.mBytesPerFrame;
175  // thus: nFrames = nBytes / (nStreams * format.mBytesPerFrame)
176  mAllocatedFrames = mAllocatedBytes / (format.NumberChannelStreams() * format.mBytesPerFrame);
177  mExternalMemory = true;
178  free(oldMemory);
179  }
180 }
181 
182 #if DEBUG
183 void AUBufferList::PrintBuffer(const char *label, int subscript, const AudioBufferList &abl, UInt32 nFrames, bool asFloats)
184 {
185  printf(" %s [%d] 0x%08lX:\n", label, subscript, long(&abl));
186  const AudioBuffer *buf = abl.mBuffers;
187  for (UInt32 i = 0; i < abl.mNumberBuffers; ++buf, ++i) {
188  printf(" [%2d] %5dbytes %dch @ %p: ", (int)i, (int)buf->mDataByteSize, (int)buf->mNumberChannels, buf->mData);
189  if (buf->mData != NULL) {
190  UInt32 nSamples = nFrames * buf->mNumberChannels;
191  for (UInt32 j = 0; j < nSamples; ++j) {
192  if (nSamples > 16 && (j % 16) == 0)
193  printf("\n\t");
194  if (asFloats)
195  printf(" %6.3f", ((float *)buf->mData)[j]);
196  else
197  printf(" %08X", (unsigned)((UInt32 *)buf->mData)[j]);
198  }
199  }
200  printf("\n");
201  }
202 }
203 #endif