android - Wav file recorded using AudioRecord sounds speeded up and skips frames when replaying -
i using audiorecord read microphone data , randomaccessfile write wav file. code:
public class mainactivity extends activity { audiomanager = null; audiorecord record =null; // audiotrack track =null; final int sample_frequency = 44100; final int size_of_record_array = 1024; // 1024 original final int wav_sample_multiplication_factor = 1; int i= 0; boolean isplaying = false; private volatile boolean keepthreadrunning; // private randomaccessfile statefile, statefiletemp, savtodisk; private randomaccessfile savtodisk; private filedescriptor fd = new filedescriptor(); private file delfile, renfile; string statefileloc = environment.getexternalstoragedirectory().getpath(); // keep hederwriter() happy private short nchannels = 1; private int srate = sample_frequency; private short mbitspersample = 16; // represents 16 bits of 1 pcm sample private int payload; class mythread extends thread{ private volatile boolean needstopassthrough; // /* mythread(){ super(); } mythread(boolean newptv){ this.needstopassthrough = newptv; } // */ // /* @override public void run(){ short[] lin = new short[size_of_record_array]; // byte[] lin = new byte[size_of_record_array]; int num = 0; // /* if(needstopassthrough){ record.startrecording(); // track.play(); } // */ while (keepthreadrunning) { // while (!isinterrupted()) { // num = record.read(lin, 0, size_of_record_array); num = record.read(lin, 0, lin.length); try { // savtodisk.write(lin); // use line if lin byte array // use loop block below if lin array of short for(i=0;i <lin.length; i++) savtodisk.writeshort(short.reversebytes(lin[i])); // payload += lin.length; // use line if lin array of byte payload = payload + (lin.length)*2; fd.sync(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } /* catch (syncfailedexception e) { // todo auto-generated catch block e.printstacktrace(); } */ } // /* record.stop(); // track.stop(); record.release(); // track.release(); // */ } // */ // /* public void stopthread(){ keepthreadrunning = false; } // */ } mythread newthread; private void init() { int min = audiorecord.getminbuffersize(sample_frequency, audioformat.channel_in_mono, audioformat.encoding_pcm_16bit); // toast.maketext(getapplicationcontext(), integer.tostring(min), toast.length_short).show(); // shows 4096 record = new audiorecord(mediarecorder.audiosource.voice_communication, sample_frequency, audioformat.channel_in_mono, audioformat.encoding_pcm_16bit, min); // int maxjitter = audiotrack.getminbuffersize(sample_frequency, audioformat.channel_out_mono, audioformat.encoding_pcm_16bit); // track = new audiotrack(audiomanager.mode_in_communication, sample_frequency, audioformat.channel_out_mono, audioformat.encoding_pcm_16bit, maxjitter, audiotrack.mode_stream); = (audiomanager) this.getsystemservice(context.audio_service); am.setmode(audiomanager.mode_in_communication); try { savtodisk = new randomaccessfile(statefileloc+"/audsampdata.wav", "rw"); } catch (filenotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } try { fd = savtodisk.getfd(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } private void writeheader(){ try { savtodisk.setlength(0); // set file length 0, prevent unexpected behavior in case file existed savtodisk.writebytes("riff"); savtodisk.writeint(0); // final file size not known yet, write 0 savtodisk.writebytes("wave"); savtodisk.writebytes("fmt "); savtodisk.writeint(integer.reversebytes(16)); // sub-chunk size, 16 pcm savtodisk.writeshort(short.reversebytes((short) 1)); // audioformat, 1 pcm savtodisk.writeshort(short.reversebytes(nchannels));// number of channels, 1 mono, 2 stereo savtodisk.writeint(integer.reversebytes(srate)); // sample rate savtodisk.writeint(integer.reversebytes(srate*nchannels*mbitspersample/8)); // byte rate, samplerate*numberofchannels*mbitspersample/8 savtodisk.writeshort(short.reversebytes((short)(nchannels*mbitspersample/8))); // block align, numberofchannels*mbitspersample/8 savtodisk.writeshort(short.reversebytes(mbitspersample)); // bits per sample savtodisk.writebytes("data"); savtodisk.writeint(0); // data chunk size not known yet, write 0 fd.sync(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } @override protected void onresume(){ super.onresume(); // newthread.stopthread(); log.d("mylog", "onresume() called"); init(); writeheader(); keepthreadrunning = true; // */ // newthread = new mythread(true); newthread = new mythread(isplaying); newthread.start(); } @override protected void onpause(){ super.onpause(); log.d("mylog", "onpause() called"); newthread.stopthread(); // android.os.process.killprocess(android.os.process.mypid()); try { savtodisk.seek(4); savtodisk.writeint(integer.reversebytes(36+payload)); savtodisk.seek(40); savtodisk.writeint(integer.reversebytes(payload)); savtodisk.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); setvolumecontrolstream(audiomanager.mode_in_communication); payload = 0; log.d("mylog","oncreate() called"); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } @override protected void ondestroy() { super.ondestroy(); newthread.stopthread(); // android.os.process.killprocess(android.os.process.mypid()); // killprocess(android.os.process.mypid()); // newthread.interrupt(); // delfile.delete(); log.d("mylog", "ondestroy() called"); /* try { savtodisk.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } */ } public void passstop(view view){ button playbtn = (button) findviewbyid(r.id.button1); // /* if(!isplaying){ record.startrecording(); // track.play(); isplaying = true; playbtn.settext("pause"); } else{ record.stop(); // track.pause(); isplaying=false; playbtn.settext("pass through"); } // */ } }
when play wav file in audio player, sounds speeded up, , seems skip frames. reasons this? believe skipping frames problem due fact have used writeshort()
function write out each element of short array stores audio sample data separately, if case please suggest workaround involves writing data shorts (and not using write(byte[])
function, because need use parts of code in main project involves obtaining audio samples in short array). why speeded up?
take @ this, question put me on right track.
android : recording audio using audiorecord class play fast forwarded
Comments
Post a Comment