Advertisement
yoga1290

Android AudioTrack PCM Testing

Aug 13th, 2011
2,154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.52 KB | None | 0 0
  1. package yoga1290.jjack;
  2.  
  3.  
  4.  
  5. import java.io.ByteArrayInputStream;
  6. import java.io.InputStream;
  7. import java.nio.ByteBuffer;
  8. import java.nio.ShortBuffer;
  9.  
  10. import android.app.Activity;
  11. import android.media.AudioManager;
  12. import android.media.AudioTrack;
  13. import android.os.Bundle;
  14. import android.view.View;
  15. import android.view.View.OnClickListener;
  16. import android.widget.Button;
  17.  
  18. public class Jjack extends Activity implements OnClickListener {
  19.     /** Called when the activity is first created. */
  20.     Button play;
  21.     @Override
  22.     public void onCreate(Bundle savedInstanceState) {
  23.         super.onCreate(savedInstanceState);
  24.         play=new Button(this);
  25.         play.setText("Play");
  26.         play.setOnClickListener(this);
  27.         setContentView(play);
  28.        
  29. //        setContentView(R.layout.main);
  30.     }
  31.  
  32.     @Override
  33.     public void onClick(View arg0) {
  34.  
  35.           //  new AudioSynth01();
  36.         //A buffer to hold two seconds monaural and one
  37.   // second stereo data at 16000 samp/sec for
  38.   // 16-bit samples
  39.   byte audioData[] = new byte[16000*4];
  40.   //The following are audio format parameters.
  41.   // They may be modified by the signal generator
  42.   // at runtime.  Values allowed by Java
  43.   // SDK 1.4.1 are shown in comments.
  44.   //TODO
  45.   float sampleRate =8000.0F;// 16000.0F;
  46.   //Allowable 8000,11025,16000,22050,44100
  47.  
  48. //  int sampleSizeInBits = 16;
  49.   //Allowable 8,16
  50.  
  51.   // int channels = 1;
  52.   //Allowable 1,2
  53.   //Each channel requires two 8-bit bytes per
  54.     // 16-bit sample.
  55.     int bytesPerSamp = 2;
  56.    
  57.    
  58.        
  59.    
  60.    
  61.  
  62.  // boolean signed = true;
  63.   //Allowable true,false
  64.    
  65. //  boolean bigEndian = true;
  66.   //Allowable true,false
  67.  
  68.    
  69.     ByteBuffer byteBuffer = ByteBuffer.wrap(audioData);
  70.     ShortBuffer shortBuffer = byteBuffer.asShortBuffer();
  71.  
  72.     int byteLength = audioData.length;
  73.     int sampLength = byteLength/bytesPerSamp;
  74.     for(int cnt = 0; cnt < sampLength; cnt++){
  75.       double time = cnt/sampleRate;
  76.       double freq = 20000F;//950.0;//arbitrary frequency
  77.       double sinValue =
  78.         (Math.sin(2*Math.PI*freq*time) +
  79.         Math.sin(2*Math.PI*(freq/1.8)*time) +
  80.         Math.sin(2*Math.PI*(freq/1.5)*time))/3.0;
  81.       shortBuffer.put((short)(16000*sinValue));
  82.     }
  83.    
  84.    
  85.           try{
  86.       //Get an input stream on the byte array
  87.       // containing the data
  88.       InputStream byteArrayInputStream =
  89.                         new ByteArrayInputStream(audioData);
  90.  
  91.       //Get the required audio format
  92. //      AudioFormat audioFormat =
  93. //        new AudioFormat(
  94. //                                sampleRate,
  95. //                                sampleSizeInBits,
  96. //                                channels,
  97. //                                signed,
  98. //                                bigEndian);
  99.      
  100.       AudioTrack track= new AudioTrack( AudioManager.STREAM_MUSIC,
  101.                 (int)sampleRate,
  102.                 android.media.AudioFormat.CHANNEL_CONFIGURATION_MONO,
  103.                 android.media.AudioFormat.ENCODING_PCM_16BIT,
  104.                
  105.                 android.media.AudioTrack.getMinBufferSize( (int)sampleRate,
  106.                         android.media.AudioFormat.CHANNEL_CONFIGURATION_STEREO,
  107.                         android.media.AudioFormat.ENCODING_PCM_16BIT ),
  108.                 AudioTrack.MODE_STREAM );
  109.       track.play();
  110.       int cnt;
  111.       byte buff[]=new byte[1024];
  112.       while((cnt=byteArrayInputStream.read(buff))>0)
  113.           track.write(buff, 0, cnt);
  114.  
  115.           }catch (Exception e) {
  116.     }//end catch
  117.         // TODO code application logic here
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement