|
| 1 | +import { BackgroundAudio } from '../types/backgroundAudio.js'; |
| 2 | +import { addCallbackFunction, addCommand, addCommandCallback } from '../utils/index.js'; |
| 3 | + |
| 4 | +type RecordingStopInternalResponse = { |
| 5 | + success: boolean; |
| 6 | + base64?: string; |
| 7 | + mimeType?: string; |
| 8 | + state?: BackgroundAudio.RecordingState; |
| 9 | + durationSeconds?: number; |
| 10 | + transcript?: string; |
| 11 | +}; |
| 12 | + |
| 13 | +type ConvertResultParams = RecordingStopInternalResponse & { |
| 14 | + base64?: string; |
| 15 | + mimeType?: string; |
| 16 | +}; |
| 17 | + |
| 18 | +const backgroundAudio = { |
| 19 | + checkPermission: function () { |
| 20 | + return addCommandCallback<BackgroundAudio.PermissionResponse>('median://backgroundAudio/checkPermission'); |
| 21 | + }, |
| 22 | + requestPermission: function () { |
| 23 | + return addCommandCallback<BackgroundAudio.PermissionResponse>('median://backgroundAudio/requestPermission'); |
| 24 | + }, |
| 25 | + startRecording: function (params: BackgroundAudio.RecordingStartParams) { |
| 26 | + let onRecordingStop: string | undefined; |
| 27 | + |
| 28 | + if (params?.onRecordingStop) { |
| 29 | + const onRecordingStopCallback = params.onRecordingStop; |
| 30 | + |
| 31 | + const callback = function (result: RecordingStopInternalResponse) { |
| 32 | + try { |
| 33 | + const data = median_background_audio_convert_result(result); |
| 34 | + onRecordingStopCallback(data); |
| 35 | + } catch (error) { |
| 36 | + onRecordingStopCallback({ success: false, error }); |
| 37 | + } |
| 38 | + }; |
| 39 | + |
| 40 | + onRecordingStop = addCallbackFunction(callback); |
| 41 | + } |
| 42 | + |
| 43 | + return addCommandCallback<BackgroundAudio.RecordingStartResponse>('median://backgroundAudio/startRecording', { |
| 44 | + ...params, |
| 45 | + onRecordingStop, |
| 46 | + }); |
| 47 | + }, |
| 48 | + stopRecording: async function () { |
| 49 | + const result = await addCommandCallback<RecordingStopInternalResponse>('median://backgroundAudio/stopRecording'); |
| 50 | + return median_background_audio_convert_result(result); |
| 51 | + }, |
| 52 | + pause: function () { |
| 53 | + return addCommandCallback<BackgroundAudio.RecordingStatusResponse>('median://backgroundAudio/pause'); |
| 54 | + }, |
| 55 | + resume: function () { |
| 56 | + return addCommandCallback<BackgroundAudio.RecordingStatusResponse>('median://backgroundAudio/resume'); |
| 57 | + }, |
| 58 | + getStatus: function () { |
| 59 | + return addCommandCallback<BackgroundAudio.RecordingStatusResponse>('median://backgroundAudio/getStatus'); |
| 60 | + }, |
| 61 | + addListener: async function (callback: (data: BackgroundAudio.RecordingEvent) => void) { |
| 62 | + const listenerIdCallback = addCallbackFunction(callback, true); |
| 63 | + const result = await addCommandCallback('median://backgroundAudio/addListener', { listenerIdCallback }); |
| 64 | + if (!result?.listenerId) { |
| 65 | + throw 'INVALID_LISTENER_ID'; |
| 66 | + } else { |
| 67 | + return result.listenerId; |
| 68 | + } |
| 69 | + }, |
| 70 | + removeListener: function (listenerId: string) { |
| 71 | + addCommand('median://backgroundAudio/removeListener', { listenerId }); |
| 72 | + }, |
| 73 | +}; |
| 74 | + |
| 75 | +function median_background_audio_convert_result(result: ConvertResultParams): BackgroundAudio.RecordingStopResponse { |
| 76 | + if (result?.success && result?.base64 && result?.mimeType) { |
| 77 | + const fileUri = median_background_audio_base64_to_url(result.base64, result.mimeType); |
| 78 | + |
| 79 | + return { |
| 80 | + success: result.success, |
| 81 | + fileUri, |
| 82 | + state: result.state, |
| 83 | + durationSeconds: result.durationSeconds, |
| 84 | + transcript: result.transcript, |
| 85 | + }; |
| 86 | + } |
| 87 | + |
| 88 | + return result; |
| 89 | +} |
| 90 | + |
| 91 | +function median_background_audio_base64_to_url(base64: string, mimeType: string) { |
| 92 | + const byteChars = atob(base64); |
| 93 | + const byteNumbers = new Array(byteChars.length); |
| 94 | + for (let i = 0; i < byteChars.length; i++) { |
| 95 | + byteNumbers[i] = byteChars.charCodeAt(i); |
| 96 | + } |
| 97 | + const byteArray = new Uint8Array(byteNumbers); |
| 98 | + const blob = new Blob([byteArray], { type: mimeType }); |
| 99 | + return URL.createObjectURL(blob); |
| 100 | +} |
| 101 | + |
| 102 | +export default backgroundAudio; |
0 commit comments