this repo has no description
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 CAAudioFileStreamer.cpp
40
41=============================================================================*/
42
43#include "CAAudioFileStreamer.h"
44
45// ____________________________________________________________________________
46
47void CAAudioFileReader::FileReadBuffer::UpdateAfterRead(SInt64 curFrame, UInt32 nFrames)
48{
49 //printf("read %ld PCM packets, file packets %qd-%qd\n", nPackets, b->mStartPacket, b->mEndPacket);
50 mEndFrame = nFrames;
51 mEndOfStream = (nFrames == 0);
52 mBufferStartFileFrame = curFrame;
53}
54
55// ____________________________________________________________________________
56
57void CAAudioFileReader::SetFile(const FSRef &inFile)
58{
59 Stop();
60 CancelAndDisposeBuffers();
61
62 delete mFile; mFile = NULL;
63 mFile = new CAAudioFile;
64 mFile->Open(inFile);
65
66 const CAStreamBasicDescription &fileFmt = mFile->GetFileDataFormat();
67 CAStreamBasicDescription iofmt;
68 iofmt.SetCanonical(fileFmt.mChannelsPerFrame, false); // deinterleaved
69 iofmt.mSampleRate = fileFmt.mSampleRate;
70 mFile->SetClientFormat(iofmt, NULL);
71
72 SetFormat(iofmt);
73}
74
75void CAAudioFileReader::Start()
76{
77 mErrorCount = 0;
78 mEndOfStream = false;
79 Prime();
80 mRunning = true;
81}
82
83void CAAudioFileReader::Stop()
84{
85 mRunning = false;
86}
87
88void CAAudioFileReader::ReadBuffer(FileReadBuffer *b)
89{
90 b->SetEmpty();
91 CABufferList *ioMemory = b->GetBufferList();
92 CABufferList *fileBuffers = GetBufferList();
93 fileBuffers->SetFrom(ioMemory);
94 UInt32 nFrames = GetBufferSizeFrames();
95 SInt64 curFrame = mFile->Tell();
96 mFile->Read(nFrames, &fileBuffers->GetModifiableBufferList());
97 b->UpdateAfterRead(curFrame, nFrames);
98}
99
100double CAAudioFileReader::GetCurrentPosition() const
101{
102 return double(GetCurrentFrame()) / double(GetNumberFrames());
103#if 0
104 double nFrames = double(GetNumberFrames()); // +1 to account for leftovers from decoder
105 if (!mRunning)
106 return double(GetCurrentFrame()) / nFrames;
107
108 if (mEndOfStream) {
109 //printf("EOF\n");
110 return 1.0;
111 }
112
113 const FileReadBuffer *b = static_cast<const FileReadBuffer *>(GetCurrentBuffer());
114 // the buffer from which we're reading
115 UInt32 startFrame, endFrame;
116 b->GetLocation(startFrame, endFrame);
117 //printf("%qd + %ld / %.f\n", b->mBufferStartFileFrame, startFrame, nFrames);
118 return double(b->mBufferStartFileFrame + startFrame) / nFrames;
119 //if (endFrame > 0) {
120 //double frac = (double(startFrame) / double(endFrame)) * double(endPacket - startPacket);
121 //packetIndex += frac;
122 //printf("frames %ld-%ld, packets %qd-%qd, frac %.3f\n",
123 // startFrame, endFrame, startPacket, endPacket, frac);
124 //}
125 //double pos = packetIndex / nPacketsPlus1;
126 //printf("%.3f / %.0f = %.3f\n", packetIndex, nPacketsPlus1, pos);
127 //return pos;
128
129 //return double(GetCurrentFrame()) / nFrames;
130#endif
131}
132
133SInt64 CAAudioFileReader::GetCurrentFrame() const
134{
135 if (!mRunning)
136 return mFile->Tell();
137 if (mEndOfStream)
138 return GetNumberFrames();
139 const FileReadBuffer *b = static_cast<const FileReadBuffer *>(GetCurrentBuffer());
140 // the buffer from which we're reading
141 UInt32 startFrame, endFrame;
142 b->GetLocation(startFrame, endFrame);
143 //printf("%qd + %ld / %.f\n", b->mBufferStartFileFrame, startFrame, nFrames);
144 return b->mBufferStartFileFrame + startFrame;
145}
146
147void CAAudioFileReader::SetCurrentPosition(double loc)
148{
149 bool wasRunning = IsRunning();
150 if (wasRunning)
151 Stop();
152 SInt64 frameNumber = SInt64(loc * GetFile().GetNumberFrames() + 0.5);
153 try {
154 GetFile().Seek(frameNumber);
155 }
156 catch (...) {
157
158 }
159 if (wasRunning)
160 Start();
161}
162
163// ____________________________________________________________________________
164
165void CAAudioFileWriter::SetFile(AudioFileID fileID)
166{
167 Stop();
168 CancelAndDisposeBuffers();
169
170 delete mFile; mFile = NULL;
171 mFile = new CAAudioFile;
172 mFile->Wrap(fileID, true);
173
174 const CAStreamBasicDescription &fileFmt = mFile->GetFileDataFormat();
175 CAStreamBasicDescription iofmt;
176 iofmt.SetCanonical(fileFmt.mChannelsPerFrame, false); // deinterleaved
177 iofmt.mSampleRate = fileFmt.mSampleRate;
178 mFile->SetClientFormat(iofmt, NULL);
179
180 SetFormat(iofmt);
181}
182
183void CAAudioFileWriter::SetFile(const FSRef &parentDir, CFStringRef filename, AudioFileTypeID filetype, const CAStreamBasicDescription &dataFormat, const CAAudioChannelLayout *layout)
184{
185 Stop();
186 CancelAndDisposeBuffers();
187
188 delete mFile; mFile = NULL;
189 mFile = new CAAudioFile;
190 mFile->CreateNew(parentDir, filename, filetype, dataFormat, layout ? &layout->Layout() : NULL);
191
192 const CAStreamBasicDescription &fileFmt = mFile->GetFileDataFormat();
193 CAStreamBasicDescription iofmt;
194 iofmt.SetCanonical(fileFmt.mChannelsPerFrame, false); // deinterleaved
195 iofmt.mSampleRate = fileFmt.mSampleRate;
196 mFile->SetClientFormat(iofmt, NULL);
197
198 SetFormat(iofmt);
199}
200
201void CAAudioFileWriter::Start()
202{
203 mErrorCount = 0;
204 mRunning = true;
205}
206
207void CAAudioFileWriter::Stop()
208{
209 Flush();
210 mRunning = false;
211}
212
213void CAAudioFileWriter::WriteBuffer(CABufferQueue::Buffer *b)
214{
215 CABufferList *ioMemory = b->GetBufferList();
216 CABufferList *fileBuffers = GetBufferList();
217 UInt32 nFrames = b->FrameCount();
218 fileBuffers->SetFrom(ioMemory, GetBytesPerFrame() * nFrames);
219 mFile->Write(nFrames, &fileBuffers->GetModifiableBufferList());
220 b->SetEmpty();
221}