package com.yonge.netty.dto;

import java.util.HashMap;
import java.util.Map;

/**
 * 不同乐器对应的音域范围
 */
public class MusicalInstrumentsPitchRange {

	private final static double STANDARD_PITCH = 440;

	private double minPitch;

	private double maxPitch;

	private MusicalInstrumentsPitchRange(double minPitch, double maxPitch) {
		this.maxPitch = maxPitch;
		this.minPitch = minPitch;
	}

	private static Map<Integer, MusicalInstrumentsPitchRange> map = new HashMap<Integer, MusicalInstrumentsPitchRange>() {

		/**
		 * 
		 */
		private static final long serialVersionUID = 7551297824160987438L;

		{
			// 以下是以440hz 作为标准音
			put(3, new MusicalInstrumentsPitchRange(261.632, 2093.056));// 长笛 c4-c7
			put(4, new MusicalInstrumentsPitchRange(138.595, 1661.258));// 中音萨克斯 #c3-#g6
			put(7, new MusicalInstrumentsPitchRange(164.818, 2637.084));// 单簧管 e3-e7
			put(5, new MusicalInstrumentsPitchRange(523.264, 2349.376));// 竖笛 c5-d7
			put(12, new MusicalInstrumentsPitchRange(196.002, 1174.688));// 小号 g3-d6
			put(13, new MusicalInstrumentsPitchRange(87.309, 698.473));// 圆号 f2-f5
			put(14, new MusicalInstrumentsPitchRange(49.001, 587.344));// 长号 g1-d5
			put(15, new MusicalInstrumentsPitchRange(82.409, 349.237));// 上低音号 e2-f4
			put(17, new MusicalInstrumentsPitchRange(41.204, 174.618));// 大号 e1-f3
		}
	};

	public static MusicalInstrumentsPitchRange get(Integer musicalInstrument, double standPitch) {
		MusicalInstrumentsPitchRange pitchRange = map.get(musicalInstrument);
		if (pitchRange != null) {
			pitchRange = new MusicalInstrumentsPitchRange(pitchRange.getMinPitch() / STANDARD_PITCH * standPitch * 2, pitchRange.getMaxPitch() / STANDARD_PITCH
					* standPitch / 2);
		}
		return pitchRange == null ? new MusicalInstrumentsPitchRange(20, 20000) : pitchRange;
	}

	public double getMinPitch() {
		return minPitch / 2;
	}

	public double getMaxPitch() {
		return maxPitch * 2;
	}

}
