• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
ExtAudioFileRead giving sawtooth-like data back
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

ExtAudioFileRead giving sawtooth-like data back


  • Subject: ExtAudioFileRead giving sawtooth-like data back
  • From: "Ollie Charles" <email@hidden>
  • Date: Wed, 28 Mar 2007 22:09:24 +0100

Hi.

I'm planning to write a sampler AudioUnit as a little hobby project,
and I'm just playing around with CoreAudio at the moment to understand
how it works. I'm trying to write a little Cocoa view that renders a
waveform of a file, so I thought using CA + AudioToolkit would be the
best way to get data from files.

However, I'm struggling to even read an AIFF file. I've looked at all
the samples I can find and it looks like I'm doing everything
correctly. All my CA calls return noerr, and ExtAudioFileRead doesn't
change my numberOfFrames variable, so I'm pretty sure it's reading.
The data I get back, however, just cycles -127 to 127; the bounds of
my datatype (char). I've tried using 4 bytes, with 32bits per channel
(which I assume should result in my data being integer). This gives me
data all around 200000, and nothing negative, so this isn't correct
either!

I'm out of ideas, so hopefully someone can shed some light on my
problem. Below is the code I'm using to do all the loading:

		OSStatus err;
		FSRef fsRef;
		ExtAudioFileRef afRef;

		// First get a FSRef to our file
		CFStringRef srFilename = CFStringCreateWithCString(NULL,
"/System/Library/Sounds/Blow.aiff", kCFStringEncodingASCII);
		CFURLRef urFilename = CFURLCreateWithFileSystemPath(NULL,
srFilename, kCFURLPOSIXPathStyle, false);
		bool loadedOk = CFURLGetFSRef(urFilename, &fsRef);

		if(!loadedOk)
		{
			NSLog(@"%s Failed to load the audio file", _cmd);
			return nil;
		}

		// Get FSCatalog info
		FSCatalogInfo fsCatInfo;
		err = FSGetCatalogInfo(&fsRef, kFSCatInfoDataSizes, &fsCatInfo,
NULL, NULL, NULL);
		XThrowIfError(err, "Could not get catalog info")

		// Now we can load the audio file
		err = ExtAudioFileOpen(&fsRef, &afRef);
		XThrowIfError(err, "Could not load audio file - ExtAudioFileOpen");

		// Introspect the file format from this file now, so we know what we
are dealing with
		AudioStreamBasicDescription format;
		UInt32 pSize = sizeof(format);
		err = ExtAudioFileGetProperty(afRef,
kExtAudioFileProperty_FileDataFormat, &pSize, &format);
		XThrowIfError(err, "Could not get
kExtAudioFileProperty_FileDataFormat property");

		// Set the client format:
		AudioStreamBasicDescription outFormat;
		memset(&outFormat, 0, sizeof(AudioStreamBasicDescription));
		outFormat.mFormatID = kAudioFormatLinearPCM;
		outFormat.mSampleRate = format.mSampleRate;
		outFormat.mChannelsPerFrame = 1;
		outFormat.mBytesPerPacket = 1;
		outFormat.mFramesPerPacket = 1;
		outFormat.mBytesPerFrame = 1;
		outFormat.mBitsPerChannel = 8;


		pSize = sizeof(outFormat);
		err = AudioFormatGetProperty(kAudioFormatProperty_FormatInfo, 0,
NULL, &pSize, &outFormat); // Doesn't change any of the above data...

		err = ExtAudioFileSetProperty(afRef,
kExtAudioFileProperty_ClientDataFormat, pSize, &outFormat);
		XThrowIfError(err, "Could not set client data format
kExtAudioFileProperty_ClientDataFormat");

		// Create our AudioBufferList
		SInt64 totalAudioFrames;
		pSize = sizeof(totalAudioFrames);
		err = ExtAudioFileGetProperty(afRef,
kExtAudioFileProperty_FileLengthFrames, &pSize, &totalAudioFrames);
		XThrowIfError(err, "Colud not get property
kExtAudioFileProperty_FileLengthFrames");

		UInt32 numberOfFrames = totalAudioFrames;
		buffers.mNumberBuffers = 1;
		buffers.mBuffers[0].mNumberChannels = 1;
		buffers.mBuffers[0].mDataByteSize = fsCatInfo.dataLogicalSize;
		buffers.mBuffers[0].mData = calloc(totalAudioFrames, sizeof(char));

		// And read the data into the buffer
		err = ExtAudioFileRead(afRef, &numberOfFrames, &buffers);
		XThrowIfError(err, "Could not read the audio file ExtAudioFileRead");

Again, no compile errors, no warnings and no runtime errors that I can
find... just invalid data. I am reading the buffer back with this
(ugly) code at the moment, so maybe this is my problem:

unsigned int i = 0;
		for(  i = 0; i < numberOfFrames; i += 1 )
		{
			NSLog(@"%s %i", _cmd, (char)(buffers.mBuffers[0].mData + i));
		}

Any ideas?
Oliver Charles
_______________________________________________
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


  • Prev by Date: making a Cocoa view not grab the keyboard
  • Next by Date: setting Input property for USB headsets
  • Previous by thread: making a Cocoa view not grab the keyboard
  • Next by thread: setting Input property for USB headsets
  • Index(es):
    • Date
    • Thread