Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
BartSchuurmans committed Dec 24, 2011
0 parents commit 25a924e
Show file tree
Hide file tree
Showing 11 changed files with 391 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/*
47 changes: 47 additions & 0 deletions build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<project name="midi2chord" default="build" basedir=".">
<property name="project.name" value="midi2chord" />

<property name="src.dir" value="src" />
<property name="lib.dir" value="lib" />
<property name="build.dir" value="build" />
<property name="build.classes.dir" value="${build.dir}/classes" />
<property name="build.lib.dir" value="${build.dir}/lib" />

<path id="classpath">
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
</path>

<target name="all" depends="build">
</target>

<target name="build" depends="build-prepare">
<javac srcdir="${src.dir}"
destdir="${build.classes.dir}"
debug="yes"
deprecation="no"
optimize="on"
includeantruntime="no">
<classpath refid="classpath" />
</javac>
</target>

<target name="build-prepare">
<mkdir dir="${build.dir}" />
<mkdir dir="${build.classes.dir}" />
<mkdir dir="${build.lib.dir}" />
</target>

<target name="jar" depends="build">
<jar destfile="${build.lib.dir}/${project.name}.jar" basedir="${build.classes.dir}">
<manifest>
<attribute name="Main-Class" value="com.minnozz.midi2chord.MIDI2ChordApp" />
</manifest>
</jar>
</target>

<target name="clean">
<delete dir="${build.dir}" />
</target>
</project>
12 changes: 12 additions & 0 deletions src/com/minnozz/midi2chord/CLIChordDisplayer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.minnozz.midi2chord;

public class CLIChordDisplayer extends ChordDisplayer {
@Override
public void display(Chord chord) {
if(chord == null) {
System.out.println("Current chord: unknown");
} else {
System.out.println("Current chord: "+ chord.getName());
}
}
}
21 changes: 21 additions & 0 deletions src/com/minnozz/midi2chord/Chord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.minnozz.midi2chord;

import java.util.ArrayList;

public class Chord {
private String name;
private ArrayList<Note> notes;

public Chord(String name, ArrayList<Note> notes) {
this.name = name;
this.notes = notes;
}

public String getName() {
return name;
}

public ArrayList<Note> getNotes() {
return notes;
}
}
5 changes: 5 additions & 0 deletions src/com/minnozz/midi2chord/ChordDisplayer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.minnozz.midi2chord;

public abstract class ChordDisplayer {
public abstract void display(Chord chord);
}
23 changes: 23 additions & 0 deletions src/com/minnozz/midi2chord/ChordFinder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.minnozz.midi2chord;

import java.util.ArrayList;

public class ChordFinder {
public ArrayList<Chord> find(ArrayList<Note> notes) {
ArrayList<Chord> options = new ArrayList<Chord>();

// TODO

return options;
}

public Chord findMostLikely(ArrayList<Note> notes) {
ArrayList<Chord> options = find(notes);

if(options.isEmpty()) {
return null;
}

return options.get(0);
}
}
77 changes: 77 additions & 0 deletions src/com/minnozz/midi2chord/MIDI2ChordApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.minnozz.midi2chord;

import java.util.ArrayList;

public class MIDI2ChordApp implements Runnable {
private NoteSource source;
private ChordFinder chordFinder;
private ChordDisplayer displayer;

public MIDI2ChordApp(NoteSource source, ChordDisplayer displayer) {
this.source = source;
this.displayer = displayer;
}

private static NoteSource createNoteSource(String type) {
if(type.equals("midi")) {
return new MIDINoteSource();
}
throw new IllegalArgumentException();
}

private static ChordDisplayer createChordDisplayer(String type) {
if(type.equals("cli")) {
return new CLIChordDisplayer();
}
throw new IllegalArgumentException();
}

public void run() {
chordFinder = new ChordFinder();

source.registerNoteListener(new NoteListener() {
public void onConnect() {
System.out.println("Note listener connected");
}

public void onDisconnect() {
System.out.println("Note listener disconnected");
}

public void onUpdate(ArrayList<Note> notes) {
Chord chord = chordFinder.findMostLikely(notes);
displayer.display(chord);
}
});

if(source.connect()) {
System.out.println("Source is connected");
} else {
System.out.println("Error connecting to source");
System.exit(1);
}
}

public static void main(String[] args) {
if(args.length != 2) {
usage();
}

NoteSource source;
ChordDisplayer displayer;
try {
source = createNoteSource(args[0]);
displayer = createChordDisplayer(args[1]);

new MIDI2ChordApp(source, displayer).run();
} catch(IllegalArgumentException e) {
System.out.println("Error: "+ e.getMessage());
usage();
}
}

private static void usage() {
System.out.println("Usage: MIDI2ChordApp <noteSourceType> <chordDisplayerType>");
System.exit(1);
}
}
77 changes: 77 additions & 0 deletions src/com/minnozz/midi2chord/MIDINoteSource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.minnozz.midi2chord;

import java.util.ArrayList;
import javax.sound.midi.*;

public class MIDINoteSource extends NoteSource {
private Transmitter transmitter;
private ArrayList<Note> currentNotes;

@Override
protected boolean _connect() {
System.out.println("Connecting MIDI note source");

try {
transmitter = MidiSystem.getTransmitter(); // Default MIDI source
} catch(MidiUnavailableException e) {
System.out.println(e.getMessage());
return false;
}

currentNotes = new ArrayList<Note>();
transmitter.setReceiver(new MIDINoteSourceReceiver());

return true;
}

@Override
protected void _disconnect() {
if(transmitter != null) {
transmitter.close();
transmitter = null;
}
}

private class MIDINoteSourceReceiver implements Receiver {
public void close() {
// Ignore
}

public void send(MidiMessage m, long timeStamp) {
if(m instanceof ShortMessage) {
ShortMessage message = (ShortMessage)m;

switch(message.getStatus()) {
case ShortMessage.NOTE_ON:
onNoteOn(message.getData1());
break;
case ShortMessage.NOTE_OFF:
onNoteOff(message.getData1());
break;
}
}
}
}

private void onNoteOn(int noteNumber) {
Note note = new Note(noteNumber);

synchronized(currentNotes) {
if(!currentNotes.contains(note)) {
currentNotes.add(note);
broadcastUpdate(currentNotes);
}
}
}

private void onNoteOff(int noteNumber) {
Note note = new Note(noteNumber);

synchronized(currentNotes) {
if(currentNotes.contains(note)) {
currentNotes.remove(note);
broadcastUpdate(currentNotes);
}
}
}
}
41 changes: 41 additions & 0 deletions src/com/minnozz/midi2chord/Note.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.minnozz.midi2chord;

import java.lang.Math;

public class Note {
final public static String[] NOTES = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};

private int noteNumber;

public Note(int noteNumber) {
this.noteNumber = noteNumber;
}

public static String getName(int noteNumber) {
return NOTES[noteNumber % 12];
}

public static int getOctive(int noteNumber) {
return (int)(Math.floor(noteNumber / 12) - 1);
}

public int getNoteNumber() {
return noteNumber;
}

public String getName() {
return getName(noteNumber);
}

public int getOctive() {
return getOctive(noteNumber);
}

public String getNameWithOctive() {
return getName() + getOctive();
}

public boolean equals(Note note) {
return (getNoteNumber() == note.getNoteNumber());
}
}
9 changes: 9 additions & 0 deletions src/com/minnozz/midi2chord/NoteListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.minnozz.midi2chord;

import java.util.ArrayList;

public interface NoteListener {
public void onConnect();
public void onDisconnect();
public void onUpdate(ArrayList<Note> notes);
}
Loading

0 comments on commit 25a924e

Please sign in to comment.