/*
* Musical Skill Coachh - An interactive midi device friendly program to help music students improve their skills
* Copyright (C) 2011 Paul-Emile Gaudet
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package music.modules;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import javax.swing.JLabel;
import music.lib.MusicScoreNote;
import music.midi.MidiReceiver;
import music.midi.MidiTransmitter;
import music.ui.lib.Bar;
import music.ui.lib.MetronomeCanvas;
import music.ui.panels.Timing;
public class NoteTiming implements ActionListener
{
protected Timing timingPanel;
protected MidiReceiver mRec;
protected MidiTransmitter mTrans;
protected MetronomeCanvas metronome;
protected JLabel statusBar;
protected boolean started;
protected Random rand;
protected int nextKey;
protected ArrayList<MusicScoreNote> scaleNotes;
public NoteTiming(MetronomeCanvas metronome)
{
timingPanel = null;
mRec = null;
mTrans = null;
scaleNotes = null;
this.metronome = metronome;
started = false;
rand = new Random();
long l = System.currentTimeMillis();
int counter = (int) ((l % 1000) + ((l / 1000) % 3));
while (counter > 0)
{
rand.nextInt();
counter--;
}
nextKey = (rand.nextInt() % (96 - 36)) + 36;
}
public Timing getTbStaff()
{
return timingPanel;
}
public void setPanel(Timing timingPanel)
{
this.timingPanel = timingPanel;
}
public MidiReceiver getmRec()
{
return mRec;
}
public void setmRec(MidiReceiver mRec)
{
this.mRec = mRec;
if (mRec != null)
mRec.addActionListener(this);
}
public MidiTransmitter getmTrans()
{
return mTrans;
}
public void setmTrans(MidiTransmitter mTrans)
{
this.mTrans = mTrans;
}
public JLabel getStatusBar()
{
return statusBar;
}
public void setStatusBar(JLabel statusBar)
{
this.statusBar = statusBar;
}
public void setFixedScale(ArrayList<MusicScoreNote> scaleNotes)
{
this.scaleNotes = scaleNotes;
}
public void start()
{
nextKey = ((rand.nextInt() & 0xff) % (96 - 36)) + 36;
if (timingPanel != null)
{
timingPanel.removeNotes();
// timingPanel.addMusicTime(2, 4);
Bar bar = null;
if ((scaleNotes == null) || (scaleNotes.size() < 1))
timingPanel.addNote(bar, 60, 0, 8);
else
{
MusicScoreNote msn;
Iterator<MusicScoreNote> iterator = scaleNotes.iterator();
while (iterator.hasNext())
{
msn = iterator.next();
timingPanel.addNote(bar, msn.getMidiNote(), 0, 8);
}
}
//timingPanel.start();
}
if (metronome != null)
metronome.setBeatsPerMinute(60);
if (statusBar != null)
{
if ((mTrans == null) || (mTrans.hasDevice() == false))
statusBar.setText("TIMING EXERCISE REQUIRES A MIDI INSTRUMENT. Try to maintain the beat as much as possible.");
else
statusBar.setText("Try to maintain the beat as much as possible.");
}
started = true;
}
public void stop()
{
if (metronome != null)
metronome.setBeatsPerMinute(0);
started = false;
}
@Override
public void actionPerformed(ActionEvent e)
{
rand.nextInt();
if (!started)
return;
String source = e.getActionCommand();
int force = 0;
if (source != null)
{
try
{
Integer integer = new Integer(source);
force = integer.intValue();
}
catch (Exception nfException)
{
}
}
if ((timingPanel != null) && (metronome != null))
timingPanel.addTiming(metronome.getMetroDelta(), force);
}
}