public static AudioFormat createAudioFormat2(AudioFormat format, int newSampleRate) { AudioFormat af = new AudioFormat(format.sampleRate, format.sampleSizeInBits, format.channelCount, format.signed, format.bigEndian); af.sampleRate = newSampleRate; return af; }
public static AudioCodecMeta createAudioCodecMeta3(String fourcc, ByteBuffer codecPrivate, AudioFormat format, boolean pcm, Label[] labels) { AudioCodecMeta self = new AudioCodecMeta(fourcc, codecPrivate); self.sampleSize = format.getSampleSizeInBits() >> 3; self.channelCount = format.getChannels(); self.sampleRate = format.getSampleRate(); self.endian = format.isBigEndian() ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN; self.pcm = pcm; self.labels = labels; return self; }
public static WavHeader create(AudioFormat af, int size) { WavHeader w = emptyWavHeader(); w.dataSize = size; FmtChunk fmt = newFmtChunk(); int bitsPerSample = af.getSampleSizeInBits(); int bytesPerSample = bitsPerSample / 8; int sampleRate = (int) af.getSampleRate(); w.fmt.bitsPerSample = (short) bitsPerSample; w.fmt.blockAlign = (short) (af.getFrameSize()); w.fmt.byteRate = (int) af.getFrameRate() * af.getFrameSize(); w.fmt.numChannels = (short) af.getChannels(); w.fmt.sampleRate = (int) af.getSampleRate(); return w; }
public static String lookupFourcc(AudioFormat format) { if (format.getSampleSizeInBits() == 16 && !format.isBigEndian()) return "sowt"; else if (format.getSampleSizeInBits() == 24) return "in24"; else throw new IllegalArgumentException("Audio format " + format + " is not supported."); }
/** * Creates wav header for the specified audio format * * @param format * @param samples */ public static WavHeader createWavHeader(AudioFormat format, int samples) { WavHeader w = new WavHeader("RIFF", 40, "WAVE", new FmtChunk((short) 1, (short) format.getChannels(), format.getSampleRate(), format.getSampleRate() * format.getChannels() * (format.getSampleSizeInBits() >> 3), (short) (format.getChannels() * (format.getSampleSizeInBits() >> 3)), (short) format.getSampleSizeInBits()), 44, calcDataSize(format.getChannels(), format.getSampleSizeInBits() >> 3, samples)); return w; }
public static int fromInt(int[] data, int len, AudioFormat format, ByteBuffer buf) { if (!format.isSigned()) throw new NotSupportedException("Unsigned PCM is not supported ( yet? )."); if (format.getSampleSizeInBits() != 16 && format.getSampleSizeInBits() != 24) throw new NotSupportedException(format.getSampleSizeInBits() + " bit PCM is not supported ( yet? )."); if (format.isBigEndian()) { if (format.getSampleSizeInBits() == 16) { return fromInt16BE(buf, data, len); } else { return fromInt24BE(buf, data, len); } } else { if (format.getSampleSizeInBits() == 16) { return fromInt16LE(buf, data, len); } else { return fromInt24LE(buf, data, len); } } }
@Override public int getNOutputs() { return format.getChannels(); } }
void addAudioSampleEntry(AudioFormat format) { AudioSampleEntry ase = AudioSampleEntry.compressedAudioSampleEntry(codec2fourcc.get(codec), (short) 1, (short) 16, format.getChannels(), format.getSampleRate(), 0, 0, 0); addSampleEntry(ase); } }
/** * Deinterleaves audio samples from inb into outs using sample size from * format * * @param format * @param inb * @param outs */ public static void deinterleave(AudioFormat format, ByteBuffer inb, ByteBuffer[] outs) { int bytesPerSample = format.getSampleSizeInBits() >> 3; int bytesPerFrame = bytesPerSample * outs.length; while (inb.remaining() >= bytesPerFrame) { for (int j = 0; j < outs.length; j++) { for (int i = 0; i < bytesPerSample; i++) { outs[j].put(inb.get()); } } } } }
private double audioFrameDuration(AudioTagHeader audioTagHeader) { switch (audioTagHeader.getCodec()) { case AAC: return ((double) 1024) / audioTagHeader.getAudioFormat().getSampleRate(); case MP3: return ((double) 1152) / audioTagHeader.getAudioFormat().getSampleRate(); default: throw new RuntimeException("Audio codec:" + audioTagHeader.getCodec() + " is not supported."); } }
public int read(int[] samples, int max) throws IOException { // Safety net max = Math.min(max, samples.length); ByteBuffer bb = ByteBuffer.allocate(format.samplesToBytes(max)); int read = src.read(bb); bb.flip(); AudioUtil.toInt(format, bb, samples); return format.bytesToFrames(read); }
public int read(ByteBuffer buf) throws IOException { int maxRead = format.framesToBytes(format.bytesToFrames(buf.remaining())); return NIOUtils.readL(_in, buf, maxRead); }
@Override public DemuxerTrackMeta getMeta() { AudioFormat format = header.getFormat(); AudioCodecMeta audioCodecMeta = org.jcodec.common.AudioCodecMeta.fromAudioFormat(format); long totalFrames = dataSize / format.getFrameSize(); return new DemuxerTrackMeta(TrackType.AUDIO, Codec.PCM, (double) totalFrames / format.getFrameRate(), null, (int) totalFrames, null, null, audioCodecMeta); }
@Override public AudioBuffer decodeFrame(ByteBuffer frame, ByteBuffer dst) throws IOException { return new AudioBuffer(frame, format, frame.remaining() / format.getFrameSize()); }
public void close() throws IOException { out.setPosition(0); WavHeader.createWavHeader(format, format.bytesToFrames(written)).write(out); NIOUtils.closeQuietly(out); }
private static void copy(AudioFormat format, ReadableByteChannel is, SeekableByteChannel[] out) throws IOException { ByteBuffer[] outs = new ByteBuffer[out.length]; for (int i = 0; i < out.length; i++) { outs[i] = ByteBuffer.allocate(format.framesToBytes(4096)); } ByteBuffer inb = ByteBuffer.allocate(format.framesToBytes(4096) * out.length); while (is.read(inb) != -1) { inb.flip(); AudioUtil.deinterleave(format, inb, outs); inb.clear(); for (int i = 0; i < out.length; i++) { outs[i].flip(); out[i].write(outs[i]); outs[i].clear(); } } } }
@Override public Packet nextFrame() throws IOException { ByteBuffer data = NIOUtils.fetchFromChannel(ch, frameSize * FRAMES_PER_PKT); if (!data.hasRemaining()) return null; long oldPts = pts; int duration = data.remaining() / frameSize; pts += duration; return Packet.createPacket(data, oldPts, header.getFormat().getFrameRate(), data.remaining() / frameSize, frameNo++, FrameType.KEY, null); }
public PCMMP4MuxerTrack addPCMAudioTrack(AudioFormat format) { return addPCMTrack((int) format.getSampleRate(), 1, (format.getSampleSizeInBits() >> 3) * format.getChannels(), audioSampleEntry(format)); }
public static int toInt(AudioFormat format, ByteBuffer buf, int[] samples) { if (!format.isSigned()) throw new NotSupportedException("Unsigned PCM is not supported ( yet? )."); if (format.getSampleSizeInBits() != 16 && format.getSampleSizeInBits() != 24) throw new NotSupportedException(format.getSampleSizeInBits() + " bit PCM is not supported ( yet? )."); if (format.isBigEndian()) { if (format.getSampleSizeInBits() == 16) { return toInt16BE(buf, samples); } else { return toInt24BE(buf, samples); } } else { if (format.getSampleSizeInBits() == 16) { return toInt16LE(buf, samples); } else { return toInt24LE(buf, samples); } } }
public static String lookupFourcc(AudioFormat format) { if (format.getSampleSizeInBits() == 16 && !format.isBigEndian()) return "sowt"; else if (format.getSampleSizeInBits() == 24) return "in24"; else throw new IllegalArgumentException("Audio format " + format + " is not supported."); }