• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps self Audio Record

amirihana

Newbie
Hello,

I am trying to run some audio tests on smartphones. what i want to do is to generate a tone from the phone's speaker AND record it by the same phone's microphone at the same exact time.
the tone generations runs pretty well, but is it possible to self record?
any ideas?
 
thnx but not really.. i want the phone to generate and record his own sound at the same time (SELF-recording?).

i tried to code multiple threads at the same time, one that generates and the other listens and records but it didnt work.
 
here's the code i m trying :


Code:
public class MainActivity extends Activity {  
	
	int duration=3;
	int sampleRate=44100;
	int numSample=duration*sampleRate;
	double sample[]=new double[numSample];
	double freq1=18000;
	double freq2=19000;
	byte generatedSnd[]= new byte[2*numSample];
	Handler handler=new Handler();
        private static String mFileName = null;
        private static final String LOG_TAG = "GenerateNrecord";
        MediaRecorder  mRecorder=null;
	
	@Override 
 protected void onCreate(Bundle savedInstanceState) {  

     super.onCreate(savedInstanceState);  
      setContentView(R.layout.activity_main);

   }    
	
 protected void onResume()
	{
		super.onResume();
		Thread thread=new Thread(new Runnable()
		{
			public void run()
			{
				genTone();
				handler.post(new Runnable()
				{
					public void run()
					{
						playSound();
						handler.post(new Runnable()
						{
							public void run()
							{
								startRecording();
								 new Timer().schedule(new TimerTask() 
								 {

						                @Override
						                public void run()
						                {
						                    runOnUiThread(new Runnable() 
						                    {

						                        @Override
						                        public void run() 
						                        { stopRecording();
						                        
						                        }         
						                  
						               });
							}   
						}, duration);
					}
				});
			}
		
		
						
			}); 
		
	}
		}); 
		thread.start();
	}		
				
				
				
 void genTone(){

		double instfreq=0, numerator;
		for (int i=0;i<numSample; i++ )
		{
			numerator=(double)(i)/numSample;
			 instfreq	=freq1+(numerator*(freq2-freq1));
			sample[i]=Math.sin(2*Math.PI*i/(sampleRate/instfreq));
			
		}
		 int idx = 0;
	        for (final double dVal : sample) {
	            // scale to maximum amplitude
	            final short val = (short) ((dVal * 32767));
	            // in 16 bit wav PCM, first byte is the low order byte
	            generatedSnd[idx++] = (byte) (val & 0x00ff);
	            generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8);

	        }
		}
		void playSound(){
			AudioTrack audioTrack= null;
			try{
	         audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, generatedSnd.length, AudioTrack.MODE_STATIC);
	        audioTrack.write(generatedSnd, 0, generatedSnd.length);
	        audioTrack.play();
			}
			catch(Exception e)
			{
				System.out.print(e);
			}
	}
	
 private void startRecording() {
		mRecorder = new MediaRecorder();
	        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
	        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
	        mRecorder.setOutputFile(mFileName);
	        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
	        try {
	            mRecorder.prepare();
	        } catch (IOException e) {
	            Log.e(LOG_TAG, "prepare() failed");
	        }

	        mRecorder.start();
	       mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
               mFileName += "/audiorecordtest.3gp";
	    }
		
  private void stopRecording() {
		       
		           
		       mRecorder.stop();
		       mRecorder.reset();
		       mRecorder.release();
		       mRecorder = null;


		        
		    }

    
		}
 
if i test the mediaRecorder code alone it works very well, but when i run them together the generation works but i cannot tell about the recording since the file is always empty.
 
Back
Top Bottom