@nualang/nualang-ui-components 0.1.1218 → 0.1.1219

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.
@@ -691,7 +691,6 @@ function useExerciseState({
691
691
  setTimeout(() => {
692
692
  // Check if transcript has been reset
693
693
  if (interimTranscript || finalTranscript) {
694
- console.log("Transcript not reset, resetting now.");
695
694
  resetTranscript();
696
695
  }
697
696
  }, 800);
@@ -154,22 +154,12 @@ function useRecognition(props = {}) {
154
154
  */
155
155
  const initRecording = async (gcloudParams, onData, onError) => {
156
156
  try {
157
- const {
158
- mediaDevices
159
- } = navigator;
160
- const [availableMicrophones, activeMicrophone, sampleRate] = await (0, _index.getMicrophoneInfo)(mediaDevices);
161
157
  await setupAudioContext();
162
- let sampleRateToUse = sampleRate;
163
- if (sampleRate && sampleRate < 8000) {
164
- sampleRateToUse = 8000;
165
- } else if (sampleRate && sampleRate > 48000) {
166
- sampleRateToUse = 48000;
167
- }
168
158
  const handleSuccess = function (stream) {
169
159
  const gcloudConfig = {
170
160
  config: {
171
161
  encoding: "LINEAR16",
172
- sampleRateHertz: sampleRateToUse || 16000,
162
+ sampleRateHertz: 16000,
173
163
  languageCode: gcloudParams.languageCode || "en-US",
174
164
  profanityFilter: true,
175
165
  enableWordTimeOffsets: false,
@@ -182,6 +172,7 @@ function useRecognition(props = {}) {
182
172
  interimResults: gcloudParams.interimResults // If you want interim results, set this to true
183
173
  };
184
174
  socket.emit("startGoogleCloudStream", gcloudConfig, version); // init socket Google Speech Connection
175
+
185
176
  streamRef.current = stream;
186
177
  audioInputRef.current = audioContextRef.current.createMediaStreamSource(stream);
187
178
  serverErrorRef.current = null;
@@ -193,6 +184,15 @@ function useRecognition(props = {}) {
193
184
  const audioData = event.data;
194
185
  socket.emit("binaryData", audioData, version);
195
186
  };
187
+ processorRef.current.port.onmessage = event => {
188
+ const audioData = event.data;
189
+ let sampleRate = 44100; // Default sample rate
190
+ if (audioContextRef?.current?.sampleRate && audioContextRef?.current?.sampleRate !== 44100) {
191
+ sampleRate = audioContextRef.current.sampleRate;
192
+ }
193
+ const result = (0, _index.downsampleBuffer)(audioData, sampleRate, 16000);
194
+ socket.emit("binaryData", result, version);
195
+ };
196
196
  if (window.MediaRecorder) {
197
197
  mediaRecorderRef.current = new MediaRecorder(stream);
198
198
  mediaRecorderRef.current.ondataavailable = e => {
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.formatCourseMemberCompletions = exports.domainPattern = exports.convertToAlphanumeric = exports.containsInvalidSymbols = exports.checkIsGeneratedEmail = exports.capitalize = exports.calcPercentageCompletion = exports.calcCompletions = void 0;
6
+ exports.formatCourseMemberCompletions = exports.downsampleBuffer = exports.domainPattern = exports.convertToAlphanumeric = exports.containsInvalidSymbols = exports.checkIsGeneratedEmail = exports.capitalize = exports.calcPercentageCompletion = exports.calcCompletions = void 0;
7
7
  exports.formatCurrency = formatCurrency;
8
8
  exports.formatNumberTotal = exports.formatMarkdownNewlines = exports.formatFileSize = void 0;
9
9
  exports.getBase64ImageFromUrl = getBase64ImageFromUrl;
@@ -454,14 +454,36 @@ const getMicrophoneInfo = async mediaDevices => {
454
454
  const availableMicrophoneNames = microphones?.filter(device => device.deviceId !== "default").map(device => device.label).join(", ") || "No available microphones detected";
455
455
  const activeMicrophone = microphones?.length > 0 ? microphones.find(mic => mic.deviceId === "default") || microphones[0] : null;
456
456
  const activeMicrophoneName = activeMicrophone?.label.split("Default - ").pop() || "No active microphone detected";
457
-
458
- // Create an AudioContext with the stream from the active microphone
459
-
460
- const capabilities = activeMicrophone.getCapabilities();
461
- let sampleRate = capabilities?.sampleRate?.max || capabilities?.sampleRate?.exact;
462
- return [availableMicrophoneNames, activeMicrophoneName, sampleRate];
457
+ return [availableMicrophoneNames, activeMicrophoneName];
463
458
  } catch (error) {
464
459
  console.error("Error accessing the microphone:", error);
465
460
  }
466
461
  };
467
- exports.getMicrophoneInfo = getMicrophoneInfo;
462
+ exports.getMicrophoneInfo = getMicrophoneInfo;
463
+ const downsampleBuffer = (buffer, sampleRate, outSampleRate) => {
464
+ if (outSampleRate == sampleRate) {
465
+ return buffer;
466
+ }
467
+ if (outSampleRate > sampleRate) {
468
+ throw new Error("downsampling rate show be smaller than original sample rate");
469
+ }
470
+ var sampleRateRatio = sampleRate / outSampleRate;
471
+ var newLength = Math.round(buffer.length / sampleRateRatio);
472
+ var result = new Int16Array(newLength);
473
+ var offsetResult = 0;
474
+ var offsetBuffer = 0;
475
+ while (offsetResult < result.length) {
476
+ var nextOffsetBuffer = Math.round((offsetResult + 1) * sampleRateRatio);
477
+ var accum = 0,
478
+ count = 0;
479
+ for (var i = offsetBuffer; i < nextOffsetBuffer && i < buffer.length; i += 1) {
480
+ accum += buffer[i];
481
+ count += 1;
482
+ }
483
+ result[offsetResult] = Math.min(1, accum / count) * 0x7fff;
484
+ offsetResult += 1;
485
+ offsetBuffer = nextOffsetBuffer;
486
+ }
487
+ return result.buffer;
488
+ };
489
+ exports.downsampleBuffer = downsampleBuffer;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nualang/nualang-ui-components",
3
- "version": "0.1.1218",
3
+ "version": "0.1.1219",
4
4
  "main": "dist/index.js",
5
5
  "files": [
6
6
  "dist",
@@ -52,6 +52,7 @@
52
52
  "luxon": "^3.3.0",
53
53
  "moment": "^2.29.4",
54
54
  "n2words": "^1.21.0",
55
+ "patch-package": "^8.0.0",
55
56
  "pino": "^9.2.0",
56
57
  "pixabay-api": "^1.0.4",
57
58
  "pre-commit": "^1.2.2",
@@ -133,7 +134,6 @@
133
134
  "intersection-observer": "^0.12.0",
134
135
  "jsdom": "^25.0.0",
135
136
  "msw": "^1.2.3",
136
- "patch-package": "^8.0.0",
137
137
  "prettier": "^3.2.5",
138
138
  "react": "^18.2.0",
139
139
  "react-dom": "^18.2.0",