Skip to content

Instantly share code, notes, and snippets.

@trueroad
Last active July 19, 2022 14:05
Show Gist options
  • Save trueroad/2626654d4ca5d0c5bf44c32837dfc53a to your computer and use it in GitHub Desktop.
Save trueroad/2626654d4ca5d0c5bf44c32837dfc53a to your computer and use it in GitHub Desktop.
BLE MIDI JSONL time converter (t2ns.py: ms to ns, ns2t.py: ns to ms)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
BLE MIDI JSONL time converter (ns to ms).
https://gist.github.com/trueroad/2626654d4ca5d0c5bf44c32837dfc53a
Copyright (C) 2022 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.
"""
import json
import sys
from typing import Any, Dict
def main() -> None:
"""
メイン.
標準入力から JSONL を読み込み、
キー 'ns' の値になっている ns 単位の時刻情報を
キー 't' の値になっている ms 単位の時刻情報に変換し、
キー 'data' の値はそのままスルーして、
標準出力へ書き込む。
Recieve from BLE MIDI Peripherals with python bleak.
https://gist.github.com/trueroad/fdb0a450c1699d67fe02f4a31b44c895
で出力した JSONL を、ms 単位の時刻情報を要するシステム向けに
変換するツールである。
"""
line: str
for line in sys.stdin:
jd_in: Dict[str, Any]
try:
jd_in = json.loads(line)
except json.decoder.JSONDecodeError:
print(f'cannot decode: {line}', end='', file=sys.stderr)
continue
jd_out: Dict[str, Any] = {'t': jd_in['ns'] * 0.000001,
'data': jd_in['data']}
print(json.dumps(jd_out))
if __name__ == '__main__':
main()
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
BLE MIDI JSONL time converter (ms to ns).
https://gist.github.com/trueroad/2626654d4ca5d0c5bf44c32837dfc53a
Copyright (C) 2022 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.
"""
import json
import sys
from typing import Any, Dict
def main() -> None:
"""
メイン.
標準入力から JSONL を読み込み、
キー 't' の値になっている ms 単位の時刻情報を
キー 'ns' の値になっている ns 単位の時刻情報に変換し、
キー 'data' の値はそのままスルーして、
標準出力へ書き込む。
ms 単位の時刻情報で記録された JSONL を
Emulate BLE MIDI Peripherals with python bless.
https://gist.github.com/trueroad/aa2507bf64c97f34aab324d8278b5686
向けに変換するツールである。
"""
line: str
for line in sys.stdin:
jd_in: Dict[str, Any]
try:
jd_in = json.loads(line)
except json.decoder.JSONDecodeError:
print(f'cannot decode: {line}', end='', file=sys.stderr)
continue
jd_out: Dict[str, Any] = {'ns': int(jd_in['t'] * 1000000),
'data': jd_in['data']}
print(json.dumps(jd_out))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment