Skip to content

Commit 3e63895

Browse files
mkopcinsMateusz Kopciński
andauthored
docs: fix errors in docs in spoech to text examples (#388)
## Description <!-- Provide a concise and descriptive summary of the changes implemented in this PR. --> ### Type of change - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] Documentation update (improves or adds clarity to existing documentation) ### Tested on - [ ] iOS - [ ] Android ### Testing instructions <!-- Provide step-by-step instructions on how to test your changes. Include setup details if necessary. --> ### Screenshots <!-- Add screenshots here, if applicable --> ### Related issues <!-- Link related issues here using #issue-number --> ### Checklist - [ ] I have performed a self-review of my code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have updated the documentation accordingly - [ ] My changes generate no new warnings ### Additional notes <!-- Include any additional information, assumptions, or context that reviewers might need to understand this PR. --> --------- Co-authored-by: Mateusz Kopciński <mateusz.kopcinski@swmansnion.com>
1 parent 5d441b2 commit 3e63895

6 files changed

Lines changed: 63 additions & 27 deletions

File tree

docs/docs/natural-language-processing/useSpeechToText.md

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const loadAudio = async (url: string) => {
4949
).then(({ uri }) => {
5050
return audioContext.decodeAudioDataSource(uri);
5151
});
52-
return audioBuffer?.getChannelData(0);
52+
return Array.from(audioBuffer?.getChannelData(0));
5353
};
5454

5555
const audioUrl = ...; // URL with audio to transcribe
@@ -215,26 +215,38 @@ await model.transcribe(mySpanishAudio, SpeechToTextLanguage.Spanish);
215215
## Example
216216

217217
```typescript
218-
import { Button, Text } from 'react-native';
218+
import { Button, Text, View } from 'react-native';
219219
import { useSpeechToText } from 'react-native-executorch';
220+
import * as FileSystem from 'expo-file-system';
221+
import { AudioContext } from 'react-native-audio-api';
220222

221223
function App() {
222-
const { loadAudio, transcribe, sequence, error } = useSpeechToText({
224+
const { transcribe, sequence, error } = useSpeechToText({
223225
modelName: 'whisper',
224226
});
225227

226-
const audioUrl = ...; // URL with audio to transcribe
228+
const loadAudio = async (url: string) => {
229+
const audioContext = new AudioContext({ sampleRate: 16e3 });
230+
const audioBuffer = await FileSystem.downloadAsync(
231+
url,
232+
FileSystem.documentDirectory + '_tmp_transcribe_audio.mp3'
233+
).then(({ uri }) => {
234+
return audioContext.decodeAudioDataSource(uri);
235+
});
236+
return Array.from(audioBuffer?.getChannelData(0));
237+
};
238+
239+
const audioUrl = '...;' // URL with audio to transcribe
227240

228241
return (
229242
<View>
230243
<Button
231244
onPress={async () => {
232-
await loadAudio(audioUrl);
233-
await transcribe();
234-
}
245+
await transcribe(await loadAudio(audioUrl));
246+
}}
235247
title="Transcribe"
236248
/>
237-
<Text>{error ? error : sequence}</Text>
249+
<Text>{error ? error.message : sequence}</Text>
238250
</View>
239251
);
240252
}

docs/docs/typescript-api/SpeechToTextModule.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const loadAudio = async (url: string) => {
1919
).then(({ uri }) => {
2020
return audioContext.decodeAudioDataSource(uri);
2121
});
22-
return audioBuffer?.getChannelData(0);
22+
return Array.from(audioBuffer?.getChannelData(0));
2323
};
2424

2525
const audioUrl = ...; // URL with audio to transcribe

docs/versioned_docs/version-0.3.x/hookless-api/SpeechToTextModule.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const loadAudio = async (url: string) => {
2020
).then(({ uri }) => {
2121
return audioContext.decodeAudioDataSource(uri);
2222
});
23-
return audioBuffer?.getChannelData(0);
23+
return Array.from(audioBuffer?.getChannelData(0));
2424
};
2525

2626
const audioUrl = ...; // URL with audio to transcribe

docs/versioned_docs/version-0.3.x/speech-to-text/useSpeechToText.md

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const loadAudio = async (url: string) => {
5050
).then(({ uri }) => {
5151
return audioContext.decodeAudioDataSource(uri);
5252
});
53-
return audioBuffer?.getChannelData(0);
53+
return Array.from(audioBuffer?.getChannelData(0));
5454
};
5555

5656
const audioUrl = ...; // URL with audio to transcribe
@@ -109,26 +109,38 @@ Before running the model's `transcribe` method be sure to obtain waveform of the
109109
## Example
110110

111111
```typescript
112-
import { Button, Text } from 'react-native';
112+
import { Button, Text, View } from 'react-native';
113113
import { useSpeechToText } from 'react-native-executorch';
114+
import * as FileSystem from 'expo-file-system';
115+
import { AudioContext } from 'react-native-audio-api';
114116

115117
function App() {
116-
const { loadAudio, transcribe, sequence, error } = useSpeechToText({
118+
const { transcribe, sequence, error } = useSpeechToText({
117119
modelName: 'whisper',
118120
});
119121

120-
const audioUrl = ...; // URL with audio to transcribe
122+
const loadAudio = async (url: string) => {
123+
const audioContext = new AudioContext({ sampleRate: 16e3 });
124+
const audioBuffer = await FileSystem.downloadAsync(
125+
url,
126+
FileSystem.documentDirectory + '_tmp_transcribe_audio.mp3'
127+
).then(({ uri }) => {
128+
return audioContext.decodeAudioDataSource(uri);
129+
});
130+
return Array.from(audioBuffer?.getChannelData(0));
131+
};
132+
133+
const audioUrl = '...;' // URL with audio to transcribe
121134

122135
return (
123136
<View>
124137
<Button
125138
onPress={async () => {
126-
await loadAudio(audioUrl);
127-
await transcribe();
128-
}
139+
await transcribe(await loadAudio(audioUrl));
140+
}}
129141
title="Transcribe"
130142
/>
131-
<Text>{error ? error : sequence}</Text>
143+
<Text>{error ? error.message : sequence}</Text>
132144
</View>
133145
);
134146
}

docs/versioned_docs/version-0.4.x/natural-language-processing/useSpeechToText.md

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const loadAudio = async (url: string) => {
4949
).then(({ uri }) => {
5050
return audioContext.decodeAudioDataSource(uri);
5151
});
52-
return audioBuffer?.getChannelData(0);
52+
return Array.from(audioBuffer?.getChannelData(0));
5353
};
5454

5555
const audioUrl = ...; // URL with audio to transcribe
@@ -215,26 +215,38 @@ await model.transcribe(mySpanishAudio, SpeechToTextLanguage.Spanish);
215215
## Example
216216

217217
```typescript
218-
import { Button, Text } from 'react-native';
218+
import { Button, Text, View } from 'react-native';
219219
import { useSpeechToText } from 'react-native-executorch';
220+
import * as FileSystem from 'expo-file-system';
221+
import { AudioContext } from 'react-native-audio-api';
220222

221223
function App() {
222-
const { loadAudio, transcribe, sequence, error } = useSpeechToText({
224+
const { transcribe, sequence, error } = useSpeechToText({
223225
modelName: 'whisper',
224226
});
225227

226-
const audioUrl = ...; // URL with audio to transcribe
228+
const loadAudio = async (url: string) => {
229+
const audioContext = new AudioContext({ sampleRate: 16e3 });
230+
const audioBuffer = await FileSystem.downloadAsync(
231+
url,
232+
FileSystem.documentDirectory + '_tmp_transcribe_audio.mp3'
233+
).then(({ uri }) => {
234+
return audioContext.decodeAudioDataSource(uri);
235+
});
236+
return Array.from(audioBuffer?.getChannelData(0));
237+
};
238+
239+
const audioUrl = '...;' // URL with audio to transcribe
227240

228241
return (
229242
<View>
230243
<Button
231244
onPress={async () => {
232-
await loadAudio(audioUrl);
233-
await transcribe();
234-
}
245+
await transcribe(await loadAudio(audioUrl));
246+
}}
235247
title="Transcribe"
236248
/>
237-
<Text>{error ? error : sequence}</Text>
249+
<Text>{error ? error.message : sequence}</Text>
238250
</View>
239251
);
240252
}

docs/versioned_docs/version-0.4.x/typescript-api/SpeechToTextModule.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const loadAudio = async (url: string) => {
1919
).then(({ uri }) => {
2020
return audioContext.decodeAudioDataSource(uri);
2121
});
22-
return audioBuffer?.getChannelData(0);
22+
return Array.from(audioBuffer?.getChannelData(0));
2323
};
2424

2525
const audioUrl = ...; // URL with audio to transcribe

0 commit comments

Comments
 (0)