All files / src/lib/events eventTextSetSound.js

85.71% Statements 24/28
70% Branches 14/20
50% Functions 1/2
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 1391x   1x 1x 1x         1x           1x                                                                                                                                                                 1x             6x 6x 6x   6x 2x 4x 1x 1x 3x 2x 2x 2x     2x     2x 1x 1x           1x                  
const l10n = require("../helpers/l10n").default;
 
const id = "EVENT_TEXT_SET_SOUND_EFFECT";
const groups = ["EVENT_GROUP_DIALOGUE", "EVENT_GROUP_MUSIC"];
const subGroups = {
  EVENT_GROUP_DIALOGUE: "EVENT_GROUP_MUSIC",
  EVENT_GROUP_MUSIC: "EVENT_GROUP_DIALOGUE",
};
 
const autoLabel = (fetchArg, input) => {
  return input.type !== "none"
    ? l10n("EVENT_TEXT_SET_SOUND_EFFECT")
    : l10n("EVENT_TEXT_REMOVE_SOUND_EFFECT");
};
 
const fields = [
  {
    key: "type",
    type: "soundEffect",
    label: l10n("FIELD_SOUND_EFFECT"),
    description: l10n("FIELD_SOUND_EFFECT_PLAY_DESC"),
    defaultValue: "tone",
    flexBasis: "60%",
    allowNone: true,
  },
  {
    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,
    width: "50%",
  },
  {
    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: 300,
    width: "50%",
  },
  {
    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.05,
    width: "50%",
  },
  {
    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",
      },
    ],
    width: "50%",
  },
];
 
const compile = (input, helpers) => {
  const {
    textSetSoundBeep, //
    textSetSoundTone,
    textSetSoundCrash,
    textSetSound,
    textRemoveSound,
  } = helpers;
  let seconds = typeof input.duration === "number" ? input.duration : 0.5;
  let frames = Math.floor(seconds * 60);
 
  if (input.type === "none" || !input.type) {
    textRemoveSound();
  } else if (input.type === "beep") {
    const pitch = typeof input.pitch === "number" ? input.pitch : 4;
    textSetSoundBeep(9 - pitch, frames);
  } 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;
    }
    textSetSoundTone(period, frames);
  } else if (input.type === "crash") {
    textSetSoundCrash(frames);
  } else E{
    textSetSound(input.type, input.effect);
  }
};
 
module.exports = {
  id,
  description: l10n("EVENT_TEXT_SET_SOUND_EFFECT_DESC"),
  autoLabel,
  groups,
  subGroups,
  fields,
  compile,
};