def convert_midi(fp): _duration_keys = set() stream = converter.parse(fp) partitions = instrument.partitionByInstrument(stream) # print([(part.getInstrument().instrumentName, len(part.flat.notes)) for part in partitions]) # 获取第一个小节(Measure)中的节拍数 _press_time_dict = defaultdict(list) partition = None for part_sub in partitions: if part_sub.getInstrument().instrumentName.lower() == 'piano' and len(part_sub.flat.notes) > 0: partition = part_sub continue if partition is None: return None, None for _note in partition.flat.notes: _duration = str(_note.duration.quarterLength) if isinstance(_note, NoteClass.Note): _press_time_dict[str(_note.offset)].append([str(_note.pitch), _duration]) _duration_keys.add(_duration) if isinstance(_note, ChordClass.Chord): press_list = _press_time_dict[str(_note.offset)] _duration_keys.add(_duration) for sub_note in _note.notes: press_list.append([str(sub_note.pitch), _duration]) return _press_time_dict, _duration_keys def get_total_keys(_midi_list): _total_keys = set() for _press_time_dict in _midi_list: for step in _press_time_dict.values(): for item in step: _total_keys.add(item[0]) return _total_keys
时间: 2024-03-30 19:34:46 浏览: 14
这段代码的作用是将一个midi文件转换成按键按下的时间和时值的字典,并计算出midi文件中一共用了哪些按键。具体实现过程是:先使用music21库的converter.parse()函数将midi文件解析成Music21 Stream对象,然后使用instrument.partitionByInstrument()函数将Stream对象按照乐器进行分轨,找到钢琴乐器所在的分轨,然后遍历所有音符,按照时间将音符存储在一个字典_press_time_dict中,字典的key为音符的开始时间,value为一个列表,其中每个元素为一个按键和对应的时值;同时记录每个音符的时值,并将这些时值存入一个set集合_duration_keys中。最后使用get_total_keys()函数遍历所有_press_time_dict,将其中出现过的所有按键存储到另外一个set集合_total_keys中,即为midi文件中使用的所有按键。
相关问题
_duration_keys = set() stream = converter.parse(fp) partitions = instrument.partitionByInstrument(stream)
这段代码涉及了 `music21` 库中的一些函数和对象。具体来说:
- `_duration_keys = set()` 定义了一个空集合 `_duration_keys`,用于存储音符的时值。
- `stream = converter.parse(fp)` 使用 `converter.parse()` 函数将文件 `fp` 解析为 `music21` 中的音乐流(`stream`)对象。
- `partitions = instrument.partitionByInstrument(stream)` 使用 `instrument.partitionByInstrument()` 函数将音乐流按照乐器进行分轨,得到一个列表 `partitions`,其中每个元素都是一个只包含单个乐器音轨的音乐流对象。
这段代码使用了 `music21` 库中的一些基本的函数和对象,用于读取和处理音乐文件。其中,`converter.parse()` 函数用于将音乐文件解析为 `music21` 中的音乐流对象,而 `instrument.partitionByInstrument()` 函数用于将音乐流按照乐器进行分轨,方便后续的处理。
def get_argparser(): parser = argparse.ArgumentParser()
Sorry, as an AI language model, I don't have access to the previous context or the purpose of the function you shared. But based on the function signature, it seems like this is a Python function that returns an instance of the `ArgumentParser` class from the `argparse` module. The purpose of this class is to create a parser object that can parse command-line arguments and options.
Here's an example usage of the `ArgumentParser` class:
```
import argparse
def get_argparser():
parser = argparse.ArgumentParser()
parser.add_argument('--name', help='Enter your name')
parser.add_argument('--age', type=int, help='Enter your age')
return parser
if __name__ == '__main__':
parser = get_argparser()
args = parser.parse_args()
print(f"Hello {args.name}, you are {args.age} years old!")
```
In the above example, the `get_argparser()` function creates an instance of the `ArgumentParser` class and adds two arguments to it - `name` and `age`. The `parse_args()` method of the parser object is then called to parse the command-line arguments and options. Finally, the values of the `name` and `age` arguments are printed using the `print()` function.