Java Code Examples for be.tarsos.dsp.AudioDispatcher#run()

The following examples show how to use be.tarsos.dsp.AudioDispatcher#run() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RafsRepStrategy.java    From Panako with GNU Affero General Public License v3.0 6 votes vote down vote up
private void processMonitorQuery(float[] audioData,QueryResultHandler handler, double timeStamp,Set<Integer> avoid){
	int samplerate = Config.getInt(Key.RAFS_SAMPLE_RATE);
	int size = Config.getInt(Key.RAFS_FFT_SIZE);
	int overlap = size - Config.getInt(Key.RAFS_FFT_STEP_SIZE);
	
	AudioDispatcher d;
	try {
		d = AudioDispatcherFactory.fromFloatArray(audioData, samplerate, size, overlap);
		d.setZeroPadFirstBuffer(true);
		final RafsExtractor processor = new RafsExtractor(null,true);
		d.addAudioProcessor(processor);
		d.run();
		queryForMonitor(processor.fingerprints, processor.fingerprintProbabilities, 10 , avoid, handler);
	} catch (UnsupportedAudioFileException e) {
		LOG.severe("Unsupported audio");
	}
}
 
Example 2
Source File: NCteQStrategy.java    From Panako with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void monitor(String query, final int maxNumberOfReqults,Set<Integer> avoid,
		final QueryResultHandler handler) {
	
	int samplerate = Config.getInt(Key.NCTEQ_SAMPLE_RATE);
	int size = Config.getInt(Key.MONITOR_STEP_SIZE) * samplerate;
	int overlap = Config.getInt(Key.MONITOR_OVERLAP) * samplerate;
	final ConstantQ constanQ = createConstantQ();
	
	AudioDispatcher d = AudioDispatcherFactory.fromPipe(query, samplerate, size, overlap);
	d.addAudioProcessor(new AudioProcessor() {
		@Override
		public boolean process(AudioEvent audioEvent) {
			double timeStamp = audioEvent.getTimeStamp() - Config.getInt(Key.MONITOR_OVERLAP);
			processMonitorQuery(audioEvent.getFloatBuffer().clone(), maxNumberOfReqults, handler,timeStamp,constanQ);
			return true;
		}
		
		@Override
		public void processingFinished() {
		}
	});
	d.run();

}
 
Example 3
Source File: RafsStrategy.java    From Panako with GNU Affero General Public License v3.0 6 votes vote down vote up
private static RafsPacker extractPacker(File f, int fileIndex, boolean trackProbabilities){
	final int sampleRate = Config.getInt(Key.RAFS_SAMPLE_RATE);//2250Hz Nyquist frequency
	final int size = Config.getInt(Key.RAFS_FFT_SIZE);
	final int overlap =  size - Config.getInt(Key.RAFS_FFT_STEP_SIZE); //about an fft every 11.6ms (64/5500)
	String file = f.getAbsolutePath();
	AudioDispatcher d = AudioDispatcherFactory.fromPipe(file, sampleRate, size, overlap);
	d.setZeroPadFirstBuffer(true);
	RafsExtractor ex = new RafsExtractor(file, trackProbabilities);
	RafsPacker packer = new RafsPacker(ex,trackProbabilities);
	//String baseName = f.getName();
	d.setZeroPadFirstBuffer(true);
	d.addAudioProcessor(ex);
	d.addAudioProcessor(packer);
	d.run();
	return packer;
}
 
Example 4
Source File: RafsCliTest.java    From Panako with GNU Affero General Public License v3.0 6 votes vote down vote up
private static List<BitSetWithID> extractPackedPrints(File f,int fileIndex){		
	final int sampleRate = Config.getInt(Key.RAFS_SAMPLE_RATE);//2250Hz Nyquist frequency
	final int size = Config.getInt(Key.RAFS_FFT_SIZE);
	final int overlap =  size - Config.getInt(Key.RAFS_FFT_STEP_SIZE); 
	String file = f.getAbsolutePath();
	AudioDispatcher d = AudioDispatcherFactory.fromPipe(file, sampleRate, size, overlap);
	RafsExtractor ex = new RafsExtractor(file, true);
	RafsPacker packer = new RafsPacker(ex,true);
	//String baseName = f.getName();
	d.setZeroPadFirstBuffer(true);
	d.addAudioProcessor(ex);
	d.addAudioProcessor(packer);
	d.run();
	List<BitSetWithID> prints = new ArrayList<>();
	
	for (Map.Entry<Float, BitSet> frameEntry : packer.packedFingerprints.entrySet()) {
		int offset = (int) (frameEntry.getKey() * 1000);
		prints.add(new BitSetWithID(fileIndex * (1L<<32)  + offset, frameEntry.getValue()));
	}
	return prints;		
}
 
