/** * Gets the size in bytes of an audio sample of a specific * <tt>AudioFormat</tt>. * * @param format the <tt>AudioFormat</tt> to get the size in bytes of an * audio sample of * @return the size in bytes of an audio sample of the specified * <tt>format</tt> */ public static int getSampleSizeInBytes(AudioFormat format) { int sampleSizeInBits = format.getSampleSizeInBits(); switch (sampleSizeInBits) { case 8: return 1; case 16: return 2; default: return sampleSizeInBits / 8; } }
/** * Init the converter to the new format * @param inFormat AudioFormat */ private void initConverter(AudioFormat inFormat) { lastFormat = inFormat; inputSampleSize = inFormat.getSampleSizeInBits(); bigEndian = inFormat.getEndian()==AudioFormat.BIG_ENDIAN; }
/** * Gets the maximum possible value for an audio sample of a specific * <tt>AudioFormat</tt>. * * @param outFormat the <tt>AudioFormat</tt> of which to get the maximum * possible value for an audio sample * @return the maximum possible value for an audio sample of the specified * <tt>AudioFormat</tt> * @throws UnsupportedFormatException if the specified <tt>outFormat</tt> * is not supported by the underlying implementation */ private static int getMaxOutSample(AudioFormat outFormat) throws UnsupportedFormatException { switch(outFormat.getSampleSizeInBits()) { case 8: return Byte.MAX_VALUE; case 16: return Short.MAX_VALUE; case 32: return Integer.MAX_VALUE; case 24: default: throw new UnsupportedFormatException( "Format.getSampleSizeInBits()", outFormat); } }
/** * Initializes and opens a new instance of {@link #resampler} if the * <tt>Format</tt>-related state of this instance deems its existence * necessary. */ private void maybeOpenResampler() { AudioFormat inFormat = this.effectiveFormat; AudioFormat outFormat = this.format; // We are able to translate between mono and stereo. if ((inFormat.getSampleRate() == outFormat.getSampleRate()) && (inFormat.getSampleSizeInBits() == outFormat.getSampleSizeInBits())) { return; } Codec resampler = WASAPIRenderer.maybeOpenResampler(inFormat, outFormat); if (resampler == null) { throw new IllegalStateException( "Failed to open a codec to resample [" + inFormat + "] into [" + outFormat + "]."); } else this.resampler = resampler; }
/** * Gets the frame size measured in bytes defined by a specific * <tt>Format</tt>. * * @param format the <tt>Format</tt> to determine the frame size in * bytes of * @return the frame size measured in bytes defined by the specified * <tt>Format</tt> */ private static int getFrameSizeInBytes(Format format) { AudioFormat audioFormat = (AudioFormat) format; int frameSizeInBits = audioFormat.getFrameSizeInBits(); if (frameSizeInBits <= 0) return (audioFormat.getSampleSizeInBits() / 8) * audioFormat.getChannels(); return (frameSizeInBits <= 8) ? 1 : (frameSizeInBits / 8); }
= audioFormat.getSampleSizeInBits();
/** * {@inheritDoc} */ @Override public Format setOutputFormat(Format format) { Format setOutputFormat = super.setOutputFormat(format); if (setOutputFormat != null) { AudioFormat af = (AudioFormat) setOutputFormat; outputFrameSize = (af.getSampleSizeInBits() / 8) * af.getChannels(); outputSampleRate = (int) af.getSampleRate(); } return setOutputFormat; } }
private void connect() { AudioFormat format = (AudioFormat) getFormat(); int channels = format.getChannels(); if (channels == Format.NOT_SPECIFIED) channels = 1; int sampleSizeInBits = format.getSampleSizeInBits(); double sampleRate = format.getSampleRate(); int framesPerBuffer = (int) ((sampleRate * MacCoreAudioDevice.DEFAULT_MILLIS_PER_BUFFER) / (channels * 1000)); bytesPerBuffer = (sampleSizeInBits / 8) * channels * framesPerBuffer; // Know the Format in which this MacCoreaudioStream will output audio // data so that it can report it without going through its DataSource. this.format = new AudioFormat( AudioFormat.LINEAR, sampleRate, sampleSizeInBits, channels, AudioFormat.LITTLE_ENDIAN, AudioFormat.SIGNED, Format.NOT_SPECIFIED /* frameSizeInBits */, Format.NOT_SPECIFIED /* frameRate */, Format.byteArray); }
if (outputFormat != null) numberOfOutputChannels = outputFormat.getChannels(); inputSampleSize = inFormat.getSampleSizeInBits();
|| (inputCast.getSampleSizeInBits() != 8 && inputCast.getSampleSizeInBits() != Format.NOT_SPECIFIED) || (inputCast.getChannels() != 1 && inputCast.getChannels() != Format.NOT_SPECIFIED)
/** * Gets an <tt>AudioFormat</tt> instance which matches a specific * <tt>AudioFormat</tt> and has 1 channel if the specified * <tt>AudioFormat</tt> has its number of channels not specified. * * @param format the <tt>AudioFormat</tt> to get a match of * @return if the specified <tt>format</tt> has a specific number of * channels, <tt>format</tt>; otherwise, a new <tt>AudioFormat</tt> instance * which matches <tt>format</tt> and has 1 channel */ private static AudioFormat fixChannels(AudioFormat format) { if (Format.NOT_SPECIFIED == format.getChannels()) format = (AudioFormat) format .intersects( new AudioFormat( format.getEncoding(), format.getSampleRate(), format.getSampleSizeInBits(), 1)); return format; }
@Override public Format[] getSupportedOutputFormats(Format input) { if (input == null) return outputFormats; else { if (!(input instanceof AudioFormat)) { return new Format[] {null}; } final AudioFormat inputCast = (AudioFormat) input; if (!inputCast.getEncoding().equals(AudioFormat.GSM_RTP)) { return new Format[] {null}; } final AudioFormat result = new AudioFormat( AudioFormat.GSM, inputCast.getSampleRate(), inputCast.getSampleSizeInBits(), inputCast.getChannels(), inputCast.getEndian(), inputCast.getSigned(), inputCast.getFrameSizeInBits(), inputCast.getFrameRate(), inputCast.getDataType()); return new Format[] {result}; } }
/** * Returns the supported output formats * @param in Format * @return Format[] */ public Format[] getSupportedOutputFormats(Format in) { if (in == null) { return new Format[]{new AudioFormat(AudioFormat.ALAW)}; } if (matches(in, inputFormats) == null) { return new Format[1]; } if (! (in instanceof AudioFormat)) { return new Format[]{new AudioFormat(AudioFormat.ALAW)}; } AudioFormat af = (AudioFormat) in; return new Format[] { new AudioFormat( AudioFormat.ALAW, af.getSampleRate(), af.getSampleSizeInBits(), af.getChannels()) }; }
= format.getChannels() * (((int) format.getSampleRate()) / 50) * (format.getSampleSizeInBits() / 8);
= format.getChannels() * (((int) format.getSampleRate()) / 50) * (format.getSampleSizeInBits() / 8);
af.getEncoding(), af.getSampleRate(), af.getSampleSizeInBits(), af.getChannels(), af.getEndian(),
outputAudioFormat.getEncoding(), outputAudioFormat.getSampleRate(), outputAudioFormat.getSampleSizeInBits(), outputAudioFormat.getChannels(), outputAudioFormat.getEndian(),
af.getEncoding(), af.getSampleRate(), af.getSampleSizeInBits(), af.getChannels(), af.getEndian(),
outputAudioFormat.getEncoding(), outputAudioFormat.getSampleRate(), outputAudioFormat.getSampleSizeInBits(), outputAudioFormat.getChannels(), outputAudioFormat.getEndian(),
af.getEncoding(), af.getSampleRate(), af.getSampleSizeInBits(), af.getChannels(), af.getEndian(),