Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
twitchard committed Nov 18, 2024
1 parent 3b14a19 commit 419fcf3
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 16 deletions.
17 changes: 10 additions & 7 deletions evi-react-native-example/EVIExample/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,15 @@ const App = () => {
// system audio to linear 16 PCM, a standard format recognized by EVI. For linear16 PCM
// you must send a `session_settings` message to EVI to inform EVI of the
// correct sampling rate.
chatSocket.sendSessionSettings({
audio: {
encoding: "linear16",
channels: 1,
sampleRate: NativeAudio.sampleRate,
},
});
if (NativeAudio.isLinear16PCM) {
chatSocket.sendSessionSettings({
audio: {
encoding: "linear16",
channels: 1,
sampleRate: NativeAudio.sampleRate,
},
});
}
});
chatSocket.on("message", handleIncomingMessage);

Expand All @@ -135,6 +137,7 @@ const App = () => {
if (chatSocket.readyState !== WebSocket.OPEN) {
return;
}
console.log(typeof base64EncodedAudio)
chatSocket.sendAudioInput({ data: base64EncodedAudio });
}
);
Expand Down
1 change: 1 addition & 0 deletions evi-react-native-example/EVIExample/modules/audio/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export async function unmute(): Promise<void> {
}

export const sampleRate = AudioModule.sampleRate
export const isLinear16PCM = AudioModule.isLinear16PCM

const emitter = new EventEmitter(AudioModule ?? NativeModulesProxy.Audio);
export async function stopRecording(): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class AudioModule: Module {
public func definition() -> ModuleDefinition {
Name("Audio")

Constants(["sampleRate": Microphone.sampleRate])
Constants(["sampleRate": Microphone.sampleRate, "isLinear16PCM": Microphone.isLinear16PCM])

Events("onAudioInput", "onError")

Expand Down
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,
};

0 comments on commit 419fcf3

Please sign in to comment.