问题
I am trying to implement linear convolution using Core Audio, I have the algorithm implemented and working, but I am trying to write the output of this into a .wav audio file. Here is the code for the algorithm...
//Create array containing output of convolution (size of array1 + array2 - 1)
float *COutput;
COutput = (float *)malloc(((size1+size2)-1)* sizeof(float));
int sizeOutput = ((size1 + size2)-1);
//Convolution Algorithm!!!
for (i=0; i<sizeOutput; i++) {
COutput[i]=0;
for (j=0; j<sizeCArray1; j++) {
if (((i-j)+1) > 0) {
COutput[i] += CArray1[i - j] * CArray2[j];
}
}
}
I need to write the float values within COutput (a standard array of floats) into an audio file. Am I right in assuming I need to send these float values to an AudioBuffer within an AudioBufferList initially? Or is there a simple way of doing this?
Many thanks for any help or guidance!
回答1:
The free DiracLE time stretching library ( http://dirac.dspdimension.com ) has utility code that converts ABLs (AudioBufferLists) into float arrays and vice-versa as part of their example code. Check out their EAFRead and EAFWrite classes, they're exactly what you're looking for.
回答2:
Yes, I'd put them in an AudioBuffer and that into an AudioBufferList. After that you can write them to a file using ExtAudioFileWrite() on a ExtAudioFileRef that was created using ExtAudioFileCreateNew().
Audio File documentation: http://developer.apple.com/library/mac/#documentation/MusicAudio/Reference/ExtendedAudioFileServicesReference/Reference/reference.html%23//apple_ref/doc/uid/TP40007912
回答3:
This is a late answer, but I struggled to get a float *
buffer to write to a file in Swift.
Posting this example in case it helps someone.
enum AudioFileError: ErrorType {
case FailedToCreate(OSStatus)
case FailedToWrite(OSStatus)
case FailedToClose(OSStatus)
}
func writeAudioData(audioData:NSData, toFile destination:NSURL, description:AudioStreamBasicDescription) throws {
//get a pointer to the float buffer
let floatBuffer = UnsafeMutablePointer<Float>(audioData.bytes)
//get an AudioBufferList from the float buffer
let buffer = AudioBuffer(mNumberChannels: 1, mDataByteSize: UInt32(audioData.length), mData: floatBuffer)
var bufferList = AudioBufferList(mNumberBuffers: 1, mBuffers: buffer)
//create the CAF file using the stream description
var file = ExtAudioFileRef()
var result:OSStatus = noErr
var streamDescription = description
withUnsafePointer(&streamDescription) { streamDescription in
withUnsafeMutablePointer(&file) { file in
result = ExtAudioFileCreateWithURL(destination, kAudioFileCAFType, streamDescription, nil, AudioFileFlags.EraseFile.rawValue, file)
}
}
if result != noErr {
throw AudioFileError.FailedToCreate(result)
}
//write the AudioBufferList to the file
withUnsafeMutablePointer(&bufferList) { bufferList in
result = ExtAudioFileWrite(file, UInt32(audioData.length / sizeof(Float)), bufferList)
}
if result != noErr {
throw AudioFileError.FailedToWrite(result)
}
//close the file
result = ExtAudioFileDispose(file)
if result != noErr {
throw AudioFileError.FailedToClose(result)
}
}
来源:https://stackoverflow.com/questions/5231856/how-to-write-array-of-float-values-to-audio-file-in-core-audio