Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi, I'm a beginner in VB.Net and I am trying to make a project that can reverse an audio file (e.g .wav) Sure I am aware that there are alot of existing application that do this just like audacity. But I would really like to learn how it works. So far I tried converting it to byte array and tried to reverse the array. But It doesn't work. Any help would be surely appreciated. Thank you. :)
VB
Dim aStream As FileStream = Nothing

                      aStream = New FileStream(Home_Vid.vidOutDest & "soundOutput.mp3", FileMode.Open, FileAccess.Read)

                  Dim br As New BinaryReader(aStream)

                  Dim array As Byte() = br.ReadBytes(CInt(aStream.Length))
                  System.Array.Reverse(array)
                  System.IO.File.WriteAllBytes(Home_Vid.vidOutDest & "soundOutput2.mp3", array)
Posted
Comments
Sergey Alexandrovich Kryukov 3-Feb-15 12:10pm    
Interesting question (I even up-voted it with 4), but your approach is way too naive. I answered the question; it will take considerably more work.
—SA

MP3 as most other audio file have an header at the beginning of the file (and maybe even inside - I'm not sure where ID3 are stored).

Also, MP3 is a compressed format, so you must first uncompress the data before manipulating it.

Wave file are easier to work with (as BMP files are for images). You have to read the header to know the format (number of channels, bits per sample) and then with that information you can then start reversingg the data. Only for mono file with 8 bits sample would simple reversing (of the audio part of the file) would do. For any other case, you have to figure out how many bytes are used per sample and reverse those chunks.
 
Share this answer
 
No, this is not how it works. The problem is: you revert not only data, but also metadata. Besides, the file is compressed. Here is what you can do:

  1. unpack MPEG3 file to WAV (transcode)
  2. with WAV file, determine the size of metadata (there is a lot of documentation on the file header structure; you will need to parse it to find out where metadata ends and data starts);
  3. read metadata is one array of byte, say, metadata;
  4. from that point, read data points in an another array of bytes, say data;
  5. invert data (you can do it per-byte only for 8-bit audio; should it be 16-bit audio, you would need to invert per 16-bit data points);
  6. write metadata immediately followed by data in a new file; you got an inverted WAV file;
  7. if you need, pack the resulting WAV file to MPEG3 (trancode).


It would be quite a project.

Trancoding part could be easy. I usually advice FFMpeg (libav). You can use existing utility or some .NET wrapper. I explained it all in my past answers:
how to convert image to video in C#[^],
HTML5 and Mime, Streaming a video?[^],
How to trim the video using Directshow!?[^].

Determining of the WAV file metadata size (to see at what position data starts) is harder, but also not a rocket surgery. I personally have done only WAV generation, not parsing. I would take some time. You can start here: http://www.topherlee.com/software/pcm-tut-wavformat.html[^].

—SA
 
Share this answer
 
Comments
JKLMNOP_03 3-Feb-15 18:58pm    
Thank you so much, This is very helpful. :D I personally thought my approach is way too simple. And that I have no chance in doing it that way. Its just that I haven't familiarize myself in working with audio files (This is my first time trying to do it.) I have used ffmpeg on this project and I will surely try this. Really, thank you. :)
Sergey Alexandrovich Kryukov 3-Feb-15 22:15pm    
All right, very good. You are very welcome.
So, will you accept this answer formally? In all cases, your follow-up questions will be welcome.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900