Please post the code that you have, or at least the relevant parts of it.
Make sure that you have included 'math.h'
Regards,
Vi
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
//#include<conio.h>
double log10(double num);
FILE *fp;
unsigned char volumeon=0x60;
unsigned char volumeoff=0x00;
void noteoff(unsigned char data1,unsigned long value);
struct mthd_chunk
{
char id[4];
unsigned char Length[4]; /* This will be 6 */
/* Here are the 6 bytes */
unsigned char Format[2];
unsigned char NumTrack[2];
unsigned char Division[2];
};
struct MTRK_CHUNK
{
/* Here's the 8 byte header that all chunks must have */
char ID[4]; /* This will be 'M','T','r','k' */
unsigned long Length; /* This will be the actual size of Data[]
*/
};
void WriteVarLen(unsigned long value)
{
unsigned long buffer;
buffer = value & 0x7F;
while ( (value >>= 7) )
{
buffer <<= 8;
buffer |= ((value & 0x7F) | 0x80);
}
while (1)
{
putc(buffer,fp);
if (buffer & 0x80)
buffer >>= 8;
else
break;
}
}
void noteon(unsigned char data1,unsigned long value)
{
unsigned char data;
WriteVarLen(value);
data=0x90;
fwrite(&data,sizeof(unsigned char),1,fp);
fprintf(fp,"%c",data1);
fwrite(&volumeon,sizeof(unsigned char),1,fp);
noteoff(data1,129);
}
void noteoff(unsigned char data1,unsigned long value)
{
unsigned char data;
WriteVarLen(value);
data=0x80;
fwrite(&data,sizeof(unsigned char),1,fp);
fprintf(fp,"%c",data1);
fwrite(&volumeoff,sizeof(unsigned char),1,fp);
}
int main()
{
int i;
struct mthd_chunk mthd;
struct MTRK_CHUNK mtrk;
unsigned char buffer[4];
char p;
int p2;
//double result,f;
float f,f1,f2,p1;
unsigned long value;
unsigned char data1;
mthd.id[0]=0x4d;
//546864;
mthd.id[1]=0x54;
mthd.id[2]=0x68;
mthd.id[3]=0x64;
mthd.Length[0]=0x00;
mthd.Length[1]=0x00;
mthd.Length[2]=0x00;
mthd.Length[3]=0x06;
mthd.Format[0]=0x00;
mthd.Format[1]=0x00;
mthd.NumTrack[0]=0x00;
mthd.NumTrack[1]=0x01;
mthd.Division[0]=0x00;
mthd.Division[1]=0x80;
mtrk.ID[0]=0x4d;
mtrk.ID[1]=0x54;
mtrk.ID[2]=0x72;
mtrk.ID[3]=0x6b;
mtrk.Length=0x00000014;
fp=fopen("twinkle3.mid","w+");
fwrite(mthd.id, 1, 4, fp);
fwrite(mthd.Length, 1, 4, fp);
fwrite(&mthd.Format,1,2,fp);
fwrite(&mthd.NumTrack,1,2,fp);
fwrite(&mthd.Division,1,2,fp);
fwrite(mtrk.ID, 1, 4, fp);
fwrite(&mtrk.Length, sizeof(unsigned long), 1, fp);
//result=log(f);
for(i=0;i<8;i++)
{
printf("enter value of f\n");
scanf("%f",&f);
f1=f/440.0;
f2=log2f(f1);
p1=69+12*f2;
//printf("\n%lf\n",p1);
p2=(int) p1;
//printf("\n%d\n",p2);
//p=(char) p2;*/
// p='0'+34;
// printf("\n%c\n",p);
noteon(p2,0.5);
}
/*noteon(57.33,0.5); noteon(56.06,0.5);
noteon(62.73,0.5);
noteon(64.24,0.5);
noteon(62.17,0.5);
noteon(58.99,0.5);
noteon(56.89,0.5);noteon(56.69,0.5);*/
buffer[0]=0x00;
buffer[1]=0xff;
buffer[2]=0x2f;
buffer[3]=0x00;
fwrite(buffer, sizeof(char), 4, fp);
fclose(fp);
//printf("%ld",result);
//getch();
}