File size: 2,078 Bytes
f42b4a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { v4 as uuidv4 } from "uuid"

import { generateSeed } from "../utils/generateSeed"
import { ClapSegment } from "./types"
import { isValidNumber } from "../utils/isValidNumber"

export function newSegment(maybeSegment?: Partial<ClapSegment>) {

  const startTimeInMs =
    isValidNumber(maybeSegment?.startTimeInMs)
    ? (maybeSegment?.startTimeInMs || 0)
    : 0

  const assetDurationInMs =
    isValidNumber(maybeSegment?.assetDurationInMs)
    ? (maybeSegment?.assetDurationInMs || 0)
    : 1000

  const endTimeInMs =
    isValidNumber(maybeSegment?.endTimeInMs)
    ? (maybeSegment?.endTimeInMs || 0)
    : (startTimeInMs + assetDurationInMs)

  const segment: ClapSegment = {
    id: typeof maybeSegment?.id === "string" ? maybeSegment.id : uuidv4(),
    track: isValidNumber(maybeSegment?.track) ? (maybeSegment?.track || 0) : 0,
    startTimeInMs,
    endTimeInMs,
    category: typeof maybeSegment?.category === "string" ? maybeSegment.category : "generic",
    modelId: typeof maybeSegment?.modelId === "string" ? maybeSegment.modelId : "",
    sceneId: typeof maybeSegment?.sceneId === "string" ? maybeSegment.sceneId : "",
    prompt: typeof maybeSegment?.prompt === "string" ? maybeSegment.prompt : "",
    label: typeof maybeSegment?.label === "string" ? maybeSegment.label : "",
    outputType: typeof maybeSegment?.outputType === "string" ? maybeSegment.outputType : "text",
    renderId: typeof maybeSegment?.renderId === "string" ? maybeSegment.renderId : "",
    status: typeof maybeSegment?.status === "string" ? maybeSegment.status : "to_generate",
    assetUrl: typeof maybeSegment?.assetUrl === "string" ? maybeSegment.assetUrl : "",
    assetDurationInMs,
    createdBy: typeof maybeSegment?.createdBy === "string" ?  maybeSegment.createdBy : "ai",
    editedBy: typeof maybeSegment?.editedBy === "string" ?  maybeSegment.editedBy : "ai",
    outputGain: isValidNumber(maybeSegment?.outputGain) ? (maybeSegment?.outputGain || 0) : 0,
    seed: isValidNumber(maybeSegment?.seed) ? (maybeSegment?.seed || 0) : generateSeed()
  }

  return segment
}