UNPKG

n8n-nodes-piapi

Version:

Community n8n nodes for PiAPI - integrate generative AI capabilities (image, video, audio, 3D) into your workflows

214 lines 8.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TextToSpeech = void 0; const n8n_workflow_1 = require("n8n-workflow"); const GenericFunctions_1 = require("../shared/GenericFunctions"); class TextToSpeech { constructor() { this.description = { displayName: 'PiAPI Text to Speech', name: 'textToSpeech', icon: 'file:../piapi.svg', group: ['transform'], version: 1, description: 'Generate speech audio from text using PiAPI TTS', defaults: { name: 'Text to Speech', }, inputs: ["main"], outputs: ["main"], credentials: [ { name: 'piAPIApi', required: true, }, ], properties: [ { displayName: 'Text', name: 'text', type: 'string', typeOptions: { rows: 4, }, default: '', required: true, description: 'Text to be converted to speech', }, { displayName: 'Reference Audio Input Method', name: 'refAudioInputMethod', type: 'options', options: [ { name: 'URL', value: 'url', }, { name: 'Binary Data', value: 'binaryData', }, ], default: 'url', description: 'Method to input the reference audio data', }, { displayName: 'Reference Audio Binary Property', name: 'refAudioBinaryPropertyName', type: 'string', default: 'data', required: true, displayOptions: { show: { refAudioInputMethod: ['binaryData'], }, }, description: 'Name of the binary property containing the reference audio data', }, { displayName: 'Reference Audio URL', name: 'refAudioUrl', type: 'string', default: '', required: true, displayOptions: { show: { refAudioInputMethod: ['url'], }, }, description: 'URL of the reference audio for voice cloning', }, { displayName: 'Include Reference Text', name: 'includeRefText', type: 'boolean', default: false, description: 'Whether to include text corresponding to the reference audio', }, { displayName: 'Reference Text', name: 'refText', type: 'string', typeOptions: { rows: 2, }, default: '', displayOptions: { show: { includeRefText: [true], }, }, description: 'Text corresponding to the reference audio (can improve voice cloning quality)', }, { displayName: 'Wait For Completion', name: 'waitForCompletion', type: 'boolean', default: false, description: 'Whether to wait for the speech generation process to complete before continuing', }, { displayName: 'Max Retries', name: 'maxRetries', type: 'number', default: 20, description: 'Maximum number of retries to check task status', displayOptions: { show: { waitForCompletion: [true], }, }, }, { displayName: 'Retry Interval', name: 'retryInterval', type: 'number', default: 3000, description: 'Interval between retries in milliseconds', displayOptions: { show: { waitForCompletion: [true], }, }, }, ], }; } async execute() { var _a, _b; const items = this.getInputData(); const returnData = []; for (let i = 0; i < items.length; i++) { try { const text = this.getNodeParameter('text', i); const refAudioInputMethod = this.getNodeParameter('refAudioInputMethod', i); const includeRefText = this.getNodeParameter('includeRefText', i, false); const waitForCompletion = this.getNodeParameter('waitForCompletion', i, false); let refAudioData; if (refAudioInputMethod === 'url') { refAudioData = this.getNodeParameter('refAudioUrl', i); } else { const refAudioBinaryPropertyName = this.getNodeParameter('refAudioBinaryPropertyName', i); const refAudioBinaryData = this.helpers.assertBinaryData(i, refAudioBinaryPropertyName); if (refAudioBinaryData.mimeType && !refAudioBinaryData.mimeType.includes('audio')) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'The provided binary data is not an audio file', { itemIndex: i }); } const base64String = Buffer.from(await this.helpers.getBinaryDataBuffer(i, refAudioBinaryPropertyName)).toString('base64'); refAudioData = `data:${refAudioBinaryData.mimeType};base64,${base64String}`; } const requestBody = { model: 'Qubico/tts', task_type: 'zero-shot', input: { gen_text: text, ref_audio: refAudioData, }, config: { service_mode: 'public', }, }; if (includeRefText) { const refText = this.getNodeParameter('refText', i, ''); if (refText) { requestBody.input.ref_text = refText; } } const response = await GenericFunctions_1.piApiRequest.call(this, 'POST', '/api/v1/task', requestBody); const taskId = (_a = response.data) === null || _a === void 0 ? void 0 : _a.task_id; if (!taskId) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Failed to get a valid task ID from the API'); } let executionData; if (waitForCompletion) { const maxRetries = this.getNodeParameter('maxRetries', i, 20); const retryInterval = this.getNodeParameter('retryInterval', i, 3000); executionData = await GenericFunctions_1.waitForTaskCompletion.call(this, taskId, maxRetries, retryInterval); } else { executionData = { task_id: taskId, status: ((_b = response.data) === null || _b === void 0 ? void 0 : _b.status) || 'pending', }; } returnData.push({ json: executionData, }); } catch (error) { if (this.continueOnFail()) { returnData.push({ json: { error: error.message, }, }); continue; } throw error; } } return [returnData]; } } exports.TextToSpeech = TextToSpeech; //# sourceMappingURL=TextToSpeech.node.js.map