@lvce-editor/renderer-process 30.26.0 → 30.28.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) => {
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,44 @@ 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
+ const remoteAnalyzerInstance = {
9624
+ instance: undefined
9625
+ };
9626
+ if (trackAudioData) {
9627
+ // @ts-ignore
9628
+ audioCtx = new (window.AudioContext || window.webkitAudioContext)();
9629
+ }
9611
9630
  remoteAudio.autoplay = true;
9612
9631
  pc.ontrack = e => {
9613
9632
  remoteAudio.srcObject = e.streams[0];
9633
+ if (trackAudioData && audioCtx) {
9634
+ remoteAnalyzerInstance.instance = setupLevelMeter(audioCtx, e.streams[0]);
9635
+ }
9614
9636
  };
9615
9637
  const micStream = await navigator.mediaDevices.getUserMedia({
9616
9638
  audio: true
9617
9639
  });
9640
+ if (trackAudioData && audioCtx) {
9641
+ micAnalyzer = setupLevelMeter(audioCtx, micStream);
9642
+ }
9618
9643
  pc.addTrack(micStream.getTracks()[0]);
9619
-
9620
- // const audioCtx = new (window.AudioContext || window.webkitAudioContext)()
9621
-
9622
9644
  const dc = pc.createDataChannel('oai-events');
9623
9645
  dc.addEventListener('message', e => {
9624
9646
  port.postMessage(e.data);
9625
- }
9626
-
9627
- // handleServerEvent(JSON.parse(e.data))
9628
- );
9647
+ });
9629
9648
 
9630
9649
  // 3. Standard WebRTC offer/answer handshake against the Realtime API.
9631
9650
  const offer = await pc.createOffer();
9632
9651
  await pc.setLocalDescription(offer);
9633
9652
  pcs[uid] = {
9653
+ audioCtx,
9634
9654
  connection: pc,
9655
+ micAnalyzer,
9635
9656
  micStream,
9636
- port
9657
+ port,
9658
+ remoteAnalyzer: remoteAnalyzerInstance
9637
9659
  };
9638
9660
  return offer.sdp;
9639
9661
  };
@@ -9655,6 +9677,8 @@ const setRemoteDescription = async options => {
9655
9677
  type
9656
9678
  });
9657
9679
  };
9680
+
9681
+ // TODO this has a race conditon when start/stop is pressed fast
9658
9682
  const stopWebRtcAudioStream = async options => {
9659
9683
  const {
9660
9684
  uid
@@ -9663,7 +9687,9 @@ const stopWebRtcAudioStream = async options => {
9663
9687
  if (!pc) {
9664
9688
  return;
9665
9689
  }
9690
+ // TODO use disposableMap maybe?
9666
9691
  const {
9692
+ audioCtx,
9667
9693
  connection,
9668
9694
  micStream,
9669
9695
  port
@@ -9674,6 +9700,39 @@ const stopWebRtcAudioStream = async options => {
9674
9700
  t.stop();
9675
9701
  }
9676
9702
  port.close();
9703
+ if (audioCtx) {
9704
+ await audioCtx.close();
9705
+ }
9706
+ };
9707
+ const readMicLevel = analyzer => {
9708
+ let data = new Uint8Array();
9709
+ if (analyzer) {
9710
+ data = new Uint8Array(analyzer.frequencyBinCount);
9711
+ analyzer.getByteTimeDomainData(data);
9712
+ }
9713
+ return data;
9714
+ };
9715
+ const readMicLevels = options => {
9716
+ const {
9717
+ uid
9718
+ } = options;
9719
+ const pc = pcs[uid];
9720
+ if (!pc) {
9721
+ return {
9722
+ micAnalyzerData: new Uint8Array(),
9723
+ remoteAnalyzerData: new Uint8Array()
9724
+ };
9725
+ }
9726
+ const {
9727
+ micAnalyzer,
9728
+ remoteAnalyzer
9729
+ } = pc;
9730
+ const micAnalyzerData = readMicLevel(micAnalyzer);
9731
+ const remoteAnalyzerData = readMicLevel(remoteAnalyzer.instance);
9732
+ return {
9733
+ micAnalyzerData,
9734
+ remoteAnalyzerData
9735
+ };
9677
9736
  };
9678
9737
 
9679
9738
  const commandMap = {
@@ -9757,6 +9816,7 @@ const commandMap = {
9757
9816
  'Viewlet.sendMultiple': sendMultiple,
9758
9817
  'Viewlet.setBounds': setBounds$1,
9759
9818
  'Viewlet.show': show,
9819
+ 'WebRtc.readMicLevels': readMicLevels,
9760
9820
  'WebRtc.setRemoteDescription': setRemoteDescription,
9761
9821
  'WebRtc.startWebRtcAudioStream': startWebRtcAudioStream,
9762
9822
  '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.28.0",
4
4
  "keywords": [
5
5
  "lvce-editor",
6
6
  "renderer-process"