/** Play a random http stream. */
  public void playStream()
      throws EncoderException, LineUnavailableException, IOException, UnsupportedAudioFileException,
          InterruptedException {
    String source;

    SourceDataLine line;
    DataLine.Info info;

    // The source stream
    source = "http://mp3.streampower.be/stubru-high.mp3";
    source = "http://mp3.streampower.be/klara-high.mp3";

    // Set the transcoding to WAV PCM, 16bits LE, 16789Hz (to make sure resampling is done).
    Attributes attributes = DefaultAttributes.WAV_PCM_S16LE_STEREO_44KHZ.getAttributes();
    attributes.setSamplingRate(16789);

    // Stream the same file with on the fly decoding:
    AudioInputStream streamedAudioInputStream = Streamer.stream(source, attributes);
    AudioFormat audioFormat = Streamer.streamAudioFormat(attributes);

    byte[] streamBuffer = new byte[1024];

    info = new DataLine.Info(SourceDataLine.class, audioFormat);
    line = (SourceDataLine) AudioSystem.getLine(info);
    line.open(audioFormat);
    line.start();

    while (streamedAudioInputStream.available() > streamBuffer.length) {
      int bytesRead = streamedAudioInputStream.read(streamBuffer);
      int bytesWrote = line.write(streamBuffer, 0, streamBuffer.length);
      assertEquals(
          "The number of bytes read should match the number of bytes written to the dataline",
          bytesRead,
          bytesWrote);
    }
    line.close();
    streamedAudioInputStream.close();
  }
  /**
   * Streaming and transcoding the same file should yield the exact same results. To test this an
   * mp3 is decoded and resampled and via transcoding and via streaming.
   */
  public void compareTranscodingAndStreaming(String source)
      throws EncoderException, LineUnavailableException, IOException, UnsupportedAudioFileException,
          InterruptedException {
    System.out.println("Testing: " + source);
    // Set the transcoding to WAV PCM, 16bits LE, 16789Hz (to make sure resampling is done).
    Attributes attributes = DefaultAttributes.WAV_PCM_S16LE_STEREO_44KHZ.getAttributes();
    attributes.setSamplingRate(16789);

    // Save the transcoded file:
    File temporaryTranscoded = File.createTempFile("temporaryTranscoded", ".wav");

    Transcoder.transcode(source, temporaryTranscoded.getAbsolutePath(), attributes);
    AudioInputStream transcodedAudioInputStream =
        AudioSystem.getAudioInputStream(temporaryTranscoded);

    // Stream the same file with on the fly decoding:
    AudioInputStream streamedAudioInputStream = Streamer.stream(source, attributes);

    byte[] streamBuffer = new byte[1024];
    byte[] transcodedBuffer = new byte[streamBuffer.length];

    int sampleCounter = 0;
    while (streamedAudioInputStream.available() > streamBuffer.length
        && transcodedAudioInputStream.available() > streamBuffer.length) {

      streamedAudioInputStream.read(streamBuffer);
      transcodedAudioInputStream.read(transcodedBuffer);

      for (int i = 0; i < streamBuffer.length; i++) {
        sampleCounter++;
        assertEquals(
            "Difference at sample: " + sampleCounter, transcodedBuffer[i], streamBuffer[i]);
      }
    }
    streamedAudioInputStream.close();
    transcodedAudioInputStream.close();
    temporaryTranscoded.delete();
  }