handling wave files

Discussion in 'C++' started by m.topczewski, Jun 14, 2006.

  1. m.topczewski

    m.topczewski Guest

    Hello, I'm a beginner in c++, i have to write a program that will take
    couple of segments from one wave file, and store them in another wave
    file to be played back later.

    Here's the code i have written:

    #include "stdafx.h"

    #include <cstdlib>
    #include <iostream>
    #include <windows.h>
    #include <string>

    using namespace std;

    int main(int argc, char *argv[])
    FILE *fp,*fout;
    double ts,tk;

    long start_byte,end_byte;

    fp = fopen("c:\\start.wav","rb");
    string filename="c:\\test.wav";
    fout = fopen(filename.c_str(),"wb");
    if (fp)
    BYTE id[5], *sound_buffer; //four bytes to hold 'RIFF'
    DWORD size; //32 bit value to hold file size
    short format_tag, channels, block_align, bits_per_sample; //our 16
    DWORD format_length, sample_rate, avg_bytes_sec, data_size, i; //our
    32 bit values

    fread(id, sizeof(BYTE), 4, fp); //read in first four bytes
    fwrite(id,sizeof(BYTE), 4,fout);

    if (!strcmp((char *)id, "RIFF"))
    { //we had 'RIFF' let's continue
    fread(&size, sizeof(DWORD), 1, fp);
    fwrite(&size, sizeof(DWORD), 1, fout);
    fread(id, sizeof(BYTE), 4, fp);
    fwrite(id, sizeof(BYTE), 4, fout);
    if (!strcmp((char *)id,"WAVE"))
    { //this is probably a wave file since it contained "WAVE"
    fread(id, sizeof(BYTE), 4, fp);
    fwrite(id, sizeof(BYTE), 4, fout);
    fread(&format_length, sizeof(DWORD),1,fp);
    fwrite(&format_length, sizeof(DWORD),1,fout);
    fread(&format_tag, sizeof(short), 1, fp);
    fread(&channels, sizeof(short),1,fp); //1 mono, 2 stereo
    fread(&sample_rate, sizeof(DWORD), 1, fp); //44100, 22050, etc...
    fread(&avg_bytes_sec, sizeof(DWORD), 1, fp);
    fread(&block_align, sizeof(short), 1, fp); //probably won't need
    fread(&bits_per_sample, sizeof(short), 1, fp); //8 bit or 16 bit
    fread(id, sizeof(BYTE), 4, fp); //read in 'data'

    fwrite(&format_tag, sizeof(short), 1, fout);
    fwrite(&channels, sizeof(short),1,fout);
    fwrite(&sample_rate, sizeof(DWORD), 1, fout);
    fwrite(&avg_bytes_sec, sizeof(DWORD), 1, fout);
    fwrite(&block_align, sizeof(short), 1, fout);
    fwrite(&bits_per_sample, sizeof(short), 1, fout);
    fwrite(id, sizeof(BYTE), 4, fout); //write in 'data'


    cout << start_byte << " " << end_byte << endl;

    long new_data_size=end_byte-start_byte;

    fread(&data_size, sizeof(DWORD), 1, fp); //how many bytes of sound
    data we have

    fwrite(&new_data_size, sizeof(DWORD), 1, fout);
    sound_buffer = (BYTE *) malloc (sizeof(BYTE) * data_size);
    fread(sound_buffer, sizeof(BYTE), data_size, fp);

    fwrite(sound_buffer+start_byte, sizeof(BYTE), new_data_size, fout);



    cout << start_byte << " " << end_byte << endl;

    long temp=end_byte-start_byte;

    fwrite(sound_buffer+start_byte, sizeof(BYTE), temp, fout);

    printf("Error: RIFF file but not a wave file\n");
    printf("Error: not a RIFF file\n");

    return EXIT_SUCCESS;

    but as a result of execution of such a program i should receive a file
    build of two segements from file, but i get only one written the
    ther(second) one is missing. What's wrong with that code?
    Any help would be appriciated.
    m.topczewski, Jun 14, 2006
    1. Advertisements

  2. m.topczewski

    mlimber Guest

    [snip code that mixes stdio and iostreams]
    Have you run it through your debugger? Do you have any idea where
    things are going wrong? We're not here to do your homework for you. I
    did notice that you don't close your files. That's generally bad news.

    Cheers! --M
    mlimber, Jun 14, 2006
    1. Advertisements

  3. Here you calculate the size of your first data block.
    And here you write this size to file.
    After that, you write two blocks of this size to the new file and
    wonder, why the wav player isn't aware of the two blocks.
    Well, where should I start?

    You don't need:
    Code like this is a maintenance horror:

    if (...)
    if (...)

    Do you want to write C or C++? You mix up ol' style fread/fwrite,
    malloc() and printf() with the iostream library.

    Thomas J. Gritzan, Jun 15, 2006
  4. m.topczewski

    m.topczewski Guest

    I was asking for help if you haven't noticed. Such comments like:"We're
    not here to do your homework for you" are stupid, i've asked for help
    and if couldn't help me, then don't reply.

    @Thomas - that's not it, i've placed it many places, and it didn't
    change anything, since the size of the file is ok, only data is missing
    - i guess.
    m.topczewski, Jun 15, 2006
  5. m.topczewski

    mlimber Guest

    First, please quote the message(s) your are responding to. It makes the
    conversation easier for all to follow.

    Second, "such comments" come straight from the FAQ for this group:


    What is "stupid" (I would prefer "unreasonable") is that you expect us
    to debug your program for you. Every programmer needs to be skilled in
    using his/her debugger to solve problems like the one you are having.
    Of course, if you have a specific question about the standard language
    and/or library that is not answered in the FAQ, feel free to ask it

    Cheers! --M
    mlimber, Jun 15, 2006
  6. But he is right. *you* should debug your code.
    We can help you with C++ language questions, not with 'why doesn't my
    code write a correct wave file'.
    I don't know what "it" is, since you didn't quote. It (quoting) really
    helps to read your postings.

    Anyway, if the size of the file is ok, so why should any data are missing?

    Thomas J. Gritzan, Jun 16, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.