Skip to content

Instantly share code, notes, and snippets.

@trueroad
Last active January 25, 2024 11:01
Show Gist options
  • Save trueroad/aca02ee6b36f8aee0ac7bebbdde912fc to your computer and use it in GitHub Desktop.
Save trueroad/aca02ee6b36f8aee0ac7bebbdde912fc to your computer and use it in GitHub Desktop.
Show SMF (Standard MIDI File) parameters.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Show SMF (Standard MIDI File) parameters.
https://gist.github.com/trueroad/aca02ee6b36f8aee0ac7bebbdde912fc
Copyright (C) 2024 Masamichi Hosoda.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
複数の SMF (Standard MIDI File, .mid) を入力として、
SMF タイプ、TPQN、トラック数、
SMF 全体の長さ(秒)、音符(ノート ON/OFF の組)の数、
最初の音符のノート ON 時刻(秒)、最後の音符のノート OFF 時刻(秒)、
最初の音符のノート ON から最後の音符のノート OFF までの時間(秒)と
ファイル名をタブ区切りで出力する。
"""
import sys
from typing import Optional
import smf_parse
def main() -> None:
"""Do main."""
# コマンドラインで指定された SMF ファイル名 (.mid) でループ
smf_filename: str
for smf_filename in sys.argv[1:]:
# SMF をロード
sn: smf_parse.smf_notes = smf_parse.smf_notes()
sn.load(smf_filename)
# SMF タイプ
smf_type: int
# TPQN (ticks per quater note) タイムベース
tpqn: int
# トラック数
tracks: int
smf_type, tpqn, tracks = sn.get_smf_specs()
# SMF 最後の時刻(秒)
end_of_smf_time: float = 0.0
if sn.end_of_smf is not None:
end_of_smf_time = sn.end_of_smf[0]
# 音符(ノートON/OFF の組)の数
nnotes: int = len(sn.get_notes())
# 最初の音符
note_first: Optional[smf_parse.note_container] = \
sn.get_notes()[0] if nnotes > 0 else None
# 最後の音符
note_last: Optional[smf_parse.note_container] = \
sn.get_notes()[-1] if nnotes > 0 else None
# 最初の音符のノート ON 時刻(秒)
first_time: Optional[float] = \
note_first.note_on.abs_time if note_first is not None else None
# 最後の音符のノート OFF 時刻(秒)
last_time: Optional[float] = \
note_last.note_off.abs_time if note_last is not None else None
# 最初の音符のノート ON から最後の音符のノート OFF までの時間(秒)
duration: float = (last_time - first_time) \
if last_time is not None and first_time is not None else 0.0
# 出力
print(f'{smf_type}\t{tpqn}\t{tracks}\t'
f'{end_of_smf_time}\t'
f'{nnotes}\t'
f'{first_time}\t{last_time}\t{duration}\t'
f'{smf_filename}')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment