数据类
RCT API 提供了多个数据类来表示谱面的各种元素。
类列表
BPM 类
表示 BPM 变化点。
构造函数
from rct_api import BPM
bpm = BPM(time=0, bpm=120.0)
属性
| 属性 | 类型 | 说明 |
|---|---|---|
time | float | 时间(毫秒) |
bpm | float | BPM 值 |
示例
from rct_api import get_current_chart
chart = get_current_chart()
if chart and chart.bpm:
print("=== BPM 信息 ===")
for bpm in chart.bpm:
print(f"时间: {bpm.time}ms, BPM: {bpm.bpm}")
Beat 类
用于处理拍数和时间之间的转换,支持多变速 BPM。
构造函数
方式 1:从 BPM 列表创建
from rct_api import Beat
bpm_list = [[0, 120], [4000, 140], [8000, 130]]
beat = Beat(bpm_list)
参数:
bpm_list: BPM 列表,格式为[[time(ms), bpm], ...]
方式 2:从 BPM 对象列表创建(推荐)
from rct_api import Beat, get_current_chart
chart = get_current_chart()
beat = Beat.from_bpm_objects(chart.bpm)
推荐
当处理谱面数据时,建议使用 Beat.from_bpm_objects() 方法,它可以直接使用 chart.bpm 对象列表,代码更简洁。
方法
get_time()
从拍数获取时间(beat → ms)。
语法
time = beat.get_time(beat_number)
参数
| 参数 | 类型 | 说明 |
|---|---|---|
beat_number | float | 拍数 |
返回值
对应的时间(毫秒)
示例
from rct_api import Beat, get_current_chart
chart = get_current_chart()
beat = Beat.from_bpm_objects(chart.bpm)
# 获取第 4 拍的时间
time = beat.get_time(4.0)
print(f"第 4 拍的时间: {time}ms")
# 获取第 8.5 拍的时间
time = beat.get_time(8.5)
print(f"第 8.5 拍的时间: {time}ms")
get_beat()
从时间获取拍数(ms → beat)。
语法
beats = beat.get_beat(time)
参数
| 参数 | 类型 | 说明 |
|---|---|---|
time | float | 时间(毫秒) |
返回值
对应的拍数(浮点数)
示例
from rct_api import Beat, get_current_chart
chart = get_current_chart()
beat = Beat.from_bpm_objects(chart.bpm)
# 获取 5000ms 对应的拍数
beats = beat.get_beat(5000)
print(f"5000ms 对应的拍数: {beats:.3f}")
from_bpm_objects()
类方法:从 BPM 对象列表创建 Beat 实例。
语法
beat = Beat.from_bpm_objects(bpm_objects)
参数
| 参数 | 类型 | 说明 |
|---|---|---|
bpm_objects | List[BPM] | BPM 对象列表 |
返回值
Beat 实例
示例
from rct_api import Beat, get_current_chart
chart = get_current_chart()
beat = Beat.from_bpm_objects(chart.bpm)
完整示例
# Description: 按照节拍生成Note
from rct_api import get_current_chart, save_current_chart, Beat, Note
def main():
chart = get_current_chart()
if not chart or not chart.bpm:
print("错误:无法获取谱面或 BPM 信息")
return
# 创建 Beat 转换器
beat = Beat.from_bpm_objects(chart.bpm)
# 在第 1-8 拍,每半拍生成一个 Tap
for i in range(16): # 8 拍 × 每拍 2 个Note
beat_number = 1.0 + i * 0.5
time = beat.get_time(beat_number)
degree = (i * 45) % 360
chart.add_note(Note.TAP, time, degree)
print(f"第 {beat_number:.1f} 拍 -> {time:.0f}ms")
chart.sort()
save_current_chart(chart)
print("生成完成")
if __name__ == "__main__":
main()
Speed 类
表示 Speed 变化点。
构造函数
from rct_api import Speed
speed = Speed(time=1000, speed=2.0, smooth=0)
属性
| 属性 | 类型 | 说明 |
|---|---|---|
time | float | 时间(毫秒) |
speed | float | Speed 倍率 |
smooth | int | 平滑类型(0=无,1=平滑) |
示例
from rct_api import get_current_chart
chart = get_current_chart()
if chart and chart.speed:
print("=== Speed 信息 ===")
for speed in chart.speed:
smooth_str = "平滑" if speed.smooth == 1 else "无"
print(f"时间: {speed.time}ms, 倍率: {speed.speed}x, 平滑: {smooth_str}")
Note 类
表示一个Note。
构造函数
from rct_api import Note
note = Note(type, time, degree, *extra)
Note类型常量
Note.TAP = 0 # Tap
Note.FLICK = 1 # Flick
Note.SLIDE = 2 # Slide
Note.ROTATE = 4 # Rotate
Note.CATCH = 5 # Catch
Note.BOMB = 6 # Bomb
Note.TRAIL = 11 # Trail
Note结构
不同类型的Note有不同的参数结构:
Tap Note
Note(Note.TAP, time, degree)
示例:
tap = Note(Note.TAP, 1000, 45)
Flick Note
Note(Note.FLICK, time, degree)
示例:
flick = Note(Note.FLICK, 2000, 90)
Catch Note
Note(Note.CATCH, time, degree)
示例:
catch = Note(Note.CATCH, 3000, 180)
Bomb Note
Note(Note.BOMB, time, degree)
示例:
bomb = Note(Note.BOMB, 4000, 270)
Trail Note
Note(Note.TRAIL, time, degree, delta, prev_curv, next_curv)
参数:
time: 时间(毫秒)degree: 起始角度delta: 角度变化量prev_curv: 前置曲率next_curv: 后置曲率
示例:
trail = Note(Note.TRAIL, 5000, 0, 180, 0, 0)
Rotate Note
Note(Note.ROTATE, time, degree, delta, prev_curv, next_curv)
参数:
time: 时间(毫秒)degree: 起始角度delta: 角度变化量prev_curv: 前置曲率next_curv: 后置曲率
示例:
rotate = Note(Note.ROTATE, 6000, 0, 360, 0, 0)
Slide Note
Note(Note.SLIDE, time, degree, slidetype, end_degree, snap, amount, prev_curv, next_curv)
参数:
time: 时间(毫秒)degree: 起始角度slidetype: Slide类型end_degree: 结束角度snap: 吸附参数amount: 数量参数prev_curv: 前置曲率next_curv: 后置曲率
示例:
slide = Note(Note.SLIDE, 7000, 0, 1, 180, 4, 8, 0, 0)
访问Note属性
from rct_api import get_current_chart, Note
chart = get_current_chart()
if chart and chart.notes:
for note in chart.notes[:5]: # 前 5 个Note
print(f"类型: {note.type}")
print(f"时间: {note.time}ms")
print(f"角度: {note.degree}°")
# 根据类型判断
if note.type == Note.TAP:
print(" -> Tap Note")
elif note.type == Note.TRAIL:
print(f" -> Trail Note (结束角度: {note.data[0]}°)")
print()
完整示例:使用所有数据类
# Description: 数据类综合示例
from rct_api import get_current_chart, Beat, Note
def main():
chart = get_current_chart()
if not chart:
print("错误:无法获取谱面")
return
# BPM 信息
print("=== BPM 信息 ===")
for bpm in chart.bpm:
print(f"{bpm.time}ms: {bpm.bpm} BPM")
print()
# Speed 信息
print("=== Speed 信息 ===")
for speed in chart.speed:
print(f"{speed.time}ms: {speed.speed}x (平滑: {speed.smooth})")
print()
# 使用 Beat 类分析Note
print("=== Note拍数分析 ===")
beat = Beat.from_bpm_objects(chart.bpm)
type_names = {
Note.TAP: "Tap",
Note.FLICK: "Flick",
Note.SLIDE: "Slide",
Note.ROTATE: "Rotate",
Note.CATCH: "Catch",
Note.BOMB: "Bomb",
Note.TRAIL: "Trail"
}
for i, note in enumerate(chart.notes[:10]):
note_beat = beat.get_beat(note.time)
type_name = type_names.get(note.type, f"Unknown({note.type})")
print(f"{i+1}. {type_name} - {note.time:.0f}ms (第 {note_beat:.2f} 拍) - {note.degree}°")
if __name__ == "__main__":
main()