从note block studio读取和写入.nbs文件的简单python库
pynbs的Python项目详细描述
Pynbs
A simple python library to read and write .nbs files from Open Note Block Studio. Compatible with python 2 and 3.
pynbs
使得可以轻松地在note block studio歌曲上迭代。
importpynbsfortick,chordinpynbs.read('demo_song.nbs'):print(tick,[note.keyfornoteinchord])
您还可以使用pynbs
以编程方式生成新歌曲。
importpynbsnew_file=pynbs.new_file(song_name='Hello world')new_file.notes.extend([pynbs.Note(tick=i,layer=0,instrument=0,key=i+35)foriinrange(10)])new_file.save('new_file.nbs')
安装
可以使用pip
安装包。
$ pip install pynbs
最新版本遵循NBS文件格式的最新版本 specification (版本3)。
基本用法
读取文件
您可以使用read()
函数读取和解析特定的nbs文件。
demo_song=pynbs.read('demo_song.nbs')
read()
函数返回一个pynbs
文件对象。这些物体有几个
映射NBS文件二进制结构的属性。
标题
第一个属性是文件头header
。它包含有关
文件。
header=demo_song.header
Attribute | Type | Details |
---|---|---|
^{ | ^{ | The NBS format version. |
^{ | ^{ | The amount of instruments from vanilla Minecraft in the song. |
^{ | ^{ | The length of the song, measured in ticks. |
^{ | ^{ | The id of the last layer with at least one note block in it. |
^{ | ^{ | The name of the song. |
^{ | ^{ | The author of the song. |
^{ | ^{ | The original song author of the song. |
^{ | ^{ | The description of the song. |
^{ | ^{ | The tempo of the song. |
^{ | ^{ | Whether auto-saving has been enabled. |
^{ | ^{ | The amount of minutes between each auto-save. |
^{ | ^{ | The time signature of the song. |
^{ | ^{ | The amount of minutes spent on the project. |
^{ | ^{ | The amount of times the user have left clicked. |
^{ | ^{ | The amount of times the user have right clicked. |
^{ | ^{ | The amount of times the user have added a block. |
^{ | ^{ | The amount of times the user have removed a block. |
^{ | ^{ | The file name of the original midi or schematic. |
For more information about all these fields, check out the official specification.
注释
notes
属性按顺序保存歌曲所有音符的列表。
first_note=demo_song.notes[0]
Attribute | Type | Details |
---|---|---|
^{ | ^{ | The tick at which the note plays. |
^{ | ^{ | The id of the layer in which the note is placed. |
^{ | ^{ | The id of the instrument. |
^{ | ^{ | The key of the note. (between 0 and 87) |
层
layers
属性按顺序保存歌曲所有层的列表。
first_layer=demo_song.layers[0]
Attribute | Type | Details |
---|---|---|
^{ | ^{ | The id of the layer. |
^{ | ^{ | The name of the layer. |
^{ | ^{ | The volume of the layer. |
^{ | ^{ | The stereo panning of the layer. |
仪器
instruments
属性包含
按顺序唱。
first_custom_instrument=demo_song.instruments[0]
Attribute | Type | Details |
---|---|---|
^{ | ^{ | The id of the instrument. |
^{ | ^{ | The name of the instrument. |
^{ | ^{ | The name of the sound file of the instrument. |
^{ | ^{ | The pitch of the instrument. (between 0 and 87) |
^{ | ^{ | Whether the piano should automatically press keys with the instrument when the marker passes them. |
在歌曲上迭代
在pynbs
文件对象上迭代将连续生成歌曲的所有和弦
关联的勾号。
fortick,chordindemo_song:...
chord
是在滴答声中播放的所有音符的列表。
创建新文件
可以使用new_file()
函数创建新文件。函数允许
可以使用关键字参数指定头属性。
new_file=pynbs.new_file(song_name='Hello world')
函数返回一个新的pynbs
文件对象,您现在可以编辑它
以编程方式。
保存文件
您可以使用save()
方法对文件进行编码并将其写入指定的
地点。
new_file.save('new_file.nbs')
升级旧文件
而pynbs
是最新的开源延续规范
在Minecraft Note Block Studio中,原始文件格式仍受
read()
函数,使将歌曲批量升级到新格式成为可能。
importglobimportpynbsforold_fileinglob.glob('*.nbs'):pynbs.read(old_file).save(old_file)
许可证-MIT