Package jpianotrain.staff

Source Code of jpianotrain.staff.RandomTuneFactory

/* License see bottom */
package jpianotrain.staff;

import static jpianotrain.util.ConfigurationKeys.RANDOM_TUNE_FACTORY_BOTH_HANDS;
import static jpianotrain.util.ConfigurationKeys.RANDOM_TUNE_FACTORY_LOWER_BASS;
import static jpianotrain.util.ConfigurationKeys.RANDOM_TUNE_FACTORY_LOWER_TREBLE;
import static jpianotrain.util.ConfigurationKeys.RANDOM_TUNE_FACTORY_NOTE_COUNT;
import static jpianotrain.util.ConfigurationKeys.RANDOM_TUNE_FACTORY_SCALE;
import static jpianotrain.util.ConfigurationKeys.RANDOM_TUNE_FACTORY_UPPER_BASS;
import static jpianotrain.util.ConfigurationKeys.RANDOM_TUNE_FACTORY_UPPER_TREBLE;

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

import jpianotrain.util.UserConfiguration;

import org.apache.log4j.Logger;

/**
* Random tune factory. Based on a scale
* and some basic structural information
* this factory creates a random tune.
*
* @since 0
* @author Alexander Methke
*/
public class RandomTuneFactory extends TuneFactory {
  private static final Logger log=Logger.getLogger(RandomTuneFactory.class);
 
  protected RandomTuneFactory() {
    load();
  }

  /**
   * Loads all parameters (if they exist) from
   * user configuration.
   *
   * @see UserConfiguration
   */
  protected void load() {
    UserConfiguration uc=UserConfiguration.getInstance();
    int i=uc.getIntProperty(RANDOM_TUNE_FACTORY_NOTE_COUNT, -1);
    if (i==-1) {
      return;
    }
    try {
      setBothHands(uc.getBooleanProperty(RANDOM_TUNE_FACTORY_BOTH_HANDS, false));
      setNoteCount(uc.getIntProperty(RANDOM_TUNE_FACTORY_NOTE_COUNT, 0));
      setUpperBoundBass(uc.getIntProperty(RANDOM_TUNE_FACTORY_UPPER_BASS, 0));
      setLowerBoundBass(uc.getIntProperty(RANDOM_TUNE_FACTORY_LOWER_BASS, 0));
      setUpperBoundTreble(uc.getIntProperty(RANDOM_TUNE_FACTORY_UPPER_TREBLE, 0));
      setLowerBoundTreble(uc.getIntProperty(RANDOM_TUNE_FACTORY_LOWER_TREBLE, 0));
      setScale(Scale.parse(uc.getProperty(RANDOM_TUNE_FACTORY_SCALE, null)));
    catch (Exception ex) {
      log.error("failed to load user configuration for random tune factory", ex);
      setBothHands(false);
      setNoteCount(0);
      setUpperBoundBass(0);
      setLowerBoundBass(0);
      setUpperBoundTreble(0);
      setLowerBoundTreble(0);
      setScale(null);
    }
  }
 
  /**
   * Stores all parameters in user configuration.
   *
   * @see UserConfiguration
   */
  protected void store() {
    UserConfiguration uc=UserConfiguration.getInstance();
    Scale s=getScale();
    if (s!=null) {
      uc.putProperty(RANDOM_TUNE_FACTORY_BOTH_HANDS,isBothHands());
      uc.putProperty(RANDOM_TUNE_FACTORY_UPPER_BASS,getUpperBoundBass());
      uc.putProperty(RANDOM_TUNE_FACTORY_LOWER_BASS,getLowerBoundBass());
      uc.putProperty(RANDOM_TUNE_FACTORY_UPPER_TREBLE,getUpperBoundTreble());
      uc.putProperty(RANDOM_TUNE_FACTORY_LOWER_TREBLE,getLowerBoundTreble());
      uc.putProperty(RANDOM_TUNE_FACTORY_SCALE,s.toString());
      uc.putProperty(RANDOM_TUNE_FACTORY_NOTE_COUNT, getNoteCount());
    }
  }
  @Override
  protected List<Note> createLeftHand() {
    List<Note> allowedNotes=getAllowedNotesBass();
    int maxIdx=allowedNotes.size()-1;
    List<Note> randomNotes=new ArrayList<Note>();
    for(int i=0;i<getNoteCount();i++) {
      int idx=(int)Math.round(Math.random()*maxIdx);
      randomNotes.add(allowedNotes.get(idx));
    }
    return randomNotes;
  }

  @Override
  protected List<Note> createRightHand() {
    List<Note> allowedNotes=getAllowedNotesTreble();
    int maxIdx=allowedNotes.size()-1;
    List<Note> randomNotes=new ArrayList<Note>();
    for(int i=0;i<getNoteCount();i++) {
      int idx=(int)Math.round(Math.random()*maxIdx);
      randomNotes.add(allowedNotes.get(idx));
    }
    return randomNotes;
  }
}
/*
    Copyright (C) 2008  Alexander Methke

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program (gplv3.txt).
*/
TOP

Related Classes of jpianotrain.staff.RandomTuneFactory

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.