ftp.nice.ch/pub/next/unix/communication/am.1.16.NIHS.bs.tar.gz#/am.1.16.NIHS.bs/src/snd3zyxel.c

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

/* This is snd3zyxel

   NeXT snd to ADPCM3 Zyxel-voice file converter

   Convert (stdio) sound-files to zyxel (stdout).

   Copyright 1994 (C) Thomas Funke.
   
   Changed to ADPCM3 by kiwi@belly. Based on vcnvt.c by ZyXEL Corp.
*/   

#import <sound/sound.h>
#import <sound/convertsound.h>
#import "precomp.h"

void Adaptive(char Data);

int Delta = 5;
int EstMax = 0;
int M[] = {0x399A, 0x3A9F, 0x4D14, 0x6607};


typedef unsigned char uchar;

/* by kiwi@belly */
void lin16_to_ad3 (short a)					//convert 16bit value to 3bit
{
	char TmpCompBit;
	char Data =0;
	static int Rdata = 0;
	static char Rcnt = 8;

    Rdata  &= 0xff00 ;

	if ( a -= EstMax )  {
			TmpCompBit = 4 ;
			/* ----------------------------------------------------- */
			/* If the data is negative, set flag and change the sign */
			/* ----------------------------------------------------- */
			if ( a < 0 ) {
					a = -a ;
					Data = TmpCompBit ;
			}
			/* --------------------------------------------------- */
			/* Quantize the waveform data, delta value is adaptive */
			/* --------------------------------------------------- */
			while ( ((a-=Delta)>0) && --TmpCompBit )
					Data += 1 ;
			/* ---------------------------- */
			/* Rdata is the compressed data */
			/* ---------------------------- */
			Rdata |= ( Data << 5 ) ;
	}
	/* ------------------------------------------------------ */
	/* check if the compressed data can be pack into one byte */
	/* ------------------------------------------------------ */
	TmpCompBit = 3 ;
	while ( TmpCompBit-- )     {
			Rdata <<= 1 ;
			if ( !(--Rcnt) )    {
                    putc(Rdata>>8,stdout) ;
					if (((Rdata>>8) & 255 )== 0x10)
	    			putc (0x10, stdout); // double DLE 
					Rcnt = 8 ;
			}
	}
	/* -------------------------------------- */
	/* Adaptive the Delta and Estimat Maximum */
	/* -------------------------------------- */
	Adaptive(Data);
}


void Adaptive(char DataBit)
{
int     TmpMax ;
char    SignBit ;
long    TmpDelta ;
long t0;

	/******* This part is modified for the Firmware > 6.10 **************/
	/**/    t0 = (long)EstMax ;      	                              /**/
	/**/    t0 *= 3973 ;                                              /**/
	/**/    t0 += 2048 ;                                              /**/
	/**/    t0 /= 4096 ;                                              /**/
	/**/    EstMax = (int)t0 ;                      		          /**/
	/********************************************************************/

        SignBit = DataBit & 4 ;
        DataBit &= ~(4) ;
        if ( (Delta&1) && !SignBit )
                ++EstMax ;

        /* ------------------- */
        /* Calculate the Delta */
        /* ------------------- */
        TmpDelta = Delta ;
        TmpDelta *= M[(int)DataBit] ;
        TmpDelta += 8192 ;
        TmpDelta >>= 14 ;

        /* -------------------- */
        /* Calculate the EstMax */
        /* -------------------- */
        TmpMax  = (Delta>>1) ;
        while ( DataBit-- )
                TmpMax += Delta ;
        if ( SignBit )
                EstMax -= TmpMax ;
        else
                EstMax += TmpMax ;
        Delta = (int)TmpDelta ;
}


void write_zyxel (void)
// write to stdout zyxel-data
{
    struct zyxel header = {0};
    short *p;
    char *pend;
    int err;
    

    SNDSoundStruct *sndp1, *sndp2;
    SNDSoundStruct snd;
    

    if ((err=SNDRead (0, &sndp1)) != SND_ERR_NONE) {
	fprintf (stderr, "Error reading sound file: %s\n",
	    SNDSoundError(err));
	exit (1);
    }
    

    // Convert Sound to 9600 / lin / mono
    

    snd.magic = SND_MAGIC;
    snd.dataLocation = sizeof (SNDSoundStruct);
    snd.dataSize = 0;
    snd.dataFormat = SND_FORMAT_LINEAR_16;
    snd.samplingRate = 9600;
    snd.channelCount = 1;
    sndp2 = &snd;
    

    if ((err=SNDConvertSound (sndp1, &sndp2)) != SND_ERR_NONE) {
	fprintf (stderr, "Error converting sound: %s\n", 

	    SNDSoundError(err));
	exit (1);
    }
    

    

    // Write Zyxel header
    strncpy ((char*)&header.title, "ZyXEL", sizeof(header.title));
    header.mode = ZYX_VOC;
    header.voice = ZYX_AD3; // 3 bit ADPCM
    fwrite (&header, sizeof(header), 1, stdout);
    


    // start loop 

    p = (void *) sndp2 + sndp2->dataLocation;
    pend = (void *) sndp2 + sndp2->dataSize - 6;

    while ((char *) p < pend) {
	short in[1];
        

	in[0] = *p++;
	lin16_to_ad3 (in[0]);
    }

}

int main ()
{
    write_zyxel ();
    return 0;
}

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