All files / src/lib/events eventSoundPlayEffect.js

85.71% Statements 24/28
84.21% Branches 16/19
100% Functions 1/1
85.71% Lines 24/28

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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 1541x   1x 1x   1x                                                                                                                                                                                                             1x             6x 6x 6x 6x 6x   6x 2x 2x 4x 3x 3x 3x     3x     3x 1x 1x           6x 1x       1x              
const l10n = require("../helpers/l10n").default;
 
const id = "EVENT_SOUND_PLAY_EFFECT";
const groups = ["EVENT_GROUP_MUSIC"];
 
const fields = [
  {
    key: "type",
    type: "soundEffect",
    label: l10n("FIELD_SOUND_EFFECT"),
    description: l10n("FIELD_SOUND_EFFECT_PLAY_DESC"),
    defaultValue: "beep",
    flexBasis: "60%",
  },
  {
    key: "priority",
    label: l10n("FIELD_PRIORITY"),
    description: l10n("FIELD_PRIORITY_SOUND_DESC"),
    type: "priority",
    options: [
      ["low", l10n("FIELD_LOW")],
      ["medium", l10n("FIELD_MEDIUM")],
      ["high", l10n("FIELD_HIGH")],
    ],
    defaultValue: "medium",
    flexBasis: "15%",
  },
  {
    key: "pitch",
    type: "number",
    label: l10n("FIELD_PITCH"),
    description: l10n("FIELD_PITCH_DESC"),
    conditions: [
      {
        key: "type",
        eq: "beep",
      },
    ],
    min: 1,
    max: 8,
    step: 1,
    defaultValue: 4,
  },
  {
    key: "frequency",
    type: "number",
    label: l10n("FIELD_FREQUENCY"),
    description: l10n("FIELD_FREQUENCY_DESC"),
    conditions: [
      {
        key: "type",
        eq: "tone",
      },
    ],
    min: 0,
    max: 20000,
    step: 1,
    defaultValue: 200,
  },
  {
    key: "duration",
    type: "number",
    label: l10n("FIELD_DURATION"),
    description: l10n("FIELD_DURATION_SOUND_DESC"),
    unitsField: "units",
    unitsDefault: "time",
    conditions: [
      {
        key: "type",
        in: ["beep", "crash", "tone"],
      },
    ],
    min: 0,
    max: 4.25,
    step: 0.01,
    defaultValue: 0.5,
  },
  {
    key: "wait",
    type: "checkbox",
    label: l10n("FIELD_WAIT_UNTIL_FINISHED"),
    description: l10n("FIELD_WAIT_UNTIL_FINISHED_SOUND_DESC"),
    conditions: [
      {
        key: "type",
        in: ["beep", "crash", "tone"],
      },
    ],
    defaultValue: true,
    flexBasis: "100%",
  },
  {
    key: "effect",
    type: "number",
    label: l10n("FIELD_EFFECT_INDEX"),
    description: l10n("FIELD_EFFECT_INDEX_DESC"),
    min: 0,
    max: 60,
    defaultValue: 0,
    conditions: [
      {
        key: "type",
        soundType: "fxhammer",
      },
    ],
  },
];
 
const compile = (input, helpers) => {
  const {
    soundPlayBeep, //
    soundStartTone,
    soundPlayCrash,
    soundPlay,
    wait,
  } = helpers;
  let priority = input.priority || "medium";
  let seconds = typeof input.duration === "number" ? input.duration : 0.5;
  let frames = seconds * 60;
  let shouldWait = input.wait;
 
  if (input.type === "beep" || !input.type) {
    const pitch = typeof input.pitch === "number" ? input.pitch : 4;
    soundPlayBeep(9 - pitch, frames, priority);
  } else if (input.type === "tone") {
    const freq = typeof input.frequency === "number" ? input.frequency : 200;
    let period = (2048 - 131072 / freq + 0.5) | 0;
    Iif (period >= 2048) {
      period = 2047;
    }
    Iif (period < 0) {
      period = 0;
    }
    soundStartTone(period, frames, priority);
  } else if (input.type === "crash") {
    soundPlayCrash(frames, priority);
  } else E{
    soundPlay(input.type, priority, input.effect);
    shouldWait = false;
  }
 
  if (shouldWait) {
    wait(Math.round(seconds * 60));
  }
};
 
module.exports = {
  id,
  description: l10n("EVENT_SOUND_PLAY_EFFECT_DESC"),
  groups,
  fields,
  compile,
};