From 18c8d4a3f1f0d97e794e8a0393dadb4218c1c6ef Mon Sep 17 00:00:00 2001 From: ryunix Date: Tue, 19 Sep 2023 15:27:28 +0900 Subject: [PATCH 1/2] Change preset to Preset --- src/client.ts | 6 +++--- src/preset.ts | 6 +++--- src/rest.ts | 4 ++-- src/types/preset.ts | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/client.ts b/src/client.ts index df996c4..5aa4f5e 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,8 @@ export class Client { } // Fetch presets - async fetchPresets(): Promise { + async fetchPresets(): Promise { let presets = await this.rest.getPresets(); - return presets.map((x) => new preset(x)); + return presets.map((x) => new Preset(x)); } } 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..b6c3ad4 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 } from "./types/preset"; import { synthesisParams } from "./types/synthesis"; type fetchOptions = { @@ -94,7 +94,7 @@ export class RestAPI { }); } - async getPresets(): Promise { + async getPresets(): Promise { return await this.request("GET", "/presets"); } } diff --git a/src/types/preset.ts b/src/types/preset.ts index 9501a46..46cc5a8 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; From 523c94b9cb7934590c0ae6eeba0fba49519470fd Mon Sep 17 00:00:00 2001 From: ryunix Date: Tue, 19 Sep 2023 16:58:48 +0900 Subject: [PATCH 2/2] Add preset related methods --- src/client.ts | 29 +++++++++++++++++++++++++++++ src/rest.ts | 23 ++++++++++++++++++++++- src/types/preset.ts | 4 ++++ tests/preset.ts | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 tests/preset.ts diff --git a/src/client.ts b/src/client.ts index 5aa4f5e..658e0af 100644 --- a/src/client.ts +++ b/src/client.ts @@ -70,8 +70,37 @@ export class Client { } // Fetch presets + /** + * @returns Presets + */ async fetchPresets(): Promise { let presets = await this.rest.getPresets(); 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/rest.ts b/src/rest.ts index b6c3ad4..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 = { @@ -97,4 +97,25 @@ export class RestAPI { 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 46cc5a8..a91898d 100644 --- a/src/types/preset.ts +++ b/src/types/preset.ts @@ -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();