6

更新:私はこれを理解し、自分の質問への回答として自分の解決策を投稿しました(以下)

AAC 形式の ExtAudioFileWrite を使用して、オーディオ サンプルの単純なバッファをファイルに書き込もうとしています。

以下のコードでモノバッファを.wavファイルに書き込むことでこれを達成しましたが、ステレオまたはAACファイルに対してこれを行うことはできません。

これが私がこれまでに持っているものです...

CFStringRef fPath;
fPath = CFStringCreateWithCString(kCFAllocatorDefault,
                                  "/path/to/my/audiofile/audiofile.wav",
                                  kCFStringEncodingMacRoman);


OSStatus err;

int mChannels = 1;
UInt32 totalFramesInFile = 100000;

Float32 *outputBuffer = (Float32 *)malloc(sizeof(Float32) * (totalFramesInFile*mChannels)); 


////////////// Set up Audio Buffer List ////////////

AudioBufferList outputData; 
outputData.mNumberBuffers = 1;
outputData.mBuffers[0].mNumberChannels = mChannels; 
outputData.mBuffers[0].mDataByteSize = 4 * totalFramesInFile * mChannels;
outputData.mBuffers[0].mData = outputBuffer;

Float32 audioFile[totalFramesInFile*mChannels];


for (int i = 0;i < totalFramesInFile*mChannels;i++)
{
    audioFile[i] = ((Float32)(rand() % 100))/100.0;
    audioFile[i] = audioFile[i]*0.2;
}

outputData.mBuffers[0].mData = &audioFile;


CFURLRef fileURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,fPath,kCFURLPOSIXPathStyle,false);

ExtAudioFileRef audiofileRef;

// WAVE FILES

AudioFileTypeID fileType = kAudioFileWAVEType;
AudioStreamBasicDescription clientFormat;
clientFormat.mSampleRate = 44100.0;
clientFormat.mFormatID = kAudioFormatLinearPCM;
clientFormat.mFormatFlags = 12;
clientFormat.mBitsPerChannel = 16;
clientFormat.mChannelsPerFrame = mChannels;
clientFormat.mBytesPerFrame = 2*clientFormat.mChannelsPerFrame;
clientFormat.mFramesPerPacket = 1;
clientFormat.mBytesPerPacket = 2*clientFormat.mChannelsPerFrame;


// open the file for writing
err = ExtAudioFileCreateWithURL((CFURLRef)fileURL, fileType, &clientFormat, NULL, kAudioFileFlags_EraseFile, &audiofileRef);

if (err != noErr)
{
    cout << "Problem when creating audio file: " << err << "\n";
}

// tell the ExtAudioFile API what format we'll be sending samples in
err = ExtAudioFileSetProperty(audiofileRef, kExtAudioFileProperty_ClientDataFormat, sizeof(clientFormat), &clientFormat);

if (err != noErr)
{
    cout << "Problem setting audio format: " << err << "\n";
}

UInt32 rFrames = (UInt32)totalFramesInFile;
// write the data
err = ExtAudioFileWrite(audiofileRef, rFrames, &outputData);

if (err != noErr)
{
    cout << "Problem writing audio file: " << err << "\n";
}

// close the file
ExtAudioFileDispose(audiofileRef);



NSLog(@"Done!");

私の具体的な質問は次のとおりです。

  • AAC の AudioStreamBasicDescription を設定するにはどうすればよいですか?
  • ここでステレオが正しく機能しないのはなぜですか? チャネル数 ('mChannels') を 2 に設定すると、左チャネルが正しく取得され、右チャネルに歪みが生じます。

私はどんな助けにも非常に感謝しています - 私はこれで見つけることができるほぼすべてのページを読んだと思いますが、同様の質問がありますが、通常、入力オーディオファイルから AudioStreamBasicDescription パラメータを導出するため、賢明ではありません。の結果を見ることができません。Apple のドキュメントも役に立ちません。

よろしくお願いします。

アダム

4

1 に答える 1

7

わかりました、いくつかの調査の後、私はそれを理解しました。ランダム ノイズをファイルに書き込む関数としてラップしました。具体的には、次のことができます。

  • .wav または .m4a ファイルを書き込む
  • モノラルまたはステレオをいずれかの形式で書き込みます
  • 指定したパスにファイルを書き込む

関数の引数は次のとおりです。

  • 作成する音声ファイルへのパス
  • チャネル数 (最大 2)
  • boolean: m4a で圧縮 (false の場合は pcm を使用)

ステレオ M4A ファイルの場合、関数は次のように呼び出す必要があります。

writeNoiseToAudioFile("/path/to/my/audiofile.m4a",2,true);

関数のソースは次のとおりです。私はできる限りコメントしようとしました - 私はそれが正しいことを願っています. 幸運を!コードは次のとおりです。