Example 5
Source File: NFFTStrategy.java    From Panako with GNU Affero General Public License v3.0 6 votes vote down vote up
private void processMonitorQueryToSerializeFingerprints(float[] audioBuffer,SerializedFingerprintsHandler handler,double queryOffset){
	int samplerate = Config.getInt(Key.NFFT_SAMPLE_RATE);
	int size = Config.getInt(Key.NFFT_SIZE);
	int overlap = size - Config.getInt(Key.NFFT_STEP_SIZE);
	
	AudioDispatcher d;
	try {
		d = AudioDispatcherFactory.fromFloatArray(audioBuffer, samplerate, size, overlap);
		final NFFTEventPointProcessor minMaxProcessor = new NFFTEventPointProcessor(size,overlap,samplerate);
		d.addAudioProcessor(minMaxProcessor);
		d.run();
		double queryDuration = d.secondsProcessed();
		List<NFFTFingerprint> fingerprints = new ArrayList<NFFTFingerprint>(minMaxProcessor.getFingerprints());
		handler.handleSerializedFingerprints(PanakoWebserviceClient.serializeFingerprintsToJson(fingerprints),queryDuration,queryOffset);
	} catch (UnsupportedAudioFileException e) {
		LOG.severe("Unsupported audio");
	}
}
 
Example 6
Source File: NFFTStrategy.java    From Panako with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public double store(String resource, String description) {
	int samplerate = Config.getInt(Key.NFFT_SAMPLE_RATE);
	int size = Config.getInt(Key.NFFT_SIZE);
	int overlap = size - Config.getInt(Key.NFFT_STEP_SIZE);
	
	AudioDispatcher d = AudioDispatcherFactory.fromPipe(resource, samplerate, size, overlap);
	final NFFTEventPointProcessor minMaxProcessor = new NFFTEventPointProcessor(size,overlap,samplerate);
	d.addAudioProcessor(minMaxProcessor);
	d.run();
	Set<NFFTFingerprint> fingerprints = new HashSet<NFFTFingerprint>(minMaxProcessor.getFingerprints());
	
	int identifier = FileUtils.getIdentifier(resource);
	
	
	for(NFFTFingerprint fingerprint: fingerprints){
		storage.addFingerprint(identifier, fingerprint.t1, fingerprint.hash());
	}
	
	// Store the meta data.
	storage.addAudio(identifier, description);
	
	// Commit the changes to store the fingerprints
	double durationInSeconds = d.secondsProcessed();
	storage.audioObjectAdded((int) Math.round(durationInSeconds));
	
	LOG.info(String.format("Stored %d fingerprints bundeled from %d event points for %s.",fingerprints.size(),minMaxProcessor.getEventPoints().size(),resource));
	return durationInSeconds;
}
 
Example 7
Source File: SyncSinkTests.java    From Panako with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testPipeDecoder(){
	File reference = TestUtilities.getResource("dataset/61198.wav");
	File referenceFile = TestUtilities.getResource("dataset/61198.wav");
	final float[] referenceBuffer = TestUtilities.getAudioBuffer(reference,1.0,1.5);
	
	AudioDispatcher d = AudioDispatcherFactory.fromPipe(referenceFile.getAbsolutePath(), 44100, 22050, 0,1.0,0.5);
	d.addAudioProcessor(new AudioProcessor() {
		boolean ran = false;
		@Override
		public void processingFinished() {
		}
		
		@Override
		public boolean process(AudioEvent audioEvent) {
			if(!ran){
				float[] otherBuffer = audioEvent.getFloatBuffer();
				assertEquals("Buffers should be equal in length", referenceBuffer.length, otherBuffer.length); 
				for(int i = 0 ; i < otherBuffer.length; i++){
					assertEquals("Buffers should have the same content", referenceBuffer[i], otherBuffer[i],0.0000001);
				}
			}
			ran = true;
			return true;
		}
	});
	d.run();		
}
 
Example 8
Source File: TestUtilities.java    From Panako with GNU Affero General Public License v3.0 5 votes vote down vote up
public static float[] getAudioBuffer(File file,double start,double stop){

	double sampleRate = 44100;
	int sampleStart = (int) Math.round(sampleRate * start);
	int sampleStop = (int) Math.round(sampleRate * stop);
	int diff = sampleStop - sampleStart;
	final float[] audioBuffer = new float[diff];
	
	AudioDispatcher d;
	
	d = AudioDispatcherFactory.fromPipe(file.getAbsolutePath(), 44100,diff, 0);
	d.skip(start);
	d.addAudioProcessor(new AudioProcessor() {
		boolean filled = false;
		@Override
		public void processingFinished() {
		}

		@Override
		public boolean process(AudioEvent audioEvent) {
			if(!filled){
				for (int i = 0; i < audioEvent.getFloatBuffer().length; i++) {
					audioBuffer[i] = audioEvent.getFloatBuffer()[i];
				}
				filled = true;
			}
			return false;
		}
	});
	d.run();
	
	
	
	return audioBuffer;
}
 
Example 9
Source File: NCteQStrategy.java    From Panako with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public double store(String resource, String description) {
	
	ConstantQ constantQ = createConstantQ();
	NCteQMapDBStorage storage = NCteQMapDBStorage.getInstance();
	
	int sampleRate = Config.getInt(Key.NCTEQ_SAMPLE_RATE);
	int size = constantQ.getFFTlength();
	int overlap = size - Config.getInt(Key.NCTEQ_STEP_SIZE);
	NCteQEventPointProcessor eventPointProcessor = new NCteQEventPointProcessor(constantQ,sampleRate,Config.getInt(Key.NCTEQ_STEP_SIZE));
	
	AudioDispatcher d = AudioDispatcherFactory.fromPipe(resource, sampleRate, size , overlap);
	d.addAudioProcessor(eventPointProcessor);
	d.run();
	
	int identifier = FileUtils.getIdentifier(resource);
	int hashesAdded = 0;
	
	float bucketFillFactorSum = 0.0f;
	for(NCteQFingerprint fingerprint : eventPointProcessor.getFingerprints()){
		float fillFactor = storage.addFingerprint(identifier, fingerprint.t1, fingerprint.hash(),fingerprint.timeDelta(),fingerprint.f1);
		bucketFillFactorSum += fillFactor;
		if(fillFactor != 1.0){
			hashesAdded++;
		}
	}
	LOG.info(String.format("Average hash bucket fill factor for %d hashes %.2f %%", hashesAdded, 100 * bucketFillFactorSum / (float)hashesAdded ) );
	float secondsProcessed = d.secondsProcessed();
	
	storage.addAudio(identifier, description);
	storage.audioObjectAdded(hashesAdded, Math.round(secondsProcessed));
	
	return secondsProcessed;
}
 
Example 10
Source File: NFFTStrategy.java    From Panako with GNU Affero General Public License v3.0 5 votes vote down vote up
private void processMonitorQuery(float[] audioBuffer,int maxNumberOfResults,
		QueryResultHandler handler,double queryOffset,Set<Integer> avoid){
	int samplerate = Config.getInt(Key.NFFT_SAMPLE_RATE);
	int size = Config.getInt(Key.NFFT_SIZE);
	int overlap = size - Config.getInt(Key.NFFT_STEP_SIZE);
	
	AudioDispatcher d;
	try {
		d = AudioDispatcherFactory.fromFloatArray(audioBuffer, samplerate, size, overlap);
		final NFFTEventPointProcessor minMaxProcessor = new NFFTEventPointProcessor(size,overlap,samplerate);
		d.addAudioProcessor(minMaxProcessor);
		d.run();
		List<NFFTFingerprint> fingerprints = new ArrayList<NFFTFingerprint>(minMaxProcessor.getFingerprints());
		
		final List<NFFTFingerprintQueryMatch> queryMatches = new ArrayList<NFFTFingerprintQueryMatch>();
		
		queryMatches.addAll(storage.getMatches(fingerprints, maxNumberOfResults));
		
		double queryDuration = d.secondsProcessed();
		
		if(queryMatches.isEmpty()){
			QueryResult result = QueryResult.emptyQueryResult(queryOffset,queryOffset+queryDuration);
			handler.handleEmptyResult(result);
		}else{
			for(NFFTFingerprintQueryMatch match : queryMatches){
				//avoid the results in the avoid hash set
				if(!avoid.contains(match.identifier)){
					String description = storage.getAudioDescription(match.identifier);
					handler.handleQueryResult(new QueryResult(queryOffset,queryOffset+queryDuration,String.valueOf(match.identifier), description, match.score, match.getStartTime(),100.0,100.0));
				}
			}
		}
		
	} catch (UnsupportedAudioFileException e) {
		LOG.severe("Unsupported audio");
	}
	
}
 
