@m2c2kit/core 0.3.25 → 0.3.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1254,6 +1254,7 @@ interface M2Sound {
1254
1254
  soundName: string;
1255
1255
  data: ArrayBuffer | undefined;
1256
1256
  audioBuffer: AudioBuffer | undefined;
1257
+ audioBufferSource: AudioBufferSourceNode | undefined;
1257
1258
  url: string;
1258
1259
  status: M2SoundStatus;
1259
1260
  }
@@ -4633,6 +4634,37 @@ declare class SoundPlayer extends M2Node implements SoundPlayerOptions {
4633
4634
  */
4634
4635
  constructor(options: SoundPlayerOptions);
4635
4636
  initialize(): void;
4637
+ /**
4638
+ * Remove an action from this node. If the action is running, it will be
4639
+ * stopped.
4640
+ *
4641
+ * @privateRemarks This methods overrides the `removeAction` method from the
4642
+ * `M2Node` class. It is necessary to override this method because the
4643
+ * `SoundPlayer` class has a special case for removing actions that play
4644
+ * sounds.
4645
+ *
4646
+ * @param key - key (string identifier) of the action to remove
4647
+ */
4648
+ removeAction(key: string): void;
4649
+ /**
4650
+ * Remove all actions from this node. If actions are running, they will be
4651
+ * stopped.
4652
+ *
4653
+ * @privateRemarks This methods overrides the `removeAllActions` method from
4654
+ * the `M2Node` class. It is necessary to override this method because the
4655
+ * `SoundPlayer` class has a special case for removing actions that play
4656
+ * sounds.
4657
+ */
4658
+ removeAllActions(): void;
4659
+ /**
4660
+ * Stops the audio source node for a sound play action.
4661
+ *
4662
+ * @remarks When a SoundPlayer play action is removed, the audio source node
4663
+ * must be stopped and disconnected.
4664
+ *
4665
+ * @param playAction - the play action of the sound to stop
4666
+ */
4667
+ private stopSoundActionAudio;
4636
4668
  dispose(): void;
4637
4669
  /**
4638
4670
  * Duplicates a node using deep copy.
@@ -4707,10 +4739,49 @@ declare class SoundRecorder extends M2Node implements Omit<SoundRecorderOptions,
4707
4739
  */
4708
4740
  constructor(options?: SoundRecorderOptions);
4709
4741
  initialize(): void;
4742
+ /**
4743
+ * Starts recording audio from the microphone.
4744
+ *
4745
+ * @remarks If the `SoundRecorder` is already recording, an error will be
4746
+ * thrown. If permission to use the microphone has not been granted, the
4747
+ * browser will prompt the user to allow or deny access. Denial of access
4748
+ * will result in an error being thrown. To avoid this, use the
4749
+ * `queryPermission()` and `requestPermission()` methods to check and request
4750
+ * permission, respectively, and handle the results accordingly.
4751
+ */
4710
4752
  start(): Promise<void>;
4753
+ /**
4754
+ * Stops recording audio from the microphone.
4755
+ *
4756
+ * @remarks If the `stop()` method is not awaited, the method returns a
4757
+ * Promise and the useable data will be lost.
4758
+ *
4759
+ * @returns A promise that resolves to a {@link SoundRecorderResults} object.
4760
+ * The `audioBase64` property of the object contains the recorded audio as a
4761
+ * base64 string.
4762
+ */
4711
4763
  stop(): Promise<SoundRecorderResults>;
4712
4764
  pause(): void;
4713
4765
  resume(): void;
4766
+ /**
4767
+ * Checks if the microphone permission is granted.
4768
+ *
4769
+ * @remarks This does not request permission from the user. It only queries
4770
+ * the current microphone permission state.
4771
+ *
4772
+ * @returns The `state` property ("granted", "denied", or "prompt") of
4773
+ * `PermissionStatus` or undefined if the browser does not support the
4774
+ * "microphone" permission.
4775
+ * See https://developer.mozilla.org/en-US/docs/Web/API/PermissionStatus/state
4776
+ */
4777
+ queryPermission(): Promise<string | undefined>;
4778
+ /**
4779
+ * Requests permission to use the microphone, and possibly prompts the user
4780
+ * to allow or deny access.
4781
+ *
4782
+ * @returns true if the microphone permission is granted, false if denied.
4783
+ */
4784
+ requestPermission(): Promise<boolean>;
4714
4785
  /** Is the `SoundRecorder` currently recording? */
4715
4786
  get isRecording(): boolean;
4716
4787
  /** Is the `SoundRecorder` currently paused? */
package/dist/index.js CHANGED
@@ -1892,16 +1892,18 @@ class Action {
1892
1892
  if (!playAction.started) {
1893
1893
  const m2Sound = soundManager.getSound(soundPlayer.soundName);
1894
1894
  if (m2Sound.audioBuffer) {
1895
- const source = soundManager.audioContext.createBufferSource();
1896
- source.buffer = m2Sound.audioBuffer;
1897
- source.onended = () => {
1895
+ m2Sound.audioBufferSource = soundManager.audioContext.createBufferSource();
1896
+ m2Sound.audioBufferSource.buffer = m2Sound.audioBuffer;
1897
+ m2Sound.audioBufferSource.onended = () => {
1898
1898
  playAction.running = false;
1899
1899
  playAction.completed = true;
1900
1900
  const knownDuration = performance.now() - (action.runStartTime + action.startOffset.value);
1901
1901
  action.duration.assign(knownDuration);
1902
1902
  };
1903
- source.connect(soundManager.audioContext.destination);
1904
- source.start();
1903
+ m2Sound.audioBufferSource.connect(
1904
+ soundManager.audioContext.destination
1905
+ );
1906
+ m2Sound.audioBufferSource.start();
1905
1907
  playAction.started = true;
1906
1908
  } else {
1907
1909
  if (m2Sound.status === M2SoundStatus.Error) {
@@ -8032,6 +8034,7 @@ class SoundManager {
8032
8034
  soundName: sound.soundName,
8033
8035
  data: void 0,
8034
8036
  audioBuffer: void 0,
8037
+ audioBufferSource: void 0,
8035
8038
  url,
8036
8039
  status: sound.lazy ? M2SoundStatus.Deferred : M2SoundStatus.WillFetch
8037
8040
  };
@@ -10981,9 +10984,13 @@ class Game {
10981
10984
  this.raiseTapDownEvent(node, nodeEvent, domPointerEvent);
10982
10985
  }
10983
10986
  if (node.children) {
10984
- node.children.filter((node2) => !node2.hidden).filter((node2) => node2.isDrawable).sort(
10985
- (a, b) => b.zPosition - a.zPosition
10986
- ).forEach(
10987
+ node.children.filter((node2) => !node2.hidden).filter((node2) => node2.isDrawable).sort((a, b) => {
10988
+ const zDiff = b.zPosition - a.zPosition;
10989
+ if (zDiff !== 0) {
10990
+ return zDiff;
10991
+ }
10992
+ return node.children.indexOf(b) - node.children.indexOf(a);
10993
+ }).forEach(
10987
10994
  (node2) => this.processDomPointerDown(node2, nodeEvent, domPointerEvent)
10988
10995
  );
10989
10996
  }
@@ -10997,7 +11004,6 @@ class Game {
10997
11004
  node.pressed = false;
10998
11005
  node.pressedAndWithinHitArea = false;
10999
11006
  this.raiseM2DragEndEvent(node, nodeEvent, domPointerEvent);
11000
- nodeEvent.handled = true;
11001
11007
  return;
11002
11008
  }
11003
11009
  if (node.isUserInteractionEnabled && node.pressed && node.pressedAndWithinHitArea) {
@@ -11020,9 +11026,13 @@ class Game {
11020
11026
  this.raiseM2PointerUpEvent(node, nodeEvent, domPointerEvent);
11021
11027
  }
11022
11028
  if (node.children) {
11023
- node.children.filter((node2) => !node2.hidden).filter((node2) => node2.isDrawable).sort(
11024
- (a, b) => b.zPosition - a.zPosition
11025
- ).forEach(
11029
+ node.children.filter((node2) => !node2.hidden).filter((node2) => node2.isDrawable).sort((a, b) => {
11030
+ const zDiff = b.zPosition - a.zPosition;
11031
+ if (zDiff !== 0) {
11032
+ return zDiff;
11033
+ }
11034
+ return node.children.indexOf(b) - node.children.indexOf(a);
11035
+ }).forEach(
11026
11036
  (node2) => this.processDomPointerUp(node2, nodeEvent, domPointerEvent)
11027
11037
  );
11028
11038
  }
@@ -11050,7 +11060,6 @@ class Game {
11050
11060
  x: domPointerEvent.offsetX,
11051
11061
  y: domPointerEvent.offsetY
11052
11062
  };
11053
- nodeEvent.handled = true;
11054
11063
  if (firstMoveOfDrag) {
11055
11064
  this.raiseM2DragStartEvent(node, nodeEvent, domPointerEvent);
11056
11065
  } else {
@@ -11083,9 +11092,13 @@ class Game {
11083
11092
  this.raiseM2PointerLeaveEvent(node, nodeEvent, domPointerEvent);
11084
11093
  }
11085
11094
  if (node.children) {
11086
- node.children.filter((node2) => !node2.hidden).filter((node2) => node2.isDrawable).sort(
11087
- (a, b) => b.zPosition - a.zPosition
11088
- ).forEach(
11095
+ node.children.filter((node2) => !node2.hidden).filter((node2) => node2.isDrawable).sort((a, b) => {
11096
+ const zDiff = b.zPosition - a.zPosition;
11097
+ if (zDiff !== 0) {
11098
+ return zDiff;
11099
+ }
11100
+ return node.children.indexOf(b) - node.children.indexOf(a);
11101
+ }).forEach(
11089
11102
  (node2) => this.processDomPointerMove(node2, nodeEvent, domPointerEvent)
11090
11103
  );
11091
11104
  }
@@ -11124,9 +11137,13 @@ class Game {
11124
11137
  this.raiseM2PointerLeaveEvent(node, nodeEvent, domPointerEvent);
11125
11138
  }
11126
11139
  if (node.children) {
11127
- node.children.filter((node2) => !node2.hidden).filter((node2) => node2.isDrawable).sort(
11128
- (a, b) => b.zPosition - a.zPosition
11129
- ).forEach(
11140
+ node.children.filter((node2) => !node2.hidden).filter((node2) => node2.isDrawable).sort((a, b) => {
11141
+ const zDiff = b.zPosition - a.zPosition;
11142
+ if (zDiff !== 0) {
11143
+ return zDiff;
11144
+ }
11145
+ return node.children.indexOf(b) - node.children.indexOf(a);
11146
+ }).forEach(
11130
11147
  (node2) => this.processDomPointerLeave(node2, nodeEvent, domPointerEvent)
11131
11148
  );
11132
11149
  }
@@ -11728,6 +11745,56 @@ class SoundPlayer extends M2Node {
11728
11745
  }
11729
11746
  initialize() {
11730
11747
  }
11748
+ /**
11749
+ * Remove an action from this node. If the action is running, it will be
11750
+ * stopped.
11751
+ *
11752
+ * @privateRemarks This methods overrides the `removeAction` method from the
11753
+ * `M2Node` class. It is necessary to override this method because the
11754
+ * `SoundPlayer` class has a special case for removing actions that play
11755
+ * sounds.
11756
+ *
11757
+ * @param key - key (string identifier) of the action to remove
11758
+ */
11759
+ removeAction(key) {
11760
+ const actionToRemove = this.actions.find((action) => action.key === key);
11761
+ if (actionToRemove?.type === ActionType.Play) {
11762
+ this.stopSoundActionAudio(actionToRemove);
11763
+ }
11764
+ this.actions = this.actions.filter((action) => action.key !== key);
11765
+ }
11766
+ /**
11767
+ * Remove all actions from this node. If actions are running, they will be
11768
+ * stopped.
11769
+ *
11770
+ * @privateRemarks This methods overrides the `removeAllActions` method from
11771
+ * the `M2Node` class. It is necessary to override this method because the
11772
+ * `SoundPlayer` class has a special case for removing actions that play
11773
+ * sounds.
11774
+ */
11775
+ removeAllActions() {
11776
+ while (this.actions.length) {
11777
+ const removedAction = this.actions.pop();
11778
+ if (removedAction?.type === ActionType.Play) {
11779
+ this.stopSoundActionAudio(removedAction);
11780
+ }
11781
+ }
11782
+ }
11783
+ /**
11784
+ * Stops the audio source node for a sound play action.
11785
+ *
11786
+ * @remarks When a SoundPlayer play action is removed, the audio source node
11787
+ * must be stopped and disconnected.
11788
+ *
11789
+ * @param playAction - the play action of the sound to stop
11790
+ */
11791
+ stopSoundActionAudio(playAction) {
11792
+ if (playAction.running) {
11793
+ const m2Sound = this.game.soundManager.getSound(this.soundName);
11794
+ m2Sound.audioBufferSource?.stop();
11795
+ m2Sound.audioBufferSource?.disconnect();
11796
+ }
11797
+ }
11731
11798
  dispose() {
11732
11799
  }
11733
11800
  /**
@@ -11788,12 +11855,24 @@ class SoundRecorder extends M2Node {
11788
11855
  }
11789
11856
  initialize() {
11790
11857
  }
11858
+ /**
11859
+ * Starts recording audio from the microphone.
11860
+ *
11861
+ * @remarks If the `SoundRecorder` is already recording, an error will be
11862
+ * thrown. If permission to use the microphone has not been granted, the
11863
+ * browser will prompt the user to allow or deny access. Denial of access
11864
+ * will result in an error being thrown. To avoid this, use the
11865
+ * `queryPermission()` and `requestPermission()` methods to check and request
11866
+ * permission, respectively, and handle the results accordingly.
11867
+ */
11791
11868
  async start() {
11792
11869
  if (this.isRecording) {
11793
11870
  throw new Error(
11794
11871
  "cannot start SoundRecorder because it is already started."
11795
11872
  );
11796
11873
  }
11874
+ this.audioChunks = [];
11875
+ this.endIso8601Timestamp = void 0;
11797
11876
  const supportedMimeTypes = this.getMediaRecorderSupportedAudioMimeTypes();
11798
11877
  if (supportedMimeTypes.length === 0) {
11799
11878
  throw new Error(
@@ -11810,7 +11889,7 @@ class SoundRecorder extends M2Node {
11810
11889
  audio: this.audioTrackConstraints ? this.audioTrackConstraints : true
11811
11890
  });
11812
11891
  } catch (error) {
11813
- throw new Error("Error getting user media.");
11892
+ throw new Error(`Error getting user media: ${error}.`);
11814
11893
  }
11815
11894
  if (!stream) {
11816
11895
  throw new Error("no stream.");
@@ -11833,6 +11912,16 @@ class SoundRecorder extends M2Node {
11833
11912
  this._isRecording = true;
11834
11913
  this._isPaused = false;
11835
11914
  }
11915
+ /**
11916
+ * Stops recording audio from the microphone.
11917
+ *
11918
+ * @remarks If the `stop()` method is not awaited, the method returns a
11919
+ * Promise and the useable data will be lost.
11920
+ *
11921
+ * @returns A promise that resolves to a {@link SoundRecorderResults} object.
11922
+ * The `audioBase64` property of the object contains the recorded audio as a
11923
+ * base64 string.
11924
+ */
11836
11925
  async stop() {
11837
11926
  if (!this.isRecording) {
11838
11927
  throw new Error("cannot stop SoundRecorder because it has not started.");
@@ -11892,6 +11981,52 @@ class SoundRecorder extends M2Node {
11892
11981
  this._isPaused = false;
11893
11982
  Timer.start(this.timerUuid);
11894
11983
  }
11984
+ /**
11985
+ * Checks if the microphone permission is granted.
11986
+ *
11987
+ * @remarks This does not request permission from the user. It only queries
11988
+ * the current microphone permission state.
11989
+ *
11990
+ * @returns The `state` property ("granted", "denied", or "prompt") of
11991
+ * `PermissionStatus` or undefined if the browser does not support the
11992
+ * "microphone" permission.
11993
+ * See https://developer.mozilla.org/en-US/docs/Web/API/PermissionStatus/state
11994
+ */
11995
+ async queryPermission() {
11996
+ try {
11997
+ const status = await navigator.permissions.query({
11998
+ /**
11999
+ * We use a type assertion here because the PermissionName type
12000
+ * does not include "microphone" in the TypeScript type definitions.
12001
+ */
12002
+ name: "microphone"
12003
+ });
12004
+ return status.state;
12005
+ } catch (error) {
12006
+ console.warn(
12007
+ `Error calling navigator.permissions.query({ name: "microphone" }): ${error}.`
12008
+ );
12009
+ return void 0;
12010
+ }
12011
+ }
12012
+ /**
12013
+ * Requests permission to use the microphone, and possibly prompts the user
12014
+ * to allow or deny access.
12015
+ *
12016
+ * @returns true if the microphone permission is granted, false if denied.
12017
+ */
12018
+ async requestPermission() {
12019
+ try {
12020
+ const stream = await navigator.mediaDevices.getUserMedia({
12021
+ audio: this.audioTrackConstraints ? this.audioTrackConstraints : true
12022
+ });
12023
+ stream.getTracks().forEach((track) => track.stop());
12024
+ return true;
12025
+ } catch (error) {
12026
+ console.warn(`Microphone access denied: ${error}`);
12027
+ return false;
12028
+ }
12029
+ }
11895
12030
  /** Is the `SoundRecorder` currently recording? */
11896
12031
  get isRecording() {
11897
12032
  return this._isRecording;
@@ -12045,7 +12180,7 @@ class Story {
12045
12180
  }
12046
12181
  }
12047
12182
 
12048
- console.log("\u26AA @m2c2kit/core version 0.3.25 (af00d1f0)");
12183
+ console.log("\u26AA @m2c2kit/core version 0.3.27 (1ed4ac2a)");
12049
12184
 
12050
12185
  export { Action, ActivityType, CanvasKitHelpers, ColorfulMutablePath, Composite, Constants, ConstraintType, CustomAction, Dimensions, Easings, Equal, Equals, EventStore, EventStoreMode, FadeAlphaAction, FontManager, Game, GroupAction, I18n, ImageManager, Label, LabelHorizontalAlignmentMode, LayoutConstraint, LegacyTimer, M2EventType, M2ImageStatus, M2Node, M2NodeFactory, M2NodeType, M2SoundStatus, M2c2KitHelpers, MoveAction, MutablePath, NoneTransition, PlayAction, RandomDraws, RepeatAction, RepeatForeverAction, RotateAction, ScaleAction, Scene, SceneTransition, SequenceAction, Shape, ShapeType, SlideTransition, SoundManager, SoundPlayer, SoundRecorder, Sprite, Story, TextLine, Timer, Transition, TransitionDirection, TransitionType, Uuid, WaitAction, WebColors, WebGlInfo, handleInterfaceOptions };
12051
12186
  //# sourceMappingURL=index.js.map