Howdy, Stranger!

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

Categories

.wav problem??

Nok79Nok79 Member Posts: 12
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

  • ninchagoraninchagora Member Posts: 82

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

    It is OK, but it's more C-style to write for(i=0;i<4;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);

    Why did you have to split bytes into nibbles?
    You could just shift and add bytes, or...

    You could also declare a long lp.
    Then you read it from the file like
    fread(&lp, sizeof(long), 1, fptr);

    See fread and fwrite functions.

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

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

    If you only want to output a byte (char), you should use fgetc and fputc
    instead of fscanf and fprintf with "%c" conversion. You should use fscanf and fprintf if you read from a text file - where numbers are stored as ASCII. If you have binary files use fgetc, fputc, fread, fwrite.

    You could also define a struct for the RIFF and WAVE headers and read
    whole header at once with fread.

    :
    : printf("Please enter fade start in seconds

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

    ",fstart);

    %ld is for long, right? (I don't remember what type fstart was)

    :
    : fscanf(fptr,"%c",&x);
    : scalef=(float)i/(float)(fduration);
    : float fdata = (x-80)*(scalef)+80;

    There should be 80 hex - that is 128 or 0x80.

    : fprintf(fptw,"%c",fdata);

    This is where problem is. You should have written
    fprintf(fptw,"%c",(unsigned char)fdata)

    Why?
    Because fprintf is declared as
    int fprintf(FILE* f, const char* s, ...);

    You can write anything instead of ..., but it is your responsibility to
    match the format specifiers and the parameters.
    Compiler will do implicit conversions for char or short into int, and
    float into double, but for others you must cast it explicitly.

    Actually, parameters are passed to the function via stack, from right to left.

    So in your case, compiler pushes a float onto stack, which is 32-bit, and fprintf function thinks that there is a int (which can be 32-bit or 16-bit, depending on your compiler) - int, not char, because a single char is converted to int when passed as an argument.
    So it reads an int which has nothing to do with the value you expected.


    And, one more thing - it would be faster to work witha a buffer than to process sample by sample.
    You could load several samples into buffer, process them, then write it to other file - using fread and fwrite, again.

    If you learn by a book, can you tell which one is it?




  • Nok79Nok79 Member Posts: 12
    I'm new to programming and realise that this programe could be written alot better. I'm working on my code by working through sams teach yourself c in 21 days.

    Have updated source code since last time, not sure how to implement
    fgetc, fputc, fread, fwrite into source code as i havn't really used them!
    Think there may be a problem with my IF loops where im doing calculation as time == 0 the IF loop in red dosn't seem to work??





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



    #include
    #include



    unsigned char x,riff[4],packlength[4],wave[4],fmt[4],fmt_length[4],audio_fmt[2],
    num_chan[2],samp_rate[4],bytes_sec[4],bytes_samp[2],bits_samp[2],asc_data[4],
    wavlength[4];

    int 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,wl1,wl2,wl3,wl4,wl5,wl6,wl7,wl8;

    unsigned long i,total_packlength,total_samp_rate,total_bytes_samp,
    total_bits_samp,total_wave_length,total_bytes_sec,fstart,fstop,
    fadestart_bytes,fadestop_bytes,fduration, scalef,time;


    void main()


    {


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

    ");
    exit(1);
    }
    fptw=fopen("New program.wav","wb");
    if(fptw==NULL)
    {
    printf("Cannot create a new file

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

    }
    for(i=0;i<4;i++)

    {

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

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

    }

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

    {

    fscanf(fptr,"%c",&packlength[i]); /* Package_Length */
    printf("%x

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

    }

    p1a=(packlength[0]&0x0F);
    p1b=(packlength[0]&0xF0);
    p1c=(packlength[1]&0x0F)<<8;
    p1d=(packlength[1]&0xF0)<<8;
    p1e=(packlength[2]&0x0F)<<16;
    p1f=(packlength[2]&0xF0)<<16;
    p1g=(packlength[3]&0x0F)<<24;
    p1h=(packlength[3]&0xF0)<<24;
    total_packlength = p1a + p1b + p1c + p1d + p1e + p1f + p1g + p1h;
    printf("Total package length is %ld

    ",total_packlength);

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    s1=(samp_rate[0]&0x0F);
    s2=(samp_rate[0]&0xF0);
    s3=(samp_rate[1]&0x0F)<<8;
    s4=(samp_rate[1]&0xF0)<<8;
    s5=(samp_rate[2]&0x0F)<<16;
    s6=(samp_rate[2]&0xF0)<<16;
    s7=(samp_rate[3]&0x0F)<<24;
    s8=(samp_rate[3]&0xF0)<<24;
    total_samp_rate = s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8;
    printf("Total sample rate is %ld

    ",total_samp_rate);

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

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

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

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

    ",total_bytes_sec);

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

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

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

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

    ",total_bytes_samp);

    for (i=0;i<2;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 %ld

    ",total_bits_samp);

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

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

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

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

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

    ",wavlength[i]);
    fprintf(fptw,"%c",wavlength[i]);
    }
    wl1=(wavlength[0]&0x0F);
    wl2=(wavlength[0]&0xF0);
    wl3=(wavlength[1]&0x0F)<<8;
    wl4=(wavlength[1]&0xF0)<<8;
    wl5=(wavlength[2]&0x0F)<<16;
    wl6=(wavlength[2]&0xF0)<<16;
    wl7=(wavlength[3]&0x0F)<<24;
    wl8=(wavlength[3]&0xF0)<<24;
    total_wave_length = wl1 + wl2 + wl3 + wl4 + wl5 + wl6 + wl7 + wl8;
    printf("The wave length is %ld

    ",total_wave_length);



    if (total_bytes_sec!=NULL)
    {
    time = ((total_wave_length) / (total_bytes_sec) * (10));
    }
    if (total_samp_rate!=NULL)
    {
    time = (((total_wave_length) / (total_samp_rate) * (total_bytes_samp)) * (10));
    }
    [red] if (total_bytes_samp!=NULL)
    {
    time = (((1) / (total_samp_rate)*(total_wave_length)) / (total_bytes_samp) * (10));
    }
    [black]
    printf("time is %ld

    ",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 = (long) i / (long) fduration;
    long fdata = (x - 0x80) * (scalef) + (0x80);
    fprintf(fptw,"%c",(unsigned char)fdata);







    }

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

    {
    fprintf(fptw,"%c",x);
    }

    fclose(fptr);
    fclose(fptw);

    }

    /* End of program */

  • ninchagoraninchagora Member Posts: 82



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

    I'm not sure what is this supposed to do.
    (What happens if all 3 conditions fail?
    What is the meaning of 10?
    You should have used 0 instead of NULL - NULL is meant for pointers, not for numbers, although it is equal to 0.

    I suppose that it should calculate time, if some of parameters are undefined.
    )



    Suppose you want to calculate the length of the sound in seconds.

    The wave file has:
    sample rate - samples per second
    bit depth - bits per sample per channel
    number of channels

    Therefore 1 sample has bytes_per_sample = number_of_channels * bytes_per_sample_per_channel

    For example:
    CD Audio: 44.1 kHz, 16-bit, Stereo (2 ch.) -> 2*2=4 bytes per sample
    1 second of CD audio has 44100 * 4 = 176400 bytes per second (let's call it byte rate)

    If you need length in seconds, you should divide size of audio record with byte rate.



    : for (i=0;i<=fduration;i++) {
    : fscanf(fptr,"%c",&x);
    : ...
    : ...
    : fprintf(fptw,"%c",(unsigned char)fdata);
    : }

    Here you are reading and processing 1 byte at a time.
    That is OK only if you have 8-bit mono audio.

    This is how samples are stored in WAV file, depending on bit depth and number of channels:
    (WARNING - I am not absolutely sure that this is correct)

    8-bit mono:
    Each sample is 1 unsigned byte (char).
    Therefore, 0 is the minimum sample value, and 255 is the maximum. Zero is the mid value, probably 128 (0x80).
    So, if you want to normalize it into range of real values (-1.0, 1.0) you use the formula r = (x-128)/128.0

    16-bit mono:
    Each sample is 1 signed short int - in the range -32768..32767. Zero is 0. r = x / 32768.0

    Stereo is stored like this: LRLRLR... , where L is sample for left channel, and R is sample for right channel

    If you have more channels (for example 4) it is similar: 123412341234...


    Therefore you should run one routine for processing 8-bit files, and the other for 16-bit files.


    This is how you read and write data from the *BINARY* files:

    FILE* f;
    char c;
    int cr;
    short int s;

    ...

    cr = fgetc(f); /* use int because it might return EOF, which is actually an int - hence fgetc returns int, not char */
    if(cr!=EOF) c=(char)cr; /* if not EOF, then it may be converted to char (signed or unsigned) */

    ...

    fread(&s, sizeof(short int), 1, f);
    /* for any other numeral type, or arrays - put number of elements instead of 1, and &s[0] instead of &s */
    /* I think it returns 0 if there's an end-of-file error - I'm not sure, check it */

    ...

    fputc(c,f)

    ...

    fwrite(&s, sizeof(short int), 1, f);

    ...

    If you read and write text files, then use fscanf and fprintf.



    There are many variables you have declared as long, and which should be float.

    For example, you might want to enter 0.47 seconds as input.
    So, fstart could be a float. But, pay attention on what format specifier you will use in scanf. %f is for float, and %lf is for double.

    And, which is more important:

    : for (i=0;i<=fduration;i++) {
    : fscanf(fptr,"%c",&x);
    : scalef = (long) i / (long) fduration;
    /*
    this will always give you a 0 - because you have integer division, and i is always < fduration
    you should use float division - convert at least one operand to float, like the first time;
    scalef should be float, because of multiplication in the next line.
    */
    : long fdata = (x - 0x80) * (scalef) + (0x80); /* fdata should be float too */
    : fprintf(fptw,"%c",(unsigned char)fdata);
    : }




    Anyway, what is the program supposed to do ?

    /~~~~~~~~~~~
    /
    /
    ___________/

    Is it a fade in and fade out like this, or something else?


    Once more, I'm not sure that this is 100% correct, but I hope it might help.

    For WAV format, find something on the internet.
    Try this URL: http://www.intersrv.com/~dcross/wavio.html


    Did you learn some other programming language before C?

    And how did you write the text in red color?

    [red]Oh, I see [/red]

  • Nok79Nok79 Member Posts: 12
    The [blue] IF [black] statements were to calculate time.

    And it was only for 8bit mono havnt really the time to go into stereo.

    No did not learn any other languages before i started C. I havn't been doing C for long as you can tell from the layout of my program.

    I would just like to say thank you very much for your help,
    with my problem. your C is very good how long have you been doing it for?

    Thank You..
Sign In or Register to comment.