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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "7X-TwhdTGmlc"
      },
      "source": [
        "# License"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "fCQUeZRPGnoe"
      },
      "source": [
        "> Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.\n",
        ">\n",
        "> Licensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at\n",
        ">\n",
        "> http://www.apache.org/licenses/LICENSE-2.0\n",
        ">\n",
        "> Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "rtBDkKqVGZJ8"
      },
      "source": [
        "# Introduction"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "pZ2QSsXuGbMe"
      },
      "source": [
        "In this tutorial we show how use NeMo **neural audio codecs** at inference time. To learn more about training and finetuning neural audio codecs in NeMo, check the [Audio Codec Training tutorial](https://github.com/NVIDIA/NeMo/blob/main/tutorials/tts/Audio_Codec_Training.ipynb).\n",
        "\n",
        "An audio codec typically consists of an encoder, a quantizer and a decoder, with a typical architecture depicted in the figure below.\n",
        "An audio codec can be used to encode an input audio signal into a sequence of discrete values.\n",
        "In this tutorial, the discrete values will be referred to as **audio tokens**.\n",
        "The obtained audio tokens can be decoded into an output audio signal.\n",
        "\n",
        "Audio tokens can be used to represent the input audio for an automatic speech recognition (ASR) model [[1](https://arxiv.org/abs/2309.10922), [2](https://arxiv.org/pdf/2407.03495)], or to represent the output audio of a text-to-speech (TTS) system [[3](https://arxiv.org/abs/2406.05298), [4](https://arxiv.org/pdf/2406.17957)].\n",
        "\n",
        "NeMo provides several neural audio codec models, inlcuding audio codecs and mel codecs at different sampling rates.\n",
        "The list of the available models can be found [here](https://docs.nvidia.com/nemo-framework/user-guide/latest/nemotoolkit/tts/checkpoints.html#codec-models).\n",
        "\n",
        "<div>\n",
        "<img src=\"https://github.com/NVIDIA/NeMo/releases/download/v1.22.0/nemo_audio_codec.png\" width=\"800\", height=\"400\"/>\n",
        "</div>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "3OZassNG5xff"
      },
      "source": [
        "# Setup"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "WZvQvPkIhRi3"
      },
      "outputs": [],
      "source": [
        "BRANCH = 'main'\n",
        "# Install NeMo library. If you are running locally (rather than on Google Colab), follow the instructions at https://github.com/NVIDIA/NeMo#Installation\n",
        "\n",
        "if 'google.colab' in str(get_ipython()):\n",
        "    !python -m pip install git+https://github.com/NVIDIA/NeMo.git@$BRANCH#egg=nemo_toolkit[all]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "v8NGOM0EzK8W"
      },
      "outputs": [],
      "source": [
        "import math\n",
        "import wget\n",
        "import os\n",
        "import librosa\n",
        "import torch\n",
        "import numpy as np\n",
        "import IPython.display as ipd\n",
        "import matplotlib.pyplot as plt\n",
        "from pathlib import Path\n",
        "\n",
        "\n",
        "# Utility for displaying signals and metrics\n",
        "def show_signal(signal: np.ndarray, sample_rate: int = 16000, tag: str = 'Signal'):\n",
        "    \"\"\"Show the time-domain signal and its spectrogram.\n",
        "    \"\"\"\n",
        "    fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(12, 2.5))\n",
        "\n",
        "    # show waveform\n",
        "    t = np.arange(0, len(signal)) / sample_rate\n",
        "\n",
        "    ax[0].plot(t, signal)\n",
        "    ax[0].set_xlim(0, t.max())\n",
        "    ax[0].grid()\n",
        "    ax[0].set_xlabel('time / s')\n",
        "    ax[0].set_ylabel('amplitude')\n",
        "    ax[0].set_title(tag)\n",
        "\n",
        "    n_fft = 1024\n",
        "    hop_length = 256\n",
        "\n",
        "    D = librosa.amplitude_to_db(np.abs(librosa.stft(signal, n_fft=n_fft, hop_length=hop_length)), ref=np.max)\n",
        "    img = librosa.display.specshow(D, y_axis='linear', x_axis='time', sr=sample_rate, n_fft=n_fft, hop_length=hop_length, ax=ax[1])\n",
        "    ax[1].set_title(tag)\n",
        "\n",
        "    plt.tight_layout()\n",
        "    plt.colorbar(img, format=\"%+2.f dB\", ax=ax)\n",
        "\n",
        "\n",
        "# Utility for displaying a latent representation\n",
        "def show_latent(latent: np.ndarray, tag: str):\n",
        "    plt.figure(figsize = (16, 3))\n",
        "    img = plt.imshow(latent, aspect='equal')\n",
        "    plt.colorbar(img, ax=plt.gca())\n",
        "    plt.title(tag)\n",
        "    plt.xlabel('Time frame')\n",
        "    plt.ylabel('Latent vector index')\n",
        "    plt.tight_layout()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "8ZKDMTwsEY1K"
      },
      "outputs": [],
      "source": [
        "# Working directory\n",
        "ROOT_DIR = Path().absolute() / 'codec_tutorial'\n",
        "\n",
        "# Create dataset directory\n",
        "DATA_DIR = ROOT_DIR / 'data'\n",
        "DATA_DIR.mkdir(parents=True, exist_ok=True)\n",
        "\n",
        "audio_path = DATA_DIR / 'LJ023-0089.wav'\n",
        "audio_url = \"https://multilangaudiosamples.s3.us-east-2.amazonaws.com/LJ023-0089.wav\"\n",
        "\n",
        "if not os.path.exists(audio_path):\n",
        "  wget.download(audio_url, audio_path.as_posix())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "KAbH7N427FdT"
      },
      "source": [
        "# Load a model from NGC"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ODgdGgsAAUku"
      },
      "source": [
        "Any of the [pretrained checkpoints](https://docs.nvidia.com/nemo-framework/user-guide/latest/nemotoolkit/tts/checkpoints.html#codec-models) could be used for inference.\n",
        "Here, we use `mel_codec_22khz_fullband_medium`, which works for 22.05 kHz audio signals.\n",
        "\n",
        "The model can be easily restored from NGC:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "XqAYWR65aKTx"
      },
      "outputs": [],
      "source": [
        "from nemo.collections.tts.models.audio_codec import AudioCodecModel\n",
        "\n",
        "# Optionally specify a pretrained model to fine-tune from. To train from scratch, set this to 'None'.\n",
        "model_name = 'mel_codec_22khz_fullband_medium'\n",
        "codec_model = AudioCodecModel.from_pretrained(model_name)\n",
        "codec_model.freeze()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ZnnjL28pEY1L"
      },
      "source": [
        "Show information about the loaded model:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "4xsfeHVyEY1L"
      },
      "outputs": [],
      "source": [
        "print(f'Loaded model from NeMo:')\n",
        "print(f'\\tmodel name       : {model_name}')\n",
        "print(f'\\tsample rate      : {codec_model.sample_rate} Hz')\n",
        "print(f'\\tlatent dimension : {codec_model.vector_quantizer.codebook_dim}')\n",
        "\n",
        "print('\\n\\nModel summary:')\n",
        "print(codec_model.summarize())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "fM4QPsLTnzK7"
      },
      "source": [
        "# Inference"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "tkZC6Dl7KRl6"
      },
      "source": [
        "## Processing audio\n",
        "\n",
        "Here we use the codec model to process the input audio by applying the complete model. The input signal is encoded, quantized, dequantized and decoded. Finally, a reconstructed signal is obtained."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "sYzvAYr2vo1K"
      },
      "outputs": [],
      "source": [
        "input_audio, sr = librosa.load(audio_path, sr=codec_model.sample_rate)\n",
        "\n",
        "# Shape (batch, time)\n",
        "input_audio_tensor = torch.from_numpy(input_audio).unsqueeze(dim=0).to(codec_model.device)\n",
        "\n",
        "# Shape (batch,)\n",
        "input_audio_len = torch.tensor([input_audio_tensor.size(-1)]).to(codec_model.device)\n",
        "\n",
        "# Process audio using the codec model\n",
        "output_audio_tensor, _ = codec_model(audio=input_audio_tensor, audio_len=input_audio_len)\n",
        "\n",
        "# Output audio\n",
        "output_audio = output_audio_tensor.squeeze().cpu().numpy()\n",
        "\n",
        "# Show signals\n",
        "show_signal(input_audio, tag='Input audio', sample_rate=codec_model.sample_rate)\n",
        "show_signal(output_audio, tag='Output audio', sample_rate=codec_model.sample_rate)\n",
        "\n",
        "# Play audio\n",
        "print('Input audio')\n",
        "ipd.display(ipd.Audio(input_audio, rate=codec_model.sample_rate))\n",
        "\n",
        "print('Output audio')\n",
        "ipd.display(ipd.Audio(output_audio, rate=codec_model.sample_rate))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "rynZYwg2VP5d"
      },
      "source": [
        "## Audio tokens\n",
        "\n",
        "Audio tokens can be easily computed by using the `encode` method of the `AudioCodec` model."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "ASKM_jKVEY1L"
      },
      "outputs": [],
      "source": [
        "# Convert audio to tokens\n",
        "tokens, tokens_len = codec_model.encode(audio=input_audio_tensor, audio_len=input_audio_len)\n",
        "\n",
        "print('tokens information:')\n",
        "print(f'\\tshape (batch, codebook, time frame) : {tokens.size()}')\n",
        "print(f'\\tdtype                               : {tokens.dtype}')\n",
        "print(f'\\tmin                                 : {tokens.min()}')\n",
        "print(f'\\tmax                                 : {tokens.max()}')\n",
        "\n",
        "# Number of codebooks should match the number of codebooks/groups\n",
        "if hasattr(codec_model.vector_quantizer, 'num_groups'):\n",
        "    # Group FSQ\n",
        "    assert tokens.size(1) == codec_model.vector_quantizer.num_groups\n",
        "    print(f'\\tnum_groups                          : {tokens.size(1)}')\n",
        "elif hasattr(codec_model.vector_quantizer, 'codebooks'):\n",
        "    # RVQ\n",
        "    assert tokens.size(1) == len(codec_model.vector_quantizer.codebooks)\n",
        "    print(f'\\tnum_codebooks                       : {tokens.size(1)}')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "CmliPMnDEY1L"
      },
      "source": [
        "Similarly, audio can be easily reconstructed from audio tokens using the `decode` method of the `AudioCodec` models."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "RTQ1M9PMEY1L"
      },
      "outputs": [],
      "source": [
        "# Convert tokens back to audio\n",
        "output_audio_from_tokens_tensor, _ = codec_model.decode(tokens=tokens, tokens_len=tokens_len)\n",
        "output_audio_from_tokens = output_audio_from_tokens_tensor.squeeze().cpu().numpy()\n",
        "\n",
        "# Show signals\n",
        "show_signal(output_audio_from_tokens, tag='Output audio from tokens', sample_rate=codec_model.sample_rate)\n",
        "show_signal(output_audio_from_tokens - output_audio, tag='Difference compared to forward pass', sample_rate=codec_model.sample_rate)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "kGqotZkqEY1M"
      },
      "source": [
        "## Latent representation\n",
        "\n",
        "Continuous (non-discrete) latent representation at the output of the encoder can be easily computed using the `encode_audio` method of the `AudioCodec` model."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "r-89-gG3EY1M"
      },
      "outputs": [],
      "source": [
        "# Convert audio to the encoded representation\n",
        "encoded, encoded_len = codec_model.encode_audio(audio=input_audio_tensor, audio_len=input_audio_len)\n",
        "\n",
        "print('encoded information:')\n",
        "print(f'\\tshape (batch, codebook, time frame) : {encoded.size()}')\n",
        "print(f'\\tdtype                               : {encoded.dtype}')\n",
        "print(f'\\tmin                                 : {encoded.min()}')\n",
        "print(f'\\tmax                                 : {encoded.max()}')\n",
        "\n",
        "\n",
        "# Show the encoded representation\n",
        "show_latent(encoded.squeeze().cpu().numpy(), tag='Encoder output')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "3Ory1U1uEY1M"
      },
      "source": [
        "The encoded representation can be easily converted to tokens, dequantized into a continuous latent representation and decoded back to audio."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "btmqUWNkEY1M"
      },
      "outputs": [],
      "source": [
        "# Encoder output to tokens\n",
        "tokens = codec_model.quantize(encoded=encoded, encoded_len=encoded_len)\n",
        "\n",
        "# Tokens back to a continuous vector\n",
        "dequantized = codec_model.dequantize(tokens=tokens, tokens_len=encoded_len)\n",
        "\n",
        "# Reconstruct audio\n",
        "output_audio_from_latent_tensor, _ = codec_model.decode_audio(inputs=dequantized, input_len=encoded_len)\n",
        "output_audio_from_latent = output_audio_from_latent_tensor.squeeze().cpu().numpy()\n",
        "\n",
        "# Show dequantized latent representation\n",
        "show_latent(dequantized.squeeze().cpu().numpy(), tag='Decoder input')\n",
        "\n",
        "# Show signals\n",
        "show_signal(output_audio_from_latent, tag='Output audio from latent', sample_rate=codec_model.sample_rate)\n",
        "show_signal(output_audio_from_latent - output_audio, tag='Difference compared to forward pass', sample_rate=codec_model.sample_rate)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "cMvU0WxlEY1M"
      },
      "source": [
        "# Related information"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "_LtyHHuLkNDv"
      },
      "source": [
        "To learn more about audio codec models in NeMo, look at our [documentation](https://docs.nvidia.com/nemo-framework/user-guide/latest/nemotoolkit/tts/models.html#codecs).\n",
        "\n",
        "For more information on training and finetuning neural audio codecs in NeMo, check the [Audio Codec Training tutorial](https://github.com/NVIDIA/NeMo/blob/main/tutorials/tts/Audio_Codec_Training.ipynb)."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "LeqV3VvJVOb-"
      },
      "source": [
        "# References"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Rvu4w2x_3RSY"
      },
      "source": [
        "1.   [Discrete Audio Representation as an Alternative to Mel-Spectrograms for Speaker and Speech Recognition](https://arxiv.org/abs/2309.10922)\n",
        "2.   [Codec-ASR: Training Performant Automatic Speech Recognition Systems with Discrete Speech Representations](https://arxiv.org/pdf/2407.03495)\n",
        "3.   [Spectral Codecs: Spectrogram-Based Audio Codecs for High Quality Speech Synthesis](https://arxiv.org/abs/2406.05298)\n",
        "4.   [Improving Robustness of LLM-based Speech Synthesis by Learning Monotonic Alignment](https://arxiv.org/pdf/2406.17957)"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "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.10.12"
    },
    "colab": {
      "provenance": [],
      "toc_visible": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}