开发者

Programmatically Writing PCM WAV Data in Android

开发者 https://www.devze.com 2022-12-25 01:54 出处:网络
I\'m looking for a way to programmatically save an array of shorts as PCM data.I know that this should be possible, but I haven\'t found a very easy way to do this on Android.

I'm looking for a way to programmatically save an array of shorts as PCM data. I know that this should be possible, but I haven't found a very easy way to do this on Android.

Essentially, I'm taking voltage data, and I want to save it in PCM format. My function looks something like this:

  public void audifySignal(short[] signal) {
    // Create a WAV file from the incoming signal
  }

Any suggestions would be awesome, or even references. Seems like the aud开发者_Python百科io APIs built in to android are more geared for directly recording from the mic, and not so much for lower level signal processing type work (at least for saving raw data to a file). I'd also like to avoid having to manually write the PCM file headers and what not...

Thanks!


Sam, I dunno about Android-specific libraries, but I'll go ahead and say this:

Raw PCM data is pretty straight forward. It's generally just sequential data. Maybe you need to understand the WAV format in order to understand what PCM is and how it works.

WAV is fairly widely used as a container for uncompressed audio. Gaining an understanding of how the WAV file contains the data will cast a fair bit of light on how raw digital audio works in general.

This page helped me a fair bit:

http://www.sonicspot.com/guide/wavefiles.html

Interestingly you can more or less fire ANY data at a sound-card and it'll play it. It'll probably sound crazy to us humans as the sound card doesn't care about whether it sounds garbled or not.

Whether it sounds pleasing to the ear or not will depend upon whether you've provided the correct sample size, number of channels, frequency and some PCM data that conforms to the former.

See you can't "detect" the sample size, the number of channels or the correct frequency from the raw PCM data itself. You have to store this crucial data ALONG with the PCM data so that other pieces of software can let the sound-card know how to handle your PCM data.

That's where the WAV container format comes in.

There are other formats but WAV is pretty commonplace and it's therefore a good place to start.

Cheers Tristen


You can use Android's AudioTrack to write raw PCM data that you want to get played, but it's not a function to generate the wav file or so.

0

精彩评论

暂无评论...
验证码 换一张
取 消