7

I am in the very ealy stages of developing this app but looking into it I have already reached a problem. I need to be able to play an audio file backwards (you know like to reveal hidden messages ;)). I have no experience working with audio on android and have no idea if this is even possible.

I found a question on here which solves the problem in java (Click Here For Question)

But this makes use of the javax.sound library which android does not support. Will I need this library to solve this problem or is there another way to reverse an audio file?

Community
  • 1
  • 1
SamRowley
  • 3,435
  • 7
  • 48
  • 77
  • Hi @SamRowley Are you achieve your goal with the described algorithm? I copy the first 44-byte header exactly from the original to reverse audio file, and I could not make wav audio to play? I use sox and reverse audio and check that 44-byte header is not exactly? Please if you could help me with any advice I'll appreciate it – Gueorgui Obregon Jan 26 '16 at 22:52

1 Answers1

8

A typical WAV file consists of a 44-byte header followed by the actual sample values. The size of a "frame" is dependent upon the WAV file's properties: a file that is stereo and 16-bits-per-sample will have a 4-byte frame size (two bytes for the left sample and two bytes for the right sample).

So in code, you would create a second WAV file by creating a byte array the same overall size as the original. You copy the 44-byte header from the original into the copy, and then iterate through the original frames starting at the last and working forward to the first. You copy each frame into the inverse location in the copy array (i.e. last original frame is copied into the copy array immediately after the header; second-to-last frame is copied after the first frame etc.). Then just play the reversed file.

So you don't need the javax.sound library to do this - you just need to be able to copy and manipulate bytes. FYI, not all WAV files are "canonical" like this (canonical means 44-byte header plus sample values, and nothing else). The WAV format is actually a RIFF format, which means in theory you need to do more complex extraction of the sample values. In practice (especially if you're creating the WAV files yourself) you can usually get away with a much simpler approach as I've described here.

NOTE: if your sounds are MP3 files, then reversing is a more complicated task, since the sample data are not stored as samples in an MP3 file. If you're using MP3s, one way to reverse them is to convert them to WAV and then reverse the WAV file.

MusiGenesis
  • 74,184
  • 40
  • 190
  • 334
  • I am creating the WAV file by capturing the audio from the microphone using MediaRecorder, so I am guessing this way would be fine? How would I go about getting the file saved from the sdcard into a byte array and copying the 44bit header? Thanks for your first answer it has been helpful so far and I understand the concept. – SamRowley Jan 13 '11 at 11:20
  • 1
    Yeah, the WAV file you get from MediaRecorder is most probably canonical, so you should be fine. Here's a link with some more code that should help you do what you need to do: http://www.dreamincode.net/forums/topic/171550-converting-short-array-to-byte-array-using-java-on-android-device/ – MusiGenesis Jan 13 '11 at 14:46
  • @MusiGenesis, hi thanks for the info, but I'm having a little problem. when I revert it it comes with a lot of distortion, really loud and bad noise – Ricardo Rodrigues Jun 17 '19 at 22:43