예제 #1
0
  public void decodeDsp(Packet packet) {
    if (block.synthesis(packet) == 0) {
      dsp.synthesis_blockin(block);
    }

    int samplesAvail;
    int channels = info.channels;
    while ((samplesAvail = dsp.synthesis_pcmout(pcmAll, index)) > 0) {
      float[][] pcm = pcmAll[0];
      int samplesCanRead = UNCOMP_BUFSIZE / (channels * 2);
      int samplesToRead = (samplesAvail < samplesCanRead ? samplesAvail : samplesCanRead);

      // convert floats to 16 bit signed ints and interleave
      for (int i = 0; i < channels; i++) {
        // each sample is two bytes, the sample for the 2nd
        // channel is at index 2, etc.
        int writeOff = i * 2;
        int readOff = index[i];
        for (int j = 0; j < samplesToRead; j++) {
          int val = (int) (pcm[i][readOff + j] * 32767.0);
          // guard against clipping
          if (val > 32767) {
            val = 32767;
          }
          if (val < -32768) {
            val = -32768;
          }
          uncompBuf[writeOff] = (byte) (val);
          uncompBuf[writeOff + 1] = (byte) (val >> 8);

          writeOff += 2 * channels; // each sample is 2 bytes
        }
      }

      ringBuffer.write(uncompBuf, 0, samplesToRead * channels * 2);

      // tell vorbis how many samples were actualy consumed
      dsp.synthesis_read(samplesToRead);
    }
  }