Exemplo n.º 1
0
  //  @Test
  public void playTest() throws Exception {
    logger.info("start playTest");
    SourceDataLine audioLine = null;
    MediaAudio samples = beepSamples();
    logger.info("sample is ready");

    AudioFormat format =
        new AudioFormat(
            (float) samples.getSampleRate(),
            (int) samples.getBytesPerSample() * 8,
            samples.getChannels(),
            true,
            false);
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
    audioLine = (SourceDataLine) AudioSystem.getLine(info);
    audioLine.open(format);
    logger.info("beepstart");
    audioLine.start();
    Buffer buffer = samples.getData(0);
    audioLine.write(
        buffer.getByteArray(0, samples.getDataPlaneSize(0)), 0, samples.getDataPlaneSize(0));
    audioLine.drain();
    logger.info("beepend");
    audioLine.close();
    audioLine = null;
  }
Exemplo n.º 2
0
  private void processConvert(Muxer muxer, Encoder encoder) throws Exception {
    muxer.open(null, null);
    MediaPacket packet = MediaPacket.make();
    MediaAudio samples = beepSamples();
    logger.info(samples.toString());
    if (samples.getSampleRate() != encoder.getSampleRate()
        || samples.getFormat() != encoder.getSampleFormat()
        || samples.getChannelLayout() != encoder.getChannelLayout()) {
      final MediaAudioResampler resampler =
          MediaAudioResampler.make(
              encoder.getChannelLayout(),
              encoder.getSampleRate(),
              encoder.getSampleFormat(),
              samples.getChannelLayout(),
              samples.getSampleRate(),
              samples.getFormat());
      resampler.open();
      MediaAudio spl =
          MediaAudio.make(
              samples.getNumSamples(),
              encoder.getSampleRate(),
              encoder.getChannels(),
              encoder.getChannelLayout(),
              encoder.getSampleFormat());
      resampler.resample(spl, samples);
      logger.info(spl.toString());
      logger.info("{}", spl.getNumSamples());
      Assert.assertEquals(spl.getNumSamples(), samples.getNumSamples());
      samples = spl;
    }
    logger.info(samples.toString());

    // we only have one set of samples.
    encoder.encodeAudio(packet, samples);
    logger.info("{}", packet);
    if (packet.isComplete()) muxer.write(packet, false);

    // Flush the encoders
    do {
      encoder.encodeAudio(packet, null);
      logger.info("{}", packet);
      if (packet.isComplete()) {
        muxer.write(packet, false);
      }
    } while (packet.isComplete());
    muxer.close();
  }