使用AI生成背景音乐

2024-03-13 12:30:40 浏览数 (1)

生成背景音乐可以使用Meta开源的AudioCraft项目。

开源项目地址:https://github.com/facebookresearch/audiocraft

输入:prompt,描述音乐的风格。

输出:音乐文件目录

生成音乐代码示例

简单测试生成音乐,代码如下,可能消耗比较多的GPU内存。

Prompt示例:There are obvious heavy drums and create a hot and enthusiastic atmosphere.

代码语言:javascript复制
from audiocraft.models import MusicGen
from audiocraft.data.audio import audio_write
model = MusicGen.get_pretrained('melody')
model.set_generation_params(duration=48)  # generate 8 seconds.
descriptions = ['There are obvious heavy drums and create a hot and enthusiastic atmosphere']
wav = model.generate(descriptions)  # generates 3 samples.
for idx, one_wav in enumerate(wav):
    # Will save under {idx}.wav, with loudness normalization at -14 db LUFS.
    audio_write(f'{idx}', one_wav.cpu(), model.sample_rate, strategy="loudness", loudness_compressor=True)

若需要封装成服务提供API接口,可以参考如下。

API接口代码

使用了Queue队列,考虑到资源,目前是串行的队列,后续请求堵塞,在请求结束时,才从队列中取出当前任务,表示当前任务完成。

代码语言:javascript复制
import os
import time
import logging
import torch
from datetime import datetime
from fastapi import FastAPI
from audiocraft.models import MusicGen
from audiocraft.data.audio import audio_write
from queue import Queue


logging.basicConfig(
    format='%(asctime)s - %(levelname)s - %(lineno)d: %(message)s',
    filename='music_gen_service.log',
    encoding='utf-8',
    level=logging.DEBUG)

app = FastAPI()
request_queue = Queue(maxsize=10)  # 设置队列最大容量为10

output_dir = "../data/music_gen"

@app.get('/api/music_gen')
def generate(description: str, duration: int):
    logging.info(f"music generate start, duration: {duration}, description: {description}")
    try:
        request_queue.put(1, block=True, timeout=None)  # 将请求放入队列,如果队列已满则阻塞
        logging.info(f"music generate start, duration: {duration}, description: {description}")
        now = datetime.now()
        today = "/"   now.strftime("%Y%m%d")   "/"
        timestamp = str(int(time.time() * 1000))

        description_list = []
        description_list.append(description)
        model = MusicGen.get_pretrained('small')
        model.set_generation_params(duration=duration)  # generate 'duration' seconds.
        wav = model.generate(description_list)  # generates samples based on descriptions
        for one_wav in wav:
            # 产出的音乐文件存储于../data/music_gen/{YYYYMMDD}/{timestamp}.wav中
            audio_write(os.path.join(output_dir today, timestamp), one_wav.cpu(), model.sample_rate, strategy="loudness", loudness_compressor=True)
        torch.cuda.empty_cache()
    except Exception as ee:
        logging.error(f"music generate failed, msg: {ee}, duration: {duration}, description: {description}")
        return {"code": 400, "msg": str(ee)}
    finally:
        request_queue.get()  # 处理完成后从队列中取出请求

    logging.info(f"music generate success, duration: {duration}, description: {description}")
    return {"code": 0, "msg": "Success", "data": {"path": os.path.abspath(output_dir)   today   timestamp   ".wav"}}

0 人点赞