File size: 13,593 Bytes
a03c9b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright 2024 The YourMT3 Authors.
#
# 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
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Please see the details in the LICENSE file.
"""preprocess_rnsynth.py 

RNSynth: Randomly generated note sequences using the NSynth dataset.

"""
import os
import random
import glob
import json
import logging
import numpy as np
from typing import Dict, Literal, Optional
from utils.note_event_dataclasses import Note
from utils.audio import get_audio_file_info, load_audio_file, write_wav_file, guess_onset_offset_by_amp_envelope
from utils.midi import note_event2midi
from utils.note2event import note2note_event, sort_notes, validate_notes, trim_overlapping_notes, mix_notes

# yapf: disable
QUALITY_VOCAB = [
    'bright', 'dark', 'distortion', 'fast_decay', 'long_release', 'multiphonic', 'nonlinear_env',
    'percussive', 'reverb', 'tempo-synced'
]

INSTRUMENT_FAMILY_VOCAB = [
    'bass', 'brass', 'flute', 'guitar', 'keyboard', 'mallet', 'organ', 'reed', 'string', 'vocal',
    'synth_lead'
]

INSTRUMENT_SOURCE_VOCAB = ['acoustic', 'electronic', 'synthetic']

INSTRUMENT_MAPPING = {
    # key: (instrument_family, instrument_source)
    ('bass', 'acoustic'): {'program': 32, 'channel': 0, 'allow_poly': False,},
    ('bass', 'electronic'): {'program': 33, 'channel': 0, 'allow_poly': False,},
    ('bass', 'synthetic'): {'program': 38, 'channel': 0, 'allow_poly': False,},
    ('brass', 'acoustic'): {'program': 61, 'channel': 1, 'allow_poly': True,},
    ('brass', 'electronic'): {'program': 62, 'channel': 1, 'allow_poly': True,},
    ('brass', 'synthetic'): {'program': 62, 'channel': 1, 'allow_poly': True, },
    ('flute', 'acoustic'): {'program': 73, 'channel': 2, 'allow_poly': False,},
    ('flute', 'electronic'): {'program': 76, 'channel': 2, 'allow_poly': False,},
    ('flute', 'synthetic'): {'program': 76, 'channel': 2, 'allow_poly': False,},
    ('guitar', 'acoustic'): {'program': 24, 'channel': 3, 'allow_poly': True,},
    ('guitar', 'electronic'): {'program': 27, 'channel': 3, 'allow_poly': True,},
    ('guitar', 'synthetic'): {'program': 27, 'channel': 3, 'allow_poly': True,},
    ('keyboard', 'acoustic'): {'program': 0, 'channel': 4, 'allow_poly': True,},
    ('keyboard', 'electronic'): {'program': 4, 'channel': 4, 'allow_poly': True,},
    ('keyboard', 'synthetic'): {'program': 80, 'channel': 4, 'allow_poly': True,},
    ('mallet', 'acoustic'): {'program': 12, 'channel': 5, 'allow_poly': True,},
    ('mallet', 'electronic'): {'program': 12, 'channel': 5, 'allow_poly': True,},
    ('mallet', 'synthetic'): {'program': 12, 'channel': 5, 'allow_poly': True,},
    ('organ', 'acoustic'): {'program': 16, 'channel': 6, 'allow_poly': True,},
    ('organ', 'electronic'): {'program': 18, 'channel': 6, 'allow_poly': True,},
    ('organ', 'synthetic'): {'program': 18, 'channel': 6, 'allow_poly': True,},
    ('reed', 'acoustic'): {'program': 65, 'channel': 7, 'allow_poly': True,},
    ('reed', 'electronic'): {'program': 83, 'channel': 7, 'allow_poly': True,},
    ('reed', 'synthetic'): {'program': 83, 'channel': 7, 'allow_poly': True,},
    ('string', 'acoustic'): {'program': 48, 'channel': 8, 'allow_poly': True,},
    ('string', 'electronic'): {'program': 50, 'channel': 8, 'allow_poly': True,},
    ('string', 'synthetic'): {'program': 50, 'channel': 8, 'allow_poly': True,},
    # ('vocal', 'acoustic'): [56],
    # ('vocal', 'electronic'): [56],
    # ('vocal', 'synthetic'): [56],
    ('synth_lead', 'acoustic'): {'program': 80, 'channel': 9, 'allow_poly': True,},
    ('synth_lead', 'electronic'): {'program': 80, 'channel': 9, 'allow_poly': True,},
    ('synth_lead', 'synthetic'): {'program': 80, 'channel': 9, 'allow_poly': True,},
}


