All files / addon/mixins musical-identity.js

100% Statements 25/25
91.66% Branches 11/12
100% Functions 5/5
100% Lines 25/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138                                                                                                                  13x 13x   13x 6x   7x                               4x   4x 4x         4x 122x 4x 4x                                   2x 2x 2x     2x 1x   1x     2x       6x 6x     6x 3x     6x   6x        
import { computed } from '@ember/object';
import Mixin from '@ember/object/mixin';
import { frequencyMap } from 'ember-audio/utils';
 
/**
 * This mixin allows an object to have an awareness of it's "musical identity"
 * or "note value" based on western musical standards (a standard piano).
 * If any of the following are provided, all of the remaining properties will be
 * calculated:
 *
 * 1. frequency
 * 2. identifier (i.e. "Ab1")
 * 3. letter, octave, and (optionally) accidental
 *
 * This mixin only makes sense when the consuming object is part of a collection,
 * as the only functionality it provides serves to facilitate identification.
 *
 * @public
 * @class MusicalIdentity
 */
export default Mixin.create({
  /**
   * For note `Ab5`, this would be `A`.
   *
   * @public
   * @property letter
   * @type {string}
   */
  letter: null,
 
  /**
   * For note `Ab5`, this would be `b`.
   *
   * @public
   * @property accidental
   * @type {string}
   */
  accidental: null,
 
  /**
   * For note `Ab5`, this would be `5`.
   *
   * @public
   * @property octave
   * @type {string}
   */
  octave: null,
 
  /**
   * Computed property. Value is `${letter}` or `${letter}${accidental}` if
   * accidental exists.
   *
   * @public
   * @property name
   * @type {string}
   */
  name: computed('letter', 'accidental', function () {
    const accidental = this.accidental;
    const letter = this.letter;
 
    if (accidental) {
      return `${letter}${accidental}`;
    } else {
      return letter;
    }
  }),
 
  /**
   * Computed property. The frequency of the note in hertz. Calculated by
   * comparing western musical standards (a standard piano) and the note
   * identifier (i.e. `Ab1`). If this property is set directly, all other
   * properties are updated to reflect the provided frequency.
   *
   * @public
   * @property frequency
   * @type {number}
   */
  frequency: computed('identifier', {
    get() {
      const identifier = this.identifier;
 
      Eif (identifier) {
        return frequencyMap[identifier];
      }
    },
 
    set(key, value) {
      for (let key in frequencyMap) {
        if (value === frequencyMap[key]) {
          this.set('identifier', key);
          return value;
        }
      }
    },
  }),
 
  /**
   * Computed property. Value is `${letter}${octave}` or
   * `${letter}${accidental}${octave}` if accidental exists. If this property
   * is set directly, all other properties are updated to reflect the provided
   * identifier.
   *
   * @public
   * @property identifier
   * @type {string}
   */
  identifier: computed('letter', 'octave', 'accidental', {
    get() {
      const accidental = this.accidental;
      const letter = this.letter;
      const octave = this.octave;
      let output;
 
      if (accidental) {
        output = `${letter}${accidental}${octave}`;
      } else {
        output = `${letter}${octave}`;
      }
 
      return output;
    },
 
    set(key, value) {
      const [letter] = value;
      const octave = value[2] || value[1];
      let accidental;
 
      if (value[2]) {
        accidental = value[1];
      }
 
      this.setProperties({ letter, octave, accidental });
 
      return value;
    },
  }),
});