001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.audio.util;
031
032import org.openimaj.audio.timecode.MeasuresBeatsTicksTimecode;
033import org.openimaj.time.TimeKeeper;
034
035/**
036 *      A timekeeper that generates {@link MeasuresBeatsTicksTimecode}s that allow
037 *      the position within a music score to be tracked.
038 *
039 *      @author David Dupplaw (dpd@ecs.soton.ac.uk)
040 *  @created 12 Feb 2013
041 *      @version $Author$, $Revision$, $Date$
042 */
043public class BasicMusicTimekeeper implements TimeKeeper<MeasuresBeatsTicksTimecode>
044{
045        /** The time the timekeeper is started */
046        private long lastStarted;
047        
048        /** The time to offset if the timekeeper is paused and restarted */
049        private long timeOffset;
050        
051        /** The time the timekeeper was paused */
052        private long pausedAt;
053
054        /** Whether the timekeeper is running or not */
055        private boolean isRunning;
056
057        /** The last calculated timestamp - with offsets */
058        private long currentTime;
059
060        /** The timecode */
061        private MeasuresBeatsTicksTimecode timecode = 
062                        new MeasuresBeatsTicksTimecode( 120, 0, 0, 0, 4 );
063        
064        /**
065         *      Set the tempo of the timekeeper
066         *      @param bpm The new beats per minute.
067         */
068        public void setBPM( final float bpm )
069        {
070                this.timecode = new MeasuresBeatsTicksTimecode( bpm, 
071                                this.timecode.getMeasures(), this.timecode.getBeats(), 
072                                this.timecode.getTicks(), this.timecode.beatsPerMeasure );
073        }
074        
075        /**
076         *      Returns the number of BPMs this timekeeper is running at.
077         *      @return The BPMs
078         */
079        public double getBPM()
080        {
081                return this.timecode.bpm;
082        }
083        
084        /**
085         *      {@inheritDoc}
086         *      @see org.openimaj.time.TimeKeeper#run()
087         */
088        @Override
089        public void run()
090        {
091                if( this.lastStarted == 0 )
092                        this.lastStarted = System.currentTimeMillis();
093                else
094                        if( this.supportsPause() )
095                                this.timeOffset += System.currentTimeMillis() - this.pausedAt;
096                
097                this.isRunning = true;
098        }
099
100        /**
101         *      {@inheritDoc}
102         *      @see org.openimaj.time.TimeKeeper#pause()
103         */
104        @Override
105        public void pause()
106        {
107                if( this.supportsPause() )
108                {
109                        this.isRunning = false;
110                        this.pausedAt = System.currentTimeMillis();
111                }
112        }
113
114        /**
115         *      {@inheritDoc}
116         *      @see org.openimaj.time.TimeKeeper#stop()
117         */
118        @Override
119        public void stop()
120        {
121                this.isRunning = false;
122                this.currentTime = 0;
123        }
124
125        /**
126         *      {@inheritDoc}
127         *      @see org.openimaj.time.TimeKeeper#getTime()
128         */
129        @Override
130        public MeasuresBeatsTicksTimecode getTime()
131        {
132                if( this.isRunning )
133                {
134                        // Update the current time.
135                        this.currentTime = (System.currentTimeMillis() -
136                                        this.lastStarted - this.timeOffset);
137                        this.timecode.setTimecodeInMilliseconds( this.currentTime );
138                }
139
140                return this.timecode;
141        }
142
143        /**
144         *      {@inheritDoc}
145         *      @see org.openimaj.time.TimeKeeper#seek(long)
146         */
147        @Override
148        public void seek( final long timestamp )
149        {
150                // Doesn't support seek
151        }
152
153        /**
154         *      {@inheritDoc}
155         *      @see org.openimaj.time.TimeKeeper#reset()
156         */
157        @Override
158        public void reset()
159        {
160                this.lastStarted = 0;
161                this.pausedAt = -1;
162                this.run();
163        }
164
165        /**
166         *      {@inheritDoc}
167         *      @see org.openimaj.time.TimeKeeper#supportsPause()
168         */
169        @Override
170        public boolean supportsPause()
171        {
172                return true;
173        }
174
175        /**
176         *      {@inheritDoc}
177         *      @see org.openimaj.time.TimeKeeper#supportsSeek()
178         */
179        @Override
180        public boolean supportsSeek()
181        {
182                return false;
183        }
184
185        /**
186         *      Returns the number of ticks per beat
187         *      @return the number of ticks per beat
188         */
189        public int getTicksPerBeat()
190        {
191                return this.timecode.ticksPerBeat;
192        }
193}