Re: Convert to float and more
Re: Convert to float and more
- Subject: Re: Convert to float and more
- From: john <email@hidden>
- Date: Wed, 04 Jun 2003 16:46:21 -0400
Hi Peter,
The other people on the list have pointed you in a good direction. I
also found that the AudioConverter is much faster than doing the simple
division/multiplication. Plus, it's incredibly easy to use. Below is a
snippet of code to use it.
I also agree with Jeff Moore's suggestion to not load the entire audio
file into an NSData object. Using NSFileHandle and NSFileManager are
quite easy (as is all of Cocoa!), so you may want to read and convert
the audio data from the file as you need it.
Code snip:
- (void)convertNative: (NSMutableData*)nativeBuffer toFloat:
(NSMutableData*)floatBuffer
{
AudioConverterRef theAudioConverter;
AudioStreamBasicDescription sourceFormat, destinationFormat;
unsigned long theSourceSize = [nativeBuffer length];
unsigned long theDestSize = (theSourceSize * 2);
unsigned long theResult = 0;
// make sure we have the right length for the destination buffer
[floatBuffer setLength: theDestSize];
// fill out our source description
sourceFormat.mSampleRate = 44100.0;
sourceFormat.mFormatID = kAudioFormatLinearPCM;
sourceFormat.mFormatFlags = kAudioFormatFlagIsBigEndian |
kLinearPCMFormatFlagIsSignedInteger;
sourceFormat.mBytesPerPacket = 4;
sourceFormat.mFramesPerPacket = 1;
sourceFormat.mBytesPerFrame = 4;
sourceFormat.mChannelsPerFrame = 2;
sourceFormat.mBitsPerChannel = 16;
// now fill out the destination description
destinationFormat.mSampleRate = 44100.0;
destinationFormat.mFormatID = kAudioFormatLinearPCM;
destinationFormat.mFormatFlags = kAudioFormatFlagIsBigEndian |
kAudioFormatFlagIsFloat;
destinationFormat.mBytesPerPacket = 8;
destinationFormat.mFramesPerPacket = 1;
destinationFormat.mBytesPerFrame = 8;
destinationFormat.mChannelsPerFrame = 2;
destinationFormat.mBitsPerChannel = 32;
// create our converter
theResult = AudioConverterNew(&sourceFormat, &destinationFormat,
&theAudioConverter);
// if we have an error
if (!theResult)
{ // now convert the data
theResult = AudioConverterConvertBuffer(theAudioConverter,
theSourceSize, (void*)[nativeBuffer bytes], &theDestSize,
(void*)[floatBuffer mutableBytes]);
// now get rid of it
AudioConverterDispose(theAudioConverter);
}
}
Hope this helps!
-- John
>
Hi
>
>
I have a few questions.
>
>
Im working on a Cocoa sound editing app.
>
>
When i import a file i just copy the sound bytes into a NSData and keep
>
it in the original format.
>
>
All my drawing and DSP code use floats, so if the original sound format
>
is 16bit i do a lot of sample/32768.
>
>
I was thinking of converting the samples to float when importing the
>
file, but that will double the data size, so i still need to decide
>
what's best.
>
>
I tried to search the archives for the fastest way to convert from int
>
to float and back. but did not find much,
>
someone mentioned there might be a altivec snippet for converting?
>
>
I also looked at CASampleTools.cpp ( but to be honest i don't
>
understand C++).
>
>
When i play a file through the defaultAudioOutput, and the stream
>
format is not float, is there another conversion to float within
>
coreaudio?
>
>
I also want to import/export MP3 and other compressed formats what is
>
the best way to do that?
>
>
>
Thanks
>
>
Peter Mark
_______________________________________________
coreaudio-api mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/coreaudio-api
Do not post admin requests to the list. They will be ignored.