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 CAAudioFileRecorder.cpp
40
41=============================================================================*/
42
43#include "CAAudioFileRecorder.h"
44#include "CAXException.h"
45
46CAAudioFileRecorder::CAAudioFileRecorder(int nBuffers, UInt32 bufferSizeFrames) :
47 CAAudioFileWriter(nBuffers, bufferSizeFrames),
48 mInputUnit(NULL),
49 mAudioInputPtrs(NULL)
50{
51 // open input unit
52 Component comp;
53 ComponentDescription desc;
54
55 desc.componentType = kAudioUnitType_Output;
56 desc.componentSubType = kAudioUnitSubType_HALOutput;
57 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
58 desc.componentFlags = 0;
59 desc.componentFlagsMask = 0;
60 comp = FindNextComponent(NULL, &desc);
61 XThrowIf(comp == NULL, -1, "find audio input unit");
62 XThrowIfError(OpenAComponent(comp, &mInputUnit), "open audio input unit");
63
64 UInt32 enableIO;
65 UInt32 propSize;
66
67 enableIO = 0;
68 XThrowIfError(AudioUnitSetProperty(mInputUnit,
69 kAudioOutputUnitProperty_EnableIO,
70 kAudioUnitScope_Output,
71 0,
72 &enableIO,
73 sizeof(enableIO)), "failed to disable output");
74
75 enableIO = 1;
76 XThrowIfError(AudioUnitSetProperty(mInputUnit,
77 kAudioOutputUnitProperty_EnableIO,
78 kAudioUnitScope_Input,
79 1,
80 &enableIO,
81 sizeof(enableIO)), "failed to enable input");
82
83 // select the default input device
84 propSize = sizeof(AudioDeviceID);
85 AudioDeviceID inputDevice;
86 XThrowIfError(
87 AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &propSize, &inputDevice),
88 "failed to get default input device");
89
90 XThrowIfError(
91 AudioUnitSetProperty(mInputUnit, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &inputDevice, sizeof(inputDevice)),
92 "failed to select input device");
93
94 // set render callback
95 AURenderCallbackStruct input;
96 input.inputProc = InputProc;
97 input.inputProcRefCon = this;
98 XThrowIfError(AudioUnitSetProperty(
99 mInputUnit,
100 kAudioOutputUnitProperty_SetInputCallback,
101 kAudioUnitScope_Global,
102 0,
103 &input,
104 sizeof(input)), "connect input proc to output unit");
105
106 XThrowIfError(AudioUnitInitialize(mInputUnit), "initialize audio input unit");
107
108 // get the hardware format
109 propSize = sizeof(mInputDataFormat);
110 XThrowIfError(AudioUnitGetProperty(mInputUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 1, &mInputDataFormat, &propSize), "couldn't get input unit's stream format");
111}
112
113CAAudioFileRecorder::~CAAudioFileRecorder()
114{
115 if (mInputUnit)
116 CloseComponent(mInputUnit);
117 delete mAudioInputPtrs;
118}
119
120void CAAudioFileRecorder::SetFile(const FSRef &parentFSRef, CFStringRef filename, AudioFileTypeID filetype, const CAStreamBasicDescription &dataFormat, const CAAudioChannelLayout *layout)
121{
122 delete mAudioInputPtrs; mAudioInputPtrs = NULL;
123
124 CAStreamBasicDescription fileDataFormat = dataFormat;
125 if (fileDataFormat.mSampleRate == 0.)
126 fileDataFormat.mSampleRate = mInputDataFormat.mSampleRate;
127
128 CAAudioFileWriter::SetFile(parentFSRef, filename, filetype, fileDataFormat, layout);
129
130 const CAStreamBasicDescription &fmt = GetFile().GetClientDataFormat();
131 XThrowIfError(AudioUnitSetProperty(
132 mInputUnit,
133 kAudioUnitProperty_StreamFormat,
134 kAudioUnitScope_Output,
135 1,
136 (void *)&fmt,
137 sizeof(AudioStreamBasicDescription)), "set audio input format");
138
139 GetFile().SetIOBufferSizeBytes(GetBufferSizeFrames() * fmt.mBytesPerFrame);
140
141 mAudioInputPtrs = CABufferList::New("audio input ptrs", fmt);
142}
143
144/*
145void CAAudioFileRecorder::SetFile(AudioFileID fileID)
146{
147 delete mAudioInputPtrs; mAudioInputPtrs = NULL;
148
149 CAAudioFileWriter::SetFile(fileID, mInputDataFormat.mSampleRate);
150
151 const CAStreamBasicDescription &fmt = GetFile().GetClientDataFormat();
152 XThrowIfError(AudioUnitSetProperty(
153 mInputUnit,
154 kAudioUnitProperty_StreamFormat,
155 kAudioUnitScope_Output,
156 1,
157 (void *)&fmt,
158 sizeof(AudioStreamBasicDescription)), "set audio input format");
159
160 GetFile().SetIOBufferSize(GetBufferSizeFrames() * fmt.mBytesPerFrame);
161
162 mAudioInputPtrs = CABufferList::New("audio input ptrs", fmt);
163}
164*/
165
166void CAAudioFileRecorder::Start()
167{
168 CAAudioFileWriter::Start();
169 XThrowIfError(AudioOutputUnitStart(mInputUnit), "start audio input unit");
170}
171
172void CAAudioFileRecorder::Stop()
173{
174 XThrowIfError(AudioOutputUnitStop(mInputUnit), "stop audio input unit");
175 CAAudioFileWriter::Stop();
176}
177
178OSStatus CAAudioFileRecorder::InputProc(
179 void * inRefCon,
180 AudioUnitRenderActionFlags *ioActionFlags,
181 const AudioTimeStamp * inTimeStamp,
182 UInt32 inBusNumber,
183 UInt32 inNumberFrames,
184 AudioBufferList * ioData)
185{
186 CAAudioFileRecorder *This = static_cast<CAAudioFileRecorder *>(inRefCon);
187 AudioUnitRenderActionFlags flags = 0;
188 AudioBufferList *abl = &This->mAudioInputPtrs->GetModifiableBufferList();
189 for (UInt32 i = 0; i < abl->mNumberBuffers; ++i)
190 abl->mBuffers[i].mData = NULL;
191 OSStatus err = AudioUnitRender(This->mInputUnit, &flags, inTimeStamp, 1, inNumberFrames, abl);
192 if (err)
193 return err;
194
195 This->PushBuffer(inNumberFrames, abl);
196 return noErr;
197}