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

Apps write to a wav file

amirihana

Newbie
Apr 10, 2014
15
0
i m new to Aandroid.
I wanted to write the audio stream i created into a wav file but it didnt work.
here's my code:
Code:
public class MainActivity extends Activity {  
	
	int duration=1;
	int sampleRate=44100;
	int numSample=duration*sampleRate;
	double sample[]=new double[numSample];
	double freq1=20000;
	double freq2=21000;
	byte[] generatedSnd= new byte[2*numSample];
	Handler handler=new Handler();
	
	@Override 
 protected void onCreate(Bundle savedInstanceState) {  

super.onCreate(savedInstanceState);  
setContentView(R.layout.activity_main);
Thread thread=new Thread(new Runnable(){
	public void run(){
		try {
			genTone();
		} catch (IOException e) {
			// TODO Auto-generated catch block                                                                                                                                                                                          
			                                                                                                                                                                                                  
			e.printStackTrace();
		}
		handler.post(new Runnable(){
			public void run(){
				playSound();
			}
		});
	}
});
thread.start();

   }    
	
protected void onResume()
{
	super.onResume();
	
}
	void genTone() throws IOException{
		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)); // max positive sample for signed 16 bit integers is 32767
	            // in 16 bit wave PCM, first byte is the low order byte (pcm: pulse control modulation)
	            generatedSnd[idx++] = (byte) (val & 0x00ff);
	            generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8);


	        }
	 	   DataOutputStream dd=new DataOutputStream( new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+"/test.wav" ));
		   dd.write(generatedSnd,0,2*numSample);
		   dd.close();
			        

		}
		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);
			}
	}
	
	

	
 }

I get the file with a size of 86 Kb but i cant play it on any audio format.
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones