Skip to content

Commit

Permalink
feat(example): wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jhen0409 committed Nov 6, 2024
1 parent 2d7b130 commit b9bc0e0
Showing 1 changed file with 41 additions and 11 deletions.
52 changes: 41 additions & 11 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Sound from 'react-native-sound'
import { initWhisper, libVersion, AudioSessionIos } from '../../src' // whisper.rn
import type { WhisperContext } from '../../src'
import contextOpts from './context-opts'
import { Buffer } from 'buffer'

Check failure on line 18 in example/src/App.tsx

View workflow job for this annotation

GitHub Actions / test-android

`buffer` import should occur before import of `../../src`

Check failure on line 18 in example/src/App.tsx

View workflow job for this annotation

GitHub Actions / test-ios

`buffer` import should occur before import of `../../src`

Check failure on line 18 in example/src/App.tsx

View workflow job for this annotation

GitHub Actions / test-android

`buffer` import should occur before import of `../../src`

Check failure on line 18 in example/src/App.tsx

View workflow job for this annotation

GitHub Actions / test-ios

`buffer` import should occur before import of `../../src`

const sampleFile = require('../assets/jfk.wav')

Check failure on line 20 in example/src/App.tsx

View workflow job for this annotation

GitHub Actions / test-android

'sampleFile' is assigned a value but never used

Check failure on line 20 in example/src/App.tsx

View workflow job for this annotation

GitHub Actions / test-ios

'sampleFile' is assigned a value but never used

Check failure on line 20 in example/src/App.tsx

View workflow job for this annotation

GitHub Actions / test-android

'sampleFile' is assigned a value but never used

Check failure on line 20 in example/src/App.tsx

View workflow job for this annotation

GitHub Actions / test-ios

'sampleFile' is assigned a value but never used

Expand All @@ -30,6 +31,14 @@ if (Platform.OS === 'android') {
})
}

const base64ToInt16ArrayBase64 = (base64: string) => {
const wavData = Buffer.from(base64, 'base64')

// cut header
const wavDataWithoutHeader = wavData.subarray(44)
return Buffer.from(wavDataWithoutHeader).toString('base64')
}

const styles = StyleSheet.create({
scrollview: { flexGrow: 1, justifyContent: 'center' },
container: {
Expand Down Expand Up @@ -224,19 +233,40 @@ export default function App() {
if (!whisperContext) return log('No context')

log('Start transcribing...')

try {
await RNFS.mkdir(fileDir)
if (!(await RNFS.exists(`${fileDir}/jfk.wav`)))
await RNFS.downloadFile({
fromUrl:
'https://github.com/ggerganov/whisper.cpp/raw/refs/heads/master/samples/jfk.wav',
toFile: `${fileDir}/jfk.wav`,
}).promise
} catch (e) {
log('Error downloading file:', e)
}
const sampleFileBase64 = await RNFS.readFile(
`${fileDir}/jfk.wav`,
'base64',
)
// convert to float32 PCM data
const base64 = base64ToInt16ArrayBase64(sampleFileBase64)
const startTime = Date.now()
const { stop, promise } = whisperContext.transcribe(sampleFile, {
maxLen: 1,
tokenTimestamps: true,
onProgress: (cur) => {
log(`Transcribing progress: ${cur}%`)
const { stop, promise } = whisperContext.transcribeData(
base64,
{
maxLen: 1,
tokenTimestamps: true,
onProgress: (cur) => {
log(`Transcribing progress: ${cur}%`)
},
language: 'en',
// prompt: 'HELLO WORLD',
// onNewSegments: (segments) => {
// console.log('New segments:', segments)
// },
},
language: 'en',
// prompt: 'HELLO WORLD',
// onNewSegments: (segments) => {
// console.log('New segments:', segments)
// },
})
)
setStopTranscribe({ stop })
const { result, segments } = await promise
const endTime = Date.now()
Expand Down

0 comments on commit b9bc0e0

Please sign in to comment.