Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am stuck in trying to trim a wav file. My problem is that when I trim the file (of 32 kbps bit rate) to a 10-second clip, it is trimming it into 00:01:18. The full, original time is 1:37:13.

I have tried specifying value of CutFromStart ( 00 : 0 : 0 : 0 ) and giving CutFromEnd (00:1:37:03), but as stated, the resulting audio is 01:18 where as i was expecting that it would return first 10 seconds of wav clip... Please help.

This is the code I am using:

C#
public static void TrimWavFile(string input, string output, TimeSpan start, TimeSpan end)
 {
 using (WaveFileReader reader = new WaveFileReader(input))
 {
   using (WaveFileWriter writer = new WaveFileWriter(output, reader.WaveFormat))
            {
                int segement = reader.WaveFormat.AverageBytesPerSecond / 1000;
                Console.WriteLine(""+segement);

                int startPosition = (int)start.TotalMilliseconds * segement;

                startPosition = startPosition - startPosition % reader.WaveFormat.BlockAlign;

                int endBytes = (int)end.TotalMilliseconds * segement;
                endBytes = endBytes - endBytes % reader.WaveFormat.BlockAlign;
                int endPosition = (int)reader.Length - endBytes;


                TrimWavFile(reader, writer, startPosition, endPosition);
            }
        }
    }

private static void TrimWavFile(WaveFileReader reader, WaveFileWriter writer, int startPosition, int endPosition)
{
   reader.Position = startPosition;
   byte[] buffer = new byte[1024];
   while (reader.Position < endPosition)
   {
      int segment = (int)(endPosition - reader.Position);
      if (segment > 0)
      {
          int bytesToRead = Math.Min(segment, buffer.Length);
          int bytesRead = reader.Read(buffer, 0, bytesToRead);
          if (bytesRead > 0)
          {
                writer.WriteData(buffer, 0, bytesRead);
          }
      }
   }
 }


unnecesary code block removed
Posted
Updated 11-May-14 6:27am
v5
Comments
gggustafson 26-Apr-14 16:46pm    
If you are planning to "split" .WAV files into pieces, you must become intimately familiar with the .WAV file format. There are a number of references on the web (Google search for "wav file format").

You must be more precise in what you mean by "split". Will you be processing compressed or uncompressed files?

You must read an existing header and create a new one. Then you must copy samples from the input to the output .WAV file.

Good Luck
kingomer 27-Apr-14 3:24am    
i know about wav
gggustafson 27-Apr-14 11:15am    
Then you probably know the answer. Difficult, very difficult. Most speech to text algorithms are propriatory. And even if you could licence one, I'd be surprised if the EULA or TAC would allow you to modify it

Good luck.
kingomer 27-Apr-14 3:25am    
I am making a software which will do conversion of audio to text and i am stuck in the problem i have to pass chucks of data of wav file to the audio to text conversion function, so kindly help me out by giving me some specific running code example of how to split wav file and pass it to my function
Mitchell J. 27-Apr-14 4:18am    
I doubt you'll find anyone here who would simply "give you a function" like that, not to mention that there are probably very few people here who could write one for you without spending an hour or so becoming acquainted the .wav format.

I recommend you google for free .wav libraries, see what you can do with those, and come back here with some questions, not a plea for code.

Good luck! :-)

1 solution

Just look at this line:
C#
int endPosition = (int)reader.Length - endBytes;

Here you calculate the position of TimeSpan end before the end of the file - not after the beginning of the file.
 
Share this answer
 
Comments
kingomer 12-May-14 4:26am    
Thanx for the Solution, what should i write instead of
int endPosition = (int)reader.Length - endBytes;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900