File size: 2,901 Bytes
0176e5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1cef24b
0176e5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1cef24b
 
 
 
 
 
 
 
0176e5b
 
 
 
 
 
 
379517b
 
1cef24b
 
 
0176e5b
 
1cef24b
0176e5b
 
 
 
 
1cef24b
0176e5b
1cef24b
 
 
 
b66c635
0176e5b
1cef24b
 
 
 
 
 
 
b66c635
 
 
 
 
 
 
 
 
 
 
0176e5b
 
 
 
1cef24b
0176e5b
 
 
1cef24b
 
0176e5b
 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

import {
  ClapProject,
  ClapSegment,
  getClapAssetSourceType,
  filterSegments,
  ClapSegmentFilteringMode,
  ClapSegmentCategory,
  newSegment
} from "@aitube/clap"
import { ClapCompletionMode } from "@aitube/client"
import { getSpeechBackgroundAudioPrompt } from "@aitube/engine"

import { generateSpeechWithParlerTTS } from "@/app/api/generators/speech/generateVoiceWithParlerTTS"
import { getMediaInfo } from "@/app/api/utils/getMediaInfo"
import { generateMusicWithMusicgen } from "@/app/api/v1/edit/music/generateMusicWithMusicgen"

export async function generateMusic({
  musicSegment,
  existingClap,
  newerClap,
  mode,
  turbo,
}: {
  musicSegment?: ClapSegment
  existingClap: ClapProject
  newerClap: ClapProject
  mode: ClapCompletionMode
  turbo: boolean
}): Promise<void> {
  if (!musicSegment) {
    console.log(`generateMusic(): music segment is empty, so skipping music generation.`)
    return
  }

    // for now we do something very basic

  if (musicSegment.status === "completed") {
    console.log(`generateMusic(): music segment is already generated, skipping doing it twice.`)
    return
  }
  
  // for now we do something very basic
  const prompt = musicSegment.prompt
  if (!prompt) {
    console.log(`generateMusic(): music prompt is empty, so skipping music generation.`)
    return
  }


  const durationInSec = 12 // musicSegment.assetDurationInMs / 1000

  console.log(`generateMusic(): generating a music with:\n  duration: ${durationInSec} sec\n  prompt: ${prompt}`)

  const assetUrl = await generateMusicWithMusicgen({
    prompt,
    durationInSec,
    hd: false,
    debug: true,
    neverThrow: true,
  })


  if (!assetUrl || assetUrl?.length < 30) {
    console.log(`generateMusic(): the generated assetUrl is empty, so music generation failed.`)
    return
  }

  let { durationInMs, hasAudio } = await getMediaInfo(assetUrl)

  const newProperties: Partial<ClapSegment> = {
    assetUrl,
    assetDurationInMs: durationInMs,
    outputGain: 1.0,
    status: "completed"
  }


  if (!hasAudio) {
    console.warn(`generateMusic(): the generated music waveform appears to be silent (might be a ffprobe malfunction)`)
    // return
    // we have a bug on AiTube, basically the ffmpeg probe isn't working,
    // because it doesn't find ffmpeg
    // if think the issue is how the Dockerfile is formed
    // so until this is fixed, we need to fake a "correct" result
    newProperties.assetDurationInMs = musicSegment.assetDurationInMs
  }

  if (mode !== ClapCompletionMode.FULL) {
    console.log(`generateMusic(): adding music to a new clap file`)
    newerClap.segments.push(newSegment({
      ...musicSegment,
      ...newProperties,
    }))
  } else {
    console.log(`generateMusic(): overwriting the music inside the existing clap file`)
    // this will update the existing clap (normally)
    Object.assign(musicSegment, newProperties)
  }
}