File size: 4,772 Bytes
9c0c5c8
 
0f3ac5f
 
 
 
 
 
 
 
9c0c5c8
 
63ee254
0f3ac5f
9c0c5c8
 
 
0f3ac5f
 
 
 
 
0954180
0f3ac5f
 
9c0c5c8
 
 
 
63ee254
9c0c5c8
 
 
 
0f3ac5f
c17b696
f30235e
9c0c5c8
 
 
 
63ee254
9c0c5c8
 
 
 
c1e3d89
 
 
 
 
 
 
9c0c5c8
 
f4441f8
 
 
 
 
825c8bf
f4441f8
 
0f3ac5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c0c5c8
 
63ee254
9c0c5c8
 
63ee254
9c0c5c8
0f3ac5f
9c0c5c8
 
 
 
63ee254
9c0c5c8
 
63ee254
9c0c5c8
 
 
 
 
 
 
63ee254
9c0c5c8
 
63ee254
9c0c5c8
 
 
 
f4441f8
 
 
 
 
825c8bf
f4441f8
 
9c0c5c8
 
63ee254
9c0c5c8
 
63ee254
9c0c5c8
 
 
 
 
0f3ac5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c0c5c8
 
 
0f3ac5f
9c0c5c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63ee254
9c0c5c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3de63898",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/teticio/audio-diffusion/blob/master/notebooks/test_mel.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "81fbd495",
   "metadata": {},
   "outputs": [],
   "source": [
    "try:\n",
    "    # are we running on Google Colab?\n",
    "    import google.colab\n",
    "    !git clone -q https://github.com/teticio/audio-diffusion.git\n",
    "    %cd audio-diffusion\n",
    "    %pip install -q -r requirements.txt\n",
    "except:\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "218fcdf1",
   "metadata": {},
   "outputs": [],
   "source": [
    "from datasets import load_dataset\n",
    "from IPython.display import Audio\n",
    "from audiodiffusion import Mel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5e4f8ee5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# These are the default parameters. If you change any of them, you may have to adjust the others.\n",
    "mel = Mel(x_res=256,\n",
    "          y_res=256,\n",
    "          hop_length=512,\n",
    "          sample_rate=22050,\n",
    "          n_fft=2048,\n",
    "          n_iter=32)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2178c3f",
   "metadata": {},
   "source": [
    "### Transform slice of audio to mel spectrogram"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f32bb35e",
   "metadata": {},
   "outputs": [],
   "source": [
    "try:\n",
    "    # are we running on Google Colab?\n",
    "    from google.colab import files\n",
    "    audio_file = list(files.upload().keys())[0]\n",
    "except:\n",
    "    audio_file = \"/home/teticio/Music/Music/A Tribe Called Quest/The Anthology/08 Can I Kick It_.mp3\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "61dbcd2e",
   "metadata": {},
   "outputs": [],
   "source": [
    "mel.load_audio(audio_file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ccadcc0f",
   "metadata": {},
   "outputs": [],
   "source": [
    "image = mel.audio_slice_to_image(15)\n",
    "image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8cec79c6",
   "metadata": {},
   "outputs": [],
   "source": [
    "image.width, image.height"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe112fef",
   "metadata": {},
   "source": [
    "### Transform mel spectrogram back to audio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0b268a54",
   "metadata": {},
   "outputs": [],
   "source": [
    "audio = mel.image_to_audio(image)\n",
    "Audio(data=audio, rate=mel.get_sample_rate())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0f1f2006",
   "metadata": {},
   "source": [
    "### Select a random image from the training set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f29f025",
   "metadata": {},
   "outputs": [],
   "source": [
    "ds = load_dataset('teticio/audio-diffusion-256')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e002482d",
   "metadata": {},
   "outputs": [],
   "source": [
    "image = ds['train'].shuffle().select(range(1))['image'][0]\n",
    "image"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a801fc5",
   "metadata": {},
   "source": [
    "### Convert to audio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a2421827",
   "metadata": {},
   "outputs": [],
   "source": [
    "audio = mel.image_to_audio(image)\n",
    "Audio(data=audio, rate=mel.get_sample_rate())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2281fb55",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "huggingface",
   "language": "python",
   "name": "huggingface"
  },
  "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.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}