Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Fade in Fade out

Nok79Nok79 Posts: 12Member
Please can someone help me I had to write a program to open a .wav file then to fade_in fade_out the wave file, but when the user is asked to input the fade_in fade_out times and play back the .wav file, there is no fade_in fade_out but silence?? can some body help me.


/* Audio_Effect.c - this program opens a wave file then attemps a fade_in fade_out */


#include
#include




unsigned char x,riff[4],lp[4],wave[4],f[4],fl[4],af[2],
cn[2],sr[4],bpsec[4],bpsamp[2],bits_samp[2],asc_data[4],
wl[4];

float scalef;

int i,p1a,p1b,p1c,p1d,p1e,p1f,p1g,p1h,s1,s2,s3,s4,s5,s6,s7,s8,bpsec1,bpsec2,bpsec3,bpsec4,
bpsec5,bpsec6,bpsec7,bpsec8,bytpsamp1,bytpsamp2,bytpsamp3,bytpsamp4,
bitsamp1,bitsamp2,bitsamp3,bitsamp4,w1,w2,w3,w4,w5,w6,w7,w8,fla,flb,flc,
fld,fle,flf,flg,flh,fduration;

long unsigned int total_package_length,total_format_length,total_sample_rate,total_bytes_sec,
total_bytes_samp,total_wave_length,total_bits_samp,fstart,fstop,time,fadestart_bytes, fadestop_bytes;




void main()


