Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27360

VS 2010 [RESOLVED] Concatenate .wav files via byte array

$
0
0
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.
vb.net Code:
  1. Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
  2.         Dim Byt1() As Byte = IO.File.ReadAllBytes("h.wav")
  3.         Dim Byt2() As Byte = IO.File.ReadAllBytes("e.wav")
  4.         Join(Byt1, Byt2)
  5.         Byt1 = IO.File.ReadAllBytes("temp.wav")
  6.         Byt2 = IO.File.ReadAllBytes("l.wav")
  7.         Join(Byt1, Byt2)
  8.         Byt1 = IO.File.ReadAllBytes("temp.wav")
  9.         Byt2 = IO.File.ReadAllBytes("ow.wav")
  10.         Join(Byt1, Byt2)
  11.         Dim snd As New System.Media.SoundPlayer("temp.wav")
  12.         snd.Play()
  13.     End Sub
  14.  
  15.     Private Sub Join(ByRef byt1() As Byte, byt2() As Byte)
  16.  
  17.         Dim Lgth As Integer
  18.         Dim SaveHeader(43) As Byte
  19.  
  20.         'Save the header
  21.         System.Array.Copy(Byt1, 0, SaveHeader, 0, 43)
  22.         Dim FinalData(byt1.Length - 44) As Byte
  23.  
  24.         'Save the data from the first file
  25.         System.Array.Copy(Byt1, 44, FinalData, 0, Byt1.Length - 44)
  26.  
  27.         'Save the data from the second file
  28.         ReDim Preserve FinalData(byt1.Length + byt2.Length - 44)
  29.         System.Array.Copy(byt2, 44, FinalData, byt1.Length - 44, byt2.Length - 44)
  30.  
  31.         'save length of new data
  32.         Lgth = FinalData.Length
  33.  
  34.         'create array to hold final output
  35.         Dim Output() As Byte
  36.         ReDim Output(FinalData.Length + 43)
  37.  
  38.         'add header
  39.         System.Array.Copy(SaveHeader, Output, 43)
  40.  
  41.         'add data
  42.         System.Array.Copy(FinalData, 0, Output, 44, FinalData.Length)
  43.  
  44.         'adjust data length
  45.         Dim bytes() As Byte = BitConverter.GetBytes(Lgth)
  46.         System.Array.Copy(bytes, 0, Output, 40, 4)
  47.         ByteArrayToFile("temp.wav", Output)
  48.  
  49.     End Sub
  50.  
  51.     ' <summary>
  52.     ' Function to save byte array to a file
  53.     ' </summary>
  54.     ' <param name="_FileName">File name to save byte array</param>
  55.     ' <param name="_ByteArray">Byte array to save to external file</param>
  56.     ' <returns>Return true if byte array save successfully, if not return false</returns>
  57.     Public Sub ByteArrayToFile(ByVal _FileName As String, ByVal _ByteArray() As Byte)
  58.         Try
  59.             'Dim myInt As Int32
  60.             'myint = BitConverter.ToInt32(_ByteArray, 40)
  61.             'MsgBox("Length of file " & BitConverter.ToString(BitConverter.GetBytes(_ByteArray.Length)) & ". Length stored in header " & BitConverter.ToString(BitConverter.GetBytes(myInt)))
  62.             '' Open file for readin
  63.             Dim _FileStream As New System.IO.FileStream(_FileName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write)
  64.             ' Writes a block of bytes to this stream using data from a byte array.
  65.             _FileStream.Write(_ByteArray, 0, _ByteArray.Length)
  66.             ' close file stream
  67.             _FileStream.Close()
  68.  
  69.         Catch _Exception As Exception
  70.             ' Error
  71.             Console.WriteLine("Exception caught in process: {0}", _Exception.ToString())
  72.         End Try
  73.         ' error occured, return false
  74.  
  75.     End Sub

Viewing all articles
Browse latest Browse all 27360

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>