Note that there are some explanatory texts on larger screens.

plurals
  1. POExtracting Amplitude Data from Linear PCM on the iPhone
    primarykey
    data
    text
    <p>I'm having difficulty extracting amplitude data from linear PCM on the iPhone stored in a audio.caf.</p> <p>My questions are:</p> <ol> <li>Linear PCM stores amplitude samples as 16-bit values. Is this correct?</li> <li>How is amplitude stored in packets returned by AudioFileReadPacketData()? When recording mono linear PCM, isn't each sample, (in one frame, in one packet) just an array for SInt16? What is the byte order (big endian vs. little endian)?</li> <li>What does each step in linear PCM amplitude mean physically?</li> <li>When linear PCM is recorded on the iPhone, is the center point 0 (SInt16) or 32768 (UInt16)? What do the max min values mean in the physical wave form/air pressure?</li> </ol> <p>and a bonus question: Are there sound/air pressure wave forms that the iPhone mic can't measure?</p> <p>My code follows:</p> <pre><code>// get the audio file proxy object for the audio AudioFileID fileID; AudioFileOpenURL((CFURLRef)audioURL, kAudioFileReadPermission, kAudioFileCAFType, &amp;fileID); // get the number of packets of audio data contained in the file UInt64 totalPacketCount = [self packetCountForAudioFile:fileID]; // get the size of each packet for this audio file UInt32 maxPacketSizeInBytes = [self packetSizeForAudioFile:fileID]; // setup to extract the audio data Boolean inUseCache = false; UInt32 numberOfPacketsToRead = 4410; // 0.1 seconds of data UInt32 ioNumPackets = numberOfPacketsToRead; UInt32 ioNumBytes = maxPacketSizeInBytes * ioNumPackets; char *outBuffer = malloc(ioNumBytes); memset(outBuffer, 0, ioNumBytes); SInt16 signedMinAmplitude = -32768; SInt16 signedCenterpoint = 0; SInt16 signedMaxAmplitude = 32767; SInt16 minAmplitude = signedMaxAmplitude; SInt16 maxAmplitude = signedMinAmplitude; // process each and every packet for (UInt64 packetIndex = 0; packetIndex &lt; totalPacketCount; packetIndex = packetIndex + ioNumPackets) { // reset the number of packets to get ioNumPackets = numberOfPacketsToRead; AudioFileReadPacketData(fileID, inUseCache, &amp;ioNumBytes, NULL, packetIndex, &amp;ioNumPackets, outBuffer); for (UInt32 batchPacketIndex = 0; batchPacketIndex &lt; ioNumPackets; batchPacketIndex++) { SInt16 packetData = outBuffer[batchPacketIndex * maxPacketSizeInBytes]; SInt16 absoluteValue = abs(packetData); if (absoluteValue &lt; minAmplitude) { minAmplitude = absoluteValue; } if (absoluteValue &gt; maxAmplitude) { maxAmplitude = absoluteValue; } } } NSLog(@"minAmplitude: %hi", minAmplitude); NSLog(@"maxAmplitude: %hi", maxAmplitude); </code></pre> <p>With this code I almost always get a min of 0 and a max of 128! That makes no sense to me.</p> <p>I'm recording the audio using the AVAudioRecorder as follows:</p> <pre><code>// specify mono, 44.1 kHz, Linear PCM with Max Quality as recording format NSDictionary *recordSettings = [[NSDictionary alloc] initWithObjectsAndKeys: [NSNumber numberWithFloat: 44100.0], AVSampleRateKey, [NSNumber numberWithInt: kAudioFormatLinearPCM], AVFormatIDKey, [NSNumber numberWithInt: 1], AVNumberOfChannelsKey, [NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey, nil]; // store the sound file in the app doc folder as calibration.caf NSString *documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSURL *audioFileURL = [NSURL fileURLWithPath:[documentsDir stringByAppendingPathComponent: @"audio.caf"]]; // create the audio recorder NSError *createAudioRecorderError = nil; AVAudioRecorder *newAudioRecorder = [[AVAudioRecorder alloc] initWithURL:audioFileURL settings:recordSettings error:&amp;createAudioRecorderError]; [recordSettings release]; if (newAudioRecorder) { // record the audio self.recorder = newAudioRecorder; [newAudioRecorder release]; self.recorder.delegate = self; [self.recorder prepareToRecord]; [self.recorder record]; } else { NSLog(@"%@", [createAudioRecorderError localizedDescription]); } </code></pre> <p>Thanks for any insight you can offer. This is my first project using Core Audio, so feel free to tear apart my approach!</p> <p>P.S. I have tried to searched the Core Audio list archives, but the request keeps giving an error: ( <a href="http://search.lists.apple.com/?q=linear+pcm+amplitude&amp;cmd=Search%21&amp;ul=coreaudio-api" rel="nofollow noreferrer">http://search.lists.apple.com/?q=linear+pcm+amplitude&amp;cmd=Search%21&amp;ul=coreaudio-api</a> )</p> <p>P.P.S. I have looked at:</p> <p><a href="http://en.wikipedia.org/wiki/Sound_pressure" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Sound_pressure</a></p> <p><a href="http://en.wikipedia.org/wiki/Linear_PCM" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Linear_PCM</a></p> <p><a href="http://wiki.multimedia.cx/index.php?title=PCM" rel="nofollow noreferrer">http://wiki.multimedia.cx/index.php?title=PCM</a></p> <p><a href="https://stackoverflow.com/questions/742546/get-the-amplitude-at-a-given-time-within-a-sound-file">Get the amplitude at a given time within a sound file?</a></p> <p><a href="http://music.columbia.edu/pipermail/music-dsp/2002-April/048341.html" rel="nofollow noreferrer">http://music.columbia.edu/pipermail/music-dsp/2002-April/048341.html</a></p> <p>I have also read the entirety of the Core Audio Overview and most of the Audio Session Programming Guide, but my questions remain.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload