merging two wav files

i'm trying to merge two wav files. i have been trying to do this for around 5 hours, but i havent been able to . i tried so many things using file handling , opening in binary mode,etc . but i havent been able to merge two wav files. I was able so separate the header data from the sound data and merge them back but i was not able to merge 2 wave files .. this is my code for what i have done... can anyone please help me out?

#include <iostream>
#include <ios>
#include <iomanip>
#include <fstream>


using namespace std;


unsigned char readByte(ifstream &thisFile){
char buffer;
thisFile.get(buffer);
return (unsigned char)buffer;
}


int main()
{
ifstream binheaderFileA("headerh",ios::binary); // for reading the header file
ifstream bindataFileA("datah",ios::binary) ; // data from first sound file
ifstream bindataFileB("datae",ios::binary); // data from second sound file


ofstream wavFileA("he.wav",ios::binary); // output


unsigned char byteA;
unsigned char byteB;
unsigned char byteC;


binheaderFileA.seekg(0,ios::end);

int leng1 = binheaderFileA.tellg();
cout << leng1 << endl;
binheaderFileA.seekg(0,ios::beg);
wavFileA.seekp(0,ios::beg);


for(int i = 0 ; i < leng1 ; i++) // loop to add header file to he.wav
{
byteA = readByte(binheaderFileA);
wavFileA << byteA;
}
binheaderFileA.close();

bindataFileA.seekg(0,ios::end);
int leng2 = bindataFileA.tellg();
cout << leng2 << endl;
bindataFileA.seekg(0,ios::beg);
int pos;
pos = wavFileA.tellp();
cout << pos << endl << endl;

for(int i = 0; i < leng2 ; i++) // loop to add data from first sound file to he.wav
{
byteB = readByte(bindataFileA);
wavFileA << byteB;
}

pos = wavFileA.tellp();
cout << pos << endl << endl;

bindataFileB.seekg(0,ios::end);
int leng3 = bindataFileB.tellg();
bindataFileB.seekg(0,ios::beg);
cout<<leng3<<endl;
for(int i= 0 ; i < leng3 ; i++)
{
byteC = readByte(bindataFileB); // loop to add data from 2nd sound file to he.wav

wavFileA << byteC;
}
pos = wavFileA.tellp();
cout << pos << endl << endl;
bindataFileA.close();
bindataFileB.close();

wavFileA.close();


return 0;
}
It would help if your code is in code tags: http://cplusplus.com/articles/firedraco1/

Also, what do you mean by "merge"? Do you mean you want to append one at the end of the other? Or do you want to mix them so they both play simultaneously? Or something else?
sorry about that .. i didn't know there was something like that .. i will from now on..

i want to append one at the end of the other . But after doing some research i found that i should make changes in the header file in order to add more data to the end of the original file. I tried one method( which is actually trial and error . i found out the byte where the size of the data chunk is stored and i modified it to a larger value. here is the following modification. logically is correct but i dont think this is the right way to do it.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <ios>
#include <iomanip>
#include <fstream>
#include <string>


using namespace std;


unsigned char readByte(ifstream &thisFile){
char buffer;
thisFile.get(buffer);
return (unsigned char)buffer;
}


int main()
{ 
 ifstream binheaderFileA("headerh",ios::binary);    // for reading the header file
 ifstream bindataFileA("datah",ios::binary) ;      // data from first sound file
 ifstream bindataFileB("datae",ios::binary);      // data from second sound file

 
 ofstream wavFileA(h,ios::binary);        // output


 unsigned char byteA;
 unsigned char byteB;
 unsigned char byteC;


 binheaderFileA.seekg(0,ios::end);
 
int leng1 = binheaderFileA.tellg();
 cout << leng1 << endl;
 binheaderFileA.seekg(0,ios::beg);
 wavFileA.seekp(0,ios::beg);
 
 
 for(int i = 0 ; i < leng1 ; i++)                   // loop to add header file to he.wav
  {
   if(i==leng1-6)
  {
   byteA = 30;                                  // to change the size of the data subchunk
   wavFileA << byteA;

  }
   else
   {byteA = readByte(binheaderFileA);
   wavFileA << byteA;}
  }
  binheaderFileA.close();




 bindataFileA.seekg(0,ios::end);
 int leng2 = bindataFileA.tellg();
 cout << leng2 << endl;
 bindataFileA.seekg(0,ios::beg);
 int pos;
 pos = wavFileA.tellp();
 cout << pos << endl << endl;
 
 for(int i = 0; i < leng2 ; i++)                 // loop to add data from first sound file to he.wav
  {
   byteB = readByte(bindataFileA);
   wavFileA << byteB;
  }

  pos = wavFileA.tellp();
 cout << pos << endl << endl;

 bindataFileB.seekg(0,ios::end);
 int leng3 = bindataFileB.tellg();
 bindataFileB.seekg(0,ios::beg);
 cout<<leng3<<endl;
 for(int i= 0 ; i < leng3 ; i++)
 {
  byteC = readByte(bindataFileB);           // loop to add data from 2nd sound file to he.wav
  
  wavFileA << byteC;
 }
 pos = wavFileA.tellp();
 cout << pos << endl << endl;
  bindataFileA.close();
 bindataFileB.close();

 wavFileA.close();

 
 return 0;
}
Topic archived. No new replies allowed.