Re: Getting raw audio data from an AIFF file
Re: Getting raw audio data from an AIFF file
- Subject: Re: Getting raw audio data from an AIFF file
- From: Peter <email@hidden>
- Date: Mon, 17 Feb 2003 10:47:07 -0600
Hello everyone,
I'm about to check out the AudioFile API but would like clarification
before I abandon where I am. I've been working on using file calls and
wanted to ask about reading in the headers. I'm just beginning so
robustness and coolness are not adjectives that apply to this code.
I'm using an AIFC copied straight from a CD to the Finder. No
importing has been done.
OS 10.2.4 and Project Builder Version 2.1 (December 2002 Developer
Tools)
Newest Core
I want to skip all headers after the FORM chunk is read and go to the
SSND chunk so I can start playing the audio. While my chunkID is
'SSND' (a four char array), I print it out and get 'SSND\277\260'
instead. Why would there be more than the four chars printing out?
This is a problem because my chunk skipping routine doesn't know when
to stop. You can see that I stop now by doing four char comparisons.
Ugh.
Here's some _optional_ weirdness that I have; it involves the debugger.
See that AIFC type check? When I run my program, strcmp evaluate to
true but when I debug, that if statement evaluates to true, and the
program returns an error. It's commented out here so I can get down to
the other code while I debug. It's weird that two different actions
occur when running or debugging without a code modification. Since I
have no idea what to do, I tried swapping "#import
<Foundation/Foundation.h>" with "#include <string.h>" and had nothing
change.
I've been looking at The Omni Group's SoundExample for reference and
changing things to chars and ints instead of everything being unsigned
ints. I can understand my code better. No bit-shifting or "and-ing"
is involved. Any other raw code examples pop into your mind?
I appreciate any help. It would be great if it was a "Wow, I made a
stupid-newbie mistake." type of problem.
Peter
code follows...
/***header file***/
#include <stdio.h>
#import <Foundation/Foundation.h>
typedef struct {
char groupID[4];
unsigned long fileSize;
char fileType[4];
} iffChunk;
typedef struct {
char chunkID[4];
unsigned long chunkSize;
} anyChunk;
typedef struct {
unsigned long offset;
unsigned long blockSize;
char soundData[];
} soundDataChunk;
/***end header file***/
/***implementation file***/
#include "AIFFReader.h"
int main (int argc, const char * argv[]) {
iffChunk form;
anyChunk chunk;
soundDataChunk soundData;
FILE* soundFile;
char* pathToStr = "/Volumes/My
Precious/Users/gappin/Desktop/Monica.aifc";
soundFile = fopen(pathToStr, "r");
if ( !soundFile ) {
perror( pathToStr );
return 1;
}
// groupID
fread( &form.groupID, sizeof(form.groupID), 1, soundFile );
printf( "group ID:'%s'\n", form.groupID );
// comparison operators work for integers but not strings
if ( strcmp( form.groupID, "FORM" ) ) {
printf( "File is not a IFF file.\n" ); fflush( stdout );
return 1;
}
// fileSize
fread(&form.fileSize, sizeof(form.fileSize), 1, soundFile);
printf("file size:'%lX'\n", form.fileSize);
// fileType
fread(&form.fileType, sizeof(form.fileType), 1, soundFile);
printf("groupID:'%s'\n", form.fileType);
/* if ( strcmp( form.fileType, "AIFC" ) ) {
printf( "File is not a AIFC file.\n\n" ); fflush( stdout );
return 1;
}
*/
// skip ahead to the SSND chunk
while ( 1 ) {
fread( &chunk.chunkID, sizeof(chunk.chunkID), 1, soundFile );
fread( &chunk.chunkSize, sizeof(chunk.chunkSize), 1, soundFile
);
// if ( strcmp( chunk.chunkID, "SSND" ) == 0 )
if ( chunk.chunkID[0] == 'S' && chunk.chunkID[1] == 'S' &&
chunk.chunkID[2] == 'N' && chunk.chunkID[3] == 'D')
{
break;
}
printf( "skipping chunk: '%s'\tsize: '%lX'\n", chunk.chunkID,
chunk.chunkSize );
// skip the chunk data
// SEEK_CUR is the current place in file
fseek( soundFile, chunk.chunkSize, SEEK_CUR);
}
printf( "chunk:%s\tsize:%lX", chunk.chunkID, chunk.chunkSize );
//deal with SSND here...
fclose(soundFile);
return 0;
}
/***end implementation file***/
On Thursday, February 13, 2003, at 06:54 AM, Brian Willoughby wrote:
Ed Friese <email@hidden> asks:
[ I was under the impression that the AudioFile stuff in the
[ AudioToolkit framework was the right thing to use for this kind
[ of thing. Is that not the case?
AudioToolkit/AudioFile is just one of many options that has been
mentioned in
this thread. It is by no means *the right thing* to use.
Particularly when
you consider that AudioFile is a C function based API, compared to the
flagship
Cocoa, which is Objective C and message based.
SndKit will probably fit best within a proper Cocoa application. But
it has
been pointed out that you can roll your own AIFF support using
NSFileHandle -
you could even use standard Unix FILE* library calls (as I have). It
all
depends upon how much you trust the support of the framework you
select vs. how
well you think you can do with your own AIFF parsing.
The AppKit NSSound is probably based on AudioFile - if it isn't yet,
then it
should be. SndKit could be based on AudioFile (except that AudioFile
is newer
than SndKit), unless AudioFile is as limited in its support of AIFF
variations
as NSSound is.
Brian Willoughby
Sound Consulting
On Wednesday, February 12, 2003, at 04:55 PM, Brian Willoughby wrote:
Fortunately, ever since NeXT let the SoundKit go to the Stanford Music
Department (I believe that was the path it took), expert programmers
have been maintaining the original SoundKit code in the renamed
SndKit, which is distributed as part of the MusicKit. Check out
MusicKit.org - a good starting place might be
http://www.musickit.org/MusicKitConcepts/thesndkit.html for your
particular interests.
The two options above are the most Objective-C-friendly choices I am
aware of at this time. I would recommend the SndKit "Snd" object over
hacking too deeply into NSSound. You should find that the SndKit is
as easy to use as Cocoa, due to its object oriented design, as opposed
to QuickTime or other old-school standard C libraries.
_______________________________________________
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.