This commit is contained in:
MomentQYC 2024-10-05 21:56:28 +08:00
parent 8d2d3617c3
commit 54d6dc3dc6
6 changed files with 40 additions and 50 deletions

View File

@ -92,6 +92,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Authorization': 'Bearer ' + instance.hfAuthKey, 'Authorization': 'Bearer ' + instance.hfAuthKey,
Accept: 'audio/flac, */*',
}, },
body: JSON.stringify({ body: JSON.stringify({
inputs: note.text, inputs: note.text,
@ -99,18 +100,14 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
timeout: 60000, timeout: 60000,
}); });
let contentType = res.headers.get('content-type') || 'application/octet-stream'; let contentType = res.headers.get('Content-Type') || 'application/octet-stream';
if (contentType === 'audio/flac') {
return res.body;
} else {
throw new ApiError(meta.errors.unavailable);
}
if (res.headers.get('content-type') === 'audio/flac') {
return {
body: res.body,
headers: {
'Content-Type': contentType,
}
};
} else {
throw new ApiError(meta.errors.unavailable);
}
}); });
} }
} }

View File

@ -623,17 +623,16 @@ function emitUpdReaction(emoji: string, delta: number) {
} }
watch(convert, (newBlob) => { watch(convert, (newBlob) => {
if (converturl.value && converturl.value.url) { try {
URL.revokeObjectURL(converturl.value.url); if (newBlob) {
} converturl.value = { url: newBlob };
} else {
if (newBlob) { converturl.value = null;
converturl.value = { url: newBlob }; }
} else { } catch (error) {
converturl.value = null; console.error('Failed to create URL:', error);
} }
}); });
console.log(converturl)
onUnmounted(() => { onUnmounted(() => {
if (converturl.value && converturl.value.url) { if (converturl.value && converturl.value.url) {

View File

@ -558,16 +558,6 @@ function loadConversation() {
} }
watch(convert, (newBlob) => { watch(convert, (newBlob) => {
/*
try {
if (converturl.value && converturl.value.url) {
URL.revokeObjectURL(converturl.value.url);
}
} catch (error) {
console.error('Failed to revoke URL:', error);
}
*/
try { try {
if (newBlob) { if (newBlob) {
converturl.value = { url: newBlob }; converturl.value = { url: newBlob };
@ -577,7 +567,6 @@ watch(convert, (newBlob) => {
} catch (error) { } catch (error) {
console.error('Failed to create URL:', error); console.error('Failed to create URL:', error);
} }
}); });
onUnmounted(() => { onUnmounted(() => {

View File

@ -302,27 +302,32 @@ export function getNoteMenu(props: {
async function convert(): Promise<void> { async function convert(): Promise<void> {
if (props.convert.value != null) return; if (props.convert.value != null) return;
props.converting.value = true; props.converting.value = true;
const res = await misskeyApi('notes/tts', { const res = await misskeyApi('notes/tts', {
noteId: appearNote.id, noteId: appearNote.id,
}, undefined, undefined, true); }, undefined, undefined, true);
const convertdata = await res.json(); try {
const contentType = convertdata.headers['Content-Type']; if (res.body instanceof ReadableStream) {
const reader = res.body.getReader();
const chunks: Uint8Array[] = [];
if (contentType?.startsWith('audio/')) { while (true) {
console.log('Buffer:', convertdata.body._readableState.buffer[0].data); const { done, value } = await reader.read();
if (done) break;
chunks.push(value);
}
const buffers = new Uint8Array(convertdata.body._readableState.buffer[0].data).buffer; const audioBlob = new Blob(chunks, { type: 'audio/flac' });
try { props.convert.value = URL.createObjectURL(audioBlob);
const blob = new Blob([buffers], { type: contentType }); } else {
props.convert.value = URL.createObjectURL(blob); console.error('Response body is not a ReadableStream');
} catch (e) {
console.error('Failed to create Blob or Object URL:', e);
} }
} else { } catch (e) {
console.error('API did not return audio data.'); console.error('Failed to create Blob or Object URL:', e);
} }
props.converting.value = false; props.converting.value = false;
} }
@ -382,7 +387,7 @@ export function getNoteMenu(props: {
if ($i.policies.canUseTTS && instance.ttsAvailable) { if ($i.policies.canUseTTS && instance.ttsAvailable) {
menuItems.push({ menuItems.push({
icon: 'ti ti-headphone', icon: 'ti ti-headphones',
text: 'TTS', text: 'TTS',
action: convert, action: convert,
}); });

View File

@ -2773,7 +2773,7 @@ type NotesTranslateResponse = operations['notes___translate']['responses']['200'
type NotesTTSRequest = operations['notes___tts']['requestBody']['content']['application/json']; type NotesTTSRequest = operations['notes___tts']['requestBody']['content']['application/json'];
// @public (undocumented) // @public (undocumented)
type NotesTTSResponse = operations['notes___tts']['responses']['200']['content']['audio/flac']; type NotesTTSResponse = unknown;
// @public (undocumented) // @public (undocumented)
type NotesUnrenoteRequest = operations['notes___unrenote']['requestBody']['content']['application/json']; type NotesUnrenoteRequest = operations['notes___unrenote']['requestBody']['content']['application/json'];

View File

@ -452,7 +452,7 @@ export type NotesTimelineResponse = operations['notes___timeline']['responses'][
export type NotesTranslateRequest = operations['notes___translate']['requestBody']['content']['application/json']; export type NotesTranslateRequest = operations['notes___translate']['requestBody']['content']['application/json'];
export type NotesTranslateResponse = operations['notes___translate']['responses']['200']['content']['application/json']; export type NotesTranslateResponse = operations['notes___translate']['responses']['200']['content']['application/json'];
export type NotesTTSRequest = operations['notes___tts']['requestBody']['content']['application/json']; export type NotesTTSRequest = operations['notes___tts']['requestBody']['content']['application/json'];
export type NotesTTSResponse = operations['notes___tts']['responses']['200']['content']['audio/flac']; export type NotesTTSResponse = unknown;
export type NotesUnrenoteRequest = operations['notes___unrenote']['requestBody']['content']['application/json']; export type NotesUnrenoteRequest = operations['notes___unrenote']['requestBody']['content']['application/json'];
export type NotesUserListTimelineRequest = operations['notes___user-list-timeline']['requestBody']['content']['application/json']; export type NotesUserListTimelineRequest = operations['notes___user-list-timeline']['requestBody']['content']['application/json'];
export type NotesUserListTimelineResponse = operations['notes___user-list-timeline']['responses']['200']['content']['application/json']; export type NotesUserListTimelineResponse = operations['notes___user-list-timeline']['responses']['200']['content']['application/json'];