gpt4 book ai didi

ios - 如何在 iPhone 中使用 AudioBuffer 写入从麦克风本地录制的音频文件?

转载 作者:可可西里 更新时间:2023-11-01 04:23:41 26 4
gpt4 key购买 nike

我是音频框架的新手,有人帮我编写通过从麦克风捕获播放的音频文件吗?

下面是通过 iphone 扬声器播放麦克风输入的代码,现在我想将音频保存在 iphone 中以备将来使用。

我从这里找到了使用麦克风录制音频的代码 http://www.stefanpopp.de/2011/capture-iphone-microphone/

/**

Code start from here for playing the recorded voice

*/

static OSStatus playbackCallback(void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData) {

/**
This is the reference to the object who owns the callback.
*/
AudioProcessor *audioProcessor = (AudioProcessor*) inRefCon;

// iterate over incoming stream an copy to output stream
for (int i=0; i < ioData->mNumberBuffers; i++) {
AudioBuffer buffer = ioData->mBuffers[i];

// find minimum size
UInt32 size = min(buffer.mDataByteSize, [audioProcessor audioBuffer].mDataByteSize);

// copy buffer to audio buffer which gets played after function return
memcpy(buffer.mData, [audioProcessor audioBuffer].mData, size);

// set data size
buffer.mDataByteSize = size;

// get a pointer to the recorder struct variable
Recorder recInfo = audioProcessor.audioRecorder;
// write the bytes
OSStatus audioErr = noErr;
if (recInfo.running) {
audioErr = AudioFileWriteBytes (recInfo.recordFile,
false,
recInfo.inStartingByte,
&size,
&buffer.mData);
assert (audioErr == noErr);
// increment our byte count
recInfo.inStartingByte += (SInt64)size;// size should be number of bytes
audioProcessor.audioRecorder = recInfo;

}
}

return noErr;
}