CHANNEL_INFO = {
    0: {'name': 'bass', 'max_poly': 1},
    1: {'name': 'brass', 'max_poly': 4},
    2: {'name': 'flute', 'max_poly': 1},
    3: {'name': 'guitar', 'max_poly': 6},
    4: {'name': 'keyboard', 'max_poly': 8},
    5: {'name': 'mallet', 'max_poly': 4},
    6: {'name': 'organ', 'max_poly': 8},
    7: {'name': 'reed', 'max_poly': 2},
    8: {'name': 'string', 'max_poly': 4},
    9: {'name': 'synth_lead', 'max_poly': 2},
}
# yapf: enable


class RandomNSynthGenerator(object):

    def __init__(self, channel_info: Dict=CHANNEL_INFO):
        self.num_channels = len(channel_info)
        self.channel_info = channel_info
        self.channel_max_poly = [channel_info[ch]['max_poly'] for ch in range(self.num_channels)]
        
        # channel_space_left[ch]: current state of empty space for notes left in channel
        self.channel_space_left = [0] * self.num_channels
        for ch in range(self.num_channels):
            self.reset_space_left(ch)

    def reset_space_left(self, ch: int):
        max_poly = self.channel_max_poly[ch]
        if max_poly == 1:
            self.channel_space_left[ch] = 1
        else:
            self.channel_space_left[ch] = np.random.randint(1, max_poly + 1 )




def setup_logger(log_file: str) -> logging.Logger:
    logger = logging.getLogger('my_logger')
    logger.setLevel(logging.DEBUG)
    file_handler = logging.FileHandler(log_file)
    formatter = logging.Formatter('%(asctime)s:%(levelname)s:%(message)s')
    file_handler.setFormatter(formatter)
    if not logger.handlers:
        logger.addHandler(file_handler)
    console_handler = logging.StreamHandler()
    console_handler.setLevel(logging.DEBUG)
    console_formatter = logging.Formatter('%(levelname)s - %(message)s')
    console_handler.setFormatter(console_formatter)
    logger.addHandler(console_handler)
    return logger


def get_duration_by_detecting_offset(audio_file: os.PathLike,
                                     side_info: Optional[str] = None,
                                     offset_threshold: float = 0.02) -> float:
    fs, n_frames, _ = get_audio_file_info(audio_file)
    x = load_audio_file(audio_file, fs=fs)
    if side_info is not None and 'fast_decay' in side_info or 'percussive' in side_info:
        x = x[:int(fs * 2.0)]  # limit to 1.5 sec

    _, offset, _ = guess_onset_offset_by_amp_envelope(
        x, fs=fs, onset_threshold=0., offset_threshold=offset_threshold, frame_size=128)
    offset = min(offset, n_frames)
    dur_sec = np.floor((offset / fs) * 1000) / 1000
    return dur_sec


def random_key_cycle(d: Dict):
    keys = list(d.keys())
    while True:
        random.shuffle(keys)
        for i, key in enumerate(keys):
            is_last_element = (i == len(keys) - 1)  # Check if it's the last element in the cycle
            yield (d[key], is_last_element)


def create_sound_info(base_dir: os.PathLike, logger: logging.Logger,
                      split: Literal['train', 'validation', 'test'], metadata_file: os.PathLike):
    """Create a dictionary of sound info from the metadata file."""
    with open(metadata_file, 'r') as f:
        metadata = json.load(f)
    logger.info(f'Loaded {metadata_file}. Number of examples: {len(metadata)}')

    # Create a sound_info dictionary
    sound_info = {}  # key: nsynth_id, value: dictionary of sound info
    count_skipped = 0
    skipped_instrument_family = set()
    for i, (k, v) in enumerate(metadata.items()):
        if i % 5000 == 0:
            print(f'Creating sound info {i} / {len(metadata)}')
        nsynth_id = v['note']
        instrument_family = v['instrument_family_str']
        instrument_source = v['instrument_source_str']
        audio_file = os.path.join(base_dir, split, 'audio', k + '.wav')
        if not os.path.exists(audio_file):
            raise FileNotFoundError(audio_file)
        dur_sec = get_duration_by_detecting_offset(
            audio_file, side_info=v['qualities_str'], offset_threshold=0.001)

        if INSTRUMENT_MAPPING.get((instrument_family, instrument_source), None) is not None:
            sound_info[nsynth_id] = {
                'audio_file':
                    audio_file,
                'program':
                    INSTRUMENT_MAPPING[instrument_family, instrument_source]['program'],
                'pitch':
                    int(v['pitch']),
                'velocity':
                    int(v['velocity']),
                'channel_group':
                    INSTRUMENT_MAPPING[instrument_family, instrument_source]['channel'],
                'dur_sec':
                    dur_sec,
            }
        else:
            count_skipped += 1
            skipped_instrument_family.add(instrument_family)
    logger.info(f'Created sound info. Number of examples: {len(sound_info)}')
    logger.info(f'Number of skipped examples: {count_skipped}, {skipped_instrument_family}')
    del metadata

    # Regroup sound_info by channel_group
    sound_info_by_channel_group = {}  # key: channel_group, value: list of sound_info
    num_channel_groups = 10
    for i in range(num_channel_groups):
        sound_info_by_channel_group[i] = {}
    for nsynth_id, info in sound_info.items():
        channel_group = info['channel_group']
        sound_info_by_channel_group[channel_group][nsynth_id] = info
    del sound_info
    channel_group_counts = [
        (CHANNEL_INFO[k]['name'], len(v)) for k, v in sound_info_by_channel_group.items()
    ]
    logger.info('Count of sound_info in each channel_group: {}'.format(channel_group_counts))
    return sound_info_by_channel_group, num_channel_groups






