amplitude meter from mp3 data chunk?
amplitude meter from mp3 data chunk?
- Subject: amplitude meter from mp3 data chunk?
- From: Jerry Stratton <email@hidden>
- Date: Thu, 4 Dec 2008 10:54:22 -0800
I have an MP3 stream I'd like to display a modulation or amplitude
meter for, most likely using NSLevelIndicator.
I've been able to create an NSURLConnection successfully, and I have a
delegate for didReceiveData that gets a data chunk. I've verified with
NSSound that it is the sound I'm expecting. But how do I convert that
mp3 sound into something I can calculate average amplitude on?
I don't need to play the sound or do anything with it except display
the amplitude for feedback purposes.
What I think that I want is something like:
NSData *decodedChunk = [AudioConvertToRaw dataChunk];
Or even
NSUInteger *averageAmplitude = [soundChunk averageAmplitude];
But I'm expecting that it isn't that simple. I've looked around
Apple's CoreAudio, QuickTime, and NSSound web sites, and based on a
search of this list's archives (and the CoreAudio examples) it looks
like I want to use AudioConverterFillComplexBuffer to get back an
AudioBufferList, and I can then average the amplitude myself over the
raw data. But I still can't see how to do this. I'm getting a
parameter error (-50) from AudioConverterFillComplexBuffer.
This is what I have so far:
- (void) connection:(NSURLConnection *)connection didReceiveData:
(NSData *)dataChunk {
//NSSound *soundChunk = [[NSSound alloc] initWithData:dataChunk];
//[soundChunk play]; //this does play, so it understands that this is
mp3 data
AudioConverterRef mp3Converter;
AudioStreamBasicDescription inFormat, outFormat;
AudioBufferList convertedData;
UInt32 dataSize;
inFormat.mFormatID = kAudioFormatMPEGLayer3;
outFormat.mFormatID = kAudioFormatLinearPCM;
AudioConverterNew(&inFormat, &outFormat, &mp3Converter);
dataSize = [dataChunk length];
#ifdef DEBUG
NSLog(@"data size: %u", dataSize);
#endif
OSStatus err = AudioConverterFillComplexBuffer(mp3Converter,
provideMP3fromData, dataChunk, &dataSize, &convertedData, NULL);
NSUInteger byteCount = convertedData.mBuffers[0].mDataByteSize;
#ifdef DEBUG
NSLog(@"byte count: %u, %i", byteCount, err);
#endif
}
This is the provideMP3fromData function, which I'm sure also has
problems but as far as I can tell it is never called:
OSStatus provideMP3fromData (
AudioConverterRef inAudioConverter,
UInt32 *ioNumberDataPackets,
AudioBufferList *ioData,
AudioStreamPacketDescription **outDataPacketDescription,
void *inUserData) {
#ifdef DEBUG
NSLog(@"%@", @"Hello?");
#endif
NSData *data = (NSData *) inUserData;
ioData->mBuffers[0].mNumberChannels = 1;
ioData->mBuffers[0].mData = (void *) [data bytes];
ioData->mBuffers[0].mDataByteSize = [data length];
return noErr;
}
Jerry Stratton
--
"The major difference between a thing that might go wrong and a thing
that cannot possibly go wrong is that when a thing that cannot
possibly go wrong goes wrong it usually turns out to be impossible to
get at and repair."--Douglas Adams (Mostly Harmless)
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Coreaudio-api mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden