Hello,
While compiling my code, I'm receiving a "Data access misaligned address violation" error. I tried to find the reason for the error, but am unable to pin point the location. I am trying to manipulate the amplitude envelope of an audio file, but this error occurs when i'm trying to multiply an array with float datatype with an array of short datatype. My code is as follows.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
FILE *fr, *fw;
short *buf, *buf1, buffer[110516];
float mul[55257];
int j;
if((fr=fopen("/var/tmp/test2.wav", "rb"))==NULL)
{
printf("\nCannot open wav file\n");
}
if((fw=fopen("/var/tmp/tmsg2.wav", "wb"))==NULL)
{
printf("\nCannot open tmsg2 file-to write\n");
}
mul[0]=0;
for(j=1;j<55257;j++)
{
mul[j]=mul[j-1]+0.00002; //generate a linearly increasing array
}
buf=(short *)malloc(sizeof(short));
for(j=0;j<110514;j++)
{
fread(buf, sizeof(short), (size_t)1, fr);
buffer[j]=*buf; //create a buffer with amplitude values samples
}
free(buf);
for(j=0; j<55257; j++)
{
buffer[j]=buffer[j]*mul[j]; //manipulate amplitude so that the volume increases gradually
}
buf1=&buffer[0];
for(j=0;j<110514;j++)
{
fwrite(buf1+j, sizeof(short), (size_t)1, fw);
}
fclose(fr);
fclose(fw);
return 0;
}