/* * MidiMesser - © 2003 Andrew R. Brown * * 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 2 * 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, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ import jm.JMC; import jm.music.data.*; import jm.music.tools.*; import jm.util.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MidiMesser implements JMC, ActionListener { private JButton loadBtn, saveBtn, processBtn, playBtn, stopBtn; private JComboBox processList; private JTextField modValue; private Score score; private JLabel units; static Thread pauseThread; public static void main(String[] args) { new MidiMesser(); } public MidiMesser() { processList = new JComboBox(); modValue = new JTextField("0.0", 3); score = new Score(); units = new JLabel(); makeGUI(); } private void makeGUI() { JFrame window = new JFrame("MIDI Messer: by Andrew Brown"); JPanel mainPanel = new JPanel(new BorderLayout()); Box centerPanel = new Box(BoxLayout.X_AXIS); window.getContentPane().add(mainPanel); JPanel filePanel = new JPanel(new GridLayout(4,1)); loadBtn = new JButton("Load MIDI file"); loadBtn.addActionListener(this); filePanel.add(loadBtn); saveBtn = new JButton("Save MIDI file"); saveBtn.addActionListener(this); saveBtn.setEnabled(false); filePanel.add(saveBtn); playBtn = new JButton("Play"); playBtn.addActionListener(this); playBtn.setEnabled(false); filePanel.add(playBtn); stopBtn = new JButton("Stop"); stopBtn.addActionListener(this); stopBtn.setEnabled(false); filePanel.add(stopBtn); centerPanel.add(filePanel); JPanel processPanel = new JPanel(new GridLayout(3,1)); JLabel processLabel = new JLabel("Select Process"); processLabel.setHorizontalAlignment(SwingConstants.CENTER); processPanel.add(processLabel); JPanel modPanel = new JPanel(); processList.addActionListener(this); processList.setEnabled(false); processList.addItem("Add to rhythm value"); processList.addItem("Multiply rhythm value"); processList.addItem("Add to note duration"); processList.addItem("Multiply note duration"); processList.addItem("Multiply intervals"); processList.addItem("Shuffle pitches"); modPanel.add(processList); modValue.setEnabled(false); modPanel.add(modValue); units.setPreferredSize(new Dimension(65, 20)); modPanel.add(units); processPanel.add(modPanel); processBtn = new JButton("Apply Process"); processBtn.addActionListener(this); processBtn.setEnabled(false); processPanel.add(processBtn); centerPanel.add(processPanel); mainPanel.add(centerPanel, "Center"); // padding mainPanel.add(new JPanel(), "North"); JLabel advert = new JLabel("Made with jMusic"); advert.setFont(new Font("Helvetica", Font.PLAIN, 9)); advert.setHorizontalAlignment(SwingConstants.CENTER); mainPanel.add(advert, "South"); mainPanel.add(new JPanel(), "East"); mainPanel.add(new JPanel(), "West"); // display window.pack(); window.setResizable(false); window.setVisible(true); } public void actionPerformed(ActionEvent e) { if(e.getSource() == processList) { modValue.setEnabled(true); if (processList.getSelectedIndex() == 0) { // add rhythm modValue.setText("0.0"); units.setText("beats"); } if (processList.getSelectedIndex() == 1) { // multiply rhythm modValue.setText("1.0"); units.setText("multiplier"); } if (processList.getSelectedIndex() == 2) { // add duration modValue.setText("0.0"); units.setText("beats"); } if (processList.getSelectedIndex() == 3) { // multiply duration modValue.setText("1.0"); units.setText("multiplier"); } if (processList.getSelectedIndex() == 4) { // multiply intervals modValue.setText("1.0"); units.setText("multiplier"); } if (processList.getSelectedIndex() == 5) { // randomise pitch order modValue.setText(""); modValue.setEnabled(false); units.setText(""); } } if(e.getSource() == loadBtn) { readMidiFile(); saveBtn.setEnabled(true); playBtn.setEnabled(true); processList.setEnabled(true); processBtn.setEnabled(true); modValue.setEnabled(true); } if(e.getSource() == saveBtn) writeMidiFile(); if(e.getSource() == processBtn) processScore(); if(e.getSource() == playBtn) playScore(); if(e.getSource() == stopBtn) stopScore(); } private void readMidiFile() { Read.midi(score); } private void writeMidiFile() { Write.midi(score); } private void playScore() { stopBtn.setEnabled(true); playBtn.setEnabled(false); Play.midi(score, false); midiWait(score, this); } private void stopScore() { Play.stopMidi(); stopBtn.setEnabled(false); playBtn.setEnabled(true); } private static void midiWait(final Score score, final MidiMesser app) { pauseThread = new Thread( new Runnable() { public void run() { try { pauseThread.sleep((int)(score.getEndTime() * 60.0 /score.getTempo() * 1000.0 + 1000.0)); } catch (Exception e) {System.out.println("Error in pause thread");} app.stopScore(); }}); pauseThread.start(); } private void processScore() { String valString = modValue.getText(); System.out.println("processing score... [" + processList.getSelectedIndex() + "]"); if(valString.length() == 0 && processList.getSelectedIndex() != 5) return; if (processList.getSelectedIndex() == 0) { // add rhythm System.out.println("Add to length of note " + valString); Mod.addToLength(score, Double.parseDouble(valString)); } if (processList.getSelectedIndex() == 1) { // multipy length System.out.println("Multiply note length by "+ valString); Mod.elongate(score, Double.parseDouble(valString)); } if (processList.getSelectedIndex() == 2) { // add duration System.out.println("Add to duration " + valString); Mod.addToDuration(score, Double.parseDouble(valString)); } if (processList.getSelectedIndex() == 3) { // multipy duration System.out.println("Multiply duration by "+ valString); Mod.increaseDuration(score, Double.parseDouble(valString)); } if (processList.getSelectedIndex() == 4) { // multipy intervals System.out.println("Multiply each interval by "+ valString); Mod.expandIntervals(score, Double.parseDouble(valString)); } if (processList.getSelectedIndex() == 5) { // shuffle pitches System.out.println("Randomising pitch order "); Mod.shuffle(score); } } }