Note that there are some explanatory texts on larger screens.

plurals
  1. POMix two files audio wav on android use short array
    primarykey
    data
    text
    <p>to write my programs, but it does not work so well, I do not know where I was wrong. Before that I used byte [] to store data from wav (it works pretty good but noisy) so I switched to short [], but the results were very bad.</p> <p><strong>This my code:</strong></p> <pre><code>public class Mix extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); try { mixSound(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void mixSound() throws IOException { in1 = getResources().openRawResource(R.raw.media_b); //16-bit little-endian, 1411kbps, 44100Hz, 2 channels in2 = getResources().openRawResource(R.raw.media_c); //16-bit little-endian, 1411kbps, 44100Hz, 2 channels List&lt;Short&gt; music1 = createMusicArray(in1); List&lt;Short&gt; music2 = createMusicArray(in2); completeStreams(music1, music2); short[] arrayMusic1 = buildShortArray(music1);; short[] arrayMusic2 = buildShortArray(music2); output = new short[arrayMusic1.length]; for (int i = 0; i &lt; output.length; i++) { } saveToFile(); } /** * createMusicArray reads the stream and returns a list of short objects (the samples) */ public List&lt;Short&gt; createMusicArray (InputStream ins) throws IOException { List&lt;Short&gt; musicList = new ArrayList&lt;Short&gt;(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] byteArray = new byte[50*1024]; int i = Integer.MAX_VALUE; while ((i = ins.read(byteArray, 0, byteArray.length)) &gt; 0) { baos.write(byteArray, 0, i); } for (int j = 0; j &lt; byteArray.length; j++) { short objShort = (short)(byteArray[j]); musicList.add(objShort); } return musicList; } /** * completeStreams normalizes the streams by adding a series of '0' shorts at the end of smaller files. At the end the 2 files have all the same length. */ public void completeStreams(List&lt;Short&gt; mListShort_1, List&lt;Short&gt; mListShort_2) { //TODO: check length int size_a = mListShort_1.size(); int size_b = mListShort_2.size(); if (size_a &gt; size_b){ // adding series of '0' for (int i = size_b+1; i &lt;= size_a; i++) { mListShort_2.set(i, (short) 0); } } else if (size_a &lt; size_b) { for (int i = size_a+1; i &lt;= size_b; i++) { mListShort_1.set(i, (short) 0); } } else { //do nothing } } private byte[] shortArrayToByteArray(short[] shortArr) { /** int index; int iterations = shortArr.length; ByteBuffer byteBuffer = ByteBuffer.allocate(shortArr.length * 2); for(index = 0; index != iterations; ++index){ byteBuffer.putShort(shortArr[index]); } return byteBuffer.array(); */ int short_index, byte_index; int iterations = shortArr.length; byte [] buffer = new byte[shortArr.length * 2]; short_index = byte_index = 0; for(/*NOP*/; short_index != iterations; /*NOP*/) { buffer[byte_index] = (byte) (shortArr[short_index] &amp; 0x00FF); buffer[byte_index + 1] = (byte) ((shortArr[short_index] &amp; 0xFF00) &gt;&gt; 8); ++short_index; byte_index += 2; } return buffer; } private byte[] intToByteArray(int i) { byte[] b = new byte[4]; b[0] = (byte) (i &amp; 0x00FF); b[1] = (byte) ((i &gt;&gt; 8) &amp; 0x000000FF); b[2] = (byte) ((i &gt;&gt; 16) &amp; 0x000000FF); b[3] = (byte) ((i &gt;&gt; 24) &amp; 0x000000FF); return b; } private byte[] shortToByteArray(short data) { byte[] b = new byte[2]; b[0] = (byte) (data &amp; 0xff); b[1] = (byte) ((data &gt;&gt; 8) &amp; 0xff); return b; } public static long byteArrayToLong(byte[] b) { int start = 0; int i = 0; int len = 4; int cnt = 0; byte[] tmp = new byte[len]; for (i = start; i &lt; (start + len); i++) { tmp[cnt] = b[i]; cnt++; } long accum = 0; i = 0; for (int shiftBy = 0; shiftBy &lt; 32; shiftBy += 8) { accum |= ((long) (tmp[i] &amp; 0xff)) &lt;&lt; shiftBy; i++; } return accum; } </code></pre> <p>}</p> <p>Can you help me. Thank you very much!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload