diff --git a/src/client.ts b/src/client.ts index df996c4..658e0af 100644 --- a/src/client.ts +++ b/src/client.ts @@ -1,6 +1,6 @@ import { RestAPI } from "./rest"; import { audioQuery } from "./audio_query"; -import { preset } from "./preset"; +import { Preset } from "./preset"; // voicevox client /** @@ -70,8 +70,37 @@ export class Client { } // Fetch presets - async fetchPresets(): Promise { + /** + * @returns Presets + */ + async fetchPresets(): Promise { let presets = await this.rest.getPresets(); - return presets.map((x) => new preset(x)); + return presets.map((x) => new Preset(x)); + } + + // Add preset + /** + * @param preset - Preset + * @returns Preset ID + */ + async addPreset(preset: Preset): Promise { + return await this.rest.addPreset({ ...preset }); + } + + // Update preset + /** + * @param preset - Preset + * @returns Preset ID + */ + async updatePreset(preset: Preset): Promise { + return await this.rest.updatePreset({ ...preset }); + } + + // Delete preset + /** + * @param id - Preset ID + */ + async deletePreset(id: number): Promise { + return await this.rest.deletePreset(id); } } diff --git a/src/preset.ts b/src/preset.ts index fdea368..6212348 100644 --- a/src/preset.ts +++ b/src/preset.ts @@ -1,7 +1,7 @@ -import { preset as presetT } from "./types/preset"; +import { Preset as PresetT } from "./types/preset"; // preset -export class preset { +export class Preset { public id: number; public name: string; public speaker_uuid: string; @@ -13,7 +13,7 @@ export class preset { public prePhonemeLength: number; public postPhonemeLength: number; - constructor(preset: presetT) { + constructor(preset: PresetT) { this.id = preset.id; this.name = preset.name; this.speaker_uuid = preset.speaker_uuid; diff --git a/src/rest.ts b/src/rest.ts index b2f14bc..a10de78 100644 --- a/src/rest.ts +++ b/src/rest.ts @@ -3,7 +3,7 @@ import { createAudioQueryOptions, createAudioQueryFromPresetOptions, } from "./types/audioquery"; -import { preset } from "./types/preset"; +import { Preset, DeletePresetOptions } from "./types/preset"; import { synthesisParams } from "./types/synthesis"; type fetchOptions = { @@ -94,7 +94,28 @@ export class RestAPI { }); } - async getPresets(): Promise { + async getPresets(): Promise { return await this.request("GET", "/presets"); } + + async addPreset(preset: Preset): Promise { + return await this.request("POST", "/add_preset", { + body: preset, + }); + } + + async updatePreset(preset: Preset): Promise { + return await this.request("POST", "/update_preset", { + body: preset, + }); + } + + async deletePreset(id: number): Promise { + let params: DeletePresetOptions = { + id: id, + }; + return await this.request("POST", "/delete_preset", { + params: params, + }); + } } diff --git a/src/types/preset.ts b/src/types/preset.ts index 9501a46..a91898d 100644 --- a/src/types/preset.ts +++ b/src/types/preset.ts @@ -1,4 +1,4 @@ -export interface preset { +export interface Preset { id: number; name: string; speaker_uuid: string; @@ -10,3 +10,7 @@ export interface preset { prePhonemeLength: number; postPhonemeLength: number; } + +export interface DeletePresetOptions { + id: number; +} diff --git a/tests/preset.ts b/tests/preset.ts new file mode 100644 index 0000000..0f9c514 --- /dev/null +++ b/tests/preset.ts @@ -0,0 +1,33 @@ +import assert from "assert"; +import Client from "../dist"; +import { Preset } from "../dist/preset"; + +const client = new Client("http://127.0.0.1:50021"); + +async function main() { + const presets = await client.fetchPresets(); + const preset = new Preset({ + id: 1000, + name: "string", + speaker_uuid: "string", + style_id: 0, + speedScale: 0, + pitchScale: 0, + intonationScale: 0, + volumeScale: 0, + prePhonemeLength: 0, + postPhonemeLength: 0, + }); + + preset.id = await client.addPreset(preset); + assert(presets.length + 1 === (await client.fetchPresets()).length); + + preset.speedScale = 2; + await client.updatePreset(preset); + assert(presets.length + 1 === (await client.fetchPresets()).length); + + await client.deletePreset(preset.id); + assert(presets.length === (await client.fetchPresets()).length); +} + +main();