Skip to content

Fix React 18 compatibility and audio issues in browser example #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/react/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SKIP_PREFLIGHT_CHECK=true
14 changes: 14 additions & 0 deletions examples/react/config-overrides.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const webpack = require('webpack');

module.exports = function override(config) {
config.resolve = config.resolve || {};
config.resolve.fallback = config.resolve.fallback || {};
config.resolve.fallback.buffer = require.resolve('buffer/');
config.plugins = config.plugins || [];
config.plugins.push(
new webpack.ProvidePlugin({
Buffer: ['buffer', 'Buffer'],
})
);
return config;
};
132 changes: 130 additions & 2 deletions examples/react/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions examples/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"@types/react": "^18.0.18",
"@types/react-dom": "^18.0.6",
"antd": "^4.23.0",
"buffer": "^6.0.3",
"microphone-stream": "6.0.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand All @@ -19,9 +20,9 @@
"vosk-browser": "../../lib"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test",
"eject": "react-scripts eject"
},
"eslintConfig": {
Expand All @@ -44,6 +45,7 @@
},
"devDependencies": {
"@types/readable-stream": "^2.3.14",
"@types/styled-components": "^5.1.26"
"@types/styled-components": "^5.1.26",
"react-app-rewired": "^2.2.1"
}
}
14 changes: 14 additions & 0 deletions examples/react/src/audiostreamer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import { Duplex, DuplexOptions } from "readable-stream";
import { KaldiRecognizer } from "vosk-browser";

// Polyfill process for browser - fixes process.nextTick errors
if (typeof globalThis.process === 'undefined') {
globalThis.process = {
env: {},
nextTick: function (cb: (...args: any[]) => void, ...args: any[]) {
return Promise.resolve().then(() => cb(...args));
}
} as any;
} else if (typeof globalThis.process.nextTick !== 'function') {
globalThis.process.nextTick = function (cb: (...args: any[]) => void, ...args: any[]) {
return Promise.resolve().then(() => cb(...args));
};
}

export class AudioStreamer extends Duplex {
constructor(public recognizer: KaldiRecognizer, options?: DuplexOptions) {
super(options);
Expand Down
14 changes: 14 additions & 0 deletions examples/react/src/file-upload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ import { UploadOutlined } from "@ant-design/icons";
import styled from "styled-components";
import { KaldiRecognizer } from "vosk-browser";

// Polyfill process for browser - fixes process.nextTick errors
if (typeof globalThis.process === 'undefined') {
globalThis.process = {
env: {},
nextTick: function (cb: (...args: any[]) => void, ...args: any[]) {
return Promise.resolve().then(() => cb(...args));
}
} as any;
} else if (typeof globalThis.process.nextTick !== 'function') {
globalThis.process.nextTick = function (cb: (...args: any[]) => void, ...args: any[]) {
return Promise.resolve().then(() => cb(...args));
};
}

const StyledButton = styled(Button)`
box-sizing: border-box;
margin-left: 0.5rem;
Expand Down
11 changes: 6 additions & 5 deletions examples/react/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "antd/dist/antd.min.css";

import React from "react";
import ReactDOM from "react-dom";
import { createRoot } from "react-dom/client";
import styled from "styled-components";

import "./index.css";
Expand All @@ -13,8 +13,10 @@ const Wrapper = styled.div`
margin: auto;
justify-content: center;
`;

ReactDOM.render(
//
const container = document.getElementById("root");
const root = createRoot(container!);
root.render(
<React.StrictMode>
<Wrapper>
<h1>Vosk-Browser Speech Recognition Demo</h1>
Expand Down Expand Up @@ -43,6 +45,5 @@ ReactDOM.render(
under Apache 2.0 license.
</p>
</Wrapper>
</React.StrictMode>,
document.getElementById("root")
</React.StrictMode>
);
15 changes: 14 additions & 1 deletion examples/react/src/microphone.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,24 @@ import { Button } from "antd";
import { AudioMutedOutlined, AudioOutlined } from "@ant-design/icons";
import MicrophoneStream from "microphone-stream";
import React, { useCallback, useEffect, useState } from "react";

import { AudioStreamer } from "./audiostreamer";
import { audioBucket } from "./audiobucket";
import { KaldiRecognizer } from "vosk-browser";

// Polyfill process for browser - fixes process.nextTick errors
if (typeof globalThis.process === 'undefined') {
globalThis.process = {
env: {},
nextTick: function (cb: (...args: any[]) => void, ...args: any[]) {
return Promise.resolve().then(() => cb(...args));
}
} as any;
} else if (typeof globalThis.process.nextTick !== 'function') {
globalThis.process.nextTick = function (cb: (...args: any[]) => void, ...args: any[]) {
return Promise.resolve().then(() => cb(...args));
};
}

interface Props {
recognizer: KaldiRecognizer | undefined;
ready: boolean;
Expand Down