ftp.nice.ch/pub/next/unix/audio/sndutil.1.3.s.tar.gz#/sndutil-1.3/sndtools/sndrealloc.c

This is sndrealloc.c in view mode; [Download] [Up]

/* Test Mike Minnick's sndRealloc() function */

#import <stdio.h>
#import <sound/sound.h>
#import <math.h>
#import <mach/mach_error.h>

#define AMP 32768
#define PI M_PI

void CheckError(int err)
{
    if (err != 0) printf("err=%d\n", err);
}

SNDSoundStruct *sndRealloc(SNDSoundStruct *Osound, int NewDataSize)
{
    SNDSoundStruct *TmpSound ;
    int OldSize = Osound->dataSize + Osound->dataLocation;
    int error ;
    
    /*
     * Allocate memory for the new sound.
     */
    error = vm_allocate(task_self(),(vm_address_t *)&TmpSound, NewDataSize, 1);
    
    if ( error != KERN_SUCCESS ) {
        mach_error("vm_allocate returned value of: ", error);
        exit(1);
    }
    
    /*
     * Round up old size to next highest page multiple.
     */
    if (OldSize & (vm_page_size-1))
        OldSize = (OldSize + vm_page_size) & ~(vm_page_size-1);
    
    /*
     * Copy the old sound to the new, including the header.
     */
    error = vm_copy(task_self(), 
                    (vm_address_t) Osound, 
                    (vm_size_t) OldSize, 
                    (vm_address_t) TmpSound) ;
    
    if ( error != KERN_SUCCESS ) {
        mach_error("vm_copy returned value of: ", error);
        exit(1);
    }
    
    /*
     * Set the new sound's data size to the new size.
     */
    TmpSound->dataSize = NewDataSize;
    
    /*
     * Free the old sound's memory.
     */
    SNDFree(Osound);
    
    return(TmpSound);
}


void main(int argc, char *argv[])
{
    SNDSoundStruct *NewSound ;  /* sound structures */
    short *SoundBuffer, *StartSoundBuffer ;
    int SRate = 44100, dataFormat = 3, dataSize, channels = 1, infoSize = 4;
    int DurationInSamples, BytesPerSample, i = 0 ;
    float omega ;
    float Duration ;
    
    if (argc != 2) {
	fprintf(stderr,
		"%s - Test sndRealloc() function for changing sound size in place.\n"
		"\nUsage: %s <duration> \n"
		"     A sine wave at requested duration is generated and played,\n"
		"     Then its duration is doubled and played again, with the second"
		"     half being an octave higher.\n", argv[0], argv[0]);
	exit(1);
    }
    
    Duration = atof(argv[1]) ; 
    dataSize = Duration * SRate * sizeof(short);
    CheckError(SNDAlloc(&NewSound, dataSize, dataFormat, SRate, 
                        channels,infoSize));
    CheckError(SNDGetDataPointer(NewSound, (char **)&SoundBuffer,
				 &DurationInSamples, &BytesPerSample));
    
    StartSoundBuffer = SoundBuffer ;
    omega = 2 * PI * 440 / SRate ; 
    fprintf(stderr,"DurationInSamples = %d\n", DurationInSamples);
    while ( DurationInSamples-- )
        *SoundBuffer++ = AMP * sin( omega * i++ ) ;
    
    CheckError(SNDStartPlaying(NewSound,1,0,0,0,0));
    CheckError(SNDWait(1));
    
    dataSize = 2 * Duration * SRate * sizeof(short);
    
    NewSound = sndRealloc(NewSound, dataSize);
    
    CheckError(SNDGetDataPointer(NewSound, (char **)&SoundBuffer,
                                 &DurationInSamples, &BytesPerSample));
    fprintf(stderr,"DurationInSamples = %d\n", DurationInSamples);
    
    i = 0 ; 
    
    DurationInSamples /= 2 ;
    SoundBuffer += DurationInSamples ;
    omega = 2 * PI * 880 / SRate ;
    
    while ( DurationInSamples-- )
        *SoundBuffer++ = AMP * sin( omega * i++ ) ;
    
    CheckError(SNDStartPlaying(NewSound,1,0,0,0,0));
    CheckError(SNDWait(1));
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.