package yakimusic.sound;
import java.io.File;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import org.jfugue.Pattern;
import org.jfugue.Player;
public class YMLSong {
private Pattern song;
private int layer;
private static Player player = new Player();
public boolean parar;
private static YMLSong instance;
public YMLSong(int tempo) {
song = new Pattern("T" + tempo);
layer = 1;
instance = this;
}
public static YMLSong getInstance() {
return instance;
}
public void addTrack(String instrument, String sounds) {
String instrumentID = YMLInstrument.getInstrumentRealName(instrument);
String s = "V" + layer + " I[" + instrumentID + "] " + sounds;
// System.out.println(s);
Pattern aux = new Pattern(s);
// Pattern round = new Pattern("V" + layer);
// round.add(aux);
song.add(aux);
layer++;
}
public void exportToMidi() {
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
fc.showSaveDialog(null);
File f = fc.getSelectedFile();
if (f != null) {
try {
player.saveMidi(song, f);
} catch (IOException e) {
// TODO Auto-generated catch block
System.err.println("No se puede exportar la canción.");
}
}
}
public void stop() {
if (player != null) {
if (player.isPlaying()) {
player.stop();
}
}
}
public void testPlay() throws IOException {
// player.saveMidi(song, new File("/home/sgm/Escritorio/prueba.midi"));
// System.out.println("Guardado. Ahora sonará");
if(player.isPlaying()){
JOptionPane
.showMessageDialog(
null,
"Ya hay una canción en reproducción.\nDeténgala antes de intentar reproducir otra.",
"Error", JOptionPane.ERROR_MESSAGE);
}else
new Thread() {
@Override
public void run() {
parar = false;
player.play(song);
while (!parar && player.isPlaying())
;
if (player.isPlaying())
player.stop();
}
}.start();
}
public void addRhythm(YMLRhythm rhythm, int times) {
Pattern p = rhythm.getRhythmPattern();
p.repeat(times);
song.add(p);
}
public void addRhythm(YMLRhythm rhythm) {
addRhythm(rhythm, 1);
}
public static YMLSong genExampleSong() {
YMLSong song = new YMLSong(120);
song
.addTrack(
"PIANO",
"E5s A5s C6s B5s E5s B5s D6s C6i E6i G#5i E6i | A5s E5s A5s C6s B5s E5s B5s D6s C6i A5i Ri");
song
.addTrack(
"FLUTE",
"Rs Rs Rs Rs E5s B5s D6s C6i E6i G#5i E6i | A5s E5s A5s C6s B5s E5s B5s D6s C6i A5i Ri");
song
.addTrack("HARMONICA",
"Rs Rs Rs Rs Rs Rs Rs Ri Ri Ri Ri | A5s E5s A5s C6s B5s E5s B5s D6s C6i A5i Ri");
song.addRhythm(YMLRhythm.genHouseRhythm('t'), 3);
return song;
}
public static YMLSong genTitanic() {
YMLSong song = new YMLSong(70);
song
.addTrack(
"PIANO",
"E6s D#6s | E6s D#6s E6s B5s D#6s C6s | A5i Rs C5s E5s A5s | B5i Rs E5s G#5s B5s | C6i Rs E5s E6s D#6s | E6s D#6s E6s B5s D#6s C6s |");
song
.addTrack(
"PIANO",
"Ri | Rq Ri | A3s E4s A4s Rs Ri | E3s E4s G#4s Rs Ri | A3s E4s A4s Rs Ri | Rq Ri | ");
return song;
}
public static YMLSong genPinkFloyd() {
// Another Brick in the Wall (Pink Floyd)
YMLSong song = new YMLSong(450);
// Guitarra 1
song
.addTrack(
"GUITAR",
"D5h D5q D5q D5h D5q D5q D5h D5q D5q D5h D5q D5q | D5h D5q D5q D5h D5q D5q D5h D5q D5q D5h D5q D5q | "
+ "C5q D5q Rq C5q D5h D5q D5q D5q D5h D5q D5h D5q D5q | D5h D5q D5q D5h D5q D5q D5h D5q D5q F5q D5q G5q F5q | D5h D5q D5q D5h D5q D5q D5h D5q D5q D5h D5q D5q |"
+ "D5q Rh D5h D5q D5h D5q D5q D5h D5q D5q D5h C5q A4q | D5h D5q D5q D5h D5q D5q D5h D5q D5q D5h D5q D5q | A5h A5q A5q A5h A5q A5q A5h A5q A5q F5q D5q G5q F5q |"
+ "D5w E5h F5h F5w Rh Rq E5q | D5w E5h F5h F5h E5h Rw | Rw Rw Rw Rw | Rw Rw Rw Rw | D5w E5h F5h F5h E5w E5h | A6w A6h A6h A6w Rw |"
+ "Rw Rw Rw Rw | Rw Rw Rw Rh E5h | D5w E5w F5h E5w E5h |"
+ "D5w E5h F5h E5w Rh | Rw Rw Rw Rw | Rw Rw Rw Rw |"
+ "D5h E5w E5h F5q E5h E5q E5w | D5w E5h F5h F5w D5h G5h | G5w Rw Rw Rw |"
+ "Rw Rw Rw Rw | Rw Rw Rw Rw | G6q G6q Rq G6h F6h A6h G6q F6q F6h G6h |"
+ "F6w Rw Rw Rw | Rw Rw Rw Rw | Rw F5h F5q F5q F5h F5q F5q F5w |"); // Hechas
// las
// 9
// primeras
// paginas
// Guitarra 2
song
.addTrack(
"GUITAR",
"D5h D5q D5q D5h D5q D5q D5h D5q D5q D5h D5q D5q | D5h D5q D5q D5h D5q D5q D5h D5q D5q D5h D5q D5q | "
+ "C5q D5q Rq C5q D5h D5q D5q D5q D5h D5q D5h D5q D5q | D5h D5q D5q D5h D5q D5q D5h D5q D5q F5q D5q G5q F5q | D5w Rw D5q D5q D5q D5q D5h D5q D5q | "
+ "D5h D5q D5q D5h D5q D5q D5h D5q D5q D5h C5q A4q | D5w Rw D5q D5q D5q D5q D5h D5q D5q | D5h D5q D5q D5h D5q D5q D5h D5q D5q F5q D5q G5q F5q | "
+ "D5h D5q D5q D5h D5q D5q D5h D5q D5q D5h D5q D5q | D5h D5q D5q D5h D5q D5q D5h D5q D5q D5h D5q D5q | D5h D5q D5q D5h D5q D5q D5h D5q D5q D5h D5q D5q |"
+ " D5h D5q D5q D5h D5q D5q D5h D5q D5q D5h C5q A4q | D5h D5q D5q D5h D5q D5q D5h D5q D5q D5h D5q D5q |D5h D5q D5q D5h D5q D5q D5h D5q D5q D5h D5q D5q |"
+ " D5h D5q D5q D5h D5q D5q D5q D5h D5q D5h D5q D5q | D5h D5q D5q D5h D5q D5q D5h D5q D5q D5h F5q D5q | D5h D5q D5q D5h D5q D5q D5q D5q D5q D5q D5h D5q D5q |"
+ "D5h D5q D5q D5h D5q D5q D5h D5q D5q D5h D5q D5q | D5h D5q D5q D5h D5q D5q D5h D5q D5q D5h D5q D5q | D5h D5q D5q D5h D5q D5q D5h D5q D5q F5q D5q G5q F5q |"
+ "D5h D5q D5q D5h D5q D5q D5h D5q D5q D5h D5q D5q | D5h D5q D5q D5h D5q D5q D5h D5q D5q D5h F4q F4q | G4h G4q G4q G4h G4q G4q G4q G4h G4q G4h G4q G4q |"
+ "G4h G4q G4q G4h G4q G4q G4q G4h G4q G4h G4q G4q | G4h G4q G4q G4h G4q G4q G4q G4h G4q G4h G4q G4q | G4h G4q G4q G4h G4q G4q G4q G4h G4q G4h G4q G4q |"
+ "D5h D5q D5q D5h D5q D5q D5h D5q D5q D5h D5q D5q | D5h D5q D5q D5h D5q D5q D5h D5q D5q D5h D5q D5q | A5q F5h F5q F5h F5q F5q A5q A5q A5q A5q A5h A5q A5q"); // Hechas
// las
// 9
// primeras
// paginas
// Bateria
YMLRhythm ritmo = new YMLRhythm();
// ritmo.addRhythmTrack("ACOUSTIC-SNARE",
// ".... | .... | *.*. | *.*. | *.*. | *.*. | *.*. | *.*. | **** | **** | **** | **** | **** | **** | **** | **** | **** | **** | **** | **** | **** | **** | **** | **** | **** | *.*. | .... | **** | **** | **** | **** | ",
// 'w');
ritmo
.addRhythmTrack(
"BASS-DRUM",
".... | .... | *.*. | *.*. | *.*. | *.*. | *.*. | *.*. | **** | **** | **** | **** | **** | **** | **** | **** | **** | **** | **** | **** | **** | **** | **** | **** | **** | *.*. | .... | **** | **** | **** | **** | ",
'w');
ritmo
.addRhythmTrack(
"BASS-DRUM",
".... | .... | *.*. | *.*. | *.*. | *.*. | *.*. | *.*. | **** | **** | **** | **** | **** | **** | **** | **** | **** | **** | **** | **** | **** | **** | **** | **** | **** | *.*. | .... | **** | **** | **** | **** | ",
'w');
ritmo
.addRhythmTrack(
"HIHAT-OPEN",
".... | .... | *.*. | *.*. | *.*. | *.*. | *.*. | *.*. | *.*. | *.*. | *.*. | *.*. | *... | *... | *... | *... | *... | *.*. | *.*. | *.*. | *.*. | *.*. | *.*. | *.*. | *.*. | *.*. | *.*. | *.*. | *.*. | *.*. | *.*. | ",
'w');
ritmo
.addRhythmTrack(
"BASS-DRUM",
".... | .... | .... | .... | .... | .... | .... | .... | .... | .... | .... | .... | **** | **** | **** | **** | **** | **** | **** | **** | **** | **** | **** | **** | **** | .... | .... | **** | **** | **** | **** | ",
'w');
song.addRhythm(ritmo, 1);
// song.addTrack("PIANO",
// "G5h G5h G5h G5h G5h G5h G5h G5h G5h G5h G5h G5h G5h G5h G5h G5h G5h G5h G5h G5h G5h G5h G5h G5h");
// song.addRhythm(ritmo, 5);
return song;
}
}