Advertisement
Vita94

WaveCutOff

Apr 23rd, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.02 KB | None | 0 0
  1. private int DataOffset(byte[] data)
  2.         {
  3.             int offset = 0;
  4.             try
  5.             {
  6.                 //finding the start of data
  7.                 while (true)
  8.                 {
  9.                     if (data[offset] == 'd' && data[offset + 1] == 'a' && data[offset + 2] == 't' && data[offset + 3] == 'a')
  10.                         break;
  11.                     offset++;
  12.                 }
  13.                 return offset + 4 + 4; //move to the offset of the start of the data two fields DATA and DataSize both 4 bytes
  14.             }
  15.             catch
  16.             {
  17.                 return 0; // not found
  18.             }
  19.         }
  20.         public bool WaveCutOff(string filePath, int[] channelValues)
  21.         {
  22.             byte[] bytes = File.ReadAllBytes(filePath);
  23.             int startOffset = DataOffset(bytes);
  24.             int dataLength = (bytes.Length - startOffset);
  25.             byte[] data = new byte[dataLength];
  26.  
  27.             int channel = 0;
  28.             int segmentCount = 0;
  29.             for(int i = 0; i < dataLength; i++)
  30.             {
  31.                 if (segmentCount >= 2)
  32.                 {
  33.                     channel++;
  34.                     segmentCount = 0;
  35.                 }
  36.                 if (channel >= channelValues.Length)
  37.                     channel = 0;
  38.  
  39.                /* if (channel == 0)
  40.                     data[i] = 255;
  41.                 else
  42.                     data[i] = 0;*/
  43.  
  44.                 data[i] = (byte)(Math.Abs(bytes[startOffset + i] - channelValues[channel]) % 255);
  45.                 segmentCount++;
  46.             }
  47.  
  48.             FileStream fstream = new FileStream(filePath + ".mod.wav", FileMode.Create);
  49.             BinaryWriter bpisac = new BinaryWriter(fstream);
  50.  
  51.             //write the header
  52.             for (int i = 0; i < startOffset; i++)
  53.                 bpisac.Write(bytes[i]);
  54.             //write the data
  55.             for (int i = 0; i < data.Length; i++)
  56.                 bpisac.Write(data[i]);
  57.  
  58.             fstream.Close();
  59.  
  60.             return true;
  61.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement