Package yakimusic.sound

Source Code of yakimusic.sound.SoundInterface

package yakimusic.sound;

import java.io.IOException;
import java.util.HashMap;

import org.jfugue.Pattern;
import org.jfugue.Player;
import org.jfugue.Instrument;
import org.jfugue.Rhythm;

public class SoundInterface {

  static Player player = new Player();
  private static HashMap<String, String> instrumentsMap;
  private static final char[] caracteres = new char[] { 'a', 'b', 'c', 'd',
      'e', 'f', 'g', 'h', 'i', 'j', 'k' }; // El numero de canales de los
                          // ritmos se limita al
                          // numero de letras de este
                          // vector. Poner mas

  private static String getInstrumentRealName(String instrument) {
    String realName = null;
    if (instrumentsMap == null) {
      instrumentsMap = new HashMap<String, String>();
      instrumentsMap.put("ELECTRIC-PIANO", "ELECTRIC_PIANO");
      instrumentsMap.put("ROCK-ORGAN", "ROCK_ORGAN");
      instrumentsMap.put("CHURCH-ORGAN", "CHURCH_ORGAN");
      instrumentsMap.put("ACOUSTIC-SNARE", "ACOUSTIC_SNARE");
      instrumentsMap.put("BASS-DRUM", "BASS_DRUM");
      // ...
    }
    realName = instrumentsMap.get(instrument);
    if (realName == null)
      realName = instrument;
    return realName;
  }

  public static void playSounds(String instrument, final String sounds,
      final int tempo) {
    final String instrumentID = getInstrumentRealName(instrument);
    Thread hilo = new Thread() {
      @Override
      public void run() {
        Player player = new Player();
        player.play("T" + tempo + " I[" + instrumentID + "] " + sounds);
        System.out.println("Fin de reproducción de " + instrumentID);
      }
    };
    hilo.start();
  }

  public static void test(){
    Pattern doubleMeasureRest = new Pattern("Rw Rw");

    // "Frere Jacques"
    Pattern pattern1 = new Pattern("C5q D5q E5q C5q");

    // "Dormez-vous?"
    Pattern pattern2 = new Pattern("E5q F5q G5h");

    // "Sonnez les matines"
    Pattern pattern3 = new Pattern("G5i A5i G5i F5i E5q C5q");

    // "Ding ding dong"
    Pattern pattern4 = new Pattern("C5q G4q C5h");

    // Put all of the patters together to form the song
    Pattern song = new Pattern();
    song.add(pattern1, 2); // Adds 'pattern1' to 'song' twice
    song.add(pattern2, 2); // Adds 'pattern2' to 'song' twice
    song.add(pattern3, 2); // Adds 'pattern3' to 'song' twice
    song.add(pattern4, 2); // Adds 'pattern4' to 'song' twice

    // Create the first voice
    Pattern round1 = new Pattern("V0");
    round1.add(song);

    // Create the second voice
    Pattern round2 = new Pattern("V1");
    round2.add(doubleMeasureRest);
    round2.add(song);

    // Create the third voice
    Pattern round3 = new Pattern("V2");
    round3.add(doubleMeasureRest, 2);
    round3.add(song);

    // Put the voices together
    Pattern roundSong = new Pattern();
    roundSong.add(round1);
    roundSong.add(round2);
    roundSong.add(round3);

    // Play the song!
    player.play(roundSong);
  }
 
  public static void playTracks(final String[] instruments,
      final String[] sounds, final int[] tempos) {
    Pattern finalSong = new Pattern();
   
    for (int i = 0; i < instruments.length && i < sounds.length && i < tempos.length; i++) {
      final String instrumentID = getInstrumentRealName(instruments[i]);
      final int tempo = tempos[i];
      Pattern song = new Pattern("T" + tempo + " I[" + instrumentID + "] " + sounds[i]);
      Pattern round = new Pattern("V" + i);
      round.add(song);
      //Pattern song = new Pattern("G5i A5i G5i F5i E5q C5q");
      finalSong.add(round);
    }
   
    //finalSong.add(rhythm);   
    player.play(finalSong);
  }

  public static void playRhythm(String[] instruments, final String[] rhythms) {
    Rhythm rhythm = new Rhythm();
    int layer = 1;
    for (int i = 0; i < instruments.length && i < rhythms.length; i++) {
      String instrumID = getInstrumentRealName(instruments[i]);
      char caracter = caracteres[i];
      String aux = rhythms[i].replace("*", caracter + "");

      rhythm.setLayer(layer, aux);
      rhythm.addSubstitution('.', "Ri");
      rhythm.addSubstitution(caracter, "[" + instrumID + "]i");
      layer++;
    }
    final Rhythm final_rhythm = rhythm;
    Thread bateria = new Thread() {
      @Override
      public void run() {
        Pattern pattern = final_rhythm.getPattern();
        pattern.repeat(3);
        Player player = new Player();
        player.play(pattern);
        System.out.println("Fin de reproducción de ritmo");
      }
    };
    bateria.start();
  }

  private static Pattern getRhythmPattern(String[] instruments, String[] rhythms){
    Rhythm rhythm = new Rhythm();
    int layer = 1;
    for (int i = 0; i < instruments.length && i < rhythms.length; i++) {
      String instrumID = getInstrumentRealName(instruments[i]);
      char caracter = caracteres[i];
      String aux = rhythms[i].replace("*", caracter + "");

      rhythm.setLayer(layer, aux);
      rhythm.addSubstitution('.', "Ri");
      rhythm.addSubstitution(caracter, "[" + instrumID + "]i");
      layer++;
    }
    Pattern pattern = rhythm.getPattern();
    return pattern;   
  }
 
  // Metodo de pruebas
  public static void main(String[] args) throws IOException {   
    //playTracks(new String[] { "FLUTE", "FLUTE" }, new String[] {"E5s D#5s E5s D#5s E5s B4s D5s C5s A4s", "Rs Rs Rs D#6s E6s B5s D5s C6s A5s" }, new int[] { 60, 60 });
    //playTracks(new String[] { "PIANO", "FLUTE"}, new String[] {"E5s A5s C6s B5s E5s B5s D6s C6i E6i G#5i E6i | A5s E5s A5s C6s B5s E5s B5s D6s C6i A5i Ri", "Rs Rs Rs Rs Rs Rs D5s C5i E5i G#4i E5i | A4s E4s A4s C5s B4s E4s B4s D5s C5i A4i Ri"}, new int[] { 60, 120 }, getRhythmPattern(new String[] { "BASS-DRUM", "ACOUSTIC-SNARE" }, new String[] { "**.*..*....", "..*....*..." }));
    //test();
    //playRhythm(new String[] { "BASS-DRUM", "ACOUSTIC-SNARE" }, new String[] { "**.*..*....", "..*....*..." });
    //YMLRhythm.genHouseRhythm('t').testPlay(4);
    //YMLSong.genExampleSong().testPlay();
    YMLSong.genPinkFloyd().testPlay();
    //YMLSong.genTitanic().testPlay();
   
   
  }

}
TOP

Related Classes of yakimusic.sound.SoundInterface

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.