void writeNoiseToAudioFile(char *fName,int mChannels,bool compress_with_m4a)
{
OSStatus err; // to record errors from ExtAudioFile API functions

// create file path as CStringRef
CFStringRef fPath;
fPath = CFStringCreateWithCString(kCFAllocatorDefault,
                                  fName,
                                  kCFStringEncodingMacRoman);


// specify total number of samples per channel
UInt32 totalFramesInFile = 100000;      

/////////////////////////////////////////////////////////////////////////////
////////////// Set up Audio Buffer List For Interleaved Audio ///////////////
/////////////////////////////////////////////////////////////////////////////

AudioBufferList outputData; 
outputData.mNumberBuffers = 1;
outputData.mBuffers[0].mNumberChannels = mChannels;    
outputData.mBuffers[0].mDataByteSize = sizeof(AudioUnitSampleType)*totalFramesInFile*mChannels;



/////////////////////////////////////////////////////////////////////////////
//////// Synthesise Noise and Put It In The AudioBufferList /////////////////
/////////////////////////////////////////////////////////////////////////////

// create an array to hold our audio
AudioUnitSampleType audioFile[totalFramesInFile*mChannels];

// fill the array with random numbers (white noise)
for (int i = 0;i < totalFramesInFile*mChannels;i++)
{
    audioFile[i] = ((AudioUnitSampleType)(rand() % 100))/100.0;
    audioFile[i] = audioFile[i]*0.2;
    // (yes, I know this noise has a DC offset, bad)
}

// set the AudioBuffer to point to the array containing the noise
outputData.mBuffers[0].mData = &audioFile;


/////////////////////////////////////////////////////////////////////////////
////////////////// Specify The Output Audio File Format /////////////////////
/////////////////////////////////////////////////////////////////////////////


// the client format will describe the output audio file
AudioStreamBasicDescription clientFormat;

// the file type identifier tells the ExtAudioFile API what kind of file we want created
AudioFileTypeID fileType;

// if compress_with_m4a is tru then set up for m4a file format
if (compress_with_m4a)
{
    // the file type identifier tells the ExtAudioFile API what kind of file we want created
    // this creates a m4a file type
    fileType = kAudioFileM4AType;

    // Here we specify the M4A format
    clientFormat.mSampleRate         = 44100.0;
    clientFormat.mFormatID           = kAudioFormatMPEG4AAC;
    clientFormat.mFormatFlags        = kMPEG4Object_AAC_Main;
    clientFormat.mChannelsPerFrame   = mChannels;
    clientFormat.mBytesPerPacket     = 0;
    clientFormat.mBytesPerFrame      = 0;
    clientFormat.mFramesPerPacket    = 1024;
    clientFormat.mBitsPerChannel     = 0;
    clientFormat.mReserved           = 0;
}
else // else encode as PCM
{
    // this creates a wav file type
    fileType = kAudioFileWAVEType;

    // This function audiomatically generates the audio format according to certain arguments
    FillOutASBDForLPCM(clientFormat,44100.0,mChannels,32,32,true,false,false);
}



/////////////////////////////////////////////////////////////////////////////
///////////////// Specify The Format of Our Audio Samples ///////////////////
/////////////////////////////////////////////////////////////////////////////

// the local format describes the format the samples we will give to the ExtAudioFile API
AudioStreamBasicDescription localFormat;
FillOutASBDForLPCM (localFormat,44100.0,mChannels,32,32,true,false,false);



/////////////////////////////////////////////////////////////////////////////
///////////////// Create the Audio File and Open It /////////////////////////
/////////////////////////////////////////////////////////////////////////////

// create the audio file reference
ExtAudioFileRef audiofileRef;

// create a fileURL from our path
CFURLRef fileURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,fPath,kCFURLPOSIXPathStyle,false);

// open the file for writing
err = ExtAudioFileCreateWithURL((CFURLRef)fileURL, fileType, &clientFormat, NULL, kAudioFileFlags_EraseFile, &audiofileRef);

if (err != noErr)
{
    cout << "Problem when creating audio file: " << err << "\n";
}


/////////////////////////////////////////////////////////////////////////////
///// Tell the ExtAudioFile API what format we'll be sending samples in /////
/////////////////////////////////////////////////////////////////////////////

// Tell the ExtAudioFile API what format we'll be sending samples in 
err = ExtAudioFileSetProperty(audiofileRef, kExtAudioFileProperty_ClientDataFormat, sizeof(localFormat), &localFormat);

if (err != noErr)
{
    cout << "Problem setting audio format: " << err << "\n";
}

/////////////////////////////////////////////////////////////////////////////
///////// Write the Contents of the AudioBufferList to the AudioFile ////////
/////////////////////////////////////////////////////////////////////////////

UInt32 rFrames = (UInt32)totalFramesInFile;
// write the data
err = ExtAudioFileWrite(audiofileRef, rFrames, &outputData);

if (err != noErr)
{
    cout << "Problem writing audio file: " << err << "\n";
}


/////////////////////////////////////////////////////////////////////////////
////////////// Close the Audio File and Get Rid Of The Reference ////////////
/////////////////////////////////////////////////////////////////////////////

// close the file
ExtAudioFileDispose(audiofileRef);


NSLog(@"Done!");
}

AudioToolbox フレームワークをインポートし、ヘッダー ファイルを含めることを忘れないでください。

#import <AudioToolbox/AudioToolbox.h>
于 2012-09-26T17:05:12.283 に答える