BeatRootOnsetEventHandler.java (2885B)
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.beatroot; 25 26 import java.util.Iterator; 27 28 import be.tarsos.dsp.onsets.OnsetHandler; 29 30 /** 31 * Forms a bridge between the BeatRoot beat tracking system and an 32 * interchangeable onset detector. The beat tracker does not work in real-time. 33 * First all onsets need to be detected. In a post-processing step a beat 34 * estimation is done using reocurring inter onset intervals (IOI's). To return 35 * the time of the beats an OnsetHandler is abused. 36 * 37 * @author Joren Six 38 */ 39 public class BeatRootOnsetEventHandler implements OnsetHandler { 40 41 private final EventList onsetList = new EventList(); 42 43 @Override 44 public void handleOnset(double time, double salience) { 45 double roundedTime = Math.round(time *100 )/100.0; 46 Event e = newEvent(roundedTime,0); 47 e.salience = salience; 48 onsetList.add(e); 49 } 50 51 52 /** 53 * Creates a new Event object representing an onset or beat. 54 * 55 * @param time 56 * The time of the beat in seconds 57 * @param beatNum 58 * The index of the beat or onset. 59 * @return The Event object representing the beat or onset. 60 */ 61 private Event newEvent(double time, int beatNum) { 62 return new Event(time,time, time, 56, 64, beatNum, 0, 1); 63 } 64 65 /** 66 * Guess the beats using the populated list of onsets. 67 * 68 * @param beatHandler 69 * Use this handler to get the time of the beats. The salience of 70 * the beat is not calculated: -1 is returned. 71 */ 72 public void trackBeats(OnsetHandler beatHandler){ 73 AgentList agents = null; 74 // tempo not given; use tempo induction 75 agents = Induction.beatInduction(onsetList); 76 agents.beatTrack(onsetList, -1); 77 Agent best = agents.bestAgent(); 78 if (best != null) { 79 best.fillBeats(-1.0); 80 EventList beats = best.events; 81 Iterator<Event> eventIterator = beats.iterator(); 82 while(eventIterator.hasNext()){ 83 Event beat = eventIterator.next(); 84 double time = beat.keyDown; 85 beatHandler.handleOnset(time, -1); 86 } 87 } else { 88 System.err.println("No best agent"); 89 } 90 } 91 92 }