Skip to content

Commit

Permalink
Ver(1.4): main()/error handling, rename module to hachiko_bapu
Browse files Browse the repository at this point in the history
  • Loading branch information
duangsuse committed Apr 26, 2020
1 parent b9c6457 commit 9b8db00
Show file tree
Hide file tree
Showing 13 changed files with 28 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,5 @@ dmypy.json
# Project specified
*.srt
*.mid
*.mp4
*.ogg
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
'''

from datetime import timedelta
from srt import Subtitle, parse, compose
from srt import Subtitle, compose
from srt import parse as fromSrt

from os import linesep

Expand Down Expand Up @@ -48,7 +49,6 @@ def cfgOrDefault(value, f, x):
return value if value != None else f(x)



from re import compile
PAT_LRC_ENTRY = compile(r"[\[<](\d{2}):(\d{2}).(\d{2})[>\]] ?([^<\n]*)")

Expand All @@ -67,6 +67,7 @@ def formatLine(line):
return fmtFst +sep1+ sep1.join([header(t, surr2) + s for (t, s) in line[1:]])
return linesep.join(map(formatLine, lrc_lines))


def fromLrc(text, min_len):
td = lambda t: timedelta(seconds=t)
return [Subtitle(i+1, td(t), td(t+min_len), s) for i, (t, s) in enumerate(readLrc(text))]
Expand Down Expand Up @@ -101,7 +102,8 @@ def main(args = argv[1:]):
use_lrc = cfg.file == "lrc"
inSameLine = lambda a, b: abs((a.start if use_lrc else a.end) - b.start).total_seconds() < cfg.dist

data = list(flatMap(lambda t: fromLrc(t, cfg.min_len), readLines("lrc")) if use_lrc else parse(open(cfg.file).read()))
#v regex findall has input size limitations...
data = list(flatMap(lambda t: fromLrc(t, cfg.min_len), readLines("lrc")) if use_lrc else fromSrt(open(cfg.file).read()))
print(" ".join([f"{srt.start.total_seconds()};{srt.content}" for srt in data]))

print("== lyrics")
Expand All @@ -110,3 +112,5 @@ def main(args = argv[1:]):

with open(cfg.o, "w+") as srtf:
srtf.write(compose(intoSrt(result, cfg.sep)))

if __name__ == "__main__": main()
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,5 @@ def main(args = argv[1:]):
(mode, paths) = (modes["from"], args[0:]) if mname not in modes else (modes[mname], args[1:])
for path in paths:
with open(path, "r") as ins: mode(ins)

if __name__ == "__main__": main()
File renamed without changes.
File renamed without changes.
16 changes: 13 additions & 3 deletions hachiko/hachi.py → hachiko_bapu/hachi.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ def splitAs(type, transform = int, delim = ","):
playDuration = env("PLAY_DURATION", splitAs(list, transform=float), [0.3, 0.5, 1.5])

from pkg_resources import resource_filename
INSTRUMENT_SF2 = env("SFONT", str, resource_filename(__name__, "instrument.sf2"))
def resInstrumentSf2(name = "instrument.sf2") -> str:
try: sfont = resource_filename(__name__, name)
except ModuleNotFoundError: sfont = name
return env("SFONT", str, sfont)

INSTRUMENT_SF2 = resInstrumentSf2()
sampleRate = env("SAMPLE_RATE", int, 44100)
sfontPreset = 0 #< used twice

Expand Down Expand Up @@ -117,6 +122,9 @@ def gameCenterText(text, cx=0.5, cy=0.5):
bg.blit(rtext, textpos)
pygame.display.flip()

def clamlySetFont(synth, path, preset):
try: synth.setFont(path, preset)
except OSError: print(f"{path} is required to enable note playback!")

def guiReadPitches(note_base, reducer, onKey = lambda ctx, k: (), caption = "Add Pitches"):
gameWindow(caption, WINDOW_DIMEN)
Expand All @@ -126,7 +134,7 @@ def playSec(n_sec, pitch):
synth.noteSwitch(pitch)
timeout(n_sec, synth.noteoff)

synth.setFont(INSTRUMENT_SF2, sfontPreset)
clamlySetFont(synth, INSTRUMENT_SF2, sfontPreset)
synth.start()
playSec(playDuration[1], note_base)

Expand Down Expand Up @@ -204,7 +212,7 @@ def guiReadTimeline(pitchz, reducer, play = None, caption = "Add Timeline", seek
mus.play()

synth = NoteSynth(sampleRate)
synth.setFont(INSTRUMENT_SF2, sfontPreset)
clamlySetFont(synth, INSTRUMENT_SF2, sfontPreset)
synth.start()

onPausePlay = CallFlagTimed(mus.unpause, mus.pause)
Expand Down Expand Up @@ -258,3 +266,5 @@ def onEvent(event):
for event in pygame.event.get(): onEvent(event)
except NonlocalReturn: break
return reducer.finish()

if __name__ == "__main__": main()
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def parse_requirements(requirements):
return list(filter(lambda s: s.strip() != "", items))

setup(
name="hachiko-bapu", version="0.1.3",
name="hachiko-bapu", version="0.1.4",
python_requires=">=3.5",
author="duangsuse", author_email="fedora-opensuse@outlook.com",
url="https://github.com/duangsuse-valid-projects/Hachiko",
Expand Down Expand Up @@ -39,8 +39,8 @@ def parse_requirements(requirements):
},
entry_points={
"console_scripts": [
"hachiko = hachiko.hachi:main",
"srt2mid = hachiko.cli_tools.srt2mid:main",
"lrc_merge = hachiko.cli_tools.lrc_merge:main"
"hachiko = hachiko_bapu.hachi:main",
"srt2mid = hachiko_bapu.cli_tools.srt2mid:main",
"lrc_merge = hachiko_bapu.cli_tools.lrc_merge:main"
]
})

0 comments on commit 9b8db00

Please sign in to comment.