File size: 11,944 Bytes
c8a28b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# JASCO\n",
    "Welcome to JASCO's demo jupyter notebook. \n",
    "Here you will find a self-contained example of how to use JASCO for temporally controlled music generation.\n",
    "\n",
    "You can choose a model from the following selection:\n",
    "1. facebook/jasco-chords-drums-400M - 10s music generation conditioned on text, chords and drums, 400M parameters\n",
    "2. facebook/jasco-chords-drums-1B - 10s music generation conditioned on text, chords and drums, 1B parameters\n",
    "3. facebook/jasco-chords-drums-melody-400M - 10s music generation conditioned on text, chords, drums and melody, 400M parameters\n",
    "4. facebook/jasco-chords-drums-melody-1B - 10s music generation conditioned on text, chords, drums and melody, 1B parameters\n",
    "\n",
    "First, we start by initializing the JASCO model:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os \n",
    "from audiocraft.models import JASCO\n",
    "\n",
    "model = JASCO.get_pretrained('facebook/jasco-chords-drums-melody-400M', chords_mapping_path='../assets/chord_to_index_mapping.pkl')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next, let us configure the generation parameters. Specifically, you can control the following:\n",
    "* `cfg_coef_all` (float, optional): Coefficient used for classifier free guidance - fully conditional term. \n",
    "                                    Defaults to 5.0.\n",
    "* `cfg_coef_txt` (float, optional): Coefficient used for classifier free guidance - additional text conditional term. \n",
    "                                    Defaults to 0.0.\n",
    "\n",
    "When left unchanged, JASCO will revert to its default parameters."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.set_generation_params(\n",
    "    cfg_coef_all=0.0,\n",
    "    cfg_coef_txt=5.0\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next, we can go ahead and start generating music given textual prompts."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Text-conditional Generation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from audiocraft.utils.notebook import display_audio\n",
    "\n",
    "# set textual prompt\n",
    "text = \"Funky groove with electric piano playing blue chords rhythmically\"\n",
    "\n",
    "# run the model\n",
    "print(\"Generating...\")              \n",
    "output = model.generate(descriptions=[text], progress=True)\n",
    "\n",
    "# display the result\n",
    "print(f\"Text: {text}\\n\")\n",
    "display_audio(output, sample_rate=model.compression_model.sample_rate)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we can start adding temporal controls! We begin with conditioning on chord progressions:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Chords-conditional Generation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.set_generation_params(\n",
    "    cfg_coef_all=1.5,\n",
    "    cfg_coef_txt=3.0\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from audiocraft.utils.notebook import display_audio\n",
    "\n",
    "# set textual prompt\n",
    "text = \"Strings, woodwind, orchestral, symphony.\"\n",
    "\n",
    "# define chord progression\n",
    "chords = [('C', 0.0), ('D', 2.0), ('F', 4.0), ('Ab', 6.0), ('Bb', 7.0), ('C', 8.0)]\n",
    "\n",
    "# run the model\n",
    "print(\"Generating...\")\n",
    "output = model.generate_music(descriptions=[text], chords=chords, progress=True)\n",
    "\n",
    "# display the result\n",
    "print(f'Text: {text}')\n",
    "print(f'Chord progression: {chords}')\n",
    "display_audio(output, sample_rate=model.compression_model.sample_rate)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next, we can condition the generation on drum tracks:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Drums-conditional Generation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torchaudio\n",
    "from audiocraft.utils.notebook import display_audio\n",
    "\n",
    "\n",
    "# load drum prompt\n",
    "drums_waveform, sr = torchaudio.load(\"../assets/sep_drums_1.mp3\")\n",
    "\n",
    "# set textual prompt \n",
    "text = \"distortion guitars, heavy rock, catchy beat\"\n",
    "\n",
    "# run the model\n",
    "print(\"Generating...\")\n",
    "output = model.generate_music(\n",
    "    descriptions=[text],\n",
    "    drums_wav=drums_waveform,\n",
    "    drums_sample_rate=sr,\n",
    "    progress=True\n",
    ")\n",
    "\n",
    "# display the result\n",
    "print('drum prompt:')\n",
    "display_audio(drums_waveform, sample_rate=sr)\n",
    "print(f'Text: {text}')\n",
    "display_audio(output, sample_rate=model.compression_model.sample_rate)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also combine multiple temporal controls! Let's move on to generating with both chords and drums conditioning:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Drums + Chords conditioning"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torchaudio\n",
    "from audiocraft.utils.notebook import display_audio\n",
    "\n",
    "\n",
    "# load drum prompt\n",
    "drums_waveform, sr = torchaudio.load(\"../assets/sep_drums_1.mp3\")\n",
    "\n",
    "# set textual prompt \n",
    "text = \"string quartet, orchestral, dramatic\"\n",
    "\n",
    "# define chord progression\n",
    "chords = [('C', 0.0), ('D', 2.0), ('F', 4.0), ('Ab', 6.0), ('Bb', 7.0), ('C', 8.0)]\n",
    "\n",
    "# run the model\n",
    "print(\"Generating...\")\n",
    "output = model.generate_music(\n",
    "    descriptions=[text],\n",
    "    drums_wav=drums_waveform,\n",
    "    drums_sample_rate=sr,\n",
    "    chords=chords,\n",
    "    progress=True\n",
    ")\n",
    "\n",
    "# display the result\n",
    "print('drum prompt:')\n",
    "display_audio(drums_waveform, sample_rate=sr)\n",
    "print(f'Chord progression: {chords}')\n",
    "print(f'Text: {text}')\n",
    "display_audio(output, sample_rate=model.compression_model.sample_rate)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Melody + Drums + Chords conditioning - inference example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "from demucs import pretrained\n",
    "from demucs.apply import apply_model\n",
    "from demucs.audio import convert_audio\n",
    "import torch\n",
    "from audiocraft.utils.notebook import display_audio\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# --------------------------\n",
    "# First, choose file to load\n",
    "# --------------------------\n",
    "fnames = ['salience_1', 'salience_2']\n",
    "chords = [\n",
    "    [('N',  0.0), ('Eb7',  1.088000000), ('C#',  4.352000000), ('D',  4.864000000), ('Dm7',  6.720000000), ('G7',  8.256000000), ('Am7b5/G',  9.152000000)],  # for salience 1\n",
    "    [('N',  0.0), ('C',  0.320000000), ('Dm7',  3.456000000), ('Am',  4.608000000), ('F',  8.320000000), ('C',  9.216000000)]  # for salience 2\n",
    "]\n",
    "file_idx = 0  # either 0 or 1\n",
    "\n",
    "\n",
    "# ------------------------------------\n",
    "# display audio, melody map and chords\n",
    "# ------------------------------------\n",
    "def plot_chromagram(tensor):\n",
    "    # Check if tensor is a PyTorch tensor\n",
    "    if not torch.is_tensor(tensor):\n",
    "        raise ValueError('Input should be a PyTorch tensor')\n",
    "    tensor = tensor.numpy().T # C, T\n",
    "    plt.figure(figsize=(20, 20))\n",
    "    plt.imshow(tensor, cmap='binary', interpolation='nearest', origin='lower')\n",
    "    plt.show()\n",
    "\n",
    "# load salience and display the corresponding wav\n",
    "melody_prompt_wav, melody_prompt_sr = torchaudio.load(f\"../assets/{fnames[file_idx]}.wav\")\n",
    "print(\"Source melody:\")\n",
    "display_audio(melody_prompt_wav, sample_rate=melody_prompt_sr)\n",
    "melody =  torch.load(f\"../assets/{fnames[file_idx]}.th\", weights_only=True)\n",
    "plot_chromagram(melody)\n",
    "print(\"Chords:\")\n",
    "print(chords[file_idx])\n",
    "\n",
    "# --------------------------------------------------\n",
    "# use demucs to seperate the drums stem from src mix\n",
    "# --------------------------------------------------\n",
    "def _get_drums_stem(wav: torch.Tensor, sample_rate: int) -> torch.Tensor:\n",
    "    \"\"\"Get parts of the wav that holds the drums, extracting the main stems from the wav.\"\"\"\n",
    "    demucs_model = pretrained.get_model('htdemucs').to('cuda')\n",
    "    wav = convert_audio(\n",
    "        wav, sample_rate, demucs_model.samplerate, demucs_model.audio_channels)  # type: ignore\n",
    "    stems = apply_model(demucs_model, wav.cuda().unsqueeze(0), device='cuda').squeeze(0)\n",
    "    drum_stem = stems[demucs_model.sources.index('drums')]  # extract relevant stems for drums conditioning\n",
    "    return convert_audio(drum_stem.cpu(), demucs_model.samplerate, sample_rate, 1)  # type: ignore\n",
    "drums_wav = _get_drums_stem(melody_prompt_wav, melody_prompt_sr)\n",
    "print(\"Separated drums:\")\n",
    "display_audio(drums_wav, sample_rate=melody_prompt_sr)\n",
    "\n",
    "# ----------------------------------\n",
    "# Generate using the loaded controls\n",
    "# ----------------------------------\n",
    "# these are free-form texts written randomly\n",
    "texts = [\n",
    "    '90s rock with heavy drums and hammond',\n",
    "    '80s pop with groovy synth bass and drum machine',\n",
    "    'folk song with leading accordion',\n",
    "]\n",
    "\n",
    "print(\"Generating...\")\n",
    "# replacing dynammic solver with simple euler solver\n",
    "model.set_generation_params(cfg_coef_all=1.5, cfg_coef_txt=2.5, euler=True, euler_steps=50)  # manually set with euler solver\n",
    "output = model.generate_music(\n",
    "    descriptions=texts,\n",
    "    chords=chords[file_idx],\n",
    "    drums_wav=drums_wav,\n",
    "    drums_sample_rate=melody_prompt_sr,\n",
    "    melody_salience_matrix=melody.permute(1, 0),\n",
    "    progress=True\n",
    ")\n",
    "display_audio(output, sample_rate=model.compression_model.sample_rate)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "jasco_dev",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}