├── src ├── test │ ├── resources │ │ ├── A.wav │ │ ├── B.wav │ │ ├── C.wav │ │ ├── A2.wav │ │ ├── B2.wav │ │ ├── C2.wav │ │ ├── assert.wav │ │ ├── break.wav │ │ ├── class.wav │ │ ├── final.wav │ │ ├── public.wav │ │ ├── static.wav │ │ ├── abstract.wav │ │ ├── alphabet.wav │ │ ├── boolean.wav │ │ └── words.txt │ └── java │ │ └── com │ │ └── github │ │ └── davidmoten │ │ └── ar │ │ ├── Benchmarks.java │ │ ├── MicrophoneMain.java │ │ ├── FFTTest.java │ │ ├── ComplexTest.java │ │ └── AudioTest.java └── main │ └── java │ └── com │ └── github │ └── davidmoten │ ├── util │ └── Preconditions.java │ └── ar │ ├── HammingWindowFunction.java │ ├── PreEmphasisFunction.java │ ├── Util.java │ ├── MicrophoneOnSubscribe.java │ ├── DiscreteCosineTransformFunction.java │ ├── TriangularBandPassFilterFunction.java │ ├── AudioRecorder.java │ ├── TriangularBandPassFilterBankFunction.java │ ├── Complex.java │ ├── FFT.java │ └── Audio.java ├── .gitignore ├── README.md ├── pom.xml └── LICENSE /src/test/resources/A.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidmoten/audio-recognition/HEAD/src/test/resources/A.wav -------------------------------------------------------------------------------- /src/test/resources/B.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidmoten/audio-recognition/HEAD/src/test/resources/B.wav -------------------------------------------------------------------------------- /src/test/resources/C.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidmoten/audio-recognition/HEAD/src/test/resources/C.wav -------------------------------------------------------------------------------- /src/test/resources/A2.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidmoten/audio-recognition/HEAD/src/test/resources/A2.wav -------------------------------------------------------------------------------- /src/test/resources/B2.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidmoten/audio-recognition/HEAD/src/test/resources/B2.wav -------------------------------------------------------------------------------- /src/test/resources/C2.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidmoten/audio-recognition/HEAD/src/test/resources/C2.wav -------------------------------------------------------------------------------- /src/test/resources/assert.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidmoten/audio-recognition/HEAD/src/test/resources/assert.wav -------------------------------------------------------------------------------- /src/test/resources/break.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidmoten/audio-recognition/HEAD/src/test/resources/break.wav -------------------------------------------------------------------------------- /src/test/resources/class.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidmoten/audio-recognition/HEAD/src/test/resources/class.wav -------------------------------------------------------------------------------- /src/test/resources/final.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidmoten/audio-recognition/HEAD/src/test/resources/final.wav -------------------------------------------------------------------------------- /src/test/resources/public.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidmoten/audio-recognition/HEAD/src/test/resources/public.wav -------------------------------------------------------------------------------- /src/test/resources/static.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidmoten/audio-recognition/HEAD/src/test/resources/static.wav -------------------------------------------------------------------------------- /src/test/resources/abstract.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidmoten/audio-recognition/HEAD/src/test/resources/abstract.wav -------------------------------------------------------------------------------- /src/test/resources/alphabet.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidmoten/audio-recognition/HEAD/src/test/resources/alphabet.wav -------------------------------------------------------------------------------- /src/test/resources/boolean.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidmoten/audio-recognition/HEAD/src/test/resources/boolean.wav -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | pom.xml.tag 3 | pom.xml.releaseBackup 4 | pom.xml.versionsBackup 5 | pom.xml.next 6 | release.properties 7 | .settings 8 | .classpath 9 | .project 10 | -------------------------------------------------------------------------------- /src/test/resources/words.txt: -------------------------------------------------------------------------------- 1 | abstract 2 | assert 3 | boolean 4 | break 5 | byte 6 | case 7 | catch 8 | char 9 | class 10 | const 11 | continue 12 | default 13 | do 14 | double 15 | else 16 | enum 17 | extends 18 | final 19 | finally 20 | float 21 | for 22 | goto 23 | if 24 | implements 25 | import 26 | instanceof 27 | int 28 | interface 29 | long 30 | native 31 | new 32 | package 33 | private 34 | protected 35 | public 36 | return 37 | short 38 | static 39 | strictfp 40 | super 41 | switch 42 | synchronized 43 | this 44 | throw 45 | throws 46 | transient 47 | try 48 | void 49 | volatile 50 | while 51 | -------------------------------------------------------------------------------- /src/main/java/com/github/davidmoten/util/Preconditions.java: -------------------------------------------------------------------------------- 1 | package com.github.davidmoten.util; 2 | 3 | public final class Preconditions { 4 | 5 | public static void checkNotNull(Object o) { 6 | checkNotNull(o, null); 7 | } 8 | 9 | public static void checkNotNull(Object o, String message) { 10 | if (o == null) 11 | throw new NullPointerException(message); 12 | } 13 | 14 | public static void checkArgument(boolean b, String message) { 15 | if (!b) 16 | throw new IllegalArgumentException(message); 17 | } 18 | 19 | public static void checkArgument(boolean b) { 20 | if (!b) 21 | throw new IllegalArgumentException(); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/test/java/com/github/davidmoten/ar/Benchmarks.java: -------------------------------------------------------------------------------- 1 | package com.github.davidmoten.ar; 2 | 3 | import org.openjdk.jmh.annotations.Benchmark; 4 | import org.openjdk.jmh.annotations.Scope; 5 | import org.openjdk.jmh.annotations.State; 6 | 7 | @State(Scope.Benchmark) 8 | public class Benchmarks { 9 | 10 | private final Complex[] input = createInput(); 11 | 12 | @Benchmark 13 | public void fft() { 14 | // FFT of original data 15 | FFT.fft(input); 16 | } 17 | 18 | private static Complex[] createInput() { 19 | int n = 2048; 20 | Complex[] x = new Complex[n]; 21 | 22 | // original data 23 | for (int i = 0; i < n; i++) { 24 | x[i] = new Complex(i, 0); 25 | x[i] = new Complex(-2 * Math.random() + 1, 0); 26 | } 27 | return x; 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /src/main/java/com/github/davidmoten/ar/HammingWindowFunction.java: -------------------------------------------------------------------------------- 1 | package com.github.davidmoten.ar; 2 | 3 | import rx.functions.Func1; 4 | 5 | public class HammingWindowFunction implements Func1 { 6 | 7 | private double alpha; 8 | 9 | public HammingWindowFunction(double alpha) { 10 | this.alpha = alpha; 11 | } 12 | 13 | public HammingWindowFunction() { 14 | this(0.46); 15 | } 16 | 17 | @Override 18 | public double[] call(double[] x) { 19 | if (x.length == 0) 20 | return new double[0]; 21 | else { 22 | double[] y = new double[x.length]; 23 | for (int i = 0; i < x.length; i++) { 24 | y[i] = x[i] * ((1 - alpha) - alpha * Math.cos(2 * Math.PI * i / (x.length - 1))); 25 | } 26 | return y; 27 | } 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/test/java/com/github/davidmoten/ar/MicrophoneMain.java: -------------------------------------------------------------------------------- 1 | package com.github.davidmoten.ar; 2 | 3 | import java.util.List; 4 | 5 | import rx.Observable; 6 | import rx.functions.Action1; 7 | import rx.functions.Func1; 8 | 9 | import com.fastdtw.timeseries.TimeSeries; 10 | 11 | public class MicrophoneMain { 12 | public static void main(String[] args) { 13 | Audio.microphone().buffer(100) 14 | .flatMap(new Func1, Observable>() { 15 | 16 | @Override 17 | public Observable call(List buffer) { 18 | return Audio.timeSeries(Observable.from(buffer), 256, 19 | 156, 20, 13); 20 | } 21 | }) 22 | // for each 23 | .forEach(new Action1() { 24 | 25 | @Override 26 | public void call(TimeSeries t) { 27 | System.out.println(t.size()); 28 | } 29 | }); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/test/java/com/github/davidmoten/ar/FFTTest.java: -------------------------------------------------------------------------------- 1 | package com.github.davidmoten.ar; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import org.junit.Test; 6 | 7 | public class FFTTest { 8 | 9 | private static final double PRECISION = 0.00000001; 10 | 11 | @Test 12 | public void testFft() { 13 | double[] values = new double[] { -0.0348042583933070, 0.07910192950176387, 14 | 0.7233322451735928, 0.1659819820667019 }; 15 | Complex[] a = Complex.toComplex(values); 16 | Complex[] b = FFT.fft(a); 17 | assertEquals(0.9336118983487516, b[0].re(), PRECISION); 18 | assertEquals(0, b[0].im(), PRECISION); 19 | assertEquals(-0.7581365035668999, b[1].re(), PRECISION); 20 | assertEquals(0.08688005256493803, b[1].im(), PRECISION); 21 | assertEquals(0.44344407521182005, b[2].re(), PRECISION); 22 | assertEquals(0, b[2].im(), PRECISION); 23 | assertEquals(-0.7581365035668999, b[3].re(), PRECISION); 24 | assertEquals(-0.08688005256493803, b[3].im(), PRECISION); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/github/davidmoten/ar/PreEmphasisFunction.java: -------------------------------------------------------------------------------- 1 | package com.github.davidmoten.ar; 2 | 3 | import rx.functions.Func1; 4 | 5 | /** 6 | *

7 | * This function emphasizes higher frequencies in an audio signal represented by 8 | * a an array of double. This has the effect of sharpening speech in particular. 9 | *

10 | * 11 | *

12 | * Audio samples here. 15 | *

16 | * 17 | */ 18 | public class PreEmphasisFunction implements Func1 { 19 | 20 | private final double alpha; 21 | 22 | public PreEmphasisFunction(double alpha) { 23 | this.alpha = alpha; 24 | } 25 | 26 | public PreEmphasisFunction() { 27 | this(0.95); 28 | } 29 | 30 | @Override 31 | public double[] call(double[] x) { 32 | if (x.length == 0) 33 | return new double[0]; 34 | else { 35 | double[] y = new double[x.length]; 36 | y[0] = x[0]; 37 | for (int i = 1; i < x.length; i++) { 38 | y[i] = x[i] - alpha * x[i - 1]; 39 | } 40 | return y; 41 | } 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/github/davidmoten/ar/Util.java: -------------------------------------------------------------------------------- 1 | package com.github.davidmoten.ar; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import rx.functions.Func1; 7 | 8 | public class Util { 9 | 10 | public static final Func1> TO_LIST = new Func1>() { 11 | 12 | @Override 13 | public List call(double[] values) { 14 | List list = new ArrayList(values.length); 15 | for (double value : values) 16 | list.add(value); 17 | return list; 18 | } 19 | }; 20 | 21 | public static Func1, double[]> TO_DOUBLE_ARRAY = new Func1, double[]>() { 22 | 23 | @Override 24 | public double[] call(List list) { 25 | double[] x = new double[list.size()]; 26 | for (int i = 0; i < x.length; i++) { 27 | x[i] = list.get(0).doubleValue(); 28 | } 29 | return x; 30 | } 31 | }; 32 | 33 | public static Func1, Boolean> hasSize(final int size) { 34 | return new Func1, Boolean>() { 35 | 36 | @Override 37 | public Boolean call(List list) { 38 | return list.size() == size; 39 | } 40 | }; 41 | } 42 | 43 | static int valueFromTwoBytesEndian(int b1, int b2, boolean isBigEndian) { 44 | if (b1 < 0) 45 | b1 += 0x100; 46 | if (b2 < 0) 47 | b2 += 0x100; 48 | if (!isBigEndian) 49 | return (b1 << 8) + b2; 50 | else 51 | return b1 + (b2 << 8); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # audio-recognition 2 | Matches audio to small vocabulary using fast fourier transforms and Mel Frequency Cepstral Coefficients (MFCCs). 3 | 4 | Status: *pre-alpha* 5 | 6 | About the algorithm 7 | ------------------------- 8 | A wave file is processed with the following techniques: 9 | 10 | * Pre-emphasis (emphasizes higher frequencies) 11 | * Framing (chopping up the wav into frames of say 256 values with 156 overlap) 12 | * Hamming windowing (enforce periodicity of signal so FFT behaves well) 13 | * Fast Fourier Transform (FFT) 14 | * Triangular Bandpass Filters using Mel frequencies 15 | * Discrete Cosine Transform (DCT) 16 | 17 | The above processing gives for each frame of 256 values an list of 13 decimal values (MFCCs). The first 18 | value is a function of the overall power of the signal during the frame and the rest describe 19 | the frequency spectrum. 20 | 21 | To compare wave file A with wave file B we calculate the MFCCs for each then use FastDTW to measure the 22 | distance after warping between the two sets of MFCCs. 23 | 24 | 25 | Development resources 26 | ------------------------ 27 | [Audio signal processing book](http://mirlab.org/jang/books/audiosignalprocessing/speechFeatureMfcc.asp?title=12-2%20MFCC)
28 | [Sound pattern matching using FastFourier Transform in Windows Phone](http://developer.nokia.com/community/wiki/Sound_pattern_matching_using_Fast_Fourier_Transform_in_Windows_Phone)
29 | [Mel Frequency Cepstral Coefficient (MFCC) tutorial](http://practicalcryptography.com/miscellaneous/machine-learning/guide-mel-frequency-cepstral-coefficients-mfccs/) 30 | 31 | -------------------------------------------------------------------------------- /src/main/java/com/github/davidmoten/ar/MicrophoneOnSubscribe.java: -------------------------------------------------------------------------------- 1 | package com.github.davidmoten.ar; 2 | 3 | import java.util.Arrays; 4 | 5 | import javax.sound.sampled.AudioFormat; 6 | import javax.sound.sampled.AudioSystem; 7 | import javax.sound.sampled.DataLine; 8 | import javax.sound.sampled.LineUnavailableException; 9 | import javax.sound.sampled.TargetDataLine; 10 | 11 | import rx.Observable.OnSubscribe; 12 | import rx.Subscriber; 13 | 14 | public class MicrophoneOnSubscribe implements OnSubscribe { 15 | // see 16 | // http://www.developer.com/java/other/article.php/1572251/Java-Sound-Getting-Started-Part-1-Playback.htm#Complete%20Program%20Listings 17 | 18 | private final int bufferSize; 19 | private final AudioFormat format; 20 | 21 | public MicrophoneOnSubscribe(int bufferSize, AudioFormat format) { 22 | this.bufferSize = bufferSize; 23 | this.format = format; 24 | } 25 | 26 | @Override 27 | public void call(final Subscriber child) { 28 | 29 | DataLine.Info info = new DataLine.Info(TargetDataLine.class, format); 30 | 31 | // checks if system supports the data line 32 | if (!AudioSystem.isLineSupported(info)) { 33 | child.onError(new RuntimeException("line not supported for format " 34 | + format)); 35 | return; 36 | } 37 | 38 | try { 39 | TargetDataLine line = (TargetDataLine) AudioSystem.getLine(info); 40 | line.open(format); 41 | System.out.println("Starting capture..."); 42 | line.start(); 43 | byte[] buffer = new byte[bufferSize]; 44 | while (!child.isUnsubscribed() && line.isOpen()) { 45 | int count = line.read(buffer, 0, bufferSize); 46 | if (count > 0) 47 | child.onNext(Arrays.copyOf(buffer, count)); 48 | } 49 | child.onCompleted(); 50 | line.close(); 51 | } catch (LineUnavailableException e) { 52 | child.onError(e); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/github/davidmoten/ar/DiscreteCosineTransformFunction.java: -------------------------------------------------------------------------------- 1 | package com.github.davidmoten.ar; 2 | 3 | import rx.functions.Func1; 4 | 5 | /** 6 | * Applies DCT. Threadsafe. 7 | */ 8 | public class DiscreteCosineTransformFunction implements Func1 { 9 | 10 | private final static double LOG_MINIMUM = 1e-4; 11 | 12 | private final int cepstrumSize; 13 | private final double[][] melCosine; 14 | private final int numMelFilters; 15 | 16 | public DiscreteCosineTransformFunction(int cepstrumSize, int numMelFilters) { 17 | this.cepstrumSize = cepstrumSize; 18 | this.numMelFilters = numMelFilters; 19 | this.melCosine = createMelCosine(cepstrumSize, numMelFilters); 20 | } 21 | 22 | @Override 23 | public double[] call(double[] input) { 24 | if (input.length == 0) 25 | return new double[0]; 26 | if (input.length != numMelFilters) { 27 | throw new IllegalArgumentException("input should be of length " + numMelFilters 28 | + " but was " + input.length); 29 | } 30 | double[] melspectrum = new double[input.length]; 31 | for (int i = 0; i < melspectrum.length; ++i) { 32 | melspectrum[i] = Math.log(input[i] + LOG_MINIMUM); 33 | } 34 | return applyMelCosine(melspectrum); 35 | } 36 | 37 | private static double[][] createMelCosine(int cepstrumSize, int numMelFilters) { 38 | double[][] x = new double[cepstrumSize][numMelFilters]; 39 | double period = 2 * numMelFilters; 40 | for (int i = 0; i < cepstrumSize; i++) { 41 | double frequency = 2 * Math.PI * i / period; 42 | for (int j = 0; j < numMelFilters; j++) { 43 | x[i][j] = Math.cos(frequency * (j + 0.5)); 44 | } 45 | } 46 | return x; 47 | } 48 | 49 | private double[] applyMelCosine(double[] melspectrum) { 50 | double[] cepstrum = new double[cepstrumSize]; 51 | double period = numMelFilters; 52 | double beta = 0.5; 53 | for (int i = 0; i < cepstrum.length; i++) { 54 | if (numMelFilters > 0) { 55 | double[] melcosine_i = melCosine[i]; 56 | int j = 0; 57 | cepstrum[i] += (beta * melspectrum[j] * melcosine_i[j]); 58 | for (j = 1; j < numMelFilters; j++) { 59 | cepstrum[i] += (melspectrum[j] * melcosine_i[j]); 60 | } 61 | cepstrum[i] /= period; 62 | } 63 | } 64 | return cepstrum; 65 | } 66 | } -------------------------------------------------------------------------------- /src/test/java/com/github/davidmoten/ar/ComplexTest.java: -------------------------------------------------------------------------------- 1 | package com.github.davidmoten.ar; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import org.junit.Test; 6 | 7 | public class ComplexTest { 8 | 9 | private static final double PRECISION = 0.00001; 10 | private static Complex a = new Complex(5.0, 6.0); 11 | private static Complex b = new Complex(-3.0, 4.0); 12 | 13 | // System.out.println("a = " + a); 14 | // System.out.println("b = " + b); 15 | // System.out.println("Re(a) = " + a.re()); 16 | // System.out.println("Im(a) = " + a.im()); 17 | // System.out.println("b + a = " + b.plus(a)); 18 | // System.out.println("a - b = " + a.minus(b)); 19 | // System.out.println("a * b = " + a.times(b)); 20 | // System.out.println("b * a = " + b.times(a)); 21 | // System.out.println("a / b = " + a.divides(b)); 22 | // System.out.println("(a / b) * b = " + a.divides(b).times(b)); 23 | // System.out.println("conj(a) = " + a.conjugate()); 24 | // System.out.println("|a| = " + a.abs()); 25 | // System.out.println("tan(a) = " + a.tan()); 26 | 27 | @Test 28 | public void testConstructor() { 29 | assertEquals(5.0, a.re(), PRECISION); 30 | assertEquals(6.0, a.im(), PRECISION); 31 | } 32 | 33 | @Test 34 | public void testAddition() { 35 | Complex c = a.plus(b); 36 | assertEquals(2.0, c.re(), PRECISION); 37 | assertEquals(10.0, c.im(), PRECISION); 38 | } 39 | 40 | @Test 41 | public void testSubtraction() { 42 | Complex c = a.minus(b); 43 | assertEquals(8.0, c.re(), PRECISION); 44 | assertEquals(2.0, c.im(), PRECISION); 45 | } 46 | 47 | @Test 48 | public void testMultiplication() { 49 | Complex c = a.times(b); 50 | assertEquals(-39.0, c.re(), PRECISION); 51 | assertEquals(2.0, c.im(), PRECISION); 52 | } 53 | 54 | @Test 55 | public void testDivision() { 56 | Complex c = a.times(b).divides(b); 57 | assertEquals(5.0, c.re(), PRECISION); 58 | assertEquals(6.0, c.im(), PRECISION); 59 | } 60 | 61 | @Test 62 | public void testConjugate() { 63 | Complex c = a.conjugate(); 64 | assertEquals(5.0, c.re(), PRECISION); 65 | assertEquals(-6.0, c.im(), PRECISION); 66 | } 67 | 68 | @Test 69 | public void testAbsoluteValue() { 70 | assertEquals(Math.sqrt(25 + 36), a.abs(), PRECISION); 71 | } 72 | 73 | @Test 74 | public void testTanValue() { 75 | Complex c = b.tan(); 76 | assertEquals(0.00018734, c.re(), PRECISION); 77 | assertEquals(0.99935598738, c.im(), PRECISION); 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/com/github/davidmoten/ar/TriangularBandPassFilterFunction.java: -------------------------------------------------------------------------------- 1 | package com.github.davidmoten.ar; 2 | 3 | import rx.functions.Func1; 4 | 5 | import com.github.davidmoten.util.Preconditions; 6 | 7 | public class TriangularBandPassFilterFunction implements Func1 { 8 | 9 | private final double[] weight; 10 | private final int initialFrequencyIndex; 11 | 12 | public TriangularBandPassFilterFunction(double lowestFrequency, double centreFrequency, 13 | double highestFrequency, double startFrequency, double deltaFrequency) { 14 | Preconditions.checkArgument(deltaFrequency > 0); 15 | Preconditions.checkArgument(Math.round(highestFrequency - lowestFrequency) > 0); 16 | Preconditions.checkArgument(Math.round(centreFrequency - lowestFrequency) > 0); 17 | Preconditions.checkArgument(Math.round(highestFrequency - centreFrequency) > 0); 18 | int numBuckets = (int) Math 19 | .round((highestFrequency - lowestFrequency) / deltaFrequency + 1); 20 | Preconditions.checkArgument(numBuckets > 0); 21 | 22 | double[] weight = new double[numBuckets]; 23 | double filterHeight = 2.0 / (highestFrequency - lowestFrequency); 24 | double leftGradient = filterHeight / (centreFrequency - lowestFrequency); 25 | double rightGradient = filterHeight / (centreFrequency - highestFrequency); 26 | 27 | double f; 28 | int bucketIndex = 0; 29 | // compute the weight for each frequency bucket 30 | for (f = startFrequency; f <= highestFrequency; f += deltaFrequency) { 31 | if (f < centreFrequency) 32 | weight[bucketIndex] = leftGradient * (f - lowestFrequency); 33 | else 34 | weight[bucketIndex] = filterHeight + rightGradient * (f - centreFrequency); 35 | bucketIndex++; 36 | } 37 | this.weight = weight; 38 | this.initialFrequencyIndex = (int) Math.round(startFrequency / deltaFrequency); 39 | } 40 | 41 | /* 42 | * (non-Javadoc) 43 | * 44 | * @see rx.functions.Func1#call(java.lang.Object) 45 | * 46 | * Returns the weighted average of power for the frequencies within the 47 | * confines of this triangular filter pass band. 48 | * 49 | * @parameter signal the power spectrum to be filtered 50 | * 51 | * @return the weighted average of power 52 | */ 53 | @Override 54 | public Double call(double[] signal) { 55 | double result = 0.0; 56 | for (int i = 0; i < this.weight.length; i++) { 57 | int indexSignal = this.initialFrequencyIndex + i; 58 | if (indexSignal < signal.length) { 59 | result += signal[indexSignal] * this.weight[i]; 60 | } 61 | } 62 | return result; 63 | } 64 | 65 | } -------------------------------------------------------------------------------- /src/main/java/com/github/davidmoten/ar/AudioRecorder.java: -------------------------------------------------------------------------------- 1 | package com.github.davidmoten.ar; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | 6 | import javax.sound.sampled.AudioFileFormat; 7 | import javax.sound.sampled.AudioFormat; 8 | import javax.sound.sampled.AudioInputStream; 9 | import javax.sound.sampled.AudioSystem; 10 | import javax.sound.sampled.DataLine; 11 | import javax.sound.sampled.LineUnavailableException; 12 | import javax.sound.sampled.TargetDataLine; 13 | 14 | /** 15 | * A sample program is to demonstrate how to record sound in Java author: 16 | * www.codejava.net 17 | */ 18 | public class AudioRecorder { 19 | // record duration, in milliseconds 20 | static final long RECORD_TIME = 1000; // 1 minute 21 | 22 | // path of the wav file 23 | File wavFile = new File("target/recording.wav"); 24 | 25 | // format of audio file 26 | AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE; 27 | 28 | // the line from which audio data is captured 29 | TargetDataLine line; 30 | 31 | /** 32 | * Defines an audio formats 33 | */ 34 | AudioFormat createAudioFormat() { 35 | float sampleRate = 16000; 36 | int sampleSizeInBits = 8; 37 | int channels = 2; 38 | boolean signed = true; 39 | boolean bigEndian = true; 40 | AudioFormat format = new AudioFormat(sampleRate, sampleSizeInBits, 41 | channels, signed, bigEndian); 42 | return format; 43 | } 44 | 45 | /** 46 | * Captures the sound and record into a WAV file 47 | */ 48 | void start() { 49 | try { 50 | AudioFormat format = createAudioFormat(); 51 | DataLine.Info info = new DataLine.Info(TargetDataLine.class, format); 52 | 53 | // checks if system supports the data line 54 | if (!AudioSystem.isLineSupported(info)) { 55 | System.out.println("Line not supported"); 56 | System.exit(0); 57 | } 58 | line = (TargetDataLine) AudioSystem.getLine(info); 59 | line.open(format); 60 | line.start(); // start capturing 61 | 62 | System.out.println("Start capturing..."); 63 | 64 | AudioInputStream ais = new AudioInputStream(line); 65 | 66 | System.out.println("Start recording..."); 67 | 68 | // start recording 69 | AudioSystem.write(ais, fileType, wavFile); 70 | 71 | } catch (LineUnavailableException e) { 72 | throw new RuntimeException(e); 73 | } catch (IOException e) { 74 | throw new RuntimeException(e); 75 | } 76 | } 77 | 78 | /** 79 | * Closes the target data line to finish capturing and recording 80 | */ 81 | void finish() { 82 | line.stop(); 83 | line.close(); 84 | System.out.println("Finished"); 85 | } 86 | 87 | /** 88 | * Entry to run the program 89 | */ 90 | public static void main(String[] args) { 91 | final AudioRecorder recorder = new AudioRecorder(); 92 | 93 | // creates a new thread that waits for a specified 94 | // of time before stopping 95 | Thread stopper = new Thread(new Runnable() { 96 | @Override 97 | public void run() { 98 | try { 99 | Thread.sleep(RECORD_TIME); 100 | } catch (InterruptedException ex) { 101 | ex.printStackTrace(); 102 | } 103 | recorder.finish(); 104 | } 105 | }); 106 | 107 | stopper.start(); 108 | 109 | // start recording 110 | recorder.start(); 111 | } 112 | } -------------------------------------------------------------------------------- /src/main/java/com/github/davidmoten/ar/TriangularBandPassFilterBankFunction.java: -------------------------------------------------------------------------------- 1 | package com.github.davidmoten.ar; 2 | 3 | import rx.functions.Func1; 4 | 5 | import com.github.davidmoten.util.Preconditions; 6 | 7 | public class TriangularBandPassFilterBankFunction implements Func1 { 8 | 9 | private final TriangularBandPassFilterFunction[] filters; 10 | private final int inputLength; 11 | 12 | public TriangularBandPassFilterBankFunction(double minFreq, double maxFreq, int numberFilters, 13 | int sampleRate, int inputLength) { 14 | this.inputLength = inputLength; 15 | int numberFftPoints = (inputLength - 1) << 1; 16 | this.filters = createFilters(numberFftPoints, numberFilters, minFreq, maxFreq, sampleRate); 17 | } 18 | 19 | public TriangularBandPassFilterBankFunction(int numFilters, int inputLength) { 20 | this(130, 6800, numFilters, 16000, inputLength); 21 | } 22 | 23 | private static TriangularBandPassFilterFunction[] createFilters(int numberFftPoints, 24 | int numberFilters, double minFreq, double maxFreq, int sampleRate) { 25 | Preconditions.checkArgument(numberFilters >= 0); 26 | Preconditions.checkArgument(numberFftPoints >= 0); 27 | 28 | TriangularBandPassFilterFunction[] filters = new TriangularBandPassFilterFunction[numberFilters]; 29 | 30 | double[] leftEdge = new double[numberFilters]; 31 | double[] centerFreq = new double[numberFilters]; 32 | double[] rightEdge = new double[numberFilters]; 33 | 34 | double deltaFreq = (double) sampleRate / numberFftPoints; 35 | double minFreqMel = linearToMelFrequency(minFreq); 36 | double maxFreqMel = linearToMelFrequency(maxFreq); 37 | double deltaFreqMel = (maxFreqMel - minFreqMel) / (numberFilters + 1); 38 | leftEdge[0] = nearestFrequencyBucket(minFreq, deltaFreq); 39 | double nextEdgeMel = minFreqMel; 40 | double nextEdge; 41 | for (int i = 0; i < numberFilters; i++) { 42 | nextEdgeMel += deltaFreqMel; 43 | nextEdge = melToLinearFrequency(nextEdgeMel); 44 | centerFreq[i] = nearestFrequencyBucket(nextEdge, deltaFreq); 45 | if (i > 0) 46 | rightEdge[i - 1] = centerFreq[i]; 47 | if (i < numberFilters - 1) 48 | leftEdge[i + 1] = centerFreq[i]; 49 | } 50 | nextEdgeMel = nextEdgeMel + deltaFreqMel; 51 | nextEdge = melToLinearFrequency(nextEdgeMel); 52 | rightEdge[numberFilters - 1] = nearestFrequencyBucket(nextEdge, deltaFreq); 53 | for (int i = 0; i < numberFilters; i++) { 54 | double initialFreqBin = nearestFrequencyBucket(leftEdge[i], deltaFreq); 55 | if (initialFreqBin < leftEdge[i]) 56 | initialFreqBin += deltaFreq; 57 | filters[i] = new TriangularBandPassFilterFunction(leftEdge[i], centerFreq[i], 58 | rightEdge[i], initialFreqBin, deltaFreq); 59 | } 60 | return filters; 61 | } 62 | 63 | @Override 64 | public double[] call(double[] input) { 65 | Preconditions.checkArgument(input.length == 0 || input.length == inputLength, 66 | "Window size is incorrect:" + input.length); 67 | if (input.length == 0) 68 | return new double[0]; 69 | double[] output = new double[filters.length]; 70 | for (int i = 0; i < output.length; i++) { 71 | output[i] = filters[i].call(input); 72 | } 73 | return output; 74 | } 75 | 76 | private static double linearToMelFrequency(double f) { 77 | return (2595.0 * (Math.log(1.0 + f / 700.0) / Math.log(10.0))); 78 | } 79 | 80 | private static double melToLinearFrequency(double f) { 81 | return (700.0 * (Math.pow(10.0, (f / 2595.0)) - 1.0)); 82 | } 83 | 84 | private static double nearestFrequencyBucket(double inFreq, double stepFreq) { 85 | Preconditions.checkArgument(stepFreq != 0, "step frequency is zero"); 86 | return stepFreq * Math.round(inFreq / stepFreq); 87 | } 88 | 89 | } -------------------------------------------------------------------------------- /src/main/java/com/github/davidmoten/ar/Complex.java: -------------------------------------------------------------------------------- 1 | package com.github.davidmoten.ar; 2 | 3 | import java.util.Collection; 4 | 5 | /************************************************************************* 6 | * Compilation: javac Complex.java Execution: java Complex 7 | * 8 | * Data type for complex numbers. 9 | * 10 | * The data type is "immutable" so once you create and initialize a Complex 11 | * object, you cannot change it. The "final" keyword when declaring re and im 12 | * enforces this rule, making it a compile-time error to change the .re or .im 13 | * fields after they've been initialized. 14 | * 15 | * % java Complex a = 5.0 + 6.0i b = -3.0 + 4.0i Re(a) = 5.0 Im(a) = 6.0 b + a = 16 | * 2.0 + 10.0i a - b = 8.0 + 2.0i a * b = -39.0 + 2.0i b * a = -39.0 + 2.0i a / 17 | * b = 0.36 - 1.52i (a / b) * b = 5.0 + 6.0i conj(a) = 5.0 - 6.0i |a| = 18 | * 7.810249675906654 tan(a) = -6.685231390246571E-6 + 1.0000103108981198i 19 | * 20 | *************************************************************************/ 21 | 22 | public class Complex { 23 | private final double re; // the real part 24 | private final double im; // the imaginary part 25 | 26 | // create a new object with the given real and imaginary parts 27 | public Complex(double real, double imag) { 28 | re = real; 29 | im = imag; 30 | } 31 | 32 | // return a string representation of the invoking Complex object 33 | @Override 34 | public String toString() { 35 | if (im == 0) 36 | return re + ""; 37 | if (re == 0) 38 | return im + "i"; 39 | if (im < 0) 40 | return re + " - " + (-im) + "i"; 41 | return re + " + " + im + "i"; 42 | } 43 | 44 | // return abs/modulus/magnitude and angle/phase/argument 45 | public double abs() { 46 | return Math.hypot(re, im); 47 | } // Math.sqrt(re*re + im*im) 48 | 49 | public double phase() { 50 | return Math.atan2(im, re); 51 | } // between -pi and pi 52 | 53 | // return a new Complex object whose value is (this + b) 54 | public Complex plus(Complex b) { 55 | Complex a = this; // invoking object 56 | double real = a.re + b.re; 57 | double imag = a.im + b.im; 58 | return new Complex(real, imag); 59 | } 60 | 61 | // return a new Complex object whose value is (this - b) 62 | public Complex minus(Complex b) { 63 | Complex a = this; 64 | double real = a.re - b.re; 65 | double imag = a.im - b.im; 66 | return new Complex(real, imag); 67 | } 68 | 69 | // return a new Complex object whose value is (this * b) 70 | public Complex times(Complex b) { 71 | Complex a = this; 72 | double real = a.re * b.re - a.im * b.im; 73 | double imag = a.re * b.im + a.im * b.re; 74 | return new Complex(real, imag); 75 | } 76 | 77 | // scalar multiplication 78 | // return a new object whose value is (this * alpha) 79 | public Complex times(double alpha) { 80 | return new Complex(alpha * re, alpha * im); 81 | } 82 | 83 | // return a new Complex object whose value is the conjugate of this 84 | public Complex conjugate() { 85 | return new Complex(re, -im); 86 | } 87 | 88 | // return a new Complex object whose value is the reciprocal of this 89 | public Complex reciprocal() { 90 | double scale = re * re + im * im; 91 | return new Complex(re / scale, -im / scale); 92 | } 93 | 94 | // return the real or imaginary part 95 | public double re() { 96 | return re; 97 | } 98 | 99 | public double im() { 100 | return im; 101 | } 102 | 103 | // return a / b 104 | public Complex divides(Complex b) { 105 | Complex a = this; 106 | return a.times(b.reciprocal()); 107 | } 108 | 109 | // return a new Complex object whose value is the complex exponential of 110 | // this 111 | public Complex exp() { 112 | return new Complex(Math.exp(re) * Math.cos(im), Math.exp(re) * Math.sin(im)); 113 | } 114 | 115 | // return a new Complex object whose value is the complex sine of this 116 | public Complex sin() { 117 | return new Complex(Math.sin(re) * Math.cosh(im), Math.cos(re) * Math.sinh(im)); 118 | } 119 | 120 | // return a new Complex object whose value is the complex cosine of this 121 | public Complex cos() { 122 | return new Complex(Math.cos(re) * Math.cosh(im), -Math.sin(re) * Math.sinh(im)); 123 | } 124 | 125 | // return a new Complex object whose value is the complex tangent of this 126 | public Complex tan() { 127 | return sin().divides(cos()); 128 | } 129 | 130 | // a static version of plus 131 | public static Complex plus(Complex a, Complex b) { 132 | double real = a.re + b.re; 133 | double imag = a.im + b.im; 134 | Complex sum = new Complex(real, imag); 135 | return sum; 136 | } 137 | 138 | public static Complex[] toComplex(double[] x) { 139 | Complex[] result = new Complex[x.length]; 140 | for (int i = 0; i < x.length; i++) 141 | result[i] = new Complex(x[i], 0); 142 | return result; 143 | } 144 | 145 | public static Complex[] toComplex(Collection x) { 146 | Complex[] result = new Complex[x.size()]; 147 | int i = 0; 148 | for (Number number : x) { 149 | result[i] = new Complex(number.doubleValue(), 0); 150 | i++; 151 | } 152 | return result; 153 | } 154 | 155 | } 156 | -------------------------------------------------------------------------------- /src/main/java/com/github/davidmoten/ar/FFT.java: -------------------------------------------------------------------------------- 1 | package com.github.davidmoten.ar; 2 | 3 | /************************************************************************* 4 | * Compilation: javac FFT.java Execution: java FFT N Dependencies: Complex.java 5 | * 6 | * Compute the FFT and inverse FFT of a length N complex sequence. Bare bones 7 | * implementation that runs in O(N log N) time. Our goal is to optimize the 8 | * clarity of the code, rather than performance. 9 | * 10 | * Limitations ----------- - assumes N is a power of 2 11 | * 12 | * - not the most memory efficient algorithm (because it uses an object type for 13 | * representing complex numbers and because it re-allocates memory for the 14 | * subarray, instead of doing in-place or reusing a single temporary array) 15 | * 16 | *************************************************************************/ 17 | 18 | public class FFT { 19 | 20 | public static double[] fftMagnitude(double[] signal) { 21 | Complex[] spectrum = fft(Complex.toComplex(signal)); 22 | double[] x = new double[spectrum.length]; 23 | for (int i = 0; i < x.length; i++) { 24 | x[i] = spectrum[i].abs(); 25 | } 26 | return x; 27 | } 28 | 29 | // compute the FFT of x[], assuming its length is a power of 2 30 | public static Complex[] fft(Complex[] x) { 31 | int n = x.length; 32 | 33 | // base case 34 | if (n == 1) 35 | return new Complex[] { x[0] }; 36 | 37 | // radix 2 Cooley-Tukey FFT 38 | if (n % 2 != 0) { 39 | throw new RuntimeException("n=" + n + " is not a power of 2"); 40 | } 41 | 42 | // fft of even terms 43 | Complex[] even = new Complex[n / 2]; 44 | for (int k = 0; k < n / 2; k++) { 45 | even[k] = x[2 * k]; 46 | } 47 | Complex[] q = fft(even); 48 | 49 | // fft of odd terms 50 | Complex[] odd = even; // reuse the array 51 | for (int k = 0; k < n / 2; k++) { 52 | odd[k] = x[2 * k + 1]; 53 | } 54 | Complex[] r = fft(odd); 55 | 56 | // combine 57 | Complex[] y = new Complex[n]; 58 | for (int k = 0; k < n / 2; k++) { 59 | double kth = -2 * k * Math.PI / n; 60 | Complex wk = new Complex(Math.cos(kth), Math.sin(kth)); 61 | y[k] = q[k].plus(wk.times(r[k])); 62 | y[k + n / 2] = q[k].minus(wk.times(r[k])); 63 | } 64 | return y; 65 | } 66 | 67 | // compute the inverse FFT of x[], assuming its length is a power of 2 68 | public static Complex[] ifft(Complex[] x) { 69 | int N = x.length; 70 | Complex[] y = new Complex[N]; 71 | 72 | // take conjugate 73 | for (int i = 0; i < N; i++) { 74 | y[i] = x[i].conjugate(); 75 | } 76 | 77 | // compute forward FFT 78 | y = fft(y); 79 | 80 | // take conjugate again 81 | for (int i = 0; i < N; i++) { 82 | y[i] = y[i].conjugate(); 83 | } 84 | 85 | // divide by N 86 | for (int i = 0; i < N; i++) { 87 | y[i] = y[i].times(1.0 / N); 88 | } 89 | 90 | return y; 91 | 92 | } 93 | 94 | // compute the circular convolution of x and y 95 | public static Complex[] cconvolve(Complex[] x, Complex[] y) { 96 | 97 | // should probably pad x and y with 0s so that they have same length 98 | // and are powers of 2 99 | if (x.length != y.length) { 100 | throw new RuntimeException("Dimensions don't agree"); 101 | } 102 | 103 | int N = x.length; 104 | 105 | // compute FFT of each sequence 106 | Complex[] a = fft(x); 107 | Complex[] b = fft(y); 108 | 109 | // point-wise multiply 110 | Complex[] c = new Complex[N]; 111 | for (int i = 0; i < N; i++) { 112 | c[i] = a[i].times(b[i]); 113 | } 114 | 115 | // compute inverse FFT 116 | return ifft(c); 117 | } 118 | 119 | // compute the linear convolution of x and y 120 | public static Complex[] convolve(Complex[] x, Complex[] y) { 121 | Complex ZERO = new Complex(0, 0); 122 | 123 | Complex[] a = new Complex[2 * x.length]; 124 | for (int i = 0; i < x.length; i++) 125 | a[i] = x[i]; 126 | for (int i = x.length; i < 2 * x.length; i++) 127 | a[i] = ZERO; 128 | 129 | Complex[] b = new Complex[2 * y.length]; 130 | for (int i = 0; i < y.length; i++) 131 | b[i] = y[i]; 132 | for (int i = y.length; i < 2 * y.length; i++) 133 | b[i] = ZERO; 134 | 135 | return cconvolve(a, b); 136 | } 137 | 138 | // TODO remove comment below once unit tests for all methods exist 139 | 140 | /********************************************************************* 141 | * Test client and sample execution 142 | * 143 | * % java FFT 4 x ------------------- -0.03480425839330703 144 | * 0.07910192950176387 0.7233322451735928 0.1659819820667019 145 | * 146 | * y = fft(x) ------------------- 0.9336118983487516 -0.7581365035668999 + 147 | * 0.08688005256493803i 0.44344407521182005 -0.7581365035668999 - 148 | * 0.08688005256493803i 149 | * 150 | * z = ifft(y) ------------------- -0.03480425839330703 0.07910192950176387 151 | * + 2.6599344570851287E-18i 0.7233322451735928 0.1659819820667019 - 152 | * 2.6599344570851287E-18i 153 | * 154 | * c = cconvolve(x, x) ------------------- 0.5506798633981853 155 | * 0.23461407150576394 - 4.033186818023279E-18i -0.016542951108772352 156 | * 0.10288019294318276 + 4.033186818023279E-18i 157 | * 158 | * d = convolve(x, x) ------------------- 0.001211336402308083 - 159 | * 3.122502256758253E-17i -0.005506167987577068 - 5.058885073636224E-17i 160 | * -0.044092969479563274 + 2.1934338938072244E-18i 0.10288019294318276 - 161 | * 3.6147323062478115E-17i 0.5494685269958772 + 3.122502256758253E-17i 162 | * 0.240120239493341 + 4.655566391833896E-17i 0.02755001837079092 - 163 | * 2.1934338938072244E-18i 4.01805098805014E-17i 164 | * 165 | *********************************************************************/ 166 | 167 | } -------------------------------------------------------------------------------- /src/test/java/com/github/davidmoten/ar/AudioTest.java: -------------------------------------------------------------------------------- 1 | package com.github.davidmoten.ar; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import java.awt.Color; 6 | import java.awt.Graphics2D; 7 | import java.awt.image.BufferedImage; 8 | import java.io.File; 9 | import java.io.IOException; 10 | import java.util.List; 11 | 12 | import javax.imageio.ImageIO; 13 | 14 | import org.junit.Test; 15 | 16 | import rx.functions.Action1; 17 | 18 | import com.fastdtw.dtw.FastDTW; 19 | import com.fastdtw.timeseries.TimeSeries; 20 | import com.fastdtw.util.Distances; 21 | 22 | public class AudioTest { 23 | 24 | @Test 25 | public void testReadUsingObservable() { 26 | int count = Audio 27 | .readSignal( 28 | AudioTest.class.getResourceAsStream("/alphabet.wav")) 29 | .count().toBlocking().single(); 30 | assertEquals(296934, count); 31 | } 32 | 33 | private static final int pixelsPerVerticalCell = 1; 34 | private static final int pixelsPerHorizontalCell = 8; 35 | 36 | @Test 37 | public void testReadUsingObservableAndFft() { 38 | final int frameSize = 256; 39 | final BufferedImage image = new BufferedImage( 40 | 1200 * pixelsPerHorizontalCell, frameSize 41 | * pixelsPerVerticalCell, BufferedImage.TYPE_INT_ARGB); 42 | Audio.readSignal(AudioTest.class.getResourceAsStream("/alphabet.wav")) 43 | // buffer 44 | .buffer(frameSize) 45 | // full frames only 46 | .filter(Util. hasSize(frameSize)) 47 | // to double arrays 48 | .map(Util.TO_DOUBLE_ARRAY) 49 | // extract frequencies 50 | .map(Audio.toFft()) 51 | // to list 52 | .map(Util.TO_LIST) 53 | // get all 54 | .toList() 55 | // draw image 56 | .doOnNext(draw(image)) 57 | // go 58 | .subscribe(); 59 | } 60 | 61 | @Test 62 | public void testExtractMFCCs() { 63 | Audio.timeSeries(AudioTest.class.getResourceAsStream("/A.wav")) 64 | .subscribe(); 65 | } 66 | 67 | private static TimeSeries timeSeries(String resource) { 68 | return Audio.timeSeries(AudioTest.class.getResourceAsStream(resource)) 69 | .toBlocking().single(); 70 | } 71 | 72 | private static double distance(TimeSeries a, TimeSeries b) { 73 | return FastDTW.compare(a, b, Distances.EUCLIDEAN_DISTANCE) 74 | .getDistance(); 75 | } 76 | 77 | @Test 78 | public void testDtwDifference() { 79 | System.out.println("----Letters----"); 80 | 81 | TimeSeries a = timeSeries("/A.wav"); 82 | TimeSeries a2 = timeSeries("/A2.wav"); 83 | TimeSeries b = timeSeries("/B.wav"); 84 | TimeSeries b2 = timeSeries("/B2.wav"); 85 | TimeSeries c = timeSeries("/C.wav"); 86 | TimeSeries c2 = timeSeries("/C2.wav"); 87 | System.out.println(distance(a, a)); 88 | System.out.println(distance(a, a2)); 89 | System.out.println(distance(a, b)); 90 | System.out.println(distance(a, c)); 91 | System.out.println(distance(a, b2)); 92 | System.out.println(distance(a, c2)); 93 | 94 | } 95 | 96 | @Test 97 | public void testDtwDifference2() { 98 | System.out.println("----Words----"); 99 | TimeSeries a = timeSeries("/public.wav"); 100 | TimeSeries b = timeSeries("/static.wav"); 101 | TimeSeries c = timeSeries("/final.wav"); 102 | TimeSeries d = timeSeries("/abstract.wav"); 103 | TimeSeries e = timeSeries("/assert.wav"); 104 | TimeSeries f = timeSeries("/boolean.wav"); 105 | TimeSeries g = timeSeries("/break.wav"); 106 | System.out.println(distance(a, b)); 107 | System.out.println(distance(a, c)); 108 | System.out.println(distance(a, d)); 109 | System.out.println(distance(a, e)); 110 | System.out.println(distance(a, f)); 111 | System.out.println(distance(a, g)); 112 | 113 | } 114 | 115 | private static Color toColor(double d) { 116 | return Color.getHSBColor(1f - (float) d, 1f, (float) d * 1.0f); 117 | } 118 | 119 | private static Action1>> draw(final BufferedImage image) { 120 | return new Action1>>() { 121 | @Override 122 | public void call(List> all) { 123 | Graphics2D g = image.createGraphics(); 124 | Double min = null; 125 | Double max = null; 126 | for (List list : all) { 127 | boolean isFirst = true; 128 | for (double d : list) { 129 | if (!isFirst) { 130 | double dlog = Math.max(0, Math.log10(d)); 131 | if (min == null || min > dlog) 132 | min = dlog; 133 | if (max == null || max < dlog) 134 | max = dlog; 135 | } 136 | isFirst = false; 137 | } 138 | } 139 | int sample = 0; 140 | 141 | for (List list : all) { 142 | int freq = 0; 143 | for (double d : list) { 144 | double prop = Math.max( 145 | 0, 146 | Math.min(1.0, (Math.log10(d) - min) 147 | / (max - min))); 148 | Color color = toColor(prop); 149 | g.setColor(color); 150 | g.fillRect(sample * pixelsPerHorizontalCell, 151 | (freq + list.size() / 2) % list.size() 152 | * pixelsPerVerticalCell, 153 | pixelsPerHorizontalCell, pixelsPerVerticalCell); 154 | freq++; 155 | } 156 | sample += 1; 157 | } 158 | try { 159 | ImageIO.write(image, "png", new File("target/image.png")); 160 | } catch (IOException e) { 161 | throw new RuntimeException(e); 162 | } 163 | } 164 | }; 165 | } 166 | 167 | private static Action1>> draw2(final BufferedImage image) { 168 | return new Action1>>() { 169 | @Override 170 | public void call(List> all) { 171 | Graphics2D g = image.createGraphics(); 172 | Double min = null; 173 | Double max = null; 174 | for (List list : all) { 175 | boolean isFirst = true; 176 | for (double d : list) { 177 | if (!isFirst) { 178 | if (min == null || min > d) 179 | min = d; 180 | if (max == null || max < d) 181 | max = d; 182 | } 183 | isFirst = false; 184 | } 185 | } 186 | int sample = 0; 187 | 188 | for (List list : all) { 189 | int freq = 0; 190 | for (double d : list) { 191 | double prop = Math.max(0, 192 | Math.min(1.0, (d - min) / (max - min))); 193 | Color color = toColor(prop); 194 | g.setColor(color); 195 | g.fillRect(sample * pixelsPerHorizontalCell, 196 | (freq + list.size() / 2) % list.size() 197 | * pixelsPerVerticalCell, 198 | pixelsPerHorizontalCell, pixelsPerVerticalCell); 199 | freq++; 200 | } 201 | sample += 1; 202 | } 203 | try { 204 | ImageIO.write(image, "png", new File("target/image2.png")); 205 | } catch (IOException e) { 206 | throw new RuntimeException(e); 207 | } 208 | } 209 | }; 210 | } 211 | 212 | } 213 | -------------------------------------------------------------------------------- /src/main/java/com/github/davidmoten/ar/Audio.java: -------------------------------------------------------------------------------- 1 | package com.github.davidmoten.ar; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.util.ArrayList; 6 | import java.util.Arrays; 7 | import java.util.List; 8 | 9 | import javax.sound.sampled.AudioFormat; 10 | import javax.sound.sampled.AudioInputStream; 11 | import javax.sound.sampled.AudioSystem; 12 | import javax.sound.sampled.DataLine; 13 | import javax.sound.sampled.LineUnavailableException; 14 | import javax.sound.sampled.SourceDataLine; 15 | import javax.sound.sampled.UnsupportedAudioFileException; 16 | 17 | import rx.Observable; 18 | import rx.Observable.OnSubscribe; 19 | import rx.Subscriber; 20 | import rx.functions.Func1; 21 | 22 | import com.fastdtw.timeseries.TimeSeries; 23 | import com.fastdtw.timeseries.TimeSeriesBase; 24 | import com.fastdtw.timeseries.TimeSeriesBase.Builder; 25 | 26 | public class Audio { 27 | 28 | public static Observable readSignal(final InputStream is) { 29 | 30 | return Observable.create(new OnSubscribe() { 31 | 32 | @Override 33 | public void call(Subscriber sub) { 34 | try { 35 | // Load the Audio Input Stream from the file 36 | AudioInputStream audioInputStream = AudioSystem 37 | .getAudioInputStream(is); 38 | 39 | // Get Audio Format information 40 | AudioFormat audioFormat = audioInputStream.getFormat(); 41 | 42 | // log details 43 | printAudioDetails(audioInputStream, audioFormat); 44 | 45 | // Write the sound to an array of bytes 46 | int bytesRead; 47 | byte[] data = new byte[8192]; 48 | while (!sub.isUnsubscribed() 49 | && (bytesRead = audioInputStream.read(data, 0, 50 | data.length)) != -1) { 51 | // Determine the original Endian encoding format 52 | boolean isBigEndian = audioFormat.isBigEndian(); 53 | int n = bytesRead / 2; 54 | // convert each pair of byte values from the byte 55 | // array to an Endian value 56 | for (int i = 0; i < n * 2; i += 2) { 57 | int value = Util.valueFromTwoBytesEndian(data[i], 58 | data[i + 1], isBigEndian); 59 | if (sub.isUnsubscribed()) 60 | return; 61 | else 62 | sub.onNext(value); 63 | } 64 | } 65 | sub.onCompleted(); 66 | } catch (Exception e) { 67 | sub.onError(e); 68 | } 69 | } 70 | 71 | }); 72 | } 73 | 74 | private static void printAudioDetails(AudioInputStream audioInputStream, 75 | AudioFormat audioFormat) { 76 | // Calculate the sample rate 77 | float sample_rate = audioFormat.getSampleRate(); 78 | System.out.println("sample rate = " + sample_rate); 79 | 80 | // Calculate the length in seconds of the sample 81 | float T = audioInputStream.getFrameLength() 82 | / audioFormat.getFrameRate(); 83 | System.out 84 | .println("T = " + T + " (length of sampled sound in seconds)"); 85 | 86 | // Calculate the number of equidistant points in time 87 | int num = (int) (T * sample_rate) / 2; 88 | System.out.println("n = " + num + " (number of equidistant points)"); 89 | 90 | // Calculate the time interval at each equidistant point 91 | float h = (T / num); 92 | System.out.println("h = " + h 93 | + " (length of each time interval in seconds)"); 94 | } 95 | 96 | private static final int BUFFER_SIZE = 1024; 97 | 98 | public static void play(InputStream is) { 99 | 100 | // Load the Audio Input Stream from the file 101 | AudioInputStream audioInputStream = null; 102 | try { 103 | audioInputStream = AudioSystem.getAudioInputStream(is); 104 | } catch (UnsupportedAudioFileException e) { 105 | throw new RuntimeException(e); 106 | } catch (IOException e) { 107 | throw new RuntimeException(e); 108 | } 109 | 110 | // Get Audio Format information 111 | AudioFormat audioFormat = audioInputStream.getFormat(); 112 | 113 | // Handle opening the line 114 | SourceDataLine line = null; 115 | DataLine.Info info = new DataLine.Info(SourceDataLine.class, 116 | audioFormat); 117 | try { 118 | line = (SourceDataLine) AudioSystem.getLine(info); 119 | line.open(audioFormat); 120 | } catch (LineUnavailableException e) { 121 | throw new RuntimeException(e); 122 | } 123 | 124 | // Start playing the sound 125 | line.start(); 126 | 127 | // Write the sound to an array of bytes 128 | int nBytesRead = 0; 129 | byte[] abData = new byte[BUFFER_SIZE]; 130 | while (nBytesRead != -1) { 131 | try { 132 | nBytesRead = audioInputStream.read(abData, 0, abData.length); 133 | } catch (IOException e) { 134 | e.printStackTrace(); 135 | } 136 | if (nBytesRead >= 0) { 137 | line.write(abData, 0, nBytesRead); 138 | } 139 | } 140 | 141 | // close the line 142 | line.drain(); 143 | line.close(); 144 | 145 | } 146 | 147 | public static Observable timeSeries(Observable signal, 148 | int frameSize, int skip, int numTriFilters, int numMfcCoefficients) { 149 | return signal 150 | // get frames 151 | .buffer(frameSize, skip) 152 | // full frames only 153 | .filter(Util. hasSize(frameSize)) 154 | // as array of double 155 | .map(Util.TO_DOUBLE_ARRAY) 156 | // emphasize higher frequencies 157 | .map(new PreEmphasisFunction()) 158 | // apply filter to handle discontinuities at start and end 159 | .map(new HammingWindowFunction()) 160 | // extract frequencies 161 | .map(toFft()) 162 | // tri bandpass filter 163 | .map(new TriangularBandPassFilterBankFunction(numTriFilters, 164 | frameSize)) 165 | // DCT 166 | .map(new DiscreteCosineTransformFunction(numMfcCoefficients, 167 | numTriFilters)) 168 | // make a list of the frame mfccs 169 | .toList() 170 | // trim silence at beginning and end 171 | .map(trimSilenceAtBeginningAndEnd()) 172 | // to TimeSeries 173 | .map(TO_TIME_SERIES); 174 | } 175 | 176 | public static Observable timeSeries(InputStream wave, 177 | int frameSize, int skip, int numTriFilters, int numMfcCoefficients) { 178 | return timeSeries(readSignal(wave), frameSize, skip, numTriFilters, 179 | numMfcCoefficients); 180 | } 181 | 182 | private static Func1, List> trimSilenceAtBeginningAndEnd() { 183 | return new Func1, List>() { 184 | @Override 185 | public List call(List list) { 186 | // trim from beginning 187 | int i = 0; 188 | final double POWER_THRESHOLD = 0.0; 189 | while (i < list.size() && list.get(i)[0] < POWER_THRESHOLD) 190 | i++; 191 | 192 | // trim from end 193 | int j = list.size() - 1; 194 | while (j > 0 && list.get(j)[0] < POWER_THRESHOLD) 195 | j--; 196 | return list.subList(i, j); 197 | } 198 | }; 199 | 200 | } 201 | 202 | public static Observable timeSeries(InputStream wave) { 203 | int frameSize = 256; 204 | int skip = 100; 205 | return timeSeries(wave, frameSize, 26, 13, skip); 206 | } 207 | 208 | public static Func1 toFft() { 209 | return new Func1() { 210 | 211 | @Override 212 | public double[] call(double[] signal) { 213 | return FFT.fftMagnitude(signal); 214 | } 215 | }; 216 | } 217 | 218 | public static AudioFormat createAudioFormatStandard() { 219 | float sampleRate = 16000; 220 | int sampleSizeInBits = 8; 221 | int channels = 2; 222 | boolean signed = true; 223 | boolean bigEndian = true; 224 | AudioFormat format = new AudioFormat(sampleRate, sampleSizeInBits, 225 | channels, signed, bigEndian); 226 | return format; 227 | } 228 | 229 | public static Observable microphoneRaw(int bufferSize, 230 | AudioFormat format) { 231 | 232 | return Observable.create(new MicrophoneOnSubscribe(4096, format)); 233 | } 234 | 235 | public static Observable microphone() { 236 | int bufferSize = 4096; 237 | final AudioFormat format = createAudioFormatStandard(); 238 | // get raw bytes from microphone 239 | return microphoneRaw(bufferSize, format) 240 | // byte pairs as integers 241 | .flatMap(new Func1>() { 242 | 243 | @Override 244 | public Observable call(byte[] bytes) { 245 | return Observable.from(toIntegers(format, bytes)); 246 | } 247 | }); 248 | } 249 | 250 | public static List toIntegers(final AudioFormat format, 251 | byte[] bytes) { 252 | // Determine the original Endian encoding format 253 | boolean isBigEndian = format.isBigEndian(); 254 | int n = bytes.length / 2; 255 | // convert each pair of byte values from the byte 256 | // array to an Endian value 257 | List list = new ArrayList(n); 258 | for (int i = 0; i < n * 2; i += 2) { 259 | int value = Util.valueFromTwoBytesEndian(bytes[i], bytes[i + 1], 260 | isBigEndian); 261 | list.add(value); 262 | } 263 | return list; 264 | } 265 | 266 | public static final Func1, TimeSeries> TO_TIME_SERIES = new Func1, TimeSeries>() { 267 | 268 | @Override 269 | public TimeSeries call(List list) { 270 | Builder builder = TimeSeriesBase.builder(); 271 | int time = 0; 272 | for (double[] mfccs : list) { 273 | builder = builder.add(time, dropFirst(mfccs)); 274 | time += 1; 275 | } 276 | return builder.build(); 277 | } 278 | }; 279 | 280 | private static double[] dropFirst(double[] x) { 281 | return Arrays.copyOfRange(x, 1, x.length); 282 | } 283 | } 284 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 4.0.0 6 | 7 | 8 | com.github.davidmoten 9 | sonatype-parent 10 | 0.1 11 | 12 | 13 | audio-recognition 14 | 0.4-SNAPSHOT 15 | 16 | ${project.artifactId} 17 | RxJava utilities 18 | jar 19 | 20 | http://github.com/davidmoten/audio-recognition 21 | 22 | 23 | UTF-8 24 | UTF-8 25 | 1.6 26 | 1.0.4 27 | 1.2 28 | 29 | 2.6 30 | 2.11 31 | 2.5.4 32 | 2.9.1 33 | 3.0.1 34 | 2.0-beta-2 35 | 2.0 36 | 2.4 37 | 2.4 38 | 2.4 39 | 3.3 40 | 2.2 41 | ${project.build.directory}/target/coverage-reports 42 | 43 | 44 | 45 | 46 | The Apache Software License, Version 2.0 47 | http://www.apache.org/licenses/LICENSE-2.0.txt 48 | repo 49 | A business-friendly OSS license 50 | 51 | 52 | 53 | 54 | CloudBees 55 | https://xuml-tools.ci.cloudbees.com 56 | 57 | 58 | 59 | GitHub 60 | https://github.com/davidmoten/audio-recognition/issues 61 | 62 | 63 | 2013 64 | 65 | 66 | dave 67 | Dave Moten 68 | https://github.com/davidmoten/ 69 | 70 | architect 71 | developer 72 | 73 | +10 74 | 75 | 76 | 77 | 78 | scm:git:git@github.com:davidmoten/audio-recognition.git 79 | scm:git:git@github.com:davidmoten/audio-recognition.git 80 | scm:git:git@github.com:davidmoten/audio-recognition.git 81 | HEAD 82 | 83 | 84 | 85 | 86 | io.reactivex 87 | rxjava 88 | ${rxjava.version} 89 | 90 | 91 | com.github.davidmoten 92 | fastdtw 93 | 0.1 94 | 95 | 96 | 97 | 98 | junit 99 | junit 100 | 4.11 101 | compile 102 | true 103 | 104 | 105 | 106 | org.openjdk.jmh 107 | jmh-core 108 | ${jmh.version} 109 | test 110 | 111 | 112 | 113 | org.openjdk.jmh 114 | jmh-generator-annprocess 115 | ${jmh.version} 116 | test 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | maven-compiler-plugin 126 | 3.1 127 | 128 | ${maven.compiler.target} 129 | ${maven.compiler.target} 130 | 131 | 132 | 133 | 134 | maven-site-plugin 135 | ${m3.site.version} 136 | 137 | 138 | attach-descriptor 139 | 140 | attach-descriptor 141 | 142 | 143 | 144 | 145 | 146 | 148 | 149 | org.apache.maven.plugins 150 | maven-jxr-plugin 151 | ${jxr.version} 152 | 153 | true 154 | 155 | 156 | 157 | org.codehaus.mojo 158 | cobertura-maven-plugin 159 | ${cobertura.version} 160 | 161 | false 162 | 163 | 164 | 165 | org.apache.maven.plugins 166 | maven-checkstyle-plugin 167 | ${checkstyle.version} 168 | 169 | true 170 | 171 | 172 | 173 | org.apache.maven.plugins 174 | maven-pmd-plugin 175 | ${pmd.version} 176 | 177 | ${maven.compiler.target} 178 | true 179 | 180 | 181 | 184 | 185 | org.codehaus.mojo 186 | jdepend-maven-plugin 187 | ${jdepend.version} 188 | 189 | 190 | org.codehaus.mojo 191 | javancss-maven-plugin 192 | ${javancss.version} 193 | 194 | 195 | org.apache.maven.plugins 196 | maven-project-info-reports-plugin 197 | ${project.info.version} 198 | 199 | false 200 | false 201 | 202 | 203 | 204 | org.codehaus.mojo 205 | taglist-maven-plugin 206 | ${taglist.version} 207 | 208 | 209 | org.apache.maven.plugins 210 | maven-javadoc-plugin 211 | ${javadoc.version} 212 | 213 | true 214 | 215 | 216 | 218 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | benchmark 230 | 231 | 232 | 233 | org.codehaus.mojo 234 | exec-maven-plugin 235 | 1.3.2 236 | 237 | 238 | run-benchmarks 239 | integration-test 240 | 241 | exec 242 | 243 | 244 | test 245 | java 246 | 247 | -classpath 248 | 249 | org.openjdk.jmh.Main 250 | 251 | -f 252 | -i 253 | 10 254 | -wi 255 | 3 256 | -jvmArgs 257 | -Xmx512m 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | --------------------------------------------------------------------------------