{

FILE *fptr,*fptw;
i=0;
fptr=fopen("8bit.wav","rb");
if(fptr==NULL)
{
printf("Cannot locate file

");
exit(1);
}
fptw=fopen("trialout.wav","wb");
if(fptw==NULL)
{
printf("Cannot open file

");
fclose(fptr);
exit(1);

}

for (i=0;i<=3;++i)

{
/* This is the RIFF chunk */
fscanf(fptr,"%c",&riff[i]);
printf("%x

",riff[i]);
fprintf(fptw,"%c",riff[i]);

}

for (i=0;i<=3;++i)

{
fscanf(fptr,"%c",&lp[i]);
printf("%x

",lp[i]);
fprintf(fptw,"%c",lp[i]);
}
p1a=(lp[0]&0x0F); /* Package_Length */
p1b=(lp[0]&0xF0);
p1c=(lp[1]&0x0F)<<8;
p1d=(lp[1]&0xF0)<<8;
p1e=(lp[2]&0x0F)<<16;
p1f=(lp[2]&0xF0)<<16;
p1g=(lp[3]&0x0F)<<24;
p1h=(lp[3]&0xF0)<<24;
total_package_length = p1a + p1b + p1c + p1d + p1e + p1f + p1g + p1h;
printf("Total package length is

", total_package_length);

for (i=0;i<=3;++i)

{
fscanf(fptr,"%c",&wave[i]);
printf("%x

",wave[i]);
fprintf(fptw,"%c",wave[i]);
}
/* This is the format chunk */

for (i=0;i<=3;++i)

{
fscanf(fptr,"%c",&f[i]);
printf("%x

",f[i]);
fprintf(fptw,"%c",f[i]);
}

for (i=0;i<=3;++i)

{
fscanf(fptr,"%c",&fl[i]); /* format length */
printf("%x

",fl[i]);
fprintf(fptw,"%c",fl[i]);

}
fla=(fl[0]&0x0F);
flb=(fl[0]&0xF0);
flc=(fl[1]&0x0F)<<8;
fld=(fl[1]&0xF0)<<8;
fle=(fl[2]&0x0F)<<16;
flf=(fl[2]&0xF0)<<16;
flg=(fl[3]&0x0F)<<24;
flh=(fl[3]&0xF0)<<24;
total_format_length = fla + flb + flc + fld + fle + flf + flg + flh;
printf("Total format length is

",total_format_length);


for (i=0;i<=1;++i)

{
fscanf(fptr,"%c",&af[i]);
printf("%x

",af[i]);
fprintf(fptw,"%c",af[i]);
}

for (i=0;i<=1;++i)

{
fscanf(fptr,"%c",&cn[i]); /* channel number */
printf("%x

",cn[i]);
fprintf(fptw,"%c",cn[i]);
}

for (i=0; i<=3; ++i)

{
fscanf(fptr,"%c",&sr[i]); /* sample rate */
printf("%x

",sr[i]);
fprintf(fptw,"%c",sr[i]);
}

s1=(sr[0]&0x0F);
s2=(sr[0]&0xF0);
s3=(sr[1]&0x0F)<<8;
s4=(sr[1]&0xF0)<<8;
s5=(sr[2]&0x0F)<<16;
s6=(sr[2]&0xF0)<<16;
s7=(sr[3]&0x0F)<<24;
s8=(sr[3]&0xF0)<<24;
total_sample_rate = s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8;
printf("Sample rate is

",total_sample_rate);

for (i=0; i<=3; ++i)

{
fscanf(fptr,"%c",&bpsec[i]); /* bytes per second */
printf("%x

",bpsec[i]);
fprintf(fptw,"%c",bpsec[i]);
}

bpsec1=(bpsec[0]&0x0F);
bpsec2=(bpsec[0]&0xF0);
bpsec3=(bpsec[1]&0x0F)<<8;
bpsec4=(bpsec[1]&0xF0)<<8;
bpsec5=(bpsec[2]&0x0F)<<16;
bpsec6=(bpsec[2]&0xF0)<<16;
bpsec7=(bpsec[3]&0x0F)<<24;
bpsec8=(bpsec[3]&0xF0)<<24;
total_bytes_sec = bpsec1 + bpsec2 + bpsec3 + bpsec4 + bpsec5 + bpsec6 + bpsec7 + bpsec8;
printf("total bytes per sec is

",total_bytes_sec);

for (i=0; i<=1; ++i)

{
fscanf(fptr,"%c",&bpsamp[i]); /* bytes per sample */
printf("%x

",bpsamp[i]);
fprintf(fptw,"%c",bpsamp[i]);
}

bytpsamp1=(bpsamp[0]&0x0F);
bytpsamp2=(bpsamp[0]&0xF0);
bytpsamp3=(bpsamp[1]&0x0F)<<8;
bytpsamp4=(bpsamp[1]&0xF0)<<8;
total_bytes_samp = bytpsamp1 + bytpsamp2 + bytpsamp3 + bytpsamp4;
printf("total bytes per sample is

",total_bytes_samp);

for (i=0; i<=1; ++i)

{
fscanf(fptr,"%c",&bits_samp[i]); /* bits per sample */
printf("%x

",bits_samp[i]);
fprintf(fptw,"%c",bits_samp[i]);
}

bitsamp1=(bits_samp[0]&0x0F);
bitsamp2=(bits_samp[0]&0xF0);
bitsamp3=(bits_samp[1]&0x0F)<<8;
bitsamp4=(bits_samp[1]&0xF0)<<8;
total_bits_samp = bitsamp1 + bitsamp2 + bitsamp3 + bitsamp4;
printf("total bits per sample is

",total_bits_samp);

for (i=0; i<=3; ++i)

{
/* This is the DATA chunk */
fscanf(fptr,"%c",&asc_data[i]);
printf("%x

",asc_data[i]);
fprintf(fptw,"%c",asc_data[i]);
}

for (i=0; i<=3; ++i)

{
fscanf(fptr,"%c",&wl[i]); /* wave length */
printf("%x

",wl[i]);
fprintf(fptw,"%c",wl[i]);
}
w1=(wl[0]&0x0F);
w2=(wl[0]&0xF0);
w3=(wl[1]&0x0F)<<8;
w4=(wl[1]&0xF0)<<8;
w5=(wl[2]&0x0F)<<16;
w6=(wl[2]&0xF0)<<16;
w7=(wl[3]&0x0F)<<24;
w8=(wl[3]&0xF0)<<24;
total_wave_length = w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8;
printf("The wave length is %f

",total_wave_length);


if (total_bytes_sec!=NULL)
{
time = (total_wave_length/total_bytes_sec)*10;
}
if (total_sample_rate!=NULL)
{
time = total_wave_length/(total_sample_rate*total_bytes_samp)*10;
}
if (total_bytes_samp!=NULL)
{
time = ((1/total_sample_rate)*(total_wave_length)/(total_bytes_samp))*10;
}

printf("time is %f

",time);


/* ------------------------------------------------------------------------------------------*/

/* Fade_in Fade_out part of the program.

Promting user for input of fade_in fade_out */




printf("Please enter fade start in seconds

");
scanf("%ld",&fstart);
printf(" Fade start is %ld

",fstart);

printf("Please enter fade stop time in seconds

");
scanf("%ld",&fstop);
printf(" Fade stop is %ld

",fstop);

fadestart_bytes = fstart * total_bytes_sec;

printf("the fade start time is %ld bytes

",fadestart_bytes);

fadestop_bytes = fstop * total_bytes_sec;
printf ("the fade stop time is %ld bytes

",fadestop_bytes);
fduration = fadestop_bytes - fadestart_bytes;
printf ("the fade duration is %ld bytes

", fduration);

for (i=0;i<=fduration;i++)
{
fscanf(fptr,"%c",&x);
scalef=(float)i/(float)(fduration);
float fdata = (x-80)*(scalef)+80;
fprintf(fptw,"%c",fdata);


}
while (fscanf(fptr,"%c",&x)!=EOF) /* data samples to end */
{
fprintf(fptw,"%c",x);
}

fclose(fptr);
fclose(fptw);

}

/* End of program */

Comments

  • JonathanJonathan Posts: 2,914Member
    : Please can someone help me I had to write a program to open a .wav file then to fade_in fade_out the wave file, but when the user is asked to input the fade_in fade_out times and play back the .wav file, there is no fade_in fade_out but silence?? can some body help me.
    :
    Perhaps it's a casting issue...

    : for (i=0;i<=fduration;i++)
    : {
    : fscanf(fptr,"%c",&x);
    : scalef=(float)i/(float)(fduration);
    : [red]float fdata = (x-80)*(scalef)+80; [/red]
    : fprintf(fptw,"%c",fdata);
    : }
    I think it may be the line in red. Maybe scalef is getting converted to an int because that's what it's multipllied by? Try something like:-

    float fdata = (((float) (x - 80)) * scalef) + 80;

    What's with the subtracting and adding 80? I suspect you mean 0x80 or 128.

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

  • Nok79Nok79 Posts: 12Member
    : : Please can someone help me I had to write a program to open a .wav file then to fade_in fade_out the wave file, but when the user is asked to input the fade_in fade_out times and play back the .wav file, there is no fade_in fade_out but silence?? can some body help me.
    : :
    : Perhaps it's a casting issue...
    :
    : : for (i=0;i<=fduration;i++)
    : : {
    : : fscanf(fptr,"%c",&x);
    : : scalef=(float)i/(float)(fduration);
    : : [red]float fdata = (x-80)*(scalef)+80; [/red]
    : : fprintf(fptw,"%c",fdata);
    : : }
    : I think it may be the line in red. Maybe scalef is getting converted to an int because that's what it's multipllied by? Try something like:-
    :
    : float fdata = (((float) (x - 80)) * scalef) + 80;
    :
    : What's with the subtracting and adding 80? I suspect you mean 0x80 or 128.
    :
    : Jonathan
    :
    : ###
    : for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    : (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    : /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
    :
    : tried changing it but still has silence?
    the 80 is part of a formula for fade, to make first byte 0 ie 80 - 80.
    the formula was
    byte - 80 *(1 / stop - start ) + 80

Sign In or Register to comment.