all files / src/ PitchMappers.js

95.83% Statements 23/24
100% Branches 6/6
75% Functions 3/4
95.83% Lines 23/24
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94                                20× 20× 20× 20× 20× 20×   20× 23×     21×     20× 20× 20× 20×     20×   20×                                                               20×                   29× 29× 28×       29×        
/**
 * Base Pitch Mapper
 * @private
 * @abstract
 */
class PitchMapper {
	/**
	 * Create a base Pitch Mapper
	 * @param {Object[]} seriesInfo - min and max data and frequencies
	 * @param {string} seriesInfo[].minimumDatum - minimun data value
	 * @param {string} seriesInfo[].maximumDatum - maximun data value
	 * @param {string} seriesInfo[].minimumFrequency - minimun frequency
	 * @param {string} seriesInfo[].maximumFrequency - maximun frequency
	 */
	constructor(seriesInfo) {
		// Store the info for all series
		this._minimumDatum = []
		this._maximumDatum = []
		this._minimumFrequency = []
		this._maximumFrequency = []
		this._dataRange = []
		this._frequencyRange = []
 
		seriesInfo.forEach((series, index) => {
			if (series.maximumDatum < series.minimumDatum) {
				throw Error('minimum datum should be <= maximum datum')
			}
 
			if (series.maximumFrequency < series.minimumFrequency) {
				throw Error('minimum frequency should be <= maximum frequency')
			}
 
			this._minimumDatum[index] = series.minimumDatum
			this._maximumDatum[index] = series.maximumDatum
			this._minimumFrequency[index] = series.minimumFrequency
			this._maximumFrequency[index] = series.maximumFrequency
 
			// Pre-calculate for performance (I expect) when mapping
			this._dataRange[index] =
				this._maximumDatum[index] - this._minimumDatum[index]
			this._frequencyRange[index] =
				this._maximumFrequency[index] - this._minimumFrequency[index]
		})
	}
 
	/**
	 * Map a datum to an output value
	 * @param {number} datum - the datum to be mapped
	 * @abstract
	 */
	map(datum) {
		throw Error('Base map() must be overriden.')
	}
}
 
 
/**
 * Maps each datum to a corresponding frequency.
 * @private
 * @extends {PitchMapper}
 */
class FrequencyPitchMapper extends PitchMapper {
	// TODO: dupe docs for now: https://github.com/jsdoc3/jsdoc/issues/1012
	/**
	 * Create a Frequency Pitch Mapper
	 * @param {Object[]} seriesInfo - minimum and maximums for each series
	 * @param {string} seriesInfo[].minimumDatum - minimun data value
	 * @param {string} seriesInfo[].maximumDatum - maximun data value
	 * @param {string} seriesInfo[].minimumFrequency - minimun frequency
	 * @param {string} seriesInfo[].maximumFrequency - maximun frequency
	 */
	constructor(seriesInfo) {
		super(seriesInfo)
	}
 
	/**
	 * Given a datum value, compute the corresponding frequency
	 * @param {number} series - map on which series?
	 * @param {number} datum - the datum to be mapped
	 * @returns {number} frequency for this datum
	 */
	map(series, datum) {
		let ratio
		if (this._dataRange[series]) {
			ratio = (datum - this._minimumDatum[series])
				/ this._dataRange[series]
		} else {
			ratio = 0.5
		}
		return this._minimumFrequency[series]
			+ (ratio * this._frequencyRange[series])
	}
}