-(void)prepareAudioFileToRecord{

NSArray *paths =             NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;

NSTimeInterval time = ([[NSDate date] timeIntervalSince1970]); // returned as a double
long digits = (long)time; // this is the first 10 digits
int decimalDigits = (int)(fmod(time, 1) * 1000); // this will get the 3 missing digits
// long timestamp = (digits * 1000) + decimalDigits;
NSString *timeStampValue = [NSString stringWithFormat:@"%ld",digits];
// NSString *timeStampValue = [NSString stringWithFormat:@"%ld.%d",digits ,decimalDigits];


NSString *fileName = [NSString stringWithFormat:@"test%@.caf",timeStampValue];
NSString *filePath = [basePath stringByAppendingPathComponent:fileName];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
// modify the ASBD (see EDIT: towards the end of this post!)
audioFormat.mFormatFlags = kAudioFormatFlagIsBigEndian | kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;

// set up the file (bridge cast will differ if using ARC)
OSStatus audioErr = noErr;
audioErr = AudioFileCreateWithURL((CFURLRef)fileURL,
kAudioFileCAFType,
&audioFormat,
kAudioFileFlags_EraseFile,
&audioRecorder.recordFile);


assert (audioErr == noErr);// simple error checking
audioRecorder.inStartingByte = 0;
audioRecorder.running = true;
self.audioRecorder = audioRecorder;

提前致谢巴拉

最佳答案

要将字节从 AudioBuffer 写入本地文件,我们需要 AudioFileServices 的帮助 link AudioToolbox 框架中包含的类。

从概念上讲,我们将执行以下操作 - 设置一个音频文件并维护对它的引用(我们需要可以从您在帖子中包含的渲染回调访问此引用)。我们还需要跟踪每次调用回调时写入的字节数。最后要检查的标志会让我们知道停止写入文件并关闭文件。

因为您提供的链接中的代码声明了一个 AudioStreamBasicDescription,它是 LPCM,因此比特率恒定,我们可以使用 AudioFileWriteBytes 函数(写入压缩音频涉及更多,将改用 AudioFileWritePackets 函数)。

让我们首先声明一个自定义结构(其中包含我们需要的所有额外数据)并添加此自定义结构的实例变量,并创建一个指向该结构变量的属性。我们会将其添加到 AudioProcessor 自定义类中,因为您已经可以从您在此行中进行类型转换的回调中访问该对象。

AudioProcessor *audioProcessor = (AudioProcessor*) inRefCon;

将其添加到AudioProcessor.h(在@interface 之上)

typedef struct Recorder {
AudioFileID recordFile;
SInt64 inStartingByte;
Boolean running;
} Recorder;

现在让我们添加一个实例变量并将其设为指针属性并将其分配给实例变量(这样我们就可以从回调函数中访问它)。在@interface 中添加一个名为 audioRecorder 的实例变量,并使 ASBD 对类可用。

Recorder audioRecorder;
AudioStreamBasicDescription recordFormat;// assign this ivar to where the asbd is created in the class

在方法 -(void)initializeAudio 中注释掉或删除此行,因为我们已将 recordFormat 设为 ivar。

//AudioStreamBasicDescription recordFormat;

现在将 kAudioFormatFlagIsBigEndian 格式标志添加到设置 ASBD 的位置。

// also modify the ASBD in the AudioProcessor classes -(void)initializeAudio method (see EDIT: towards the end of this post!)
recordFormat.mFormatFlags = kAudioFormatFlagIsBigEndian | kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;

最后将它添加为指向 audioRecorder 实例变量的指针的属性,不要忘记在 AudioProcessor.m 中合成它。我们将指针属性命名为 audioRecorderPointer

@property Recorder *audioRecorderPointer;

// in .m synthesise the property
@synthesize audioRecorderPointer;

现在让我们将指针分配给 ivar(这可以放在 AudioProcessor 类的 -(void)initializeAudio 方法中)

// ASSIGN POINTER PROPERTY TO IVAR
self.audioRecorderPointer = &audioRecorder;

现在在 AudioProcessor.m 中,让我们添加一个方法来设置文件并打开它,以便我们可以写入它。这应该在您开始运行 AUGraph 之前调用。

-(void)prepareAudioFileToRecord {
// lets set up a test file in the documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString *fileName = @"test_recording.aif";
NSString *filePath = [basePath stringByAppendingPathComponent:fileName];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];

// set up the file (bridge cast will differ if using ARC)
OSStatus audioErr = noErr;
audioErr = AudioFileCreateWithURL((CFURLRef)fileURL,
kAudioFileAIFFType,
recordFormat,
kAudioFileFlags_EraseFile,
&audioRecorder.recordFile);
assert (audioErr == noErr);// simple error checking
audioRecorder.inStartingByte = 0;
audioRecorder.running = true;
}

好的,我们快到了。现在我们有一个要写入的文件,以及一个可以从渲染回调访问的 AudioFileID。因此,在您发布的回调函数中,在方法末尾返回 noErr 之前添加以下内容。

// get a pointer to the recorder struct instance variable
Recorder *recInfo = audioProcessor.audioRecorderPointer;
// write the bytes
OSStatus audioErr = noErr;
if (recInfo->running) {
audioErr = AudioFileWriteBytes (recInfo->recordFile,
false,
recInfo->inStartingByte,
&size,
buffer.mData);
assert (audioErr == noErr);
// increment our byte count
recInfo->inStartingByte += (SInt64)size;// size should be number of bytes
}

当我们想要停止录音时(可能由某些用户操作调用),只需将正在运行的 bool 值设置为 false 并像这样在 AudioProcessor 类中的某处关闭文件。

audioRecorder.running = false;
OSStatus audioErr = AudioFileClose(audioRecorder.recordFile);
assert (audioErr == noErr);

编辑:样本的字节顺序需要是文件的大字节序,因此将 kAudioFormatFlagIsBigEndian 位掩码标志添加到源代码中的 ASBD,该源代码可在相关链接中找到。

有关此主题的更多信息,Apple 文档是一个很好的资源,我还推荐阅读 Chris Adamson 和 Kevin Avila 合着的“Learning Core Audio”(我拥有一本)。

关于ios - 如何在 iPhone 中使用 AudioBuffer 写入从麦克风本地录制的音频文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20043725/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com