def random_nsynth_generator(data_home: os.PathLike,
                            dataset_name: str = 'random_nsynth',
                            generation_minutes_per_file: float = 4.0) -> None:
    """
    Splits:
        'train'
        'validation'
        'test'

    Writes:
        - {dataset_name}_{split}_file_list.json: a dictionary with the following keys:
        {
            index:
            {
                'random_nsynth_id': random_nsynth_id, # = nsynth_id
                'n_frames': (int),
                'stem_file': 'path/to/stem.npy',
                'mix_audio_file': 'path/to/mix.wav',
                'notes_file': 'path/to/notes.npy',
                'note_events_file': 'path/to/note_events.npy',
                'midi_file': 'path/to/midi.mid', # this is 120bpm converted midi file from note_events
                'program': List[int],   
                'is_drum': List[int], # [0] or [1]
            }
        }
    """
    # Directory and file paths
    base_dir = os.path.join(data_home, dataset_name + '_yourmt3_16k')
    output_index_dir = os.path.join(data_home, 'yourmt3_indexes')
    os.makedirs(output_index_dir, exist_ok=True)

    # Setup logger
    log_file = os.path.join(base_dir, 'sound_genetation_log.txt')
    logger = setup_logger(log_file)

    # Load annotation json file as dictionary
    split = 'validation'
    metadata_file = os.path.join(base_dir, split, 'examples.json')

    # Create a sound_info dictionary
    sound_info_by_channel_group, num_channel_groups = create_sound_info(
        base_dir, logger, split, metadata_file)

    # Gnenerate random note sequences
    max_frames_per_file = int(generation_minutes_per_file * 60 * 16000)
    sound_gens = [
        random_key_cycle(sound_info_by_channel_group[key])
        for key in sorted(sound_info_by_channel_group.keys())
    ]

    # 5-minute audio generation
    notes = []
    y = np.zeros((num_channel_groups, max_frames_per_file), dtype=np.float32) # (C, L)
    bass_channel = 0  # loop for a cycle of bass channel generation
    cur_frame = 0
    # is_last_element_bass = False
    #while cur_frame < max_frames_per_file and is_last_element_bass == False:

        # x: source audio, y: target audio for each channel
        x_info, is_last_element = next(sound_gens[ch])
        if ch == bass_channel:
            is_last_element = is_last_element_bass
        
        # info about this channel
        onset_in_frame = cur_frame
        offset_in_frame = cur_frame + int(x_info['dur_sec'] * 16000)

        x = load_audio_file(x_info['audio_file'], fs=16000)
        x = x[:int(x_info['dur_sec'] * 16000)]
        y[ch, :] = 0




def preprocess_random_nsynth_16k(data_home=os.PathLike, dataset_name='random_nsynth') -> None:
    """
    Splits:
        'train'
        'validation'
        'test'

    Writes:
        - {dataset_name}_{split}_file_list.json: a dictionary with the following keys:
        {
            index:
            {
                'random_nsynth_id': random_nsynth_id, # = nsynth_id
                'n_frames': (int),
                'stem_file': 'path/to/stem.npy',
                'mix_audio_file': 'path/to/mix.wav',
                'notes_file': 'path/to/notes.npy',
                'note_events_file': 'path/to/note_events.npy',
                'midi_file': 'path/to/midi.mid', # this is 120bpm converted midi file from note_events
                'program': List[int],   
                'is_drum': List[int], # [0] or [1]
            }
        }
    """
    # Directory and file paths
    base_dir = os.path.join(data_home, dataset_name + '_yourmt3_16k')
    output_index_dir = os.path.join(data_home, 'yourmt3_indexes')
    os.makedirs(output_index_dir, exist_ok=True)

    # Setup logger
    log_file = os.path.join(base_dir, 'log.txt')
    logger = setup_logger(log_file)

    # Load annotation json file as dictionary
    split = 'validation'
    metadata_file = os.path.join(base_dir, split, 'examples.json')
    with open(metadata_file, 'r') as f:
        metadata = json.load(f)
    logger.info(f'Loaded {metadata_file}. Number of examples: {len(metadata)}')