Skip to content

Commit 1c50d9a

Browse files
authored
feat: add generate function to ai voice generator (#125)
1 parent 8b8c069 commit 1c50d9a

File tree

8 files changed

+116
-4
lines changed

8 files changed

+116
-4
lines changed

.sdk.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"id": "31df0519-cea5-43da-929f-50f1bfbc429b",
2+
"id": "b4a720d1-545c-494f-a7a5-df7d60093894",
33
"tracked_paths": [
44
{
55
"editable": true,

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ Valid values are: `none`, `error`, `warn`, `info`, `debug` (case insensitive). I
260260
### [v1.aiVoiceGenerator](src/resources/v1/ai-voice-generator/README.md)
261261

262262
* [create](src/resources/v1/ai-voice-generator/README.md#create) - AI Voice Generator
263+
* [generate](src/resources/v1/ai-voice-generator/README.md#generate) - AI Voice Generator Generate Workflow
263264

264265
### [v1.animation](src/resources/v1/animation/README.md)
265266

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "magic-hour",
3-
"version": "0.41.0",
3+
"version": "0.42.0",
44
"main": "./dist/index.js",
55
"types": "./dist/index.d.ts",
66
"exports": {

src/resources/v1/ai-voice-generator/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,49 @@
22

33
## Module Functions
44

5+
<!-- CUSTOM DOCS START -->
6+
### AI Voice Generate Workflow <a name="generate"></a>
7+
8+
The workflow performs the following action
9+
10+
1. upload local assets to Magic Hour storage. So you can pass in a local path instead of having to upload files yourself
11+
2. trigger a generation
12+
3. poll for a completion status. This is configurable
13+
4. if success, download the output to local directory
14+
15+
> [!TIP]
16+
> This is the recommended way to use the SDK unless you have specific needs where it is necessary to split up the actions.
17+
18+
#### Parameters
19+
20+
In addition to the parameters listed in the `create` section below, `generate` introduces 3 new parameters:
21+
22+
- `waitForCompletion` (boolean, default true): Whether to wait for the project to complete.
23+
- `downloadOutputs` (boolean, default true): Whether to download the generated files
24+
- `downloadDirectory` (string, optional): Directory to save downloaded files (defaults to current directory)
25+
26+
#### Example Snippet
27+
28+
```typescript
29+
import Client from "magic-hour";
30+
31+
const client = new Client({ token: process.env["API_TOKEN"]!! });
32+
const res = await client.v1.aiVoiceGenerator.generate(
33+
{
34+
name: "Voice Generator audio",
35+
style: { prompt: "Hello, how are you?", voiceName: "Elon Musk" },
36+
},
37+
{
38+
waitForCompletion: true,
39+
downloadOutputs: true,
40+
downloadDirectory: "outputs",
41+
},
42+
);
43+
44+
```
45+
46+
<!-- CUSTOM DOCS END -->
47+
548
### AI Voice Generator <a name="create"></a>
649

750
Generate speech from text. Each character costs 0.05 credits. The cost is rounded up to the nearest whole number.

src/resources/v1/ai-voice-generator/resource-client.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,82 @@ import {
66
ResourceClientOptions,
77
} from "make-api-request-js";
88

9+
import {
10+
GenerateOptions,
11+
GenerateRequestType,
12+
} from "magic-hour/helpers/generate-type";
13+
import { getLogger } from "magic-hour/logger";
914
import * as requests from "magic-hour/resources/v1/ai-voice-generator/request-types";
15+
import { ImageProjectsClient } from "magic-hour/resources/v1/image-projects";
1016
import * as types from "magic-hour/types";
1117
import { Schemas$V1AiVoiceGeneratorCreateBody } from "magic-hour/types/v1-ai-voice-generator-create-body";
1218
import { Schemas$V1AiVoiceGeneratorCreateResponse } from "magic-hour/types/v1-ai-voice-generator-create-response";
1319

20+
type GenerateRequest = GenerateRequestType<requests.CreateRequest, {}>;
21+
1422
export class AiVoiceGeneratorClient extends CoreResourceClient {
1523
constructor(coreClient: CoreClient, opts: ResourceClientOptions) {
1624
super(coreClient, opts);
1725
}
1826

27+
/**
28+
* AI Voice Generator
29+
*
30+
* Generate speech from text
31+
*
32+
* This method provides a convenient way to create a request and automatically wait for completion and download outputs.
33+
*
34+
* @example
35+
* ```typescript
36+
* import Client from "magic-hour";
37+
*
38+
* const client = new Client({ token: process.env["API_TOKEN"]!! });
39+
* const res = await client.v1.aiVoiceGenerator.generate(
40+
* {
41+
* name: "Voice Generator audio",
42+
* style: { prompt: "Hello, how are you?", voiceName: "Elon Musk" },
43+
* },
44+
* {
45+
* waitForCompletion: true,
46+
* downloadOutputs: true,
47+
* downloadDirectory: "outputs",
48+
* },
49+
* );
50+
* ```
51+
*/
52+
async generate(request: GenerateRequest, opts: GenerateOptions = {}) {
53+
const {
54+
waitForCompletion = true,
55+
downloadOutputs = true,
56+
downloadDirectory = undefined,
57+
...createOpts
58+
} = opts;
59+
60+
const createResponse = await this.create(request, createOpts);
61+
62+
getLogger().info(
63+
`Created AiVoiceGeneratorClient project ${createResponse.id}`,
64+
);
65+
66+
const projectsClient = new ImageProjectsClient(this._client, this._opts);
67+
68+
getLogger().debug(
69+
`Checking result for AiVoiceGeneratorClient project ${createResponse.id}`,
70+
);
71+
72+
const result = await projectsClient.checkResult(
73+
{ id: createResponse.id },
74+
{
75+
waitForCompletion,
76+
downloadOutputs,
77+
downloadDirectory,
78+
...createOpts,
79+
},
80+
);
81+
82+
return result;
83+
}
84+
1985
/**
2086
* AI Voice Generator
2187
*

src/resources/v1/animation/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55

66

7+
78
<!-- CUSTOM DOCS START -->
89
### Animation Generate Workflow <a name="generate"></a>
910

src/resources/v1/audio-projects/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Module Functions
44

55

6+
67
<!-- CUSTOM DOCS START -->
78

89
### Check results <a name="check-result"></a>
@@ -60,7 +61,7 @@ const res = await client.v1.audioProjects.delete({ id: "cuid-example" });
6061

6162
### Get audio details <a name="get"></a>
6263

63-
Get the details of an audio project. The `downloads` field will be empty unless the audio was successfully rendered.
64+
Get the details of a audio project. The `downloads` field will be empty unless the audio was successfully rendered.
6465

6566
The audio can be one of the following status
6667
- `draft` - not currently used

src/resources/v1/audio-projects/resource-client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export class AudioProjectsClient extends CoreResourceClient {
142142
/**
143143
* Get audio details
144144
*
145-
* Get the details of an audio project. The `downloads` field will be empty unless the audio was successfully rendered.
145+
* Get the details of a audio project. The `downloads` field will be empty unless the audio was successfully rendered.
146146
*
147147
* The audio can be one of the following status
148148
* - `draft` - not currently used

0 commit comments

Comments
 (0)