By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,710 Members | 1,613 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,710 IT Pros & Developers. It's quick & easy.

handling wave files

P: n/a
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;
ts=0.0;
tk=1.0;

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'
id[4]='\0';
DWORD size; //32 bit value to hold file size
short format_tag, channels, block_align, bits_per_sample; //our 16
values
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
this
fread(&bits_per_sample, sizeof(short), 1, fp); //8 bit or 16 bit
file?
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'

start_byte=block_align*(int)((double)sample_rate*t s);
end_byte=block_align*(int)((double)sample_rate*tk) ;
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);
ts=2.0;
tk=3.0;

start_byte=block_align*(int)((double)sample_rate*t s);
end_byte=block_align*(int)((double)sample_rate*tk) ;

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

long temp=end_byte-start_byte;
new_data_size=new_data_size+temp;
fwrite(sound_buffer+start_byte, sizeof(BYTE), temp, fout);

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

//system("PAUSE");
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.

Jun 14 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
m.**********@gmail.com wrote:
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:
[snip code that mixes stdio and iostreams]
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.


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

Jun 14 '06 #2

P: n/a
m.**********@gmail.com schrieb:
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> [...] cout << start_byte << " " << end_byte << endl;

long new_data_size=end_byte-start_byte;
Here you calculate the size of your first data block.

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

fwrite(&new_data_size, sizeof(DWORD), 1, fout);
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.
sound_buffer = (BYTE *) malloc (sizeof(BYTE) * data_size);
fread(sound_buffer, sizeof(BYTE), data_size, fp); What's wrong with that code?
Well, where should I start?

You don't need: #include "stdafx.h"


Code like this is a maintenance horror:

if (...)
{
if (...)
{
...
}
else
printf("Error\n");
}
else
printf("Error\n");

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

Thomas
Jun 14 '06 #3

P: n/a
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.

Jun 15 '06 #4

P: n/a
m.**********@gmail.com wrote:
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.


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:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.3

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
here.

Cheers! --M

Jun 15 '06 #5

P: n/a
m.**********@gmail.com schrieb:
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.
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'.
@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.


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
Jun 16 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.