@lvce-editor/renderer-process 30.26.0 → 30.27.0

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.
@@ -9594,10 +9594,20 @@ const queryAudio = (uid, elementLocator) => {
9594
9594
  }
9595
9595
  return remoteAudio;
9596
9596
  };
9597
+ const setupLevelMeter = (audioCtx, stream, kind) => {
9598
+ const source = audioCtx.createMediaStreamSource(stream);
9599
+ const analyser = audioCtx.createAnalyser();
9600
+ // TODO make variables confirguable
9601
+ analyser.fftSize = 512;
9602
+ analyser.smoothingTimeConstant = 0.6;
9603
+ source.connect(analyser);
9604
+ return analyser;
9605
+ };
9597
9606
  const startWebRtcAudioStream = async options => {
9598
9607
  const {
9599
9608
  elementLocator,
9600
9609
  port,
9610
+ trackAudioData,
9601
9611
  uid
9602
9612
  } = options;
9603
9613
 
@@ -9608,32 +9618,41 @@ const startWebRtcAudioStream = async options => {
9608
9618
  console.error('[webrtc] audio element not found');
9609
9619
  return;
9610
9620
  }
9621
+ let audioCtx;
9622
+ let micAnalyzer;
9623
+ let remoteAnalyzer;
9624
+ if (trackAudioData) {
9625
+ // @ts-ignore
9626
+ audioCtx = new (window.AudioContext || window.webkitAudioContext)();
9627
+ }
9611
9628
  remoteAudio.autoplay = true;
9612
9629
  pc.ontrack = e => {
9613
9630
  remoteAudio.srcObject = e.streams[0];
9631
+ if (trackAudioData && audioCtx) {
9632
+ remoteAnalyzer = setupLevelMeter(audioCtx, e.streams[0]);
9633
+ }
9614
9634
  };
9615
9635
  const micStream = await navigator.mediaDevices.getUserMedia({
9616
9636
  audio: true
9617
9637
  });
9638
+ if (trackAudioData && audioCtx) {
9639
+ micAnalyzer = setupLevelMeter(audioCtx, micStream);
9640
+ }
9618
9641
  pc.addTrack(micStream.getTracks()[0]);
9619
-
9620
- // const audioCtx = new (window.AudioContext || window.webkitAudioContext)()
9621
-
9622
9642
  const dc = pc.createDataChannel('oai-events');
9623
9643
  dc.addEventListener('message', e => {
9624
9644
  port.postMessage(e.data);
9625
- }
9626
-
9627
- // handleServerEvent(JSON.parse(e.data))
9628
- );
9645
+ });
9629
9646
 
9630
9647
  // 3. Standard WebRTC offer/answer handshake against the Realtime API.
9631
9648
  const offer = await pc.createOffer();
9632
9649
  await pc.setLocalDescription(offer);
9633
9650
  pcs[uid] = {
9634
9651
  connection: pc,
9652
+ micAnalyzer,
9635
9653
  micStream,
9636
- port
9654
+ port,
9655
+ remoteAnalyzer
9637
9656
  };
9638
9657
  return offer.sdp;
9639
9658
  };
@@ -9663,6 +9682,7 @@ const stopWebRtcAudioStream = async options => {
9663
9682
  if (!pc) {
9664
9683
  return;
9665
9684
  }
9685
+ // TODO use disposableMap maybe?
9666
9686
  const {
9667
9687
  connection,
9668
9688
  micStream,
@@ -9675,6 +9695,36 @@ const stopWebRtcAudioStream = async options => {
9675
9695
  }
9676
9696
  port.close();
9677
9697
  };
9698
+ const readMicLevels = options => {
9699
+ const {
9700
+ uid
9701
+ } = options;
9702
+ const pc = pcs[uid];
9703
+ if (!pc) {
9704
+ return {
9705
+ micAnalyzerData: new Uint8Array(),
9706
+ remoteAnalyzerData: new Uint8Array()
9707
+ };
9708
+ }
9709
+ const {
9710
+ micAnalyzer,
9711
+ remoteAnalyzer
9712
+ } = pc;
9713
+ let micAnalyzerData = new Uint8Array();
9714
+ if (micAnalyzer) {
9715
+ micAnalyzerData = new Uint8Array(micAnalyzer.frequencyBinCount);
9716
+ micAnalyzer.getByteTimeDomainData(micAnalyzerData);
9717
+ }
9718
+ let remoteAnalyzerData = new Uint8Array();
9719
+ if (remoteAnalyzer) {
9720
+ remoteAnalyzerData = new Uint8Array(remoteAnalyzer.frequencyBinCount);
9721
+ remoteAnalyzer.getByteTimeDomainData(remoteAnalyzerData);
9722
+ }
9723
+ return {
9724
+ micAnalyzerData,
9725
+ remoteAnalyzerData
9726
+ };
9727
+ };
9678
9728
 
9679
9729
  const commandMap = {
9680
9730
  'Audio.play': play,
@@ -9757,6 +9807,7 @@ const commandMap = {
9757
9807
  'Viewlet.sendMultiple': sendMultiple,
9758
9808
  'Viewlet.setBounds': setBounds$1,
9759
9809
  'Viewlet.show': show,
9810
+ 'WebRtc.readMicLevels': readMicLevels,
9760
9811
  'WebRtc.setRemoteDescription': setRemoteDescription,
9761
9812
  'WebRtc.startWebRtcAudioStream': startWebRtcAudioStream,
9762
9813
  'WebRtc.stopWebRtcAudioStream': stopWebRtcAudioStream,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/renderer-process",
3
- "version": "30.26.0",
3
+ "version": "30.27.0",
4
4
  "keywords": [
5
5
  "lvce-editor",
6
6
  "renderer-process"