{"id":21666,"date":"2025-02-21T12:19:04","date_gmt":"2025-02-21T04:19:04","guid":{"rendered":"https:\/\/www.aisharenet.com\/?p=21666"},"modified":"2025-02-21T12:19:04","modified_gmt":"2025-02-21T04:19:04","slug":"songgen","status":"publish","type":"post","link":"https:\/\/www.kdjingpai.com\/en\/songgen\/","title":{"rendered":"SongGen\uff1a\u81ea\u52a8\u751f\u6210\u6b4c\u66f2\u7684\u5355\u9636\u6bb5\u81ea\u56de\u5f52Transformer"},"content":{"rendered":"<p>SongGen \u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u5355\u9636\u6bb5\u81ea\u56de\u5f52Transformer\u6a21\u578b\uff0c\u4e13\u4e3a\u6587\u672c\u5230\u6b4c\u66f2\u7684\u751f\u6210\u4efb\u52a1\u8bbe\u8ba1\u3002\u8be5\u6a21\u578b\u80fd\u591f\u4ece\u6587\u672c\u8f93\u5165\u751f\u6210\u5305\u542b\u4eba\u58f0\u548c\u4f34\u594f\u7684\u6b4c\u66f2\u3002SongGen \u63d0\u4f9b\u4e86\u5bf9\u591a\u79cd\u97f3\u4e50\u5c5e\u6027\u7684\u7ec6\u7c92\u5ea6\u63a7\u5236\uff0c\u5305\u62ec\u6b4c\u8bcd\u3001\u4e50\u5668\u63cf\u8ff0\u3001\u97f3\u4e50\u98ce\u683c\u3001\u60c5\u7eea\u548c\u97f3\u8272\u7b49\u3002\u6b64\u5916\uff0c\u7528\u6237\u8fd8\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528\u4e09\u79d2\u949f\u7684\u53c2\u8003\u97f3\u9891\u7247\u6bb5\u8fdb\u884c\u58f0\u97f3\u514b\u9686\u3002SongGen \u652f\u6301\u4e24\u79cd\u8f93\u51fa\u6a21\u5f0f\uff1a\u6df7\u5408\u6a21\u5f0f\u76f4\u63a5\u751f\u6210\u5305\u542b\u4eba\u58f0\u548c\u4f34\u594f\u7684\u6df7\u5408\u97f3\u8f68\uff0c\u53cc\u8f68\u6a21\u5f0f\u5219\u5206\u522b\u751f\u6210\u4eba\u58f0\u548c\u4f34\u594f\u97f3\u8f68\uff0c\u4ee5\u4fbf\u4e8e\u540e\u7eed\u5e94\u7528\u3002\u8be5\u9879\u76ee\u8fd8\u63d0\u4f9b\u4e86\u81ea\u52a8\u5316\u7684\u6570\u636e\u9884\u5904\u7406\u7ba1\u9053\u548c\u9ad8\u6548\u7684\u8d28\u91cf\u63a7\u5236\u673a\u5236\uff0c\u65e8\u5728\u4fc3\u8fdb\u793e\u533a\u53c2\u4e0e\u548c\u672a\u6765\u7814\u7a76\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-21667\" title=\"SongGen\uff1a\u81ea\u52a8\u751f\u6210\u6b4c\u66f2\u7684\u5355\u9636\u6bb5\u81ea\u56de\u5f52Transformer-1\" src=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/02\/3c1098109e89133.png\" alt=\"SongGen\uff1a\u81ea\u52a8\u751f\u6210\u6b4c\u66f2\u7684\u5355\u9636\u6bb5\u81ea\u56de\u5f52Transformer-1\" width=\"1105\" height=\"422\" srcset=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/02\/3c1098109e89133.png 1105w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/02\/3c1098109e89133-768x293.png 768w\" sizes=\"auto, (max-width: 1105px) 100vw, 1105px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h2>\u529f\u80fd\u5217\u8868<\/h2>\n<ul>\n<li>\u6587\u672c\u5230\u6b4c\u66f2\u751f\u6210<\/li>\n<li>\u652f\u6301\u6b4c\u8bcd\u3001\u4e50\u5668\u63cf\u8ff0\u3001\u97f3\u4e50\u98ce\u683c\u3001\u60c5\u7eea\u548c\u97f3\u8272\u7684\u7ec6\u7c92\u5ea6\u63a7\u5236<\/li>\n<li>\u63d0\u4f9b\u4e09\u79d2\u949f\u53c2\u8003\u97f3\u9891\u7247\u6bb5\u8fdb\u884c\u58f0\u97f3\u514b\u9686<\/li>\n<li>\u6df7\u5408\u6a21\u5f0f\u548c\u53cc\u8f68\u6a21\u5f0f\u8f93\u51fa<\/li>\n<li>\u81ea\u52a8\u5316\u6570\u636e\u9884\u5904\u7406\u7ba1\u9053<\/li>\n<li>\u5f00\u6e90\u6a21\u578b\u6743\u91cd\u3001\u8bad\u7ec3\u4ee3\u7801\u3001\u6ce8\u91ca\u6570\u636e\u548c\u5904\u7406\u7ba1\u9053<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2>\u4f7f\u7528\u5e2e\u52a9<\/h2>\n<p>\u611f\u8c22\u63d0\u4f9b\u5b98\u65b9\u7684\u5b89\u88c5\u6d41\u7a0b\u4fe1\u606f\uff01\u6211\u4f1a\u6839\u636e\u8fd9\u4e9b\u5185\u5bb9\u8fdb\u884c\u4fee\u6b63\u3002\u4ee5\u4e0b\u662f\u66f4\u65b0\u540e\u7684\u4f7f\u7528\u5e2e\u52a9\u90e8\u5206\uff1a<\/p>\n<h2>\u4f7f\u7528\u5e2e\u52a9<\/h2>\n<h3>\u5b89\u88c5\u6d41\u7a0b<\/h3>\n<ol>\n<li>\u514b\u9686\u9879\u76ee\u4ed3\u5e93\uff1a<\/li>\n<\/ol>\n<pre><code>   git clone https:\/\/github.com\/LiuZH-19\/SongGen.git\r\ncd SongGen\r\n<\/code><\/pre>\n<ol start=\"2\">\n<li>\u521b\u5efa\u5e76\u6fc0\u6d3b\u65b0\u7684 Conda \u73af\u5883\uff1a<\/li>\n<\/ol>\n<pre><code>   conda create -n songgen python=3.9.18\r\nconda activate songgen\r\n<\/code><\/pre>\n<ol start=\"3\">\n<li>\u5b89\u88c5 CUDA 11.8 \u53ca\u76f8\u5173\u4f9d\u8d56\uff1a<\/li>\n<\/ol>\n<pre><code>   conda install pytorch torchvision torchaudio cudatoolkit=11.8 -c pytorch -c nvidia\r\n<\/code><\/pre>\n<ol start=\"4\">\n<li>\u4ec5\u5728\u63a8\u7406\u6a21\u5f0f\u4e0b\u4f7f\u7528 SongGen\uff1a<\/li>\n<\/ol>\n<pre><code>   pip install .\r\n<\/code><\/pre>\n<h3>\u4e0b\u8f7d\u68c0\u67e5\u70b9<\/h3>\n<p>\u8bf7\u4e0b\u8f7d xcodec \u548c songgen \u7684\u9884\u8bad\u7ec3\u6a21\u578b\u68c0\u67e5\u70b9\u3002<\/p>\n<h3>\u8fd0\u884c\u63a8\u7406<\/h3>\n<h4>\u6df7\u5408\u6a21\u5f0f<\/h4>\n<ol>\n<li>\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\uff1a<\/li>\n<\/ol>\n<pre><code>   import torch\r\nimport os\r\nfrom songgen import (\r\nVoiceBpeTokenizer,\r\nSongGenMixedForConditionalGeneration,\r\nSongGenProcessor\r\n)\r\nimport soundfile as sf\r\n<\/code><\/pre>\n<ol start=\"2\">\n<li>\u52a0\u8f7d\u9884\u8bad\u7ec3\u6a21\u578b\uff1a<\/li>\n<\/ol>\n<pre><code>   ckpt_path = \"...\"  # \u9884\u8bad\u7ec3\u6a21\u578b\u7684\u8def\u5f84\r\ndevice = \"cuda:0\" if torch.cuda.is_available() else \"cpu\"\r\nmodel = SongGenMixedForConditionalGeneration.from_pretrained(\r\nckpt_path,\r\nattn_implementation='sdpa').to(device)\r\nprocessor = SongGenProcessor(ckpt_path, device)\r\n<\/code><\/pre>\n<ol start=\"3\">\n<li>\u5b9a\u4e49\u8f93\u5165\u6587\u672c\u548c\u6b4c\u8bcd\uff1a<\/li>\n<\/ol>\n<pre><code>   lyrics = \"...\"  # \u6b4c\u8bcd\u6587\u672c\r\ntext = \"...\"  # \u97f3\u4e50\u63cf\u8ff0\u6587\u672c\r\nref_voice_path = 'path\/to\/your\/reference_audio.wav'  # \u53c2\u8003\u97f3\u9891\u8def\u5f84\uff0c\u53ef\u9009\r\nseparate = True  # \u662f\u5426\u4ece\u53c2\u8003\u97f3\u9891\u4e2d\u5206\u79bb\u4eba\u58f0\u8f68\u9053\r\n<\/code><\/pre>\n<ol start=\"4\">\n<li>\u751f\u6210\u6b4c\u66f2\uff1a<\/li>\n<\/ol>\n<pre><code>   model_inputs = processor(text=text, lyrics=lyrics, ref_voice_path=ref_voice_path, separate=True)\r\ngeneration = model.generate(**model_inputs, do_sample=True)\r\naudio_arr = generation.cpu().numpy().squeeze()\r\nsf.write(\"songgen_out.wav\", audio_arr, model.config.sampling_rate)\r\n<\/code><\/pre>\n<h4>\u53cc\u8f68\u6a21\u5f0f<\/h4>\n<ol>\n<li>\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\uff1a<\/li>\n<\/ol>\n<pre><code>   import torch\r\nimport os\r\nfrom songgen import (\r\nVoiceBpeTokenizer,\r\nSongGenDualTrackForConditionalGeneration,\r\nSongGenProcessor\r\n)\r\nimport soundfile as sf\r\n<\/code><\/pre>\n<ol start=\"2\">\n<li>\u52a0\u8f7d\u9884\u8bad\u7ec3\u6a21\u578b\uff1a<\/li>\n<\/ol>\n<pre><code>   ckpt_path = \"...\"  # \u9884\u8bad\u7ec3\u6a21\u578b\u7684\u8def\u5f84\r\ndevice = \"cuda:0\" if torch.cuda.is_available() else \"cpu\"\r\nmodel = SongGenDualTrackForConditionalGeneration.from_pretrained(\r\nckpt_path,\r\nattn_implementation='sdpa').to(device)\r\nprocessor = SongGenProcessor(ckpt_path, device)\r\n<\/code><\/pre>\n<ol start=\"3\">\n<li>\u5b9a\u4e49\u8f93\u5165\u6587\u672c\u548c\u6b4c\u8bcd\uff1a<\/li>\n<\/ol>\n<pre><code>   lyrics = \"...\"  # \u6b4c\u8bcd\u6587\u672c\r\ntext = \"...\"  # \u97f3\u4e50\u63cf\u8ff0\u6587\u672c\r\nref_voice_path = 'path\/to\/your\/reference_audio.wav'  # \u53c2\u8003\u97f3\u9891\u8def\u5f84\uff0c\u53ef\u9009\r\nseparate = True  # \u662f\u5426\u4ece\u53c2\u8003\u97f3\u9891\u4e2d\u5206\u79bb\u4eba\u58f0\u8f68\u9053\r\n<\/code><\/pre>\n<ol start=\"4\">\n<li>\u751f\u6210\u6b4c\u66f2\uff1a<\/li>\n<\/ol>\n<pre><code>   model_inputs = processor(text=text, lyrics=lyrics, ref_voice_path=ref_voice_path, separate=True)\r\ngeneration = model.generate(**model_inputs, do_sample=True)\r\nvocal_array = generation.vocal_sequences[0, :generation.vocal_audios_length[0]].cpu().numpy()\r\nacc_array = generation.acc_sequences[0, :generation.acc_audios_length[0]].cpu().numpy()\r\nmin_len = min(vocal_array.shape[0], acc_array.shape[0])\r\nvocal_array = vocal_array[:min_len]\r\nacc_array = acc_array[:min_len]\r\naudio_arr = vocal_array + acc_array\r\nsf.write(\"songgen_out.wav\", audio_arr, model.config.sampling_rate)\r\n<\/code><\/pre>\n<h3>\u8be6\u7ec6\u529f\u80fd\u64cd\u4f5c<\/h3>\n<ul>\n<li><strong>\u6587\u672c\u5230\u6b4c\u66f2\u751f\u6210<\/strong>\uff1a\u8f93\u5165\u5305\u542b\u6b4c\u8bcd\u548c\u97f3\u4e50\u63cf\u8ff0\u7684\u6587\u672c\uff0c\u6a21\u578b\u5c06\u751f\u6210\u5bf9\u5e94\u7684\u6b4c\u66f2\u97f3\u9891\u3002<\/li>\n<li><strong>\u7ec6\u7c92\u5ea6\u63a7\u5236<\/strong>\uff1a\u901a\u8fc7\u8f93\u5165\u6587\u672c\u4e2d\u7684\u63cf\u8ff0\uff0c\u7528\u6237\u53ef\u4ee5\u63a7\u5236\u751f\u6210\u6b4c\u66f2\u7684\u5404\u79cd\u5c5e\u6027\uff0c\u5982\u4e50\u5668\u3001\u98ce\u683c\u3001\u60c5\u7eea\u7b49\u3002<\/li>\n<li><strong>\u58f0\u97f3\u514b\u9686<\/strong>\uff1a\u63d0\u4f9b\u4e09\u79d2\u949f\u7684\u53c2\u8003\u97f3\u9891\u7247\u6bb5\uff0c\u6a21\u578b\u53ef\u4ee5\u6a21\u4eff\u8be5\u58f0\u97f3\u8fdb\u884c\u6b4c\u66f2\u751f\u6210\u3002<\/li>\n<li><strong>\u8f93\u51fa\u6a21\u5f0f\u9009\u62e9<\/strong>\uff1a\u6839\u636e\u9700\u6c42\u9009\u62e9\u6df7\u5408\u6a21\u5f0f\u6216\u53cc\u8f68\u6a21\u5f0f\uff0c\u7075\u6d3b\u5e94\u7528\u4e8e\u4e0d\u540c\u573a\u666f\u3002<\/li>\n<li><strong>\u6570\u636e\u9884\u5904\u7406\u7ba1\u9053<\/strong>\uff1a\u81ea\u52a8\u5316\u7684\u6570\u636e\u9884\u5904\u7406\u548c\u8d28\u91cf\u63a7\u5236\uff0c\u786e\u4fdd\u751f\u6210\u7ed3\u679c\u7684\u9ad8\u8d28\u91cf\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>SongGen \u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u5355\u9636\u6bb5\u81ea\u56de\u5f52Transformer\u6a21\u578b\uff0c\u4e13\u4e3a\u6587\u672c\u5230\u6b4c\u66f2\u7684\u751f\u6210\u4efb\u52a1\u8bbe\u8ba1\u3002\u8be5\u6a21\u578b\u80fd\u591f\u4ece\u6587\u672c\u8f93\u5165\u751f\u6210\u5305\u542b\u4eba\u58f0\u548c\u4f34\u594f\u7684\u6b4c\u66f2\u3002SongGen \u63d0\u4f9b\u4e86\u5bf9\u591a\u79cd\u97f3\u4e50\u5c5e\u6027\u7684\u7ec6\u7c92\u5ea6\u63a7\u5236\uff0c\u5305\u62ec\u6b4c\u8bcd\u3001\u4e50\u5668\u63cf\u8ff0\u3001\u97f3\u4e50\u98ce\u683c\u3001\u60c5\u7eea\u548c\u97f3\u8272\u7b49\u3002\u6b64&#8230;<\/p>\n","protected":false},"author":1,"featured_media":61881,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[230,206],"class_list":["post-21666","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tool","tag-aikaiyuanxiangmu","tag-aiyinle"],"_links":{"self":[{"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/posts\/21666","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/comments?post=21666"}],"version-history":[{"count":0,"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/posts\/21666\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/media\/61881"}],"wp:attachment":[{"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/media?parent=21666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/categories?post=21666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/tags?post=21666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}