Skip to content

Commit

Permalink
Major changes & overhauls
Browse files Browse the repository at this point in the history
-Overhauled the timing system to use the sequencer's built in timer
-Added speed control slider
-Added seek slider

-Moved the resources folder outside source. This produces smaller jars since before it packaged the resources twice for some reason.
-Re-added the IntelliJ project file
  • Loading branch information
marios8543 committed Oct 14, 2018
1 parent ab6799f commit 8b05ac8
Show file tree
Hide file tree
Showing 24 changed files with 107 additions and 65 deletions.
Binary file removed out/artifacts/BongoMidi/BongoMidi.jar
Binary file not shown.
Binary file modified out/artifacts/BongoMidi_jar/BongoMidi.jar
Binary file not shown.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
127 changes: 86 additions & 41 deletions src/com/bongo/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,24 @@
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.concurrent.TimeUnit;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.filechooser.FileNameExtensionFilter;

class Canvas extends JComponent {
public void paintComponent(Graphics g){
if(Main.parser.sequencer.getMicrosecondPosition()==Main.parser.sequencer.getMicrosecondLength()){
Main.finish();
}
Main.timeLabel.setText(String.format("%02d:%02d/%s",
TimeUnit.MICROSECONDS.toMinutes(Main.parser.sequencer.getMicrosecondPosition()),
TimeUnit.MICROSECONDS.toSeconds(Main.parser.sequencer.getMicrosecondPosition()) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MICROSECONDS.toMinutes(Main.parser.sequencer.getMicrosecondPosition()))
,Main.lenstr));
Main.seekslider.setValue((int)Main.parser.sequencer.getMicrosecondPosition());
Main.seekselftrigger=true;
g.clearRect(0, 0, Main.window.getWidth(), Main.window.getHeight());
for(int i=0;i<Main.bongos.length;i++){
Renderer.Bongo bongo = Main.bongos[i];
Expand All @@ -25,78 +38,70 @@ public void paintComponent(Graphics g){
}
class Main{
static Renderer.Bongo[] bongos = new Renderer.Bongo[16];
static JFrame window = new JFrame();
private static boolean playing = true;
private static MidiParser parser;
static final JFrame window = new JFrame();
public static MidiParser parser;
private static File file = null;
private static JButton pauseButton;
private static JLabel timeLabel;
static Boolean seekselftrigger = false;
static String lenstr;
public static JSlider seekslider;
static JLabel timeLabel;
static int seconds = 0;
private static Thread timeThread;

private static void togglePlayPause() {
if (playing) {
if (parser.sequencer.isRunning()) {
parser.sequencer.stop();
pauseButton.setText("PLAY");
timeThread.interrupt();
} else {
parser.sequencer.start();
pauseButton.setText("PAUSE");
timeThread = generateTimeThread();
timeThread.start();
}
playing = !playing;
}

private static void incrementSeconds() {
seconds++;
timeLabel.setText(String.format("%02d:%02d", seconds / 60, seconds % 60));
if (!parser.sequencer.isRunning()) {
finish();
}
}

private static void finish() {
static void finish() {
pauseButton.setText("RESTART");
pauseButton.setActionCommand("restart");
parser.sequencer.stop();
playing = false;
bongos = new Renderer.Bongo[16];
window.repaint();
timeThread.interrupt();
}

private static void restart() {
parser.sequencer.setTickPosition(0);
bongos = new Renderer.Bongo[16];
window.repaint();
window.setName("Bongo Cat MIDI Player - " + file.getName());
window.setTitle("Bongo Cat MIDI Player - " + file.getName());
seconds = 0;
togglePlayPause();
pauseButton.setActionCommand("toggle");
}

private static Thread generateTimeThread() {
return new Thread(() -> {
while (true) {
try {
Thread.sleep(1000);
incrementSeconds();
} catch (Exception e) {
break;
}
}
});
}

public static void main(String[] args) throws Exception {
Renderer.build_coords();
private static MidiParser init_player() throws Exception{
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("MIDI Files", "mid", "midi");
chooser.setFileFilter(filter);
File file = null;
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
file = chooser.getSelectedFile();
} else {
return null;
}
MidiParser private_parser = new MidiParser(file);
lenstr = String.format("%02d:%02d",
TimeUnit.MICROSECONDS.toMinutes(private_parser.sequencer.getMicrosecondLength()),
TimeUnit.MICROSECONDS.toSeconds(private_parser.sequencer.getMicrosecondLength()) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MICROSECONDS.toMinutes(private_parser.sequencer.getMicrosecondLength())));
seekslider = new JSlider(0,(int)private_parser.sequencer.getMicrosecondLength());
return private_parser;
}

public static void main(String[] args) throws Exception {
Renderer.build_coords();

parser = init_player();
if(parser==null){
System.exit(0);
}
parser = new MidiParser(file);

JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
Expand All @@ -123,11 +128,53 @@ public static void main(String[] args) throws Exception {
});
pauseButton.setAlignmentX(Component.CENTER_ALIGNMENT);

timeLabel = new JLabel("00:00");
JButton openButton = new JButton("Open file");
openButton.addActionListener(e -> {
togglePlayPause();
try{
MidiParser private_parser = init_player();
if(private_parser==null){
togglePlayPause();
}
else {
parser = private_parser;
restart();
}
}
catch (Exception ee){
JOptionPane.showMessageDialog(window, ee.getMessage(), "File open error", JOptionPane.ERROR_MESSAGE);
}
});

timeLabel = new JLabel("00:00");
openButton.setAlignmentX(Component.RIGHT_ALIGNMENT);
buttonPanel.add(openButton);
buttonPanel.add(Box.createRigidArea(new Dimension(50,0)));
buttonPanel.add(pauseButton);
buttonPanel.add(Box.createRigidArea(new Dimension(10,0)));

JSlider speedSlider = new JSlider(33, 300);
speedSlider.addChangeListener(e -> {
JSlider source = (JSlider)e.getSource();
parser.sequencer.setTempoInBPM((float) source.getValue());
});
buttonPanel.add(Box.createRigidArea(new Dimension(100,0)));
JLabel speed = new JLabel("Speed:");
buttonPanel.add(speed);
buttonPanel.add(speedSlider);

buttonPanel.add(Box.createRigidArea(new Dimension(50,0)));
seekslider = new JSlider(0,(int)parser.sequencer.getMicrosecondLength());
seekslider.addChangeListener(e -> {
JSlider source = (JSlider)e.getSource();
if(seekselftrigger){
seekselftrigger=false;
return;
}
parser.sequencer.setMicrosecondPosition(source.getValue());
});
buttonPanel.add(timeLabel);
buttonPanel.add(seekslider);

Canvas c = new Canvas();
c.setLocation(new Point(0, 0));
Expand All @@ -143,8 +190,6 @@ public static void main(String[] args) throws Exception {
window.getContentPane().setBackground(Color.WHITE);
window.getContentPane().add(Box.createRigidArea(new Dimension(0,5))); // bottom padding
window.setVisible(true);
timeThread = generateTimeThread();
timeThread.start();
parser.sequencer.start();
}
}
10 changes: 4 additions & 6 deletions src/com/bongo/MidiParser.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.bongo;

import javax.sound.midi.*;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -20,10 +18,10 @@ class MidiParser {
}


public static class creceiver implements Receiver {
Map<Integer,Integer> v_instrs = new HashMap<>();
ArrayList<Integer> v_ons = new ArrayList<>();
ArrayList<Integer> v_offs = new ArrayList<>();
static class creceiver implements Receiver {
final Map<Integer,Integer> v_instrs = new HashMap<>();
final ArrayList<Integer> v_ons = new ArrayList<>();
final ArrayList<Integer> v_offs = new ArrayList<>();

creceiver(){
for(int i=192;i<=207;i++){
Expand Down
12 changes: 6 additions & 6 deletions src/com/bongo/Note.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.bongo;

public class Note {
class Note {

Integer midi_number;
Integer channel;
Integer patch;
Renderer.Instr_Categ cpatch;
Boolean status;
final Integer midi_number;
final Integer channel;
private final Integer patch;
final Renderer.Instr_Categ cpatch;
final Boolean status;

Note(Integer channel, Integer midi_number, Integer instrument, Boolean status){
this.patch = instrument;
Expand Down
23 changes: 11 additions & 12 deletions src/com/bongo/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import javax.imageio.ImageIO;

abstract class Renderer {
private static ArrayList<Integer> xcoords = new ArrayList<>();
private static ArrayList<Integer> ycoords = new ArrayList<>();
private static ArrayList<Integer[]> coords = new ArrayList<>();
private static final ArrayList<Integer> xcoords = new ArrayList<>();
private static final ArrayList<Integer> ycoords = new ArrayList<>();
private static final ArrayList<Integer[]> coords = new ArrayList<>();

static void build_coords(){
for(int i=0;i<1024;i+=301){
Expand All @@ -23,10 +23,10 @@ static void build_coords(){
}

public static class Bongo {
Integer x;
Integer y;
Integer lastSecondValue;
Note note;
final Integer x;
final Integer y;
final Integer lastSecondValue;
final Note note;
Boolean l_hand=false;
Boolean r_hand=false;

Expand Down Expand Up @@ -61,7 +61,6 @@ public static class Bongo {
this.y = c[1];
}
this.note = note;

this.lastSecondValue = Main.seconds;
}
@Override
Expand All @@ -70,7 +69,7 @@ public String toString(){
}
}

private static ClassLoader classLoader = Renderer.class.getClassLoader();
private static final ClassLoader classLoader = Renderer.class.getClassLoader();

private static Image load_asset(String asset){
try{
Expand All @@ -97,7 +96,7 @@ public enum Instr_Categ{
Ethnic(load_asset("ensemble.png")),
Percussion(load_asset("drums.png")),
FX(load_asset("ensemble.png"));
private Image asset;
private final Image asset;
Instr_Categ(Image s){
this.asset = s;
}
Expand All @@ -111,11 +110,11 @@ public enum hands{
lh2(load_asset("l2.png")),
rh1(load_asset("r1.png")),
rh2(load_asset("r2.png"));
private Image hand;
private final Image hand;
hands(Image s){
this.hand = s;
}
public Image get_hand(){
Image get_hand(){
return hand;
}
}
Expand Down

0 comments on commit 8b05ac8

Please sign in to comment.