M
m.topczewski
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*ts);
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*ts);
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.
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*ts);
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*ts);
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.