AudioDispatcherFactory.java (3975B)
1 /* 2 * _______ _____ _____ _____ 3 * |__ __| | __ \ / ____| __ \ 4 * | | __ _ _ __ ___ ___ ___| | | | (___ | |__) | 5 * | |/ _` | '__/ __|/ _ \/ __| | | |\___ \| ___/ 6 * | | (_| | | \__ \ (_) \__ \ |__| |____) | | 7 * |_|\__,_|_| |___/\___/|___/_____/|_____/|_| 8 * 9 * ------------------------------------------------------------- 10 * 11 * TarsosDSP is developed by Joren Six at IPEM, University Ghent 12 * 13 * ------------------------------------------------------------- 14 * 15 * Info: http://0110.be/tag/TarsosDSP 16 * Github: https://github.com/JorenSix/TarsosDSP 17 * Releases: http://0110.be/releases/TarsosDSP/ 18 * 19 * TarsosDSP includes modified source code by various authors, 20 * for credits and info, see README. 21 * 22 */ 23 24 package be.tarsos.dsp.io.android; 25 26 import android.media.AudioRecord; 27 import android.media.MediaRecorder; 28 import be.tarsos.dsp.AudioDispatcher; 29 import be.tarsos.dsp.io.PipedAudioStream; 30 import be.tarsos.dsp.io.TarsosDSPAudioFormat; 31 import be.tarsos.dsp.io.TarsosDSPAudioInputStream; 32 33 /** 34 * The Factory creates {@link AudioDispatcher} objects from the 35 * configured default microphone of an Android device. 36 * It depends on the android runtime and does not work on the standard Java runtime. 37 * 38 * @author Joren Six 39 * @see AudioDispatcher 40 */ 41 public class AudioDispatcherFactory { 42 43 /** 44 * Create a new AudioDispatcher connected to the default microphone. 45 * 46 * @param sampleRate 47 * The requested sample rate. 48 * @param audioBufferSize 49 * The size of the audio buffer (in samples). 50 * 51 * @param bufferOverlap 52 * The size of the overlap (in samples). 53 * @return A new AudioDispatcher 54 */ 55 public static AudioDispatcher fromDefaultMicrophone(final int sampleRate, 56 final int audioBufferSize, final int bufferOverlap) { 57 int minAudioBufferSize = AudioRecord.getMinBufferSize(sampleRate, 58 android.media.AudioFormat.CHANNEL_IN_MONO, 59 android.media.AudioFormat.ENCODING_PCM_16BIT); 60 int minAudioBufferSizeInSamples = minAudioBufferSize/2; 61 if(minAudioBufferSizeInSamples <= audioBufferSize ){ 62 AudioRecord audioInputStream = new AudioRecord( 63 MediaRecorder.AudioSource.MIC, sampleRate, 64 android.media.AudioFormat.CHANNEL_IN_MONO, 65 android.media.AudioFormat.ENCODING_PCM_16BIT, 66 audioBufferSize * 2); 67 68 TarsosDSPAudioFormat format = new TarsosDSPAudioFormat(sampleRate, 16,1, true, false); 69 70 TarsosDSPAudioInputStream audioStream = new AndroidAudioInputStream(audioInputStream, format); 71 //start recording ! Opens the stream. 72 audioInputStream.startRecording(); 73 return new AudioDispatcher(audioStream,audioBufferSize,bufferOverlap); 74 }else{ 75 throw new IllegalArgumentException("Buffer size too small should be at least " + (minAudioBufferSize *2)); 76 } 77 } 78 79 80 /** 81 * Create a stream from a piped sub process and use that to create a new 82 * {@link AudioDispatcher} The sub-process writes a WAV-header and 83 * PCM-samples to standard out. The header is ignored and the PCM samples 84 * are are captured and interpreted. Examples of executables that can 85 * convert audio in any format and write to stdout are ffmpeg and avconv. 86 * 87 * @param source 88 * The file or stream to capture. 89 * @param targetSampleRate 90 * The target sample rate. 91 * @param audioBufferSize 92 * The number of samples used in the buffer. 93 * @param bufferOverlap 94 * The number of samples to overlap the current and previous buffer. 95 * @return A new audioprocessor. 96 */ 97 public static AudioDispatcher fromPipe(final String source,final int targetSampleRate, final int audioBufferSize,final int bufferOverlap){ 98 PipedAudioStream f = new PipedAudioStream(source); 99 TarsosDSPAudioInputStream audioStream = f.getMonoStream(targetSampleRate,0); 100 return new AudioDispatcher(audioStream, audioBufferSize, bufferOverlap); 101 } 102 }