Example 11
Source File: NFFTStrategy.java    From Panako with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void monitor(String query,final  int maxNumberOfResults,Set<Integer> avoid,
		final QueryResultHandler handler) {
	
	int samplerate = Config.getInt(Key.NFFT_SAMPLE_RATE);
	int size = Config.getInt(Key.MONITOR_STEP_SIZE) * samplerate;
	int overlap = Config.getInt(Key.MONITOR_OVERLAP) * samplerate;
	AudioDispatcher d ;
	if (query.equals(Panako.DEFAULT_MICROPHONE)){
		try {
			d = AudioDispatcherFactory.fromDefaultMicrophone(samplerate,size, overlap);
		} catch (LineUnavailableException e) {
			LOG.warning("Could not connect to default microphone!" + e.getMessage());
			e.printStackTrace();
			d = null;
		}
	}else{
		d = AudioDispatcherFactory.fromPipe(query, samplerate, size, overlap);
	}
	d.addAudioProcessor(new AudioProcessor() {
		@Override
		public boolean process(AudioEvent audioEvent) {
			double timeStamp = audioEvent.getTimeStamp() - Config.getInt(Key.MONITOR_OVERLAP);
			processMonitorQuery(audioEvent.getFloatBuffer().clone(), maxNumberOfResults, handler,timeStamp,avoid);
			return true;
		}
		
		@Override
		public void processingFinished() {
		}
	});
	d.run();
}
 
Example 12
Source File: NFFTStrategy.java    From Panako with GNU Affero General Public License v3.0 5 votes vote down vote up
public void monitor(String query,final SerializedFingerprintsHandler handler){
	
	int samplerate = Config.getInt(Key.NFFT_SAMPLE_RATE);
	int size = Config.getInt(Key.MONITOR_STEP_SIZE) * samplerate;
	int overlap = Config.getInt(Key.MONITOR_OVERLAP) * samplerate;
	AudioDispatcher d ;
	if (query.equals(Panako.DEFAULT_MICROPHONE)){
		try {
			d = AudioDispatcherFactory.fromDefaultMicrophone(samplerate,size, overlap);
		} catch (LineUnavailableException e) {
			LOG.warning("Could not connect to default microphone!" + e.getMessage());
			e.printStackTrace();
			d = null;
		}
	}else{
		d = AudioDispatcherFactory.fromPipe(query, samplerate, size, overlap);
	}
	d.addAudioProcessor(new AudioProcessor() {
		@Override
		public boolean process(AudioEvent audioEvent) {
			double timeStamp = audioEvent.getTimeStamp() - Config.getInt(Key.MONITOR_OVERLAP);
			processMonitorQueryToSerializeFingerprints(audioEvent.getFloatBuffer().clone(), handler,timeStamp);
			return true;
		}
		
		@Override
		public void processingFinished() {
		}
	});
	d.run();
}
 
Example 13
Source File: NFFTStrategy.java    From Panako with GNU Affero General Public License v3.0 5 votes vote down vote up
public List<NFFTFingerprint> extractFingerprintsFromQuery(String query){
	int samplerate = Config.getInt(Key.NFFT_SAMPLE_RATE);
	int size = Config.getInt(Key.NFFT_SIZE);
	int overlap = size - Config.getInt(Key.NFFT_STEP_SIZE);
	AudioDispatcher d = AudioDispatcherFactory.fromPipe(query, samplerate, size, overlap);
	final NFFTEventPointProcessor minMaxProcessor = new NFFTEventPointProcessor(size,overlap,samplerate);
	d.addAudioProcessor(minMaxProcessor);
	d.run();
	List<NFFTFingerprint> fingerprints = new ArrayList<NFFTFingerprint>(minMaxProcessor.getFingerprints());
	return fingerprints;
}
 
Example 14
Source File: Play.java    From Panako with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void run(String... args) {
	String inputResource = AudioResourceUtils.sanitizeResource(args[0]);
	AudioDispatcher d;
	try {
		d = AudioDispatcherFactory.fromPipe(inputResource, TARGET_SAMPLE_RATE, 2028, 0);
		d.addAudioProcessor(new AudioPlayer(JVMAudioInputStream.toAudioFormat(d.getFormat())));
		d.run();
	}  catch (LineUnavailableException e) {
		e.printStackTrace();
		System.err.print(e.getLocalizedMessage());
	}
}
 
Example 15
Source File: NFFTStreamSync.java    From Panako with GNU Affero General Public License v3.0 5 votes vote down vote up
private List<NFFTFingerprint> extractFingerprints(String resource) {
	int samplerate = Config.getInt(Key.NFFT_SAMPLE_RATE);
	int size = Config.getInt(Key.NFFT_SIZE);
	int overlap = size - Config.getInt(Key.NFFT_STEP_SIZE);

	AudioDispatcher d = AudioDispatcherFactory.fromPipe(resource, samplerate, size, overlap);
	final NFFTEventPointProcessor minMaxProcessor = new NFFTEventPointProcessor(size, overlap, samplerate);
	d.addAudioProcessor(minMaxProcessor);
	d.run();
	return new ArrayList<NFFTFingerprint>(minMaxProcessor.getFingerprints());
}
 
Example 16
Source File: RafsCompStats.java    From Panako with GNU Affero General Public License v3.0 5 votes vote down vote up
private static TreeMap<Float, BitSet> extractPackedPrints(File f,boolean trackProbabilities){		
	final int sampleRate = Config.getInt(Key.RAFS_SAMPLE_RATE);//2250Hz Nyquist frequency
	final int size = Config.getInt(Key.RAFS_FFT_SIZE);
	final int overlap = size - Config.getInt(Key.RAFS_FFT_STEP_SIZE);
	String file = f.getAbsolutePath();
	AudioDispatcher d = AudioDispatcherFactory.fromPipe(file, sampleRate, size, overlap);
	d.setZeroPadFirstBuffer(true);
	RafsExtractor ex = new RafsExtractor(file, trackProbabilities);
	//String baseName = f.getName();
	d.addAudioProcessor(ex);
	d.run();
	return ex.fingerprints;
}
 
Example 17
Source File: RafsExtractor.java    From Panako with GNU Affero General Public License v3.0 5 votes vote down vote up
public void starExtraction(){
	AudioDispatcher d = AudioDispatcherFactory.fromPipe(file, sampleRate, size, overlap);
	//every buffer has the same length
	d.setZeroPadFirstBuffer(true);
	d.addAudioProcessor(this);
	d.run();
}
 
Example 18
Source File: RafsRepStrategy.java    From Panako with GNU Affero General Public License v3.0 5 votes vote down vote up
private static RafsExtractor extractExtractor(File f, int fileIndex, boolean trackProbabilities){
	final int sampleRate = Config.getInt(Key.RAFS_SAMPLE_RATE);//2250Hz Nyquist frequency
	final int size = Config.getInt(Key.RAFS_FFT_SIZE);
	final int overlap = size -  Config.getInt(Key.RAFS_FFT_STEP_SIZE); //about an fft every 11.6ms (64/5500)
	String file = f.getAbsolutePath();
	AudioDispatcher d = AudioDispatcherFactory.fromPipe(file, sampleRate, size, overlap);
	d.setZeroPadFirstBuffer(true);
	RafsExtractor ex = new RafsExtractor(file, trackProbabilities);
	//String baseName = f.getName();
	d.addAudioProcessor(ex);
	d.run();
	return ex;
}
 
Example 19
Source File: RafsRepStrategy.java    From Panako with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void monitor(String query, int maxNumberOfReqults, Set<Integer> avoid, QueryResultHandler handler) {
	int samplerate = Config.getInt(Key.RAFS_SAMPLE_RATE);
	int size = Config.getInt(Key.MONITOR_STEP_SIZE) * samplerate;
	int overlap = Config.getInt(Key.MONITOR_OVERLAP) * samplerate;
	AudioDispatcher d ;
	if (query.equals(Panako.DEFAULT_MICROPHONE)){
		try {
			d = AudioDispatcherFactory.fromDefaultMicrophone(samplerate,size, overlap);
		} catch (LineUnavailableException e) {
			LOG.warning("Could not connect to default microphone!" + e.getMessage());
			e.printStackTrace();
			d = null;
		}
	}else{
		d = AudioDispatcherFactory.fromPipe(query, samplerate, size, overlap);
	}
	d.setZeroPadFirstBuffer(true);
	d.addAudioProcessor(new AudioProcessor() {
		@Override
		public boolean process(AudioEvent audioEvent) {
			double timeStamp = audioEvent.getTimeStamp() - Config.getInt(Key.MONITOR_OVERLAP);
			processMonitorQuery(audioEvent.getFloatBuffer().clone(), handler,timeStamp,avoid);
			return true;
		}
		
		@Override
		public void processingFinished() {
		}
	});
	d.run();
}
 
Example 20
Source File: ChromaPrintExtractor.java    From Panako with GNU Affero General Public License v3.0 4 votes vote down vote up
public void starExtraction(){
	AudioDispatcher d = AudioDispatcherFactory.fromPipe(file, sampleRate, size, overlap);
	d.addAudioProcessor(this);
	d.run();
}