-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: working on open ai example * feat: cleanup and renaming
- Loading branch information
Showing
8 changed files
with
160 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,4 +83,6 @@ react-native-audio-api*.tgz | |
# Android | ||
.kotlin | ||
|
||
|
||
# Envs | ||
.env |
125 changes: 125 additions & 0 deletions
125
apps/common-app/src/examples/TextToSpeech/TextToSpeech.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import React, { useState, FC } from 'react'; | ||
import { AudioBuffer, AudioContext } from 'react-native-audio-api'; | ||
import { ActivityIndicator, TextInput, StyleSheet } from 'react-native'; | ||
|
||
import { Container, Button, Spacer } from '../../components'; | ||
import Env from '../../utils/env'; | ||
import { colors } from '../../styles'; | ||
|
||
async function getOpenAIResponse(input: string, voice: string = 'alloy') { | ||
return await fetch('https://api.openai.com/v1/audio/speech', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Authorization': `Bearer ${Env.openAiToken}`, | ||
}, | ||
body: JSON.stringify({ | ||
model: 'tts-1-hd', | ||
voice: voice, | ||
input: input, | ||
response_format: 'pcm', | ||
}), | ||
}).then((response) => response.arrayBuffer()); | ||
} | ||
|
||
const openAISampleRate = 24000; | ||
const maxInputValue = 32768.0; | ||
|
||
// TODO: this should ideally be done using native code through .decodeAudioData | ||
function goofyResample( | ||
audioContext: AudioContext, | ||
input: Int16Array | ||
): AudioBuffer { | ||
const scale = audioContext.sampleRate / openAISampleRate; | ||
|
||
const outputBuffer = audioContext.createBuffer( | ||
2, | ||
input.length * scale, | ||
audioContext.sampleRate | ||
); | ||
|
||
const processingChannel: Array<number> = []; | ||
const upSampleChannel: Array<number> = []; | ||
|
||
for (let i = 0; i < input.length; i += 1) { | ||
processingChannel[i] = input[i] / maxInputValue; | ||
} | ||
|
||
for (let i = 0; i < input.length; i += 1) { | ||
const isLast = i === input.length - 1; | ||
const currentSample = processingChannel[i]; | ||
const nextSample = isLast ? currentSample : processingChannel[i + 1]; | ||
|
||
upSampleChannel[2 * i] = currentSample; | ||
upSampleChannel[2 * i + 1] = (currentSample + nextSample) / 2; | ||
} | ||
|
||
outputBuffer.copyToChannel(upSampleChannel, 0); | ||
outputBuffer.copyToChannel(upSampleChannel, 1); | ||
|
||
return outputBuffer; | ||
} | ||
|
||
const TextToSpeech: FC = () => { | ||
const [isLoading, setIsLoading] = useState(false); | ||
const [textToRead, setTextToRead] = useState(''); | ||
|
||
const onReadText = async () => { | ||
if (isLoading) { | ||
return; | ||
} | ||
|
||
const aCtx = new AudioContext(); | ||
|
||
setIsLoading(true); | ||
const results = await getOpenAIResponse(textToRead, 'alloy'); | ||
setIsLoading(false); | ||
|
||
const audioBuffer = goofyResample(aCtx, new Int16Array(results)); | ||
const sourceNode = aCtx.createBufferSource(); | ||
const duration = audioBuffer.duration; | ||
const now = aCtx.currentTime; | ||
|
||
sourceNode.buffer = audioBuffer; | ||
|
||
sourceNode.connect(aCtx.destination); | ||
|
||
sourceNode.start(now); | ||
sourceNode.stop(now + duration); | ||
}; | ||
|
||
return ( | ||
<Container style={styles.container}> | ||
<Spacer.Vertical size={60} /> | ||
<TextInput | ||
value={textToRead} | ||
onChangeText={setTextToRead} | ||
style={styles.textInput} | ||
multiline | ||
/> | ||
<Spacer.Vertical size={24} /> | ||
<Button onPress={onReadText} title="Read Text" /> | ||
<Spacer.Vertical size={24} /> | ||
{isLoading && <ActivityIndicator />} | ||
</Container> | ||
); | ||
}; | ||
|
||
export default TextToSpeech; | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
alignItems: 'center', | ||
}, | ||
textInput: { | ||
backgroundColor: 'transparent', | ||
borderColor: colors.border, | ||
color: colors.white, | ||
borderWidth: 1, | ||
fontSize: 16, | ||
padding: 16, | ||
width: 280, | ||
height: 200, | ||
borderRadius: 6, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './TextToSpeech'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default { | ||
openAiToken: process.env.OPENAI_API_TOKEN, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module.exports = { | ||
presets: ['module:@react-native/babel-preset'], | ||
plugins: ['react-native-reanimated/plugin'], | ||
plugins: ['react-native-reanimated/plugin', 'module:react-native-dotenv'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters