23

I'm making Braid in Java. If you rewind the time, the sound plays backward. How to play a WAV file backwards? Maybe with a stream with something like previous()? On the site of Braid can you see what I mean.

Update: Solved! See my own post.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287

3 Answers3

16

I solved it by myself

import java.io.IOException;
import javax.sound.sampled.AudioInputStream;

/**
 *
 * @author Martijn
 */
public class FrameBuffer {

    private byte[][] frames;
    private int frameSize;

    public FrameBuffer(AudioInputStream stream) throws IOException {
        readFrames(stream);
    }

    public byte[] getFrame(int i) {
        return frames[i];
    }

    public int numberFrames()
    {
        return frames.length;
    }

    public int frameSize()
    {
        return frameSize;
    }

    private void readFrames(AudioInputStream stream) throws IOException {
        frameSize = stream.getFormat().getFrameSize();
        frames = new byte[stream.available() / frameSize][frameSize];
        int i = 0;
        for (; i < frames.length; i++)
        {
            byte[] frame = new byte[frameSize];
            int numBytes = stream.read(frame, 0, frameSize);
            if (numBytes == -1)
            {
                break;
            }
            frames[i] = frame;
        }
        System.out.println("FrameSize = " + frameSize);
        System.out.println("Number frames = " + frames.length);
        System.out.println("Number frames read = " + i);
    }
}

And then:

 FrameBuffer frameStream = new FrameBuffer(austream); //austream is the audiostream
 int frame = frameStream.numberFrames() - 1;
 while (frame >= 0) {
      auline.write(frameStream.getFrame(frame), 0, frameStream.frameSize());
      frame--;
 }
ruleboy21
  • 5,510
  • 4
  • 17
  • 34
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
  • @Martijn Can you please tell what is auline in this line... " auline.write(frameStream.getFrame(frame), 0, frameStream.frameSize());" – Waqas Jun 28 '12 at 04:40
  • 1
    It is the audiochannel to the sound card. It is a `SourceDataLine` instance. Take a look at this example: http://www.anyexample.com/programming/java/java_play_wav_sound_file.xml – Martijn Courteaux Jun 28 '12 at 15:21
  • @MartijnCourteaux nice answer. can you tell me difference between "for(int i = 0; i – Chirag Patel Feb 15 '13 at 12:31
  • @chiragsaga: `int i` won't go off the stack when the loop is finished in the second example. That is it. I'm doing it because I want to know how many frames I read. Check out the println. – Martijn Courteaux Feb 17 '13 at 15:45
2

If the WAV contains PCM, reverse the order of the PCM samples. If it contains some other format it can be a lot more complex; probably easiest to just convert it to PCM first.

For more information on the WAV format, see this site.

bdonlan
  • 224,562
  • 31
  • 268
  • 324
2

Use Windows Sound Recorder (in Start --> Programs --> Accessories --> Entertainment).

It has a feature (Effects (menu) --> Reverse) to reverse a .WAV file. You could save the reversed file with a different name, and then open the appropriate one in your program.

Steven
  • 13,501
  • 27
  • 102
  • 146