PCM Audio question - Play sound files backwards?
-
Hi - I'm interested in a pet project to create a player which will play mp3s (or wavs) backwards. I've never done any mp3 encoding/decoding before, but it seems like it should be easy to do with the following algorithm: 1 - load wav file 2- extract bytes which correspond to pcm sound data 3 - create a new file with the byte order reversed 4 - play with any old sound dll. does this seem right? any suggestions on where to find a specification of a standard wav file? am i correct in assuming that pcm sound information can be directly reversed? thanks - @lrg
-
Hi - I'm interested in a pet project to create a player which will play mp3s (or wavs) backwards. I've never done any mp3 encoding/decoding before, but it seems like it should be easy to do with the following algorithm: 1 - load wav file 2- extract bytes which correspond to pcm sound data 3 - create a new file with the byte order reversed 4 - play with any old sound dll. does this seem right? any suggestions on where to find a specification of a standard wav file? am i correct in assuming that pcm sound information can be directly reversed? thanks - @lrg
Hi, I think you got the idea right, however it is important to clarify that WAV files may be compressed (ADPCM, A-LAW, u-LAW, etc.). This means that you can find many WAV files whose bytes do not correspond to plain PCM samples. In general I would say that you should modify your algorithm so that you load a file (WAV or MP3) and then normalize it to linear PCM samples. For ADPCM or x-LAW this may correspond to simple sample-based decompression schemes, for MP3 you should probably get a decoder (there are open source MP3 libraries out there). But please note that MP3 is a licensed technology (even if you get the algorithm or source code legally you still have to pay to use it). After this normalization stage you simply reverse the samples and write them back to disk (or play them directly from RAM, why not??). The x-LAW files could be played directly simply by reversing the samples (even without decompressing) and saving to file or playing directly (requesting an x-LAW audio output format). If you want to write to disk then uncompressed WAV is the easiest (just a few header bytes and you're done). If you want to write back MP3 it can be a little trickier (and subject to the same royalty restrictions as the decoder). Anyway, these comments are for simple C/C++ implementations. The COM interface, C#, JAVA, and other languages and platforms may make the task easier (or not!). About the WAV format: just search for "wav format specification" and you will find many references. For example, http://ccrma.stanford.edu/courses/422/projects/WaveFormat/[^] seems to be ok. I hope this helps, Rilhas
-
Hi, I think you got the idea right, however it is important to clarify that WAV files may be compressed (ADPCM, A-LAW, u-LAW, etc.). This means that you can find many WAV files whose bytes do not correspond to plain PCM samples. In general I would say that you should modify your algorithm so that you load a file (WAV or MP3) and then normalize it to linear PCM samples. For ADPCM or x-LAW this may correspond to simple sample-based decompression schemes, for MP3 you should probably get a decoder (there are open source MP3 libraries out there). But please note that MP3 is a licensed technology (even if you get the algorithm or source code legally you still have to pay to use it). After this normalization stage you simply reverse the samples and write them back to disk (or play them directly from RAM, why not??). The x-LAW files could be played directly simply by reversing the samples (even without decompressing) and saving to file or playing directly (requesting an x-LAW audio output format). If you want to write to disk then uncompressed WAV is the easiest (just a few header bytes and you're done). If you want to write back MP3 it can be a little trickier (and subject to the same royalty restrictions as the decoder). Anyway, these comments are for simple C/C++ implementations. The COM interface, C#, JAVA, and other languages and platforms may make the task easier (or not!). About the WAV format: just search for "wav format specification" and you will find many references. For example, http://ccrma.stanford.edu/courses/422/projects/WaveFormat/[^] seems to be ok. I hope this helps, Rilhas
Thanks Rilhas, That's just the sort of encouragement I needed. I had concluded the same thing - best to do the actual reversal on the simplest wav file. If I support compressed formats at all, I'll just convert them to basic PCM first using any old library. Awesome dude! -@LG
-
Hi - I'm interested in a pet project to create a player which will play mp3s (or wavs) backwards. I've never done any mp3 encoding/decoding before, but it seems like it should be easy to do with the following algorithm: 1 - load wav file 2- extract bytes which correspond to pcm sound data 3 - create a new file with the byte order reversed 4 - play with any old sound dll. does this seem right? any suggestions on where to find a specification of a standard wav file? am i correct in assuming that pcm sound information can be directly reversed? thanks - @lrg
@largeinsd wrote:
3 - create a new file with the byte order reversed
For PCM wave files, this will only work for 8 bit (and below) waveforms. For anything above, you want to reverse the samples not the byte order. For example, a 16 bit PCM waveform monophonic waveform takes two bytes per sample. You don't want to reverse the byte order within the sample but rather reverse the sample order themselves. So for example say we have the following values for a 16-bit monophonic waveform: Sample 1 128 - byte 1 64 - byte 2 Sample 2 78 - byte 1 241 - byte 2 To reverse these samples so that they play backwards, we reverse the sample order while leaving the byte order within the samples alone: Sample 2 78 - byte 1 241 - byte 2 Sample 1 128 - byte 1 64 - byte 2 Make sense?
-
@largeinsd wrote:
3 - create a new file with the byte order reversed
For PCM wave files, this will only work for 8 bit (and below) waveforms. For anything above, you want to reverse the samples not the byte order. For example, a 16 bit PCM waveform monophonic waveform takes two bytes per sample. You don't want to reverse the byte order within the sample but rather reverse the sample order themselves. So for example say we have the following values for a 16-bit monophonic waveform: Sample 1 128 - byte 1 64 - byte 2 Sample 2 78 - byte 1 241 - byte 2 To reverse these samples so that they play backwards, we reverse the sample order while leaving the byte order within the samples alone: Sample 2 78 - byte 1 241 - byte 2 Sample 1 128 - byte 1 64 - byte 2 Make sense?
-
leslie, makes perfect sense. swap the sample order, but preserve the byte order within. though i don't understand what you're saying with "128 - byte 1" or "78 - byte 1". what are you getting at with the 128, 64, 78, 241. thanks - @lg
@largeinsd wrote:
though i don't understand what you're saying with "128 - byte 1" or "78 - byte 1". what are you getting at with the 128, 64, 78, 241.
Those were just random numbers I picked for the byte values within the samples.
-
@largeinsd wrote:
though i don't understand what you're saying with "128 - byte 1" or "78 - byte 1". what are you getting at with the 128, 64, 78, 241.
Those were just random numbers I picked for the byte values within the samples.
-
@largeinsd wrote:
one other question: how is the data encoded for stereo?
Stereo samples are interleaved. What that means is that the samples alternate between the left and right channels. So you have the bytes that make up an individual sample and the block which is made up of two samples, left and right:
( block 1 ) ( block 2 )
left, right, left, right, etc... -
@largeinsd wrote:
one other question: how is the data encoded for stereo?
Stereo samples are interleaved. What that means is that the samples alternate between the left and right channels. So you have the bytes that make up an individual sample and the block which is made up of two samples, left and right:
( block 1 ) ( block 2 )
left, right, left, right, etc...So reversing the samples to get sound backwards effectively switches left and right !
Luc Pattyn [My Articles] [Forum Guidelines]
-
So reversing the samples to get sound backwards effectively switches left and right !
Luc Pattyn [My Articles] [Forum Guidelines]
Luc Pattyn wrote:
So reversing the samples to get sound backwards effectively switches left and right !
Switching left and right just switches the channels, so left becomes right and vice versa. I think I may be missing your point. In the case of a stereo wave file, we'd want to reverse the blocks while leaving the sample order within the blocks the same.
-
Luc Pattyn wrote:
So reversing the samples to get sound backwards effectively switches left and right !
Switching left and right just switches the channels, so left becomes right and vice versa. I think I may be missing your point. In the case of a stereo wave file, we'd want to reverse the blocks while leaving the sample order within the blocks the same.
To state it in a slightly different way, if you do what has been told so far to play the sound backwards, you also will be switching left and right channels, with probably is not be what you want. :)
Luc Pattyn [My Articles] [Forum Guidelines]
-
To state it in a slightly different way, if you do what has been told so far to play the sound backwards, you also will be switching left and right channels, with probably is not be what you want. :)
Luc Pattyn [My Articles] [Forum Guidelines]
Luc Pattyn wrote:
To state it in a slightly different way, if you do what has been told so far to play the sound backwards, you also will be switching left and right channels, with probably is not be what you want.
Right. So when reversing a stereo wave file, you'd want to reverse the block order while preserving the sample order within the blocks. :)