Iphone 从plcm到aac的Wav文件压缩
我使用以下代码录制音频文件,这些代码是我根据apple示例代码开发的Iphone 从plcm到aac的Wav文件压缩,iphone,ios,ipad,Iphone,Ios,Ipad,我使用以下代码录制音频文件,这些代码是我根据apple示例代码开发的 void AQRecorder::SetupAudioFormat(UInt32 inFormatID) { memset(&mRecordFormat, 0, sizeof(mRecordFormat)); UInt32 size = sizeof(mRecordFormat.mSampleRate); XThrowIfError(AudioSessionGetProperty( kAud
void AQRecorder::SetupAudioFormat(UInt32 inFormatID)
{
memset(&mRecordFormat, 0, sizeof(mRecordFormat));
UInt32 size = sizeof(mRecordFormat.mSampleRate);
XThrowIfError(AudioSessionGetProperty( kAudioSessionProperty_CurrentHardwareSampleRate,
&size,
&mRecordFormat.mSampleRate), "couldn't get hardware sample rate");
size = sizeof(mRecordFormat.mChannelsPerFrame);
XThrowIfError(AudioSessionGetProperty( kAudioSessionProperty_CurrentHardwareInputNumberChannels,
&size,
&mRecordFormat.mChannelsPerFrame), "couldn't get input channel count");
mRecordFormat.mFormatID = inFormatID;
if (inFormatID == kAudioFormatLinearPCM)
{
// if we want pcm, default to signed 16-bit little-endian
mRecordFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
mRecordFormat.mBitsPerChannel = 16;
mRecordFormat.mBytesPerPacket = mRecordFormat.mBytesPerFrame = (mRecordFormat.mBitsPerChannel / 8) * mRecordFormat.mChannelsPerFrame;
mRecordFormat.mFramesPerPacket = 1;
}
}
void AQRecorder::StartRecord(CFStringRef inRecordFile)
{
int i, bufferByteSize;
UInt32 size;
CFURLRef url;
try {
mFileName = CFStringCreateCopy(kCFAllocatorDefault, inRecordFile);
// specify the recording format
SetupAudioFormat(kAudioFormatLinearPCM);
// create the queue
XThrowIfError(AudioQueueNewInput(
&mRecordFormat,
MyInputBufferHandler,
this /* userData */,
NULL /* run loop */, NULL /* run loop mode */,
0 /* flags */, &mQueue), "AudioQueueNewInput failed");
// get the record format back from the queue's audio converter --
// the file may require a more specific stream description than was necessary to create the encoder.
mRecordPacket = 0;
size = sizeof(mRecordFormat);
XThrowIfError(AudioQueueGetProperty(mQueue, kAudioQueueProperty_StreamDescription,
&mRecordFormat, &size), "couldn't get queue's format");
NSString *recordFile = [NSTemporaryDirectory() stringByAppendingPathComponent: (NSString*)inRecordFile];
NSLog(recordFile);
url = CFURLCreateWithString(kCFAllocatorDefault, (CFStringRef)recordFile, NULL);
// create the audio file kAudioFileCAFType
XThrowIfError(AudioFileCreateWithURL(url, kAudioFileWAVEType, &mRecordFormat, kAudioFileFlags_EraseFile,
&mRecordFile), "AudioFileCreateWithURL failed");
CFRelease(url);
// copy the cookie first to give the file object as much info as we can about the data going in
// not necessary for pcm, but required for some compressed audio
CopyEncoderCookieToFile();
// allocate and enqueue buffers
bufferByteSize = ComputeRecordBufferSize(&mRecordFormat, kBufferDurationSeconds); // enough bytes for half a second
for (i = 0; i < kNumberRecordBuffers; ++i) {
XThrowIfError(AudioQueueAllocateBuffer(mQueue, bufferByteSize, &mBuffers[i]),
"AudioQueueAllocateBuffer failed");
XThrowIfError(AudioQueueEnqueueBuffer(mQueue, mBuffers[i], 0, NULL),
"AudioQueueEnqueueBuffer failed");
}
// start the queue
mIsRunning = true;
XThrowIfError(AudioQueueStart(mQueue, NULL), "AudioQueueStart failed");
}
catch (CAXException &e) {
char buf[256];
fprintf(stderr, "Error: %s (%s)\n", e.mOperation, e.FormatError(buf));
}
catch (...) {
fprintf(stderr, "An unknown error occurred\n");
}
}
void AQRecorder::SetupAudioFormat(UInt32 inFormatID)
{
memset(&mRecordFormat,0,sizeof(mRecordFormat));
UInt32 size=sizeof(mRecordFormat.mSampleRate);
XThrowIfError(AudioSessionGetProperty(kAudioSessionProperty_CurrentHardwareSample,
&大小,
&mRecordFormat.mSampleRate),“无法获取硬件采样率”);
size=sizeof(mRecordFormat.mChannelsPerFrame);
XThrowIfError(AudioSessionGetProperty(kAudioSessionProperty_CurrentHardwareInputNumberChannel,
&大小,
&mRecordFormat.mChannelsPerFrame),“无法获取输入通道计数”);
mRecordFormat.mFormatID=inFormatID;
if(inFormatID==kAudioFormatLinearPCM)
{
//如果我们想要pcm,默认为有符号16位小尾端
mRecordFormat.mFormatFlags=kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
mRecordFormat.mBitsPerChannel=16;
mRecordFormat.mBytesPerPacket=mRecordFormat.mBytesPerFrame=(mRecordFormat.mbitspperchannel/8)*mRecordFormat.mChannelsPerFrame;
mRecordFormat.mFramesPerPacket=1;
}
}
无效AQRecorder::StartRecord(记录文件中的CFStringRef)
{
inti,bufferByteSize;
UInt32尺寸;
cfurlRefURL;
试试{
mFileName=CFStringCreateCopy(kCFAllocatorDefault,inRecordFile);
//指定录制格式
SetupAudioFormat(kAudioFormatLinearPCM);
//创建队列
XThrowIfError(音频输入)(
&mRecordFormat,
MyInputBufferHandler,
此/*用户数据*/,
NULL/*运行循环*/,NULL/*运行循环模式*/,
0/*标志*/,&MQUE),“AudioQueueNewInput失败”);
//从队列的音频转换器获取记录格式--
//该文件可能需要比创建编码器所需的更具体的流描述。
mRecordPacket=0;
size=sizeof(mRecordFormat);
XThrowIfError(AudioQueueGetProperty(MQUE、KAUDIOQUUEProperty_StreamDescription、,
&mRecordFormat,&size),“无法获取队列的格式”);
NSString*recordFile=[NSTemporaryDirectory()stringByAppendingPathComponent:(NSString*)记录文件];
NSLog(记录文件);
url=CFURLCreateWithString(kCFAllocatorDefault,(CFStringRef)记录文件,NULL);
//创建音频文件kAudioFileCAFType
XThrowIfError(音频文件CreateWithURL(url,kAudioFileWAVEType,&mRecordFormat,kAudioFileFlags_文件,
&mRecordFile),“AudioFileCreateWithURL失败”);
CFRelease(url);
//首先复制cookie,以便为文件对象提供尽可能多的有关输入数据的信息
//pcm不需要,但某些压缩音频需要
CopyEncoderCookieToFile();
//分配和排队缓冲区
bufferByteSize=ComputerCordBufferSize(&mRecordFormat,kBufferDurationSeconds);//足够半秒的字节数
对于(i=0;i
这段代码导致大的输出文件,例如在11MB上保存2分钟
我需要修改代码,使其为AAC压缩,因此我减少了它的大小任何想法您的文件大小听起来都适合WAV文件。转换成MP3可能是一项CPU密集型任务,因此您需要对任何此类库进行一些研究,这可能会有所帮助。是否有lw大小的文件格式可供我使用