Skip to content

Commit

Permalink
Merge pull request #50 from ryunix/add-preset-related-methods
Browse files Browse the repository at this point in the history
Add preset related methods
  • Loading branch information
mc_fdc authored Sep 19, 2023
2 parents 84c580a + 523c94b commit 45aa1bc
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 9 deletions.
35 changes: 32 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RestAPI } from "./rest";
import { audioQuery } from "./audio_query";
import { preset } from "./preset";
import { Preset } from "./preset";

// voicevox client
/**
Expand Down Expand Up @@ -70,8 +70,37 @@ export class Client {
}

// Fetch presets
async fetchPresets(): Promise<preset[]> {
/**
* @returns Presets
*/
async fetchPresets(): Promise<Preset[]> {
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<number> {
return await this.rest.addPreset({ ...preset });
}

// Update preset
/**
* @param preset - Preset
* @returns Preset ID
*/
async updatePreset(preset: Preset): Promise<number> {
return await this.rest.updatePreset({ ...preset });
}

// Delete preset
/**
* @param id - Preset ID
*/
async deletePreset(id: number): Promise<void> {
return await this.rest.deletePreset(id);
}
}
6 changes: 3 additions & 3 deletions src/preset.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down
25 changes: 23 additions & 2 deletions src/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -94,7 +94,28 @@ export class RestAPI {
});
}

async getPresets(): Promise<preset[]> {
async getPresets(): Promise<Preset[]> {
return await this.request("GET", "/presets");
}

async addPreset(preset: Preset): Promise<number> {
return await this.request("POST", "/add_preset", {
body: preset,
});
}

async updatePreset(preset: Preset): Promise<number> {
return await this.request("POST", "/update_preset", {
body: preset,
});
}

async deletePreset(id: number): Promise<void> {
let params: DeletePresetOptions = {
id: id,
};
return await this.request("POST", "/delete_preset", {
params: params,
});
}
}
6 changes: 5 additions & 1 deletion src/types/preset.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface preset {
export interface Preset {
id: number;
name: string;
speaker_uuid: string;
Expand All @@ -10,3 +10,7 @@ export interface preset {
prePhonemeLength: number;
postPhonemeLength: number;
}

export interface DeletePresetOptions {
id: number;
}
33 changes: 33 additions & 0 deletions tests/preset.ts
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit 45aa1bc

Please sign in to comment.