home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Audio 4.94 - Over 11,000 Files
/
audio-11000.iso
/
msdos
/
sndbords
/
proaudio
/
pcmtlsrc
/
tpcm.arj
/
SNDFILE.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-07-29
|
5KB
|
250 lines
; /*\
; |*| $Author: BCRANE $
; |*|
; |*| $Date: 29 Jul 1992 16:58:06 $
; |*|
; |*| $Header: W:/sccs/sdkapp/sndfile.c_v 1.0 29 Jul 1992 16:58:06 BCRANE $
; |*|
; |*| $Log: W:/sccs/sdkapp/sndfile.c_v $
*
* Rev 1.0 29 Jul 1992 16:58:06 BCRANE
* Initial revision.
; |*|
; |*| $Logfile: W:/sccs/sdkapp/sndfile.c_v $
; |*|
; |*| $Modtimes$
; |*|
; |*| $Revision: 1.0 $
; |*|
; |*| $Workfile: sndfile.c $
; |*|
; \*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <io.h>
#include <sys\types.h>
#include <sys\stat.h>
typedef unsigned char uchar;
typedef unsigned int uint;
typedef unsigned long ulong;
#include "sndfile.h"
#define OKAY 0
#define ERROR -1
#define WAVTYPE 1
#define AVITYPE 2
#define BUFTYPE 99
#define VOCTYPE 98
static char *copyright= "getsndfile() - Copyright (c) 1992. Media Vision, Inc. All Rights Reserved.";
static char *programmer= "Bart Crane";
char *fhoutname;
int fhout;
getsndfile(int fhinp, char *inpname, BT *bt, long samplerate, int channels)
{
ulong br;
switch (getfiletype(fhinp))
{
case AVITYPE:
if (!(fhoutname= tempnam(".", "tpcmtmp")))
break;
if ((fhout= open(fhoutname, O_BINARY| O_WRONLY| O_CREAT| O_TRUNC, S_IWRITE| S_IREAD)) == ERROR)
free(fhoutname);
printf("AVI file: creating %s.\n", fhoutname);
case WAVTYPE:
{
CHUNK ch;
while ((br= read(fhinp, (char *) &ch, sizeof(CHUNK))) == sizeof(CHUNK))
{
if (idequals(ch.id, 'R', 'I', 'F', 'F'))
{
lseek(fhinp, 4L, SEEK_CUR);
continue;
}
if (idequals(ch.id, 'W', 'A', 'V', 'E'))
{
lseek(fhinp, -4L, SEEK_CUR);
continue;
}
if (idequals(ch.id, 'n', 'a', 'm', 'e'))
{
lseek(fhinp, ch.size, SEEK_CUR);
continue;
}
if (idequals(ch.id, 'f', 'm', 't', ' '))
{
read(fhinp, (char *) &bt->wf, sizeof(WF));
lseek(fhinp, ch.size- sizeof(WF), SEEK_CUR);
continue;
}
if (idequals(ch.id, 'd', 'a', 't', 'a'))
{
bt->fhidx= fhinp;
bt->foffset= lseek(fhinp, 0L, SEEK_CUR);
bt->offset= 0;
bt->size= ch.size;
break;
}
if (idequals(ch.id, 'L', 'I', 'S', 'T'))
{
continue;
}
if (idequals(ch.id, 'm', 'o', 'v', 'i'))
{
lseek(fhinp, -4L, SEEK_CUR);
continue;
}
if (idequals(ch.id, 'r', 'e', 'c', ' '))
{
lseek(fhinp, -4L, SEEK_CUR);
continue;
}
if (idequals(ch.id, '0', '0', 'w', 'b'))
{
char *buf= calloc(ch.size, sizeof(char));
if (buf)
{
read(fhinp, buf, ch.size);
write(fhout, buf, ch.size);
free(buf);
}
continue;
}
if (idequals(ch.id, 's', 't', 'r', 'l'))
{
lseek(fhinp, -4L, SEEK_CUR);
continue;
}
if (idequals(ch.id, 's', 't', 'r', 'h'))
{
lseek(fhinp, ch.size, SEEK_CUR);
continue;
}
if (idequals(ch.id, 's', 't', 'r', 'f'))
{
lseek(fhinp, ch.size, SEEK_CUR);
continue;
}
lseek(fhinp, ch.size, SEEK_CUR);
}
}
break;
case VOCTYPE:
case BUFTYPE:
default:
{
bt->fhidx= fhinp;
bt->foffset= lseek(fhinp, 0L, SEEK_CUR);
bt->offset= 0;
bt->size= lseek(fhinp, 0L, SEEK_END);
bt->wf.fmttag= 1;
bt->wf.channels= channels;
bt->wf.samplerate= samplerate;
bt->wf.bytespersec= bt->wf.channels* bt->wf.samplerate;
bt->wf.blockalign= bt->wf.channels;
bt->wf.datasize= 8;
{
int i;
char *n= inpname;
for (i= 0; i < 8; i++)
if (!(isxdigit(n[i])))
break;
if (i == 8 && n[8] == '.' && n[11] == '\0')
{
bt->wf.samplerate= (ulong) isdigit(n[4] ? n[4]- '0' : (n[4]& 0xDF)- 'A'+ 10)* 0x1000;
bt->wf.samplerate+= (ulong) isdigit(n[5] ? n[5]- '0' : (n[5]& 0xDF)- 'A'+ 10)* 0x0100;
bt->wf.samplerate+= (ulong) isdigit(n[6] ? n[6]- '0' : (n[6]& 0xDF)- 'A'+ 10)* 0x0010;
bt->wf.samplerate+= (ulong) isdigit(n[7] ? n[7]- '0' : (n[7]& 0xDF)- 'A'+ 10)* 0x0001;
}
}
}
break;
}
if (fhoutname)
free(fhoutname);
if (fhout)
close(fhout);
return(OKAY);
}
static char cvfstr[]= "Creative Voice File";
getfiletype(int fhinp)
{
union header
{
struct riffhdr
{
CHUNK ch;
char class[4];
} rh;
char voc[19];
} typbuf;
checkriff:
lseek(fhinp, 0L, SEEK_SET);
clrbuf((char *) &typbuf, sizeof(typbuf.rh));
if ((read(fhinp, (char *) &typbuf, sizeof(typbuf.rh))) != sizeof(typbuf.rh))
goto checkvoc;
if (idequals(typbuf.rh.ch.id, 'R', 'I', 'F', 'F'))
{
if (idequals(typbuf.rh.class, 'W', 'A', 'V', 'E'))
return(WAVTYPE);
else
if (idequals(typbuf.rh.class, 'A', 'V', 'I', ' '))
return(AVITYPE);
else
return(BUFTYPE);
}
checkvoc:
lseek(fhinp, 0L, SEEK_SET);
clrbuf((char *) &typbuf, sizeof(typbuf.voc));
if ((read(fhinp, (char *) &typbuf, sizeof(typbuf.voc))) != sizeof(typbuf.voc))
goto checkbuf;
{
int i;
for (i= 0; i < sizeof(typbuf.voc); i++)
if (cvfstr[i] != typbuf.voc[i])
break;
if (i == sizeof(typbuf.voc))
return(VOCTYPE);
}
checkbuf:
lseek(fhinp, 0L, SEEK_SET);
clrbuf((char *) &typbuf, sizeof(typbuf));
return(BUFTYPE);
}
clrbuf(char *buf, uint size)
{
while (size--)
*buf++= '\0';
}