LgNat.java :  » Music » orDrumbox-0.9.0 » com » ordrumbox » core » lgNat » Java Open Source

Java Open Source » Music » orDrumbox 0.9.0 
orDrumbox 0.9.0 » com » ordrumbox » core » lgNat » LgNat.java
// -----------------------------------------------------------------------------
// TODO refactore
// ------------------------------------------------------------------------------
package com.ordrumbox.core.lgNat;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.ordrumbox.core.description.DrumkitStyle;
import com.ordrumbox.core.description.GeneratedSound;
import com.ordrumbox.core.description.OrPattern;
import com.ordrumbox.core.description.OrTrack;
import com.ordrumbox.core.description.Song;
import com.ordrumbox.core.drumkit.Drumkit;
import com.ordrumbox.core.drumkit.Instrument;

public class LgNat {

  private static LgNat     instance;
  Random                     rnd     = new Random();

  private List<DrumkitStyle>  styles  = new ArrayList<DrumkitStyle>();

  private LgNat() {
    styles.add(new DrumkitStyle("default"));
    styles.add(new DrumkitStyle("generated"));
    styles.add(new DrumkitStyle("electro"));
    styles.add(new DrumkitStyle("accoustic"));
    styles.add(new DrumkitStyle("humain"));
    styles.add(new DrumkitStyle("vintage"));
    styles.add(new DrumkitStyle("test"));
  }

  public static LgNat getInstance() {
    if (instance == null)
      instance = new LgNat();
    return instance;
  }

  /**
   * 
   * @param drumkit
   * @param instrumentName
   * @return never null
   */
  public Instrument getNearestInstrument(Drumkit drumkit, String instrumentName) {
    InstrumentType type = InstrumentTypeManager.getInstance().getTypeFromName(instrumentName);
    Instrument instrument = null;
    instrument = chooseInstrument(drumkit, type);
    if (instrument == null) {
      InstrumentType subst;
      if (type.getInstrumentIdSubsistution1().length() > 2) {
        subst = InstrumentTypeManager.getInstance().getTypeFromName(type.getInstrumentIdSubsistution1());
        instrument = chooseInstrument(drumkit, subst);
      }
      if (instrument == null && type.getInstrumentIdSubsistution2().length() > 2) {
        subst = InstrumentTypeManager.getInstance().getTypeFromName(type.getInstrumentIdSubsistution2());
        instrument = chooseInstrument(drumkit, subst);
      }
      if (instrument == null && type.getInstrumentIdSubsistution3().length() > 2) {
        subst = InstrumentTypeManager.getInstance().getTypeFromName(type.getInstrumentIdSubsistution3());
        instrument = chooseInstrument(drumkit, subst);
      }
    }
    if (instrument == null) {
      // System.out.println("Nearest instrument  random for  =" + instrumentName + "(" + type.getDisplayName() + "="
      // + type.getInstrumentIdSubsistution1() + "-"
      // + type.getInstrumentIdSubsistution2()+ "-"
      // + type.getInstrumentIdSubsistution3()+ ")"
      // + " in drumkit" + drumkit.getDisplayName());
      instrument = getRandomInstrument(drumkit);
    }
    return instrument;
  }  /**
   * 
   * @param drumkit
   * @param instrumentName
   * @return never null
   */
  public Instrument getNearestInstrumentForStyle(Drumkit drumkit, String instrumentName, DrumkitStyle drumkitStyle) {
    InstrumentType type = InstrumentTypeManager.getInstance().getTypeFromName(instrumentName);
    Instrument instrument = null;
    instrument = chooseInstrument(drumkit, type, drumkitStyle);
    if (instrument == null) {
      InstrumentType subst;
      if (type.getInstrumentIdSubsistution1().length() > 2) {
        subst = InstrumentTypeManager.getInstance().getTypeFromName(type.getInstrumentIdSubsistution1());
        instrument = chooseInstrument(drumkit, subst, drumkitStyle);
      }
      if (instrument == null && type.getInstrumentIdSubsistution2().length() > 2) {
        subst = InstrumentTypeManager.getInstance().getTypeFromName(type.getInstrumentIdSubsistution2());
        instrument = chooseInstrument(drumkit, subst, drumkitStyle);
      }
      if (instrument == null && type.getInstrumentIdSubsistution3().length() > 2) {
        subst = InstrumentTypeManager.getInstance().getTypeFromName(type.getInstrumentIdSubsistution3());
        instrument = chooseInstrument(drumkit, subst, drumkitStyle);
      }
    }
    return instrument;
  }



  public void automaticTrackAssignationGS(Song song, boolean volume, boolean pano) {
    if (song.getGeneratedSounds().size() == 0) {
      // OrLog.print(Level.SEVERE, "automaticTrackAssignation= ERROR no generated intruments");
      return;
    }

    for (OrPattern orPattern : song.getPatterns()) {
      // System.out.println("automaticTrackAssignation pattern:"+pattern);
      for (OrTrack orTrack : orPattern.getTracks()) {
        // System.out.println("automaticTrackAssignation: "+j+"
        // track:"+track);

        GeneratedSound generatedSound = song.getGeneratedSounds().get(0);
        InstrumentType instrumentType = InstrumentTypeManager.getInstance().getTypeFromName(orTrack.getDisplayName());
        generatedSound = computegeneratedSound(song, generatedSound, instrumentType);
        if (generatedSound == null) {
          instrumentType = InstrumentTypeManager.getInstance().getTypeFromName(instrumentType.getInstrumentIdSubsistution1());
        }
        generatedSound = computegeneratedSound(song, generatedSound, instrumentType);
        if (generatedSound == null) {
          instrumentType = InstrumentTypeManager.getInstance().getTypeFromName(instrumentType.getInstrumentIdSubsistution1());
        }
        generatedSound = computegeneratedSound(song, generatedSound, instrumentType);
        if (generatedSound == null) {
          instrumentType = InstrumentTypeManager.getInstance().getTypeFromName(instrumentType.getInstrumentIdSubsistution1());
        }
        generatedSound = computegeneratedSound(song, generatedSound, instrumentType);
        if (generatedSound == null) {
          instrumentType = InstrumentTypeManager.getInstance().getTypeFromName(instrumentType.getInstrumentIdSubsistution1());
        }
        if (generatedSound == null) {
          generatedSound = song.getGeneratedSounds().get(0);
        }

        orTrack.setGeneratedSound(generatedSound);
        if (pano) {
          // orTrack.setPano(LgNat.getPanoFromType(instrument.getInstrumentType().getType()));
        }
        if (volume) {
          // orTrack.setVolume(LgNat.getVeloFromType(instrument.getInstrumentType().getType()));
        }
        // System.out.println("LgNat::automaticTrackAssignation
        // pattern:"+track.getPattern().getDisplayName()+" track:" +
        // track.getDisplayName() + "="
        // + track.getInstrument().getDisplayName() + "="+
        // track.getInstrument().getOrSample().getFileName());
      }
    }
  }

  private GeneratedSound computegeneratedSound(Song song, GeneratedSound generatedSound, InstrumentType instrumentType) {
    for (GeneratedSound generatedSoundt : song.getGeneratedSounds()) {
      // System.out.println("in bank:"+ generatedSoundt.getInstrumentType().getInstrumentId()+ " <= "+
      // instrumentType2.getInstrumentId());
      if (generatedSoundt.getInstrumentType().getInstrumentId().equals(instrumentType.getInstrumentId())) {
        // System.out.println("matched");
        return generatedSoundt;
      }
    }
    return null;
  }

  
  private Instrument chooseInstrument(Drumkit drumkit, InstrumentType type, DrumkitStyle drumkitStyle) {
    List<Instrument> liste = drumkit.getInstrumentsFromTypeAndStyle(type, drumkitStyle);
    if (liste.size() <= 0)
      return null;
    int i = Math.abs(rnd.nextInt()) % (liste.size());
    return liste.get(i);
   }
  
  /**
   */
  private Instrument chooseInstrument(Drumkit drumkit, InstrumentType type) {
    List<Instrument> liste = drumkit.getInstrumentsFromType(type);
    if (liste.size() <= 0)
      return null;
    int i = Math.abs(rnd.nextInt()) % (liste.size());
    return liste.get(i);
  }

  private Instrument getRandomInstrument(Drumkit drumkit) {
    if (drumkit.getNbInstruments() <= 0)
      return null;
    int i = Math.abs(rnd.nextInt()) % (drumkit.getNbInstruments());
    Instrument instrument = (Instrument) drumkit.getInstrument(i);
    return instrument;
  }

  public List<DrumkitStyle> getStyles() {
    return styles;
  }
  
  public int getNumStyleFromStyle(DrumkitStyle currentStyle) {
      for (int num = 0; num < LgNat.getInstance().getStyles().size();num++) {
        if (styles.get(num) == currentStyle) {
          return num;
        }
      }
      return 0;
   }

  public DrumkitStyle getDrumkitStyleFromName(String style) {
      for (int num = 0; num < LgNat.getInstance().getStyles().size();num++) {
        if (styles.get(num).getDisplayName().equals(style)) {
          return styles.get(num);
        }
      }
      return null;
    }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.