/***********************************************************************/
/*       putsamp.c                                 August 3, 1986      */
/*                                                                     */
/* Writes a FORM 8SVX given a 8SVX header and the data to write.       */
/* See inst2iff.c for an example call.                                 */
/* Closely modeled after putpict.c from the IFF disk (Fish Disk 16).   */
/* This routine calls several subs in iffw.c, which can also be found  */
/* on Fish 16.  Iff.h is also there.                                   */
/*                                                                     */
/* This program is unconditionally placed in the public domain.  Feel  */
/* free to use code from it for anything you want (even for commercial */
/* purposes).  However, don't just sell this program.  Make some       */
/* improvements first.  Let's see some music programs out there!!!     */
/*                                                                     */
/* Author:  Bobby Deen                                                 */
/*          2506 Morning Glory                                         */
/*          Richardson, Tx. 75081                                      */
/*          (214) 235-4391  (voice)                                    */
/* Send Electronic Mail to:                                            */
/*          Rising Star BBS @ (214) 231-1372 (Echomail)                */
/*       or Amiga Scope BBS @ (214) 288-1537                           */
/***********************************************************************/


#include "iff.h"
#include "iff_8svx.h"

static IFFP ifferror = 0;

#define CkErr(expression)  {if (ifferror == IFF_OKAY) ifferror = (expression);}
    
/**************************************************************************/
/* okay = PutSamp(...);                                                   */
/*                                                                        */
/* Put a sampled sound into an 8SVX IFF file.  Returns 0 if everything    */
/* went ok, non-zero otherwise.                                           */
/*                                                                        */
/**************************************************************************/

int
  PutSamp (file, v8hdr, name, copyright, author, annotation, body, bodylen)

LONG file;
struct Voice8Header *v8hdr;
char *name, *copyright, *author, *annotation;
BYTE *body;
int bodylen;
{
GroupContext fileContext, formContext;

CkErr(OpenWIFF(file, &fileContext, szNotYetKnown) );
CkErr(
    StartWGroup(&fileContext, FORM, szNotYetKnown, ID_8SVX, &formContext));

CkErr(PutCk(&formContext, ID_VHDR, sizeof(*v8hdr), (BYTE *)v8hdr));

if (name) {
   CkErr(PutCkHdr(&formContext, ID_NAME, szNotYetKnown));
   CkErr(IFFWriteBytes(&formContext, name, strlen(name)));
   CkErr(PutCkEnd(&formContext));
}

if (copyright) {
   CkErr(PutCkHdr(&formContext, ID_Copyright, szNotYetKnown));
   CkErr(IFFWriteBytes(&formContext, copyright, strlen(copyright)));
   CkErr(PutCkEnd(&formContext));
}

if (author) {
   CkErr(PutCkHdr(&formContext, ID_AUTH, szNotYetKnown));
   CkErr(IFFWriteBytes(&formContext, author, strlen(author)));
   CkErr(PutCkEnd(&formContext));
}

if (annotation) {
   CkErr(PutCkHdr(&formContext, ID_ANNO, szNotYetKnown));
   CkErr(IFFWriteBytes(&formContext, annotation, strlen(annotation)));
   CkErr(PutCkEnd(&formContext));
}

CkErr(PutCk(&formContext, ID_BODY, bodylen, body));

CkErr(EndWGroup(&formContext) );
CkErr(CloseWGroup(&fileContext) );

return(ifferror != IFF_OKAY);
}

/**************************************************************************/
/* IffErr                                                                 */
/*                                                                        */
/* Returns text corresponding to the iff error code and resets it to zero */
/*                                                                        */
/**************************************************************************/
char *IffErr()
{
char *c;

/* Message strings for IFFP codes. */
static char MsgOkay[]        = "(IFF_OKAY) No FORM 8SVX in the file." ;
static char MsgEndMark[]     = "(END_MARK) How did you get this message?" ;
static char MsgDone[]        = "(IFF_DONE) All done.";
static char MsgDos[]         = "(DOS_ERROR) The DOS returned an error." ;
static char MsgNot[]         = "(NOT_IFF) Not an IFF file." ;
static char MsgNoFile[]      = "(NO_FILE) No such file found." ;
static char MsgClientError[] = "(CLIENT_ERROR) PutSamp bug or insufficient RAM.";
static char MsgForm[]        = "(BAD_FORM) A malformed FORM 8SVX." ;
static char MsgShort[]       = "(SHORT_CHUNK) A malformed FORM 8SVX." ;
static char MsgBad[]         = "(BAD_IFF) A mangled IFF file." ;

/* THESE MUST APPEAR IN RIGHT ORDER!! */
static char *IFFPMessages[-LAST_ERROR+1] = {
   /*IFF_OKAY*/  MsgOkay,
   /*END_MARK*/  MsgEndMark,
   /*IFF_DONE*/  MsgDone,
   /*DOS_ERROR*/ MsgDos,
   /*NOT_IFF*/   MsgNot,
   /*NO_FILE*/   MsgNoFile,
   /*CLIENT_ERROR*/ MsgClientError,
   /*BAD_FORM*/  MsgForm,
   /*SHORT_CHUNK*/  MsgShort,
   /*BAD_IFF*/   MsgBad
};

c = IFFPMessages[-ifferror];
ifferror = 0;
return(c);
}

