_source/ULAW.c

/*
    File:       ULAW.c
 
    Contains:   Routine demonstrating how to parse Sun's .au sound files.
 
    Written by: Mark Cookson    
 
    Copyright:  Copyright © 1996-1999 by Apple Computer, Inc., All Rights Reserved.
 
                You may incorporate this Apple sample source code into your program(s) without
                restriction. This Apple sample source code has been provided "AS IS" and the
                responsibility for its operation is yours. You are not permitted to redistribute
                this Apple sample source code as "Apple sample source code" after having made
                changes. If you're going to re-distribute the source, we require that you make
                it clear in the source that the code was descended from Apple sample source
                code, but that you've made changes.
 
    Change History (most recent first):
                8/31/1999   Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
                
 
*/
 
#include "ULAW.h"
 
/* I threw this together in about an hour after just looking at the structure
   of some .au files that I made from some known AIFF files.  This could use
   some work to get it to deal with formats that I have no idea about, but
   since I have no idea about them, I can't do that...
*/
 
/*-----------------------------------------------------------------------*/
        OSErr   ASoundGetULAWHeader     (SoundInfoPtr theSoundInfo,
                                        long *dataStart,
                                        long *length)
/*-----------------------------------------------------------------------*/
{
    ParamBlockRec           pb;
    auHeader                ULAWHeader;
    long                    filePosition        = kInit,
                            byteCount           = kInit,
                            sampleSize          = kInit,
                            tempLength          = kInit;
    Fixed                   sampleRate          = kInit;
    OSErr                   err                 = noErr;
 
    *dataStart = kInit;
 
    err = SetFPos (theSoundInfo->refNum, fsFromStart, filePosition);
    if (err != noErr) {
        DebugPrint ("\pSetFPos failed!");
    }
    else {
        byteCount = sizeof (auHeader);
        err = FSRead (theSoundInfo->refNum, &byteCount, &ULAWHeader);
        if ((err != noErr) && (err != eofErr)) {
            DebugPrint ("\pFSRead failed!");
        }
        else {
            sampleRate = ASoundLongDoubleToFix (ULAWHeader.sampleRate);
            *dataStart = ULAWHeader.offsetToData;
            *length = ULAWHeader.dataSize;
 
            /* Get the length of the file because SoundEdit stores the uncompressed length of a file
               and not all Sun/NeXT files have the compressed length of the file in the dataSize
               field.  We depend on having the right length (the compressed length). */
 
            pb.ioParam.ioCompletion = nil;
            pb.ioParam.ioRefNum     = theSoundInfo->refNum;
            err = PBGetEOF (&pb, false);
            err = pb.ioParam.ioResult;
            if (err == noErr) {
                tempLength = (long)pb.ioParam.ioMisc;
            }
            if (*length > tempLength || *length == -1) {    /* This would mean that the uncompressed length was stored || no length was stored */
                *length = tempLength - ULAWHeader.offsetToData; /* We want the compressed length */
            }
 
            switch (ULAWHeader.dataFormat) {
                case SND_FORMAT_MULAW_8:
                    err = SetupDBHeader (theSoundInfo,
                                        sampleRate,
                                        k16BitSample,
                                        ULAWHeader.numChannels,
                                        fixedCompression,
                                        kULawCompression);
                    theSoundInfo->needsMasking = false;
                    break;
                case SND_FORMAT_LINEAR_8:
                    err = SetupDBHeader (theSoundInfo,
                                        sampleRate,
                                        k8BitSample,
                                        ULAWHeader.numChannels,
                                        notCompressed,
                                        NoneType);
                    theSoundInfo->needsMasking = true;
                    break;
                case SND_FORMAT_LINEAR_16:
                    err = SetupDBHeader (theSoundInfo,
                                        sampleRate,
                                        k16BitSample,
                                        ULAWHeader.numChannels,
                                        notCompressed,
                                        NoneType);
                    theSoundInfo->needsMasking = false;
                    break;
                default:
                    DebugPrint ("\pUnknown or unplayable encoding format");
                    err = kUnknownFormat;
                    break;
            }
        }
    }
 
    if (err != noErr) {
        DebugPrint ("\pError in ASoundGetULAWHeader");
    }
 
    *length += *dataStart;  /* Otherwise we wouldn't read the last few bytes from the end of the sound. */
 
    return err;
}