wav - Change Bitrate of AudioRecord on Android -
how can on android bits per sample changed 16 bit 8 bit (or other bitrates) in audiorecord object when recording in wav format?
this doesn't work (as discussed example here: using audiorecord 8-bit encoding in android):
private static int recorder_audio_encoding = audioformat.encoding_pcm_8bit; -->error: invalid audio format. buffersize = audiorecord.getminbuffersize(recorder_samplerate,recorder_channels, recorder_audio_encoding); recorder = new audiorecord(mediarecorder.audiosource.mic,recorder_samplerate, recorder_channels,recorder_audio_encoding, buffersize);
with header:
private void writewavefileheader( fileoutputstream out, long totalaudiolen, long totaldatalen, long longsamplerate, int channels, long byterate) throws ioexception { byte[] header = new byte[44]; header[0] = 'r'; // riff/wave header ...header content... header[32] = (byte) (2 * 16 / 8); // block align header[33] = 0; header[34] = (byte)recorder_bpp; // bits per sample, set 8 ...header content... out.write(header, 0, 44); }
you need record 16 bit samples using audioformat.encoding_pcm_16bit
because 8 bit encoding not working on android devices.
then convert 16 bit buffer 8 bit on fly prior writing wave sound file. because 8 bit wave files use unsigned integers (0-255) silence being 128 instead of 0, suggested conversion corrects that. following code skeleton:
// source above defined value buffersize , recorder object samplebuffershorts = new short[buffersize]; samplebufferbytes = new short[buffersize]; int numberofshortsread = recorder.read(samlebuffershorts, 0, buffersize); if (numberofshortsread != error_invalid_operation && numberofshortsread != error_bad_value) { (int = 0 ; < numberofshortsread ; ++i) samplebufferbytes[i] = (byte)((samplebuffershorts[i]>>8)+128); }
see https://stackoverflow.com/a/18777515/1986583 useful explanation why additional dithering improve resulting quality.
Comments
Post a Comment