-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
72 additions
and
16 deletions.
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
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
68 changes: 60 additions & 8 deletions
68
evi-react-native-example/EVIExample/modules/audio/src/AudioModule.web.ts
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,25 +1,77 @@ | ||
import { EventEmitter } from 'expo-modules-core'; | ||
import { convertBlobToBase64, convertBase64ToBlob, getAudioStream, ensureSingleValidAudioTrack, getBrowserSupportedMimeType, MimeType } from 'hume'; | ||
|
||
const emitter = new EventEmitter({} as any); | ||
|
||
let recorder: MediaRecorder | null = null; | ||
let audioStream: MediaStream | null = null; | ||
let currentAudio: HTMLAudioElement | null = null; | ||
let isMuted = false; | ||
|
||
const mimeType: MimeType = (() => { | ||
const result = getBrowserSupportedMimeType(); | ||
return result.success ? result.mimeType : MimeType.WEBM; | ||
})(); | ||
export default { | ||
async getPermissions() { | ||
console.log('Pretending to get permissions...') | ||
console.log('Requesting microphone permissions...'); | ||
await navigator.mediaDevices.getUserMedia({ audio: true }); | ||
console.log('Microphone permissions granted.'); | ||
}, | ||
|
||
async startRecording(): Promise<void> { | ||
console.log('Pretending to start recording...') | ||
console.log('Starting audio recording...'); | ||
|
||
audioStream = await getAudioStream(); | ||
ensureSingleValidAudioTrack(audioStream); | ||
|
||
recorder = new MediaRecorder(audioStream, { mimeType }); | ||
console.log(recorder) | ||
|
||
recorder.ondataavailable = async ({ data }) => { | ||
if (isMuted) return; | ||
if (data.size < 1) return; | ||
|
||
const base64EncodedAudio = await convertBlobToBase64(data); | ||
emitter.emit('onAudioInput', {base64EncodedAudio}); | ||
}; | ||
|
||
recorder.start(100); // Record audio in 100ms slices | ||
console.log('Audio recording started.'); | ||
}, | ||
|
||
async stopRecording(): Promise<void> { | ||
console.log('Pretending to stop recording...') | ||
//emitter.removeAllListeners('onAudioInput'); | ||
console.log('Stopping audio recording...'); | ||
recorder?.stop(); | ||
recorder = null; | ||
audioStream?.getTracks().forEach(track => track.stop()); | ||
audioStream = null; | ||
console.log('Audio recording stopped.'); | ||
}, | ||
|
||
async playAudio(base64EncodedAudio: string): Promise<void> { | ||
console.log('Pretending to play audio...') | ||
console.log('Playing audio...'); | ||
return new Promise((resolve) => { | ||
const audioBlob = convertBase64ToBlob(base64EncodedAudio, mimeType!); | ||
const audioUrl = URL.createObjectURL(audioBlob); | ||
currentAudio = new Audio(audioUrl); | ||
currentAudio.onended = () => resolve() | ||
currentAudio.play(); | ||
}) | ||
}, | ||
|
||
async mute(): Promise<void> { | ||
console.log('Pretending to mute...') | ||
isMuted = true; | ||
}, | ||
|
||
async unmute(): Promise<void> { | ||
console.log('Pretending to unmute...') | ||
} | ||
isMuted = false; | ||
}, | ||
|
||
async stopPlayback(): Promise<void> { | ||
currentAudio?.pause(); | ||
currentAudio = null; | ||
}, | ||
|
||
isLinear16PCM: false, | ||
}; |