/** * Process data, adding dither * * @param input a frame * @return processed frame * @throws IllegalArgumentException */ private DoubleData process(Data input) throws IllegalArgumentException { DoubleData output; assert input instanceof DoubleData; double[] inFeatures; DoubleData doubleData = (DoubleData) input; inFeatures = doubleData.getValues(); double[] outFeatures = new double[inFeatures.length]; for (int i = 0; i < inFeatures.length; ++i) { outFeatures[i] = r.nextFloat() * 2 * ditherMax - ditherMax + inFeatures[i]; outFeatures[i] = max(min(outFeatures[i], maxValue), minValue); } output = new DoubleData(outFeatures, doubleData.getSampleRate(), doubleData.getFirstSampleNumber()); return output; } }
/** * Returns the time in milliseconds at which the audio data is collected. * * @return the difference, in milliseconds, between the time the audio data is collected and midnight, January 1, * 1970 */ public long getCollectTime() { return data.getCollectTime(); }
/** * @return a string that describes the data. */ @Override public String toString() { return "SpeechClassifiedData containing " + data.toString() + " classified as " + (isSpeech ? "speech" : "non-speech"); } }
private boolean isAudioStreamContinuous(Data input) { if (input instanceof DoubleData) { DoubleData d = (DoubleData) input; if (lastSampleNum != -1 && lastSampleNum != d.getFirstSampleNumber()) { return false; } lastSampleNum = d.getFirstSampleNumber() + d.getValues().length; } else if (input instanceof DataStartSignal) lastSampleNum = -1; return true; } }
/** * Converts FloatData object to DoubleData. * @param data data to convert * @return converted data */ public static FloatData DoubleData2FloatData(DoubleData data) { int numSamples = data.getValues().length; float[] floatData = new float[numSamples]; double[] values = data.getValues(); for (int i = 0; i < values.length; i++) { floatData[i] = (float) values[i]; } return new FloatData(floatData, data.getSampleRate(), data.getFirstSampleNumber()); } }
curInBufferSize += dd.getValues().length; long firstSample = inBuffer.get(0).getFirstSampleNumber() + curFirstSamplePos; double[] values = dd.getValues(); int copyLength = Math.min(blockSizeSamples - copiedSamples, values.length - curFirstSamplePos); curInBufferSize = inBuffer.isEmpty() ? 0 : inBuffer.get(0).getValues().length - curFirstSamplePos; return new DoubleData(newSampleBlock, sampleRate, firstSample);
public static List<DoubleData> createDataInput(int numSamples, int blockSize, int sampleRate, int offSet) { List<DoubleData> datas = new ArrayList<DoubleData>(); double counter = 1; for (int i = 0; i < numSamples / blockSize; i++) { double[] values = new double[blockSize]; datas.add(new DoubleData(values, sampleRate, (long) counter + offSet)); for (int j = 0; j < values.length; j++) values[j] = counter++ + offSet; } return datas; }
/** * Returns the data values. * * @return the data values */ public double[] getValues() { return data.getValues(); }
if (data instanceof DoubleData) { sampleRate = ((DoubleData) data).getSampleRate(); double[] values = ((DoubleData) data).getValues(); short[] newShorts = Arrays.copyOf(shorts, shorts.length + values.length); for (int i = 0; i < values.length; i++) {
/** * Returns the position of the first sample in the original data. The very first sample number is zero. * * @return the position of the first sample in the original data */ public long getFirstSampleNumber() { return data.getFirstSampleNumber(); }
/** * Returns the sample rate of the data. * * @return the sample rate of the data */ public int getSampleRate() { return data.getSampleRate(); }
/** * Computes the next feature. Advances the pointers as well. * * @return the feature Data computed */ @Override protected Data computeNextFeature() { DoubleData currentCepstrum = cepstraBuffer[currentPosition]; float[] feature = new float[(window * 2 + 1) * currentCepstrum.getValues().length]; int j = 0; for (int k = -window; k <= window; k++) { int position = (currentPosition + k + cepstraBufferSize) % cepstraBufferSize; double[] buffer = cepstraBuffer[position].getValues(); for (double val : buffer) { feature[j++] = (float)val; } } currentPosition = (currentPosition + 1) % cepstraBufferSize ; return (new FloatData(feature, currentCepstrum.getSampleRate(), currentCepstrum.getFirstSampleNumber())); } }
@Test public void testInsertAtBlockEnd() { RandomSampleRepeater rsr = ConfigurationManager.getInstance(RandomSampleRepeater.class); rsr.randStreamStart = true; rsr.useRandSeed = false; rsr.initialize(); rsr.maxRepeatedSamples = 3; rsr.numInsertedSamples = 20; rsr.nextInsertionPoint = 9; // create a dummy DoubleData double[] data = new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; DoubleData dd = new DoubleData(data, 16000, 0); DoubleData extData = rsr.process(dd); double[] expectedData = new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 10}; Assert.assertEquals(extData.getFirstSampleNumber(), 20, 0); Assert.assertEquals(extData.getValues().length, expectedData.length); for (int i = 0; i < expectedData.length; i++) { Assert.assertEquals(extData.getValues()[i], expectedData[i], 0); } }
outputQueue.add(new DoubleData (myWindow, sampleRate, currentFirstSampleNumber));
/** Normalizes the list of Data. */ private void normalizeList() { for (Data data : cepstraList) { if (data instanceof DoubleData) { ((DoubleData)data).getValues()[0] -= agc; } } } }
private boolean isAudioStreamContinuous(Data input) { if (input instanceof DoubleData) { DoubleData d = (DoubleData) input; if (lastSampleNum != -1 && lastSampleNum != d.getFirstSampleNumber()) { return false; } lastSampleNum = d.getFirstSampleNumber() + d.getValues().length; } else if (input instanceof DataStartSignal) lastSampleNum = -1; return true; } }
if (data instanceof DoubleData) { sampleRate = ((DoubleData) data).getSampleRate(); double[] values = ((DoubleData) data).getValues(); short[] newShorts = Arrays.copyOf(shorts, shorts.length + values.length); for (int i = 0; i < values.length; i++) {
@Test public void testUsualInput() throws DataProcessingException { int sampleRate = 1000; input.add(new DataStartSignal(sampleRate)); input.addAll(createDataInput(600, 120, sampleRate, 0)); input.add(new DataEndSignal(0)); List<Data> output = collectOutput(100); Assert.assertEquals(output.size(), 6); Assert.assertEquals(201, ((DoubleData) output.get(2)).getFirstSampleNumber()); Assert.assertTrue(hasIncreasingOrder(output, 600)); }
@Test public void test8KhzSource() throws DataProcessingException, URISyntaxException { AudioFileDataSource dataSource = ConfigurationManager .getInstance(AudioFileDataSource.class); // Test simple WAV. File file = new File(getClass().getResource("test8k.wav").toURI()); dataSource.setAudioFile(file, null); assertThat(dataSource.getData(), instanceOf(DataStartSignal.class)); Data d = dataSource.getData(); assertThat(dataSource.getData(), instanceOf(DoubleData.class)); assertThat(((DoubleData) d).getSampleRate(), equalTo(8000)); while ((d = dataSource.getData()) instanceof DoubleData); assertThat(d, instanceOf(DataEndSignal.class)); }
/** * Process data, creating the power spectrum from an input audio frame. * * @param input input power spectrum * @return power spectrum * @throws java.lang.IllegalArgumentException */ private DoubleData process(DoubleData input) throws IllegalArgumentException { double[] in = input.getValues(); int windowLength = (in.length - 1) << 1; if (filters == null || sampleRate != input.getSampleRate()) { sampleRate = input.getSampleRate(); buildFilterbank(windowLength, numberFilters, minFreq, maxFreq); } else if (in.length != ((windowLength >> 1) + 1)) { throw new IllegalArgumentException("Window size is incorrect: in.length == " + in.length + ", numberFftPoints == " + ((windowLength >> 1) + 1)); } double[] output = new double[numberFilters]; for (int i = 0; i < numberFilters; i++) output[i] = filters[i].apply(in); DoubleData outputMelSpectrum = new DoubleData(output, sampleRate, input.getFirstSampleNumber()); return outputMelSpectrum; }