/** * Appends the given samples to the end of this sample chunk. It is expected * that the given samples are in the same format as this sample chunk; if * they are not an exception is thrown. Side-affects this sample chunk and * will return a reference to this sample chunk. * * @param sample * the samples to add * @return This sample chunk with the bytes appended */ public SampleChunk append(final SampleChunk sample) { // Check the sample formats are the same if (!sample.getFormat().equals(this.format)) throw new IllegalArgumentException("Sample types are not equivalent"); // Get the samples from the given chunk final byte[] x1 = sample.getSamplesAsByteBuffer().array(); // Create an array for the concatenated pair final byte[] newSamples = new byte[this.samples.length + x1.length]; synchronized (this.samples) { System.arraycopy(this.samples, 0, newSamples, 0, this.samples.length); } System.arraycopy(x1, 0, newSamples, this.samples.length, x1.length); // Update this object this.samples = newSamples; return this; }
/** * {@inheritDoc} * * @see org.openimaj.audio.processor.AudioProcessor#process(org.openimaj.audio.SampleChunk) */ @Override public SampleChunk process(final SampleChunk sample) throws Exception { // This allows us to retrieve samples that are scaled to 0..1 float // values final SampleBuffer sb = sample.getSampleBuffer(); for (final float s : sb) if (Math.abs(s) * this.scalar > 1) this.scalar = 1 / Math.abs(s); return sample; }
/** * The sum of the Hanning window * @param samples A representative sample * @return The sum of the hanning window */ public double getWindowSum( final SampleChunk samples ) { return this.getWindowSum( samples.getNumberOfSamples() / samples.getFormat().getNumChannels(), samples.getFormat().getNumChannels() ); }
/** * {@inheritDoc} * * @see org.openimaj.audio.samples.SampleBuffer#getSampleChunk() */ @Override public SampleChunk getSampleChunk() { final SampleChunk sc = new SampleChunk(this.byteBuffer, this.format); sc.setStartTimecode(this.timecode); return sc; }
/** * Create a new 16-bit sample buffer using the given samples and the given * audio format. * * @param samples * The samples to buffer. * @param af * The audio format. */ public SampleBuffer16Bit(final SampleChunk samples, final AudioFormat af) { this.format = af; this.shortBuffer = samples.getSamplesAsByteBuffer().asShortBuffer(); this.samples = samples.getSamples(); this.setStartTimecode(samples.getStartTimecode()); }
/** * Create a new 16-bit sample buffer using the given sample format at the * given size. It does not scale for the number of channels in the audio * format, so you must pre-multiply the number of samples by the number of * channels if you are only counting samples per channel. * * @param af * The audio format of the samples * @param nSamples * The number of samples */ public SampleBuffer16Bit(final AudioFormat af, final int nSamples) { this.format = af.clone(); this.samples = new byte[nSamples * 2]; this.shortBuffer = new SampleChunk(this.samples, this.format) .getSamplesAsByteBuffer().asShortBuffer(); }
/** * {@inheritDoc} * @see org.openimaj.audio.processor.FixedSizeSampleAudioProcessor#process(org.openimaj.audio.SampleChunk) */ @Override public SampleChunk process( final SampleChunk sample ) throws Exception { final SampleBuffer sb = sample.getSampleBuffer(); for( int c = 0; c < sample.getFormat().getNumChannels(); c++ ) { float acc = 0; for( int i = 0; i < this.coefficients.length; i++ ) acc += sb.get(i) * this.coefficients[i]; sb.set( 0, acc ); } return sample; } }
/** * {@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; } }
/** * Prepends the given samples to the front of this sample chunk. It is * expected that the given samples are in the same format as this sample * chunk; if they are not an exception is thrown. Side-affects this sample * chunk and will return a reference to this sample chunk. * * @param sample * the samples to add * @return This sample chunk with the bytes prepended */ public SampleChunk prepend(final SampleChunk sample) { // Check the sample formats are the same if (!sample.getFormat().equals(this.format)) throw new IllegalArgumentException("Sample types are not equivalent"); // Get the samples from the given chunk final byte[] x1 = sample.getSamplesAsByteBuffer().array(); // Create an array for the concatenated pair final byte[] newSamples = new byte[this.samples.length + x1.length]; // Loop through adding the new samples System.arraycopy(x1, 0, newSamples, 0, x1.length); synchronized (this.samples) { System.arraycopy(this.samples, 0, newSamples, x1.length, this.samples.length); } // Update this object this.samples = newSamples; this.setStartTimecode(sample.getStartTimecode().clone()); return this; }
ShortBuffer sb = null; ByteBuffer bb = null; if ((bb = s.getSamplesAsByteBuffer()) != null) sb = bb.asShortBuffer(); else for (int i = 1; i < s.getNumberOfSamples() / s.getFormat().getNumChannels(); i++) { this.img.drawLine( i - 1, sb.get((i - 1) * s.getFormat().getNumChannels()) / 256 + yOffset, i, sb.get(i * s.getFormat().getNumChannels()) / 256 + yOffset, 1f); final double binSize = (s.getFormat().getSampleRateKHz() * 1000) / (f.length / 2); if (s.getNumberOfSamples() != this.sampleChunkSize) { this.sampleChunkSize = s.getNumberOfSamples(); this.spectra = new FImage(800, this.sampleChunkSize / 2); DisplayUtilities.displayName(this.spectra, "spectra");
@Override public SampleChunk nextSampleChunk() { if( this.listIndex >= samples.size() ) return null; if( this.l == null || this.index >= this.l.size() ) { this.l = samples.get(this.listIndex); this.index = 0; this.listIndex++; this.timecodeOffset += this.currentTimecode; } // Get the current sample chunk final SampleChunk sc = this.l.get(this.index).getSampleChunk(); // Work out the timecode at the end of the sample chunk this.currentTimecode = (long)(sc.getStartTimecode().getTimecodeInMilliseconds() + sc.getNumberOfSamples() / sc.getFormat().getSampleRateKHz()); // Add the offset into the current timecode. sc.getStartTimecode().setTimecodeInMilliseconds( sc.getStartTimecode().getTimecodeInMilliseconds() + this.timecodeOffset ); this.index++; return sc; }
final byte[] rawBytes = aSamples.getData(). getByteArray(0, aSamples.getSize()); XuggleAudio.this.currentSamples.setSamples(rawBytes); timestampMillisecs); XuggleAudio.this.currentSamples.setStartTimecode( XuggleAudio.this.currentTimecode); XuggleAudio.this.currentSamples.getFormat().setNumChannels( XuggleAudio.this.getFormat().getNumChannels()); XuggleAudio.this.currentSamples.getFormat().setSigned( XuggleAudio.this.getFormat().isSigned()); XuggleAudio.this.currentSamples.getFormat().setBigEndian( XuggleAudio.this.getFormat().isBigEndian()); XuggleAudio.this.currentSamples.getFormat().setSampleRateKHz( XuggleAudio.this.getFormat().getSampleRateKHz());
@Override public AudioFormat getFormat() { final SampleChunk sc = this.nextSampleChunk(); this.reset(); return sc.getFormat(); } };
this.sampleBuffer.getNumberOfSamples() >= this.requiredSampleSetSize) s = s.clone(); if (this.sampleBuffer != null && this.sampleBuffer.getNumberOfSamples() > 0 && s != null) s.prepend(this.sampleBuffer); this.sampleBuffer = null; int nSamples = s.getNumberOfSamples(); s.append(nextSamples); nSamples = s.getNumberOfSamples(); this.sampleBuffer = s.getSampleSlice(start, nSamples - start); ss = s.getSampleSlice(0, this.requiredSampleSetSize); if (ss.getNumberOfSamples() < this.requiredSampleSetSize) ss.pad(this.requiredSampleSetSize);
final SampleChunk s = new SampleChunk(this.format); s.setSamples(newSamples); s.setStartTimecode(new AudioTimecode( this.getStartTimecode().getTimecodeInMilliseconds() + (long) (start / samplesPerChannelPerMillisec)));
chunks.add( synth.nextSampleChunk().clone() ); final DefaultXYDataset ds1 = AudioSampleRateConversionTest.getDataSet( chunks ); System.out.println( "Chunk "+n+" format: "+chunks.get(n).getFormat() ); resampledChunks1.add( src1.process( chunks.get(n) ).clone() ); final DefaultXYDataset ds2 = AudioSampleRateConversionTest.getDataSet( resampledChunks1 ); for( int n = 0; n < resampledChunks1.size(); n++ ) System.out.println( "1: Resampled Chunk "+n+" format: "+resampledChunks1.get(n).getFormat() ); resampledChunks2.add( src2.process( chunks.get(n) ).clone() ); final DefaultXYDataset ds3 = AudioSampleRateConversionTest.getDataSet( resampledChunks2 ); for( int n = 0; n < resampledChunks2.size(); n++ ) System.out.println( "2: Resampled Chunk "+n+" format: "+resampledChunks2.get(n).getFormat() );
this.buffer = sample.getSampleBuffer(); return sample; this.buffer = sample.clone().getSampleBuffer(); final SampleBuffer b = sample.getSampleBuffer(); for( int i = 0; i < b.size(); i++ )
chunkList.add(sc.getSampleBuffer()); sc.setStartTimecode(this.timecode);
/** * {@inheritDoc} * @see org.openimaj.audio.processor.AudioProcessor#process(org.openimaj.audio.SampleChunk) */ @Override public SampleChunk process( final SampleChunk samples ) { // Detect beats. Note that we stop as soon as we detect a beat. this.beatDetected = false; final SampleBuffer sb = samples.getSampleBuffer(); int i = 0; for(; i < sb.size(); i++ ) { if( this.beatDetected = this.processSample( sb.get(i) ) ) break; } if( this.beatDetected() ) this.beatTimecode.setTimecodeInMilliseconds( (long)( samples.getStartTimecode().getTimecodeInMilliseconds() + i * this.format.getSampleRateKHz() ) ); // System.out.println( beatDetected ); // We return the samples unaltered return samples; }
streamCache.add( sc.clone() );