コード例 #1
0
 @Test
 public void flvNellymoserTest() throws Exception {
   logger.info("flvNellymoserTest");
   Muxer muxer =
       Muxer.make(this.getClass().getCanonicalName() + ".flv_nellymoser.flv", null, null);
   Encoder encoder = Encoder.make(Codec.findEncodingCodec(Codec.ID.CODEC_ID_NELLYMOSER));
   Type findType = null;
   for (Type type : encoder.getCodec().getSupportedAudioFormats()) {
     if (findType == null) {
       findType = type;
     }
     if (type == Type.SAMPLE_FMT_S16) {
       findType = type;
       break;
     }
   }
   logger.info(findType.toString());
   int sampleRate = 44100;
   encoder.setSampleRate(sampleRate);
   Rational encoderTimeBase = Rational.make(1, sampleRate);
   encoder.setTimeBase(encoderTimeBase);
   encoder.setChannels(1);
   encoder.setChannelLayout(Layout.CH_LAYOUT_MONO);
   encoder.setSampleFormat(findType);
   encoder.setFlag(Flag.FLAG_GLOBAL_HEADER, true);
   encoder.open(null, null);
   muxer.addNewStream(encoder);
   processConvert(muxer, encoder);
   logger.info("done");
 }
コード例 #2
0
  /**
   * make sine wave humble MediaAudio.
   *
   * @return
   */
  private MediaAudio beepSamples() {
    int sampleRate = 44100; // 44.1KHz
    int sampleNum = 44100; // 44100 samples(1sec)
    int channel = 2; // 2channel(stereo)
    int tone = 440; // 440Hz tone.
    int bit = 16; // 16bit
    ByteBuffer buffer = ByteBuffer.allocate((int) sampleNum * bit * channel / 8);
    double rad = tone * 2 * Math.PI / sampleRate; // radian for each sample.
    double max = (1 << (bit - 2)) - 1; // ampletude
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    for (int i = 0; i < sampleNum; i++) {
      short data = (short) (Math.sin(rad * i) * max);
      for (int j = 0; j < channel; j++) {
        buffer.putShort(data);
      }
    }
    buffer.flip();

    logger.info("data size for 1sec buffer.:" + buffer.remaining());
    MediaAudio samples =
        MediaAudio.make(
            sampleNum, sampleRate, channel, Layout.CH_LAYOUT_STEREO, Type.SAMPLE_FMT_S16);
    samples.getData(0).put(buffer.array(), 0, 0, buffer.remaining());
    logger.info(
        "{}",
        samples.getDataPlaneSize(0)); // why this size is little bit bigger than original buffer?
    samples.setComplete(true);
    samples.setTimeBase(Rational.make(1, 44100));
    samples.setTimeStamp(0);
    samples.setNumSamples(sampleNum);
    return samples;
  }