Search code examples
c#naudio

Is it possible to trim wav(GSM) files using NAudio


I already trim pcm wav files using NAudio.But cant trim wav gsm using that code.Please help to for that..I need to split wav GSM format using NAudio.

Code is Below

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);
            }
        }
    }
}

Solution

  • You should be able to use this technique to trim GSM but you must make sure that start and end position are exact multiples of the BlockAlign of the WaveFormat.