Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. InputStream in1 = getResources().openRawResource(R.raw.myfile);
  2. InputStream in2 = getResources().openRawResource(R.raw.myfile2);
  3.  
  4. Wave w1 = new Wave(in1);
  5. short[] music1 = w1.getSampleAmplitudes();
  6. in1.close();
  7.  
  8. Wave w2 = new Wave(in2);
  9. short[] music2 = w2.getSampleAmplitudes();
  10. in2.close();
  11.  
  12. byte[] output = new byte[(music1.length > music2.length) ? music2.length
  13. : music1.length];
  14.  
  15. for (int i = 0; i < output.length; i++) {
  16.  
  17. float samplef1 = music1[i] / 32768.0f; // 2^7=128
  18. float samplef2 = music2[i] / 32768.0f;
  19.  
  20. float mixed = (samplef1 + samplef2)/2;
  21. //reduce the volume a bit:
  22. mixed *= 0.8;
  23. //hard clipping
  24. if (mixed > 1.0f)
  25. mixed = 1.0f;
  26.  
  27. if (mixed < -1.0f)
  28. mixed = -1.0f;
  29.  
  30. byte outputSample = (byte) (mixed * 128.0f);
  31. output[i] = outputSample;
  32.  
  33. } // for loop
  34.  
  35.  
  36. File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/"+"result.wav");
  37. if (f.exists()) {
  38. f.delete();
  39. }
  40.  
  41. FileOutputStream fo = new FileOutputStream(f);
  42.  
  43. WaveHeader wh = w2.getWaveHeader();
  44. MyWaveHeader mwh = new MyWaveHeader((short) wh.getAudioFormat(),
  45. (short) wh.getChannels(), wh.getSampleRate(),
  46. (short) wh.getBitsPerSample(), output.length);
  47. mwh.write(fo);
  48.  
  49. fo.write(output);
  50. fo.flush();
  51. fo.close();
  52.  
  53. // MusicPlayer
  54. MediaPlayer mp = new MediaPlayer();
  55. // Listeners
  56. mp.setOnCompletionListener(new OnCompletionListener() {
  57.  
  58. @Override
  59. public void onCompletion(MediaPlayer mp) {
  60. // TODO Auto-generated method stub
  61.  
  62. }
  63. }); // Important
  64.  
  65. mp.reset();
  66.  
  67. mp.setDataSource(Environment.getExternalStorageDirectory().getAbsolutePath() + "/"+"birdbellhigh.wav");
  68. mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
  69.  
  70. mp.prepare();
  71. mp.start();
  72.  
  73. AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
  74. 22050, AudioFormat.CHANNEL_OUT_DEFAULT,
  75. AudioFormat.ENCODING_PCM_16BIT, output.length,
  76. AudioTrack.MODE_STREAM);
  77. audioTrack.play();
  78. audioTrack.write(output, 0, output.length);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement