title: Converting array with samples into byte array
body: <p>I have two-dimensional array of integers. First index indicates the number of channels. The second one indicates the number of sample in the channel. How can I save this array into the audio file? I know, I have to convert it to byte array, but I have no idea how to do that.</p>
  
  <p>// edit</p>
  
  <p>More info. I already have a class for drawing a waveform. It is here:</p>
  
  <p><a href="http://javafaq.nu/java-example-code-716.html" rel="nofollow">http://javafaq.nu/java-example-code-716.html</a></p>
  
  <p>now I want to cut part of this wave and save it to the new file. So I have to cut part of int[][] samplesContainer, convert it to byte array (I don't know how) and then save it to file with the same format as audioInputStream.</p>
  
  <p>// edit</p>
  
  <p>OK. So the biggest problem is to write inverted function to this one:</p>
  
  <pre><code>protected int[][] getSampleArray(byte[] eightBitByteArray) {
  int[][] toReturn = new int[getNumberOfChannels()][eightBitByteArray.length / (2 * getNumberOfChannels())];
  int index = 0;
      //loop through the byte[]
      for (int t = 0; t &lt; eightBitByteArray.length;) {
          //for each iteration, loop through the channels
          for (int a = 0; a &lt; getNumberOfChannels(); a++) {
              //do the byte to sample conversion
              //see AmplitudeEditor for more info
              int low = (int) eightBitByteArray[t];
              t++;
              int high = (int) eightBitByteArray[t];
              t++;
              int sample = (high &lt;&lt; 8) + (low &amp; 0x00ff);
  
              if (sample &lt; sampleMin) {
                  sampleMin = sample;
              } else if (sample &gt; sampleMax) {
                  sampleMax = sample;
              }
              //set the value.
          toReturn[a][index] = sample;
          }
          index++;
          }
      return toReturn;
  }
  </code></pre>
  
  <p>I don't understand why there is second incrementation of t, after high. I also have no idea how can i get high and low from sample.</p>