/**************************************************************

FreqChange is a simple program to change the samples per second,
or frequency, of an 8SVX sound file.  The change is in the header.

This program is Freeware.  It may be freely distributed for private
use or for inclusion in commercial software.  My only requirement is
that my name stay attached to this code (even though it is VERY
simple).

Written by Bruce Twambly       25 Aug 1990

**************************************************************/

#include "stdio.h"
#include "string.h"
#include "stdlib.h"

int tag_vhdr=0x56484452;
int bufsize = 1024;
char *buf;
FILE *input;

extern void cleanup();

main(argc,argv)
int argc;
char *argv[];
{
	int *iptr,pos,freq;
	unsigned short *usptr;
	
	printf("FreqChange - v1.0 - AMIGA 8SVX sound file freq changer\n");
	printf("Copyright %c 1990 by Bruce Twambly\n\n",0xa9);
	
	if (argc>2) {
		printf("Usage: FreqChange <filename>\n");
		exit(0);
	}
	
	if ((buf=malloc(bufsize))==NULL) {
		printf("ERROR: Couldn't allocate buffer memory.\n");
		exit(1);
	}

	if ((input=fopen(argv[1],"r+"))==NULL)
	{
		printf("ERROR: Could not open input file.\n");
		cleanup();
		exit(1);
	}

	fread(buf,1,bufsize,input);
	
	for (pos=0,iptr=(int *)buf ; pos<bufsize ; pos+=4,iptr+=1)
	{
		if (*iptr==tag_vhdr) 
		{
			usptr = (unsigned short *) ((int)iptr+20);
			printf("Current samples per second > %d\n",*usptr);
			printf("Change  to                 > ");
			scanf("%d",&freq);
			*usptr = (unsigned short) freq;
			fseek(input,pos+20,0);
			fwrite((char *) usptr,1,2,input);
			printf("Done.\n");
			cleanup();
			exit(0);
		}
	}
	printf("ERROR: Failed to locate VHDR tag in %d bytes.\n",bufsize);
	cleanup();
	exit(1);
}

void cleanup()
{
	if (input!=NULL) fclose(input);
	if (buf!=NULL) free(buf);
}
