I want to concatenate two .wav files. The file format for a .wav is bytes 0 to 39 file details, bytes 40 to 43 length of the data chunk, 44 onwards is the data.
The method I have chosen is:
1. load two wavs as byte arrays (byt1 and byt2)
2. copy the header from byt1 and store it
3. copy the data portion of byt1 to another byte array (finalData)
4. copy the data from byt2 and add it to the end if FinalData.
5. Create a final finished byte array representation of the wav (Output)
6. Copy the previously saved header to Output.
7. store the length of FinalData.
8. Add FinalData to the end of Output
9. Convert the previously saved length (int32=4 bytes) to byte array
10. Overwrite bytes 40-43 of Output with the new length
11. Save the final file to disk.
When I run the program, there are no errors until it tries to play the file, then it says the header is corrupt. Since both wav files are created in exactly the same way, things like sample rate etc are the same, the only thing that should change is the length and therefore I should have no problem retaining the information in the header. Giving a corrupt header error probably means that the length is wrong but I have checked and checked and can't find a mistake in my maths.
this is my first foray into Byte arrays so I need another pair of more experienced eyes to look over my code please.
The method I have chosen is:
1. load two wavs as byte arrays (byt1 and byt2)
2. copy the header from byt1 and store it
3. copy the data portion of byt1 to another byte array (finalData)
4. copy the data from byt2 and add it to the end if FinalData.
5. Create a final finished byte array representation of the wav (Output)
6. Copy the previously saved header to Output.
7. store the length of FinalData.
8. Add FinalData to the end of Output
9. Convert the previously saved length (int32=4 bytes) to byte array
10. Overwrite bytes 40-43 of Output with the new length
11. Save the final file to disk.
When I run the program, there are no errors until it tries to play the file, then it says the header is corrupt. Since both wav files are created in exactly the same way, things like sample rate etc are the same, the only thing that should change is the length and therefore I should have no problem retaining the information in the header. Giving a corrupt header error probably means that the length is wrong but I have checked and checked and can't find a mistake in my maths.
this is my first foray into Byte arrays so I need another pair of more experienced eyes to look over my code please.
vb.net Code:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click Join(Byt1, Byt2) Join(Byt1, Byt2) Join(Byt1, Byt2) Dim snd As New System.Media.SoundPlayer("temp.wav") snd.Play() End Sub Private Sub Join(ByRef byt1() As Byte, byt2() As Byte) Dim Lgth As Integer Dim SaveHeader(43) As Byte 'Save the header System.Array.Copy(Byt1, 0, SaveHeader, 0, 43) Dim FinalData(byt1.Length - 44) As Byte 'Save the data from the first file System.Array.Copy(Byt1, 44, FinalData, 0, Byt1.Length - 44) 'Save the data from the second file ReDim Preserve FinalData(byt1.Length + byt2.Length - 44) System.Array.Copy(byt2, 44, FinalData, byt1.Length - 44, byt2.Length - 44) 'save length of new data Lgth = FinalData.Length 'create array to hold final output Dim Output() As Byte ReDim Output(FinalData.Length + 43) 'add header System.Array.Copy(SaveHeader, Output, 43) 'add data System.Array.Copy(FinalData, 0, Output, 44, FinalData.Length) 'adjust data length Dim bytes() As Byte = BitConverter.GetBytes(Lgth) System.Array.Copy(bytes, 0, Output, 40, 4) ByteArrayToFile("temp.wav", Output) End Sub ' <summary> ' Function to save byte array to a file ' </summary> ' <param name="_FileName">File name to save byte array</param> ' <param name="_ByteArray">Byte array to save to external file</param> ' <returns>Return true if byte array save successfully, if not return false</returns> Public Sub ByteArrayToFile(ByVal _FileName As String, ByVal _ByteArray() As Byte) Try 'Dim myInt As Int32 'myint = BitConverter.ToInt32(_ByteArray, 40) 'MsgBox("Length of file " & BitConverter.ToString(BitConverter.GetBytes(_ByteArray.Length)) & ". Length stored in header " & BitConverter.ToString(BitConverter.GetBytes(myInt))) '' Open file for readin Dim _FileStream As New System.IO.FileStream(_FileName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write) ' Writes a block of bytes to this stream using data from a byte array. _FileStream.Write(_ByteArray, 0, _ByteArray.Length) ' close file stream _FileStream.Close() Catch _Exception As Exception ' Error Console.WriteLine("Exception caught in process: {0}", _Exception.ToString()) End Try ' error occured, return false End Sub