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

View File

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

View File

@ -558,16 +558,6 @@ function loadConversation() {
}
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 {
if (newBlob) {
converturl.value = { url: newBlob };
@ -577,7 +567,6 @@ watch(convert, (newBlob) => {
} catch (error) {
console.error('Failed to create URL:', error);
}
});
onUnmounted(() => {

View File

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

View File

@ -2773,7 +2773,7 @@ type NotesTranslateResponse = operations['notes___translate']['responses']['200'
type NotesTTSRequest = operations['notes___tts']['requestBody']['content']['application/json'];
// @public (undocumented)
type NotesTTSResponse = operations['notes___tts']['responses']['200']['content']['audio/flac'];
type NotesTTSResponse = unknown;
// @public (undocumented)
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 NotesTranslateResponse = operations['notes___translate']['responses']['200']['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 NotesUserListTimelineRequest = operations['notes___user-list-timeline']['requestBody']['content']['application/json'];
export type NotesUserListTimelineResponse = operations['notes___user-list-timeline']['responses']['200']['content']['application/json'];