/** * Returns the number of samples in this sample chunk. If there are 128 * stereo samples, this method will return 256. That is, it does not * normalise for the number of channels. However, it does normalise for the * size of each sample. So if this is a 16-bit buffer of 256 bytes length, * this method will return 128. * * @return the number of samples in this sample chunk. */ public int getNumberOfSamples() { return this.samples.length / (this.format.getNBits() / 8); }
/** * Returns the number of samples in this sample chunk. If there are 128 * stereo samples, this method will return 256. That is, it does not * normalise for the number of channels. However, it does normalise for the * size of each sample. So if this is a 16-bit buffer of 256 bytes length, * this method will return 128. * * @return the number of samples in this sample chunk. */ public int getNumberOfSamples() { return this.samples.length / (this.format.getNBits() / 8); }
/** * Pads the sample chunk to the given size with zeros. * * @param requiredSampleSetSize * The required sample size */ public void pad(final int requiredSampleSetSize) { final byte[] samples = new byte[requiredSampleSetSize * (this.format.getNBits() / 8)]; System.arraycopy(this.samples, 0, samples, 0, this.samples.length); this.samples = samples; } }
/** * Pads the sample chunk to the given size with zeros. * * @param requiredSampleSetSize * The required sample size */ public void pad(final int requiredSampleSetSize) { final byte[] samples = new byte[requiredSampleSetSize * (this.format.getNBits() / 8)]; System.arraycopy(this.samples, 0, samples, 0, this.samples.length); this.samples = samples; } }
/** * Create a new 8-bit sample buffer using the given samples and the given * audio format. * * @param samples * The samples to buffer. * @param af * The audio format. */ public SampleBuffer8Bit(final SampleChunk samples, final AudioFormat af) { this.format = af; if (this.format == null || this.format.getNBits() != 8) throw new IllegalArgumentException("Number of bits " + "must be 8 if you're instantiating an 8 bit " + "sample buffer. However " + (this.format == null ? "format object was null." : "number of bits in format was " + this.format.getNBits())); this.byteBuffer = samples.getSamples(); this.timecode = samples.getStartTimecode(); }
/** * Create a {@link SampleBuffer}. * * @param af * @param size * @return new {@link SampleBuffer}. */ public static SampleBuffer createSampleBuffer(AudioFormat af, int size) { switch (af.getNBits()) { case 8: return new SampleBuffer8Bit(af, size); case 16: return new SampleBuffer16Bit(af, size); default: return null; } }
/** * From * http://www.javadocexamples.com/java_source/ccs/chaos/NoiseGrabber.java * .html Not sure if this will work for non 44.1KHz samples? * * @return */ private int calculateBufferSize() { final int nmax = (this.maxBufferSize == -1 ? this.mLine.getBufferSize() / 4 : this.maxBufferSize); final int[] FAC44100 = { 7, 7, 5, 5, 3, 3, 2, 2 }; int nwad = 1; for (int i = 0; i < 8; i++) if (nwad * FAC44100[i] <= nmax) nwad *= FAC44100[i]; return nwad << (this.getFormat().getNBits() / 8); }
/** * Create a {@link SampleBuffer}. * * @param s * @param af * @return new {@link SampleBuffer}. */ public static SampleBuffer createSampleBuffer(SampleChunk s, AudioFormat af) { switch (af.getNBits()) { case 8: return new SampleBuffer8Bit(s, af); case 16: return new SampleBuffer16Bit(s, af); default: return null; } } }
/** * From * http://www.javadocexamples.com/java_source/ccs/chaos/NoiseGrabber.java * .html Not sure if this will work for non 44.1KHz samples? * * @return */ private int calculateBufferSize() { final int nmax = (this.maxBufferSize == -1 ? this.mLine.getBufferSize() / 4 : this.maxBufferSize); final int[] FAC44100 = { 7, 7, 5, 5, 3, 3, 2, 2 }; int nwad = 1; for (int i = 0; i < 8; i++) if (nwad * FAC44100[i] <= nmax) nwad *= FAC44100[i]; return nwad << (this.getFormat().getNBits() / 8); }
/** * Create a {@link SampleBuffer}. * * @param af * @param size * @return new {@link SampleBuffer}. */ public static SampleBuffer createSampleBuffer(AudioFormat af, int size) { switch (af.getNBits()) { case 8: return new SampleBuffer8Bit(af, size); case 16: return new SampleBuffer16Bit(af, size); default: return null; } }
/** * Create a {@link SampleBuffer}. * * @param s * @param af * @return new {@link SampleBuffer}. */ public static SampleBuffer createSampleBuffer(SampleChunk s, AudioFormat af) { switch (af.getNBits()) { case 8: return new SampleBuffer8Bit(s, af); case 16: return new SampleBuffer16Bit(s, af); default: return null; } } }
/** * {@inheritDoc} */ @Override public String toString() { return "[Audio: "+getSampleRateKHz()+"KHz, "+getNBits()+"bit, "+ getNumChannels()+" channel"+(getNumChannels()>1?"s":"")+ ", "+(isSigned?"signed":"unsigned")+", "+ (isBigEndian?"big-endian":"little-endian")+"]"; }
/** * {@inheritDoc} */ @Override public String toString() { return "[Audio: "+getSampleRateKHz()+"KHz, "+getNBits()+"bit, "+ getNumChannels()+" channel"+(getNumChannels()>1?"s":"")+ ", "+(isSigned?"signed":"unsigned")+", "+ (isBigEndian?"big-endian":"little-endian")+"]"; }
/** * Updates the number of bits in the source processor's format to match the output format. * * @param ap The source processor * @param output The output format * @return The fixed source format */ private static AudioFormat getFormatBits( final AudioProcessor ap , final AudioFormat output ) { if( ap == null ) return output; final AudioFormat f = ap.getFormat().clone(); f.setNBits( output.getNBits() ); return f; }
/** * Updates the number of bits in the source processor's format to match the output format. * * @param ap The source processor * @param output The output format * @return The fixed source format */ private static AudioFormat getFormatBits( final AudioProcessor ap , final AudioFormat output ) { if( ap == null ) return output; final AudioFormat f = ap.getFormat().clone(); f.setNBits( output.getNBits() ); return f; }
/** * {@inheritDoc} * @see org.openimaj.audio.processor.AudioProcessor#process(org.openimaj.audio.SampleChunk) */ @Override public SampleChunk process( final SampleChunk sample ) throws Exception { if( sample.getFormat().getNBits() != this.outputFormat.getNBits() ) throw new IllegalArgumentException( "The number of bits in the " + "output format is not the same as the sample chunk. Use a " + "resampling conversion first before using the sample-rate " + "converter." ); if( sample.getFormat().getNumChannels() != this.outputFormat.getNumChannels() ) throw new IllegalArgumentException( "The number of channels in the " + "output format is not the same as the sample chunk. Use a " + "channel converter first before using the sample-rate " + "converter." ); if( sample.getFormat().getSampleRateKHz() == this.outputFormat.getSampleRateKHz() ) return sample; final SampleChunk sc = this.sampleConverter.process( sample, this.outputFormat ); sc.setStartTimecode( sample.getStartTimecode() ); return sc; } }
/** * {@inheritDoc} */ @Override public AudioFormat clone() { AudioFormat af = new AudioFormat( getNBits(), getSampleRateKHz(), getNumChannels() ); af.setBigEndian( isBigEndian ); af.setSigned( isSigned ); return af; }
/** * {@inheritDoc} */ @Override public AudioFormat clone() { AudioFormat af = new AudioFormat( getNBits(), getSampleRateKHz(), getNumChannels() ); af.setBigEndian( isBigEndian ); af.setSigned( isSigned ); return af; }
/** * Get a Java Sound API AudioFormat object using this object's * properties. * * @return The Java Sound API Audio Format object. */ public javax.sound.sampled.AudioFormat getJavaAudioFormat() { // Convert the OpenIMAJ audio format to a Java Sound audio format object return new javax.sound.sampled.AudioFormat( (int)this.getSampleRateKHz() * 1000, this.getNBits(), this.getNumChannels(), this.isSigned(), this.isBigEndian() ); } }
/** * Get a Java Sound API AudioFormat object using this object's * properties. * * @return The Java Sound API Audio Format object. */ public javax.sound.sampled.AudioFormat getJavaAudioFormat() { // Convert the OpenIMAJ audio format to a Java Sound audio format object return new javax.sound.sampled.AudioFormat( (int)this.getSampleRateKHz() * 1000, this.getNBits(), this.getNumChannels(), this.isSigned(), this.isBigEndian() ); } }