|
1 | | -<script lang="ts" setup> |
2 | | -import { Button } from '@proj-airi/stage-ui/components' |
3 | | -import { Option, Select } from '@proj-airi/ui' |
4 | | -import { onMounted, onUnmounted } from 'vue' |
| 1 | +<script setup lang="ts"> |
| 2 | +import { useDevicesList, useObjectUrl } from '@vueuse/core' |
| 3 | +import { BufferTarget, MediaStreamAudioTrackSource, Output, QUALITY_MEDIUM, WavOutputFormat } from 'mediabunny' |
| 4 | +import { computed, ref } from 'vue' |
5 | 5 |
|
6 | | -import { useAudioInput } from '../../composables/audio-input' |
7 | | -import { useAudioRecord } from '../../composables/audio-record' |
| 6 | +const { audioInputs } = useDevicesList({ constraints: { audio: true }, requestPermissions: true }) |
| 7 | +const constraintId = ref('') |
8 | 8 |
|
9 | | -const { audioInputs, selectedAudioInputId, start, stop, media, request } = useAudioInput() |
10 | | -const { startRecord, stopRecord } = useAudioRecord(media.stream, start) |
| 9 | +async function getMediaStreamTrack(constraint: ConstrainDOMString) { |
| 10 | + const stream = await navigator.mediaDevices.getUserMedia({ audio: { deviceId: constraint } }) |
| 11 | + return stream.getAudioTracks()[0] |
| 12 | +} |
11 | 13 |
|
12 | | -onMounted(() => request()) |
13 | | -onUnmounted(() => stop()) |
| 14 | +let output: Output | undefined |
| 15 | +let audioInputTrack: MediaStreamAudioTrack | undefined |
| 16 | +let format: string | undefined |
| 17 | +
|
| 18 | +const recorded = ref<ArrayBuffer[]>([]) |
| 19 | +const recordedUrls = computed(() => recorded.value.map(rec => useObjectUrl(new Blob([rec], { type: format })).value)) |
| 20 | +
|
| 21 | +async function handleStart() { |
| 22 | + audioInputTrack = await getMediaStreamTrack(constraintId.value) |
| 23 | + output = new Output({ format: new WavOutputFormat(), target: new BufferTarget() }) |
| 24 | +
|
| 25 | + const audioSource = new MediaStreamAudioTrackSource(audioInputTrack, { codec: 'pcm-f32', bitrate: QUALITY_MEDIUM }) |
| 26 | + audioSource.errorPromise.catch(console.error) |
| 27 | + output.addAudioTrack(audioSource) |
| 28 | +
|
| 29 | + format = await output.getMimeType() |
| 30 | + await output.start() |
| 31 | +} |
| 32 | +
|
| 33 | +async function handleStop() { |
| 34 | + await output?.finalize() |
| 35 | + const bufferTarget = output?.target as BufferTarget | undefined |
| 36 | + bufferTarget?.buffer && recorded.value.push(bufferTarget?.buffer) |
| 37 | +} |
| 38 | +
|
| 39 | +function handleCancel() { |
| 40 | + output?.cancel() |
| 41 | +} |
14 | 42 | </script> |
15 | 43 |
|
16 | 44 | <template> |
17 | 45 | <div> |
18 | | - <Select v-model="selectedAudioInputId" @change="() => start()"> |
19 | | - <template #default="{ value }"> |
20 | | - <div> |
21 | | - {{ value ? audioInputs.find(device => device.deviceId === value)?.label : 'Select Audio Input' }} |
22 | | - </div> |
23 | | - </template> |
24 | | - <template #options="{ hide }"> |
25 | | - <Option |
26 | | - v-for="device in audioInputs" |
27 | | - :key="device.deviceId" |
28 | | - :value="device.deviceId" |
29 | | - :active="device.deviceId === selectedAudioInputId" |
30 | | - @click="hide()" |
31 | | - > |
32 | | - {{ device.label }} |
33 | | - </Option> |
34 | | - </template> |
35 | | - </Select> |
36 | | - <div class="mt-4 w-full flex justify-center gap-2"> |
37 | | - <Button @click="startRecord"> |
38 | | - Start Recording |
39 | | - </Button> |
40 | | - <Button @click="stopRecord"> |
41 | | - Stop Recording |
42 | | - </Button> |
| 46 | + <div> |
| 47 | + <select v-model="constraintId"> |
| 48 | + <option value=""> |
| 49 | + Select |
| 50 | + </option> |
| 51 | + <option v-for="(item, index) of audioInputs" :key="index" :value="item.deviceId"> |
| 52 | + {{ item.label }} |
| 53 | + </option> |
| 54 | + </select> |
| 55 | + </div> |
| 56 | + <div space-x-2> |
| 57 | + <button @click="handleStart"> |
| 58 | + Start |
| 59 | + </button> |
| 60 | + <button @click="handleCancel"> |
| 61 | + Cancel |
| 62 | + </button> |
| 63 | + <button @click="handleStop"> |
| 64 | + Stop |
| 65 | + </button> |
| 66 | + </div> |
| 67 | + <div> |
| 68 | + <audio v-for="(url, index) in recordedUrls" :key="index" controls> |
| 69 | + <source :src="url" type="audio/wav"> |
| 70 | + </audio> |
43 | 71 | </div> |
44 | 72 | </div> |
45 | 73 | </template> |
0 commit comments