Skip to content

Commit

Permalink
update accompaniment
Browse files Browse the repository at this point in the history
  • Loading branch information
ldzhangyx committed Jun 28, 2023
1 parent e240338 commit 45d4156
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
35 changes: 35 additions & 0 deletions melodytalk/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def __init__(self, device):
@prompts(
name="Generate music from user input text with melody or track condition",
description="useful if you want to generate, style transfer or remix music from a user input text with a given melody or track condition."
"Unlike Accompaniment, this tool will also re-generate the given melody."
"like: remix the given melody with text description, or doing style transfer as text described with the given melody."
"The input to this tool should be a comma separated string of two, "
"representing the music_filename and the text description."
Expand Down Expand Up @@ -154,4 +155,38 @@ def inference(self, inputs):
audio_write(updated_music_filename[:-4],
wav.cpu(), sr_1, strategy="loudness", loudness_compressor=True)
print(f"\nProcessed TracksMixing, Output Music: {updated_music_filename}.")
return updated_music_filename


class Accompaniment(object):
template_model = True
def __init__(self, device, Text2MusicWithMelody, ExtractTrack, SimpleTracksMixing):
print("Initializing Accompaniment")
self.device = device
self.Text2MusicWithMelody = Text2MusicWithMelody
self.ExtractTrack = ExtractTrack
self.SimpleTracksMixing = SimpleTracksMixing

@prompts(
name="Generate accompaniment music from user input text, keeping the given melody or track",
description="useful if you want to style transfer or remix music from a user input text with a given melody."
"Unlike Text2MusicWithMelody, this tool will keep the given melody track instead of re-generate it."
"Note that the user must assign a track (it must be one of `vocals`, `drums`, `bass`, `guitar`, `piano` or `other`) to keep."
"like: keep the guitar track and remix the given music with text description, "
"or generate accompaniment as text described with the given vocal track."
"The input to this tool should be a comma separated string of three, "
"representing the music_filename, track name, and the text description."
)

def inference(self, inputs):
music_filename, track_name, text = inputs.split(",")[0].strip(), inputs.split(",")[1].strip(), inputs.split(",")[2].strip()
print(f"Generating music from text with accompaniment condition, Input Text: {text}, Previous music: {music_filename}, Track: {track_name}.")
# separate the track
updated_main_track = self.ExtractTrack.inference(f"{music_filename}, {track_name}, extract")
# generate music
updated_new_music = self.Text2MusicWithMelody.inference(f"{text}, {updated_main_track}")
# remove the track in accompaniment
updated_accompaniment = self.ExtractTrack.inference(f"{updated_new_music}, {track_name}, remove")
# mix
updated_music_filename = self.SimpleTracksMixing.inference(f"{updated_main_track}, {updated_accompaniment}")
return updated_music_filename
13 changes: 8 additions & 5 deletions melodytalk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import numpy as np
import os
import openai
import typing as tp

openai.api_key = os.getenv("OPENAI_API_KEY")

Expand Down Expand Up @@ -38,7 +39,7 @@ def decorator(func):

return decorator

def get_new_audio_name(org_audio_name, func_name="update"):
def get_new_audio_name(org_audio_name: str, func_name: str ="update") -> str:
head_tail = os.path.split(org_audio_name)
head = head_tail[0]
tail = head_tail[1]
Expand Down Expand Up @@ -86,7 +87,7 @@ def description_to_attributes(description: str) -> str:
return response.choices[0].text


def chord_generation(description: str, chord_num: int = 4) -> str:
def chord_generation(description: str, chord_num: int = 4) -> tp.List:
""" This function is a trick to generate chord sequence from the description.
:param description:
Expand All @@ -97,7 +98,7 @@ def chord_generation(description: str, chord_num: int = 4) -> str:
openai_prompt = f"""Please generate a chord sequence consists of according to the text description. Example:
Q: Generate a chord sequence for sad pop song. 4 chords.
A: Dm - Bb - F - C
A: Dm - Bb - F - C
Q: {description}. {chord_num} chords.
A:
Expand All @@ -106,12 +107,14 @@ def chord_generation(description: str, chord_num: int = 4) -> str:
response = openai.Completion.create(
model="text-davinci-003",
prompt=openai_prompt,
temperature=0,
temperature=1,
max_tokens=100,
top_p=1,
frequency_penalty=0.0,
presence_penalty=0.0,
stop=["\n"]
)

return response.choices[0].text
chord_list = [i.strip() for i in response.choices[0].text.split('-')]

return chord_list

0 comments on commit 45d4156

Please sign in to comment.