tags:

views:

280

answers:

1

Is it possible to read a sound backward with the managed version of DirectSound? If not is there another library allowing to make it easily?

+2  A: 

You can use the WaveFileReader and WaveFileWriter classes from NAudio to reverse a WAV file. You need to make sure you use the BlockAlign property of the WaveFormat to read all the bytes for a single sample (4 for stereo 16 bit audio).

    public static void ReverseWaveFile(string inputFile, string outputFile)
    {
        using (WaveFileReader reader = new WaveFileReader(inputFile))
        {
            int blockAlign = reader.WaveFormat.BlockAlign;
            using (WaveFileWriter writer = new WaveFileWriter(outputFile, reader.WaveFormat))
            {
                byte[] buffer = new byte[blockAlign];
                long samples = reader.Length / blockAlign;
                for (long sample = samples - 1; sample >= 0; sample--)
                {
                    reader.Position = sample * blockAlign;
                    reader.Read(buffer, 0, blockAlign);
                    writer.WriteData(buffer, 0, blockAlign);
                }
            }
        }
    }
Mark Heath
Accepted, but I implemented it with DirectSound.
tinmaru