Skip to content

Commit

Permalink
feat(example): add updateAudioSession
Browse files Browse the repository at this point in the history
  • Loading branch information
jhen0409 committed Oct 1, 2023
1 parent d285e2a commit 5c0d0c0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
26 changes: 25 additions & 1 deletion example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import RNFS from 'react-native-fs'
import { unzip } from 'react-native-zip-archive'
import Sound from 'react-native-sound'
import { initWhisper, libVersion } from '../../src' // whisper.rn
import { initWhisper, libVersion, AudioSessionIos } from '../../src' // whisper.rn
import type { WhisperContext } from '../../src'
import contextOpts from './context-opts'

Expand Down Expand Up @@ -93,6 +93,28 @@ const createDir = async (log: any) => {
const filterPath = (path: string) =>
path.replace(RNFS.DocumentDirectoryPath, '<DocumentDir>')

const updateAudioSession = async (log: any) => {
if (Platform.OS !== 'ios') return

// Log current audio session
// log('Category & Options:', JSON.stringify(await AudioSessionIos.getCurrentCategory()))
// log('Mode:', await AudioSessionIos.getCurrentMode())

await AudioSessionIos.setCategory(
AudioSessionIos.Category.PlayAndRecord, [
AudioSessionIos.CategoryOptions.MixWithOthers,
AudioSessionIos.CategoryOptions.AllowBluetooth,
],
)
await AudioSessionIos.setMode(AudioSessionIos.Mode.SpokenAudio)
await AudioSessionIos.setActive(true)

const categoryResult = await AudioSessionIos.getCurrentCategory()
log('Category:', categoryResult.category)
log('Category Options:', categoryResult.options.join(', '))
log('Mode:', await AudioSessionIos.getCurrentMode())
}

export default function App() {
const [whisperContext, setWhisperContext] = useState<WhisperContext | null>(null)
const [logs, setLogs] = useState([`whisper.cpp version: ${libVersion}`])
Expand Down Expand Up @@ -263,6 +285,7 @@ export default function App() {
log('Start realtime transcribing...')
try {
await createDir(log)
await updateAudioSession(log)
const { stop, subscribe } =
await whisperContext.transcribeRealtime({
language: 'en',
Expand Down Expand Up @@ -358,6 +381,7 @@ export default function App() {
log('Recorded file does not exist')
return
}
await updateAudioSession(log)
const player = new Sound(recordFile, '', (e) => {
if (e) {
log('error', e)
Expand Down
2 changes: 1 addition & 1 deletion ios/RNWhisperAudioSessionUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,4 @@ +(AVAudioSessionCategoryOptions)getOptions:(NSArray *)options {
return result;
}

@end
@end
12 changes: 8 additions & 4 deletions src/AudioSessionIos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,26 @@ const checkPlatform = () => {
}

export default {
Category: AudioSessionCategory,
CategoryOptions: AudioSessionCategoryOptions,
Mode: AudioSessionMode,

getCurrentCategory: async (): Promise<{
category: AudioSessionCategory,
options: AudioSessionCategoryOptions[],
}> => {
checkPlatform()
const { category, options } = await RNWhisper.getAudioSessionCurrentCategory()
const result = await RNWhisper.getAudioSessionCurrentCategory()
return {
category: category as AudioSessionCategory,
options: options as AudioSessionCategoryOptions[],
category: (result.category.replace('AVAudioSessionCategory', '') as AudioSessionCategory),
options: result.options?.map((option: string) => (option.replace('AVAudioSessionCategoryOptions', '') as AudioSessionCategoryOptions)),
}
},

getCurrentMode: async (): Promise<AudioSessionMode> => {
checkPlatform()
const mode = await RNWhisper.getAudioSessionCurrentMode()
return mode as AudioSessionMode
return (mode.replace('AVAudioSessionMode', '') as AudioSessionMode)
},

setCategory: async (
Expand Down
2 changes: 1 addition & 1 deletion src/NativeRNWhisper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export interface Spec extends TurboModule {
// iOS specific
getAudioSessionCurrentCategory: () => Promise<{
category: string,
options: {},
options: Array<string>,
}>;
getAudioSessionCurrentMode: () => Promise<string>;
setAudioSessionCategory: (category: string, options: Array<string>) => Promise<void>;
Expand Down

0 comments on commit 5c0d0c0

Please sign in to comment.