Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a WAV file from raw PCM data using the Android SDK

I'm trying to use the AudioRecord class to record a WAV file. The problem is that it only supplies the raw PCM data, and if I write it to a file, there is no header information, so it will not play in any media player. How can I create a WAV file from this raw data?

Or alternatively, is there any other way to record sound in Android to a WAV file (or, alternatively MP3)?

Oh, and I know that MediaRecorder can'y be used because it doesn't support either WAV or MP3 formats.

like image 228
user496854 Avatar asked Jan 23 '11 23:01

user496854


People also ask

How is PCM data stored?

In the digital domain, PCM (Pulse Code Modulation) is the most straightforward mechanism to store audio. The analog audio is sampled in accordance with the Nyquest theorem and the individual samples are stored sequentially in binary format. The wave file is the most common format for storing PCM data.


1 Answers

OK, I've got this figured out. This post was crucial in helping me: http://computermusicblog.com/blog/2008/08/29/reading-and-writing-wav-files-in-java

Basically, I used ByteArrayOutputStream to write the raw PCM data from AudioRecord, which then lets me get the byte array and its size when the process is done. I can then use that data in conjunction with the SampleRate, BitRate, and Stereo/Mono settings to create the WAV header as per the link above. The resulting file works perfectly!

like image 145
user496854 Avatar answered Oct 03 '22 10:10

user496854