plectrum

Plectrum: instrument tuner for Android
Log | Files | Refs | README | LICENSE

PipedAudioStream.java (3432B)


      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;
     25 
     26 import java.io.InputStream;
     27 
     28 import be.tarsos.dsp.util.AudioResourceUtils;
     29 
     30 
     31 /**
     32  * An audio file can be used to convert and read from. It uses libAV to convert
     33  * about any audio format to a one channel PCM stream of a chosen sample rate. There is 
     34  * support for movie files as well, the first audio channel is then used as input.
     35  * The resource is either a local file or a type of stream supported by libAV (e.g. HTTP streams);
     36  * 
     37  * For a list of audio decoders the following command is practical:
     38  * <pre>
     39 avconv -decoders | grep -E "^A" | sort
     40  
     41 
     42 A... 8svx_exp             8SVX exponential
     43 A... 8svx_fib             8SVX fibonacci
     44 A... aac                  AAC (Advanced Audio Coding)
     45 A... aac_latm             AAC LATM (Advanced Audio Coding LATM syntax)
     46 ...
     47  * </pre>
     48  */
     49 public class PipedAudioStream {
     50 	
     51 	//private final static Logger LOG = Logger.getLogger(PipedAudioStream.class.getName());
     52 	
     53 	private final String resource;
     54 	private static PipeDecoder pipeDecoder = new PipeDecoder();
     55 	
     56 	public static void setDecoder(PipeDecoder decoder){
     57 		pipeDecoder = decoder;
     58 	}
     59 	
     60 	private final PipeDecoder decoder;
     61 	public PipedAudioStream(String resource){
     62 		this.resource = AudioResourceUtils.sanitizeResource(resource);
     63 		decoder = pipeDecoder;
     64 	}
     65 	
     66 	/**
     67 	 * Return a one channel, signed PCM stream of audio of a defined sample rate. 
     68 	 * @param targetSampleRate The target sample stream.
     69 	 * @param startTimeOffset The start time offset.
     70 	 * @return An audio stream which can be used to read samples from.
     71 	 */
     72 	public TarsosDSPAudioInputStream getMonoStream(int targetSampleRate,double startTimeOffset){
     73 		return getMonoStream(targetSampleRate, startTimeOffset,-1);
     74 	}
     75 	
     76 	private TarsosDSPAudioFormat getTargetFormat(int targetSampleRate){
     77 		return new TarsosDSPAudioFormat(targetSampleRate, 16, 1, true, false);
     78 	}
     79 
     80 
     81 	/**
     82 	 * Return a one channel, signed PCM stream of audio of a defined sample rate. 
     83 	 * @param targetSampleRate The target sample stream.
     84 	 * @param startTimeOffset The start time offset.
     85 	 * @param numberOfSeconds the number of seconds to pipe. If negative the stream is processed until end of stream.
     86 	 * @return An audio stream which can be used to read samples from.
     87 	 */
     88 	public TarsosDSPAudioInputStream getMonoStream(int targetSampleRate, double startTimeOffset,
     89 			double numberOfSeconds) {
     90 		InputStream stream = null;
     91 		stream = decoder.getDecodedStream(resource, targetSampleRate,startTimeOffset,numberOfSeconds);
     92 		return new UniversalAudioInputStream(stream, getTargetFormat(targetSampleRate));
     93 	}
     94 }