/* enkel deze includes nodig voor soundproductie */
#include "hardware/custom.h"
#include "stdio.h"
/* einde van includes nodig voor soundproductie */

/* andere includes */
#include "exec/types.h"
#include "exec/memory.h"
#include "libraries/dos.h"
#include "libraries/dosextens.h"
#include "intuition/intuition.h"
 

#define INTUITION_REV 31
#define GRAPHICS_REV 31

struct IntuitionBase *IntuitionBase;
struct Window *NoBorder;
struct IntuiMessage *mesg;


 struct FileLock FileLock,*lock, *Lock();
 struct FileInfoBlock FileInfoBlock,*f_info;

struct NewWindow NewNoBorder = {
        0,              /* LeftEdge */
        0,              /* TopEdge  */
        640, 50,     /* Width, Height of this window */
        -1,             /* DetailPen */
        -1,             /* BlockPen */
        MOUSEBUTTONS,   /* IDCMP Flags */
        ACTIVATE|SMART_REFRESH|WINDOWDEPTH|WINDOWDRAG,
                        /* Window Flags:  (see below for more info) */
        NULL,           /* FirstGadget */
        NULL,           /* CheckMark   */
        (UBYTE *)"PlaySound by J. van Houtven. ",           /* Window title */
        NULL,           /* Pointer to Screen if not workbench */
        NULL,           /* Pointer to BitMap if a SUPERBITMAP window */
        0, 0,           /* minimum width, minimum height */
        0,0,            /* maximum width, maximum height */
        WBENCHSCREEN    /* type waarin window opend */
        };

LONG s1,s2, filesize;
int per;
char *filename;
short *sndbuffer, *AllocMem();
VOID OpenALL(), CloseALL(), CleanUp();


main(argc,argv)
int argc;
char *argv[];
{
 LONG i;
 ULONG mclass;


 if(argc>1)
  filename = argv[1];
 else
  {
   puts("Usage : PlaySound filename [period]\nPeriod must be between 124 - 1000, default period = 280 !\n");
   CloseALL();
  }

 if(argc>2)
 {
  per = atoi(argv[2]);
  if(per < 124 || per > 1000) per = 280;
 }
 else
  per=280;

 OpenALL();

 if((NoBorder = (struct Window *) OpenWindow(&NewNoBorder)) == 0)
  CloseALL();


 if((f_info=(struct FileInfoBlock *)AllocMem(sizeof(struct FileInfoBlock),MEMF_CLEAR))==0)
 {
  CloseALL();
 }

 if( (lock=Lock(filename,ACCESS_READ) )==0)
 {
  FreeMem(f_info,sizeof(FileInfoBlock));
  CloseALL();
 }

 if( (Examine(lock,f_info)) ==0)
 {
  FreeMem(f_info,sizeof(FileInfoBlock));
  CloseALL();
 }

 if(f_info->fib_DirEntryType>0)
 {
  puts("Well, well I cann't load a directory, can I !?\n");
  FreeMem(f_info,sizeof(FileInfoBlock));
  CloseALL();
 }

 filesize = f_info->fib_Size;

 printf("filesize = %d\n",filesize);

 FreeMem(f_info,sizeof(FileInfoBlock));
 if(lock)UnLock(lock);


 MakeSound();

 /* ======einde in stijl !====== */

 for(;;)
 {
  while((mesg= (struct IntuiMessage *)
         GetMsg(NoBorder->UserPort))==NULL);
  ReplyMsg(mesg);

  while(mesg=(struct IntuiMessage *)
     GetMsg(NoBorder->UserPort))ReplyMsg(mesg);
  break;
 } /* for */

CleanUp();
exit(TRUE);
}

/*  -------------------------------- einde van main ------------------- */

VOID OpenALL()
{
 /*==Intuition==*/

 IntuitionBase=(struct IntuitionBase *)
            OpenLibrary("intuition.library",INTUITION_REV);

 if(IntuitionBase==NULL)
    CloseALL();

}

VOID CloseALL()
{
 if(NoBorder) CloseWindow(NoBorder);
} 

VOID CleanUp()
{
 custom.dmacon = 0x0003;
 FreeMem(sndbuffer,filesize);
 CloseALL();
}




MakeSound()
{
LoadSound();
PlaySong();
}  

LoadSound()
{
FILE *fopen(), *fp;
SHORT *ptr;
LONG i;

sndbuffer = AllocMem(filesize,MEMF_CHIP);
ptr = sndbuffer;

custom . dmacon = 0x0003;
fp = fopen(filename,"r");
if (fp == NULL) return(0);

s1 = 0; s2 =filesize;
for (i=0; i<s1; i++)
	getc(fp);

for (i=s1/2; i<s2/2; i++)
	{
	*ptr++ = getc(fp)*256 + getc(fp);
	}

fclose (fp);
}

PlaySong()
{
custom . aud[0].ac_ptr = (UWORD *)sndbuffer;
custom . aud[0].ac_len = s2/2-s1/2;
custom . aud[0].ac_per = per;
custom . aud[0].ac_vol = 64;

custom . aud[1].ac_ptr = (UWORD *)sndbuffer;
custom . aud[1].ac_len = s2/2-s1/2;
custom . aud[1].ac_per = per;
custom . aud[1].ac_vol = 64;

custom . dmacon = 0x8203;
}
