-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 25a924e
Showing
11 changed files
with
391 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
build/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.