Skip to content

Commit 9b8db00

Browse files
committed
Ver(1.4): main()/error handling, rename module to hachiko_bapu
1 parent b9c6457 commit 9b8db00

File tree

13 files changed

+28
-10
lines changed

13 files changed

+28
-10
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,5 @@ dmypy.json
131131
# Project specified
132132
*.srt
133133
*.mid
134+
*.mp4
135+
*.ogg
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
'''
1313

1414
from datetime import timedelta
15-
from srt import Subtitle, parse, compose
15+
from srt import Subtitle, compose
16+
from srt import parse as fromSrt
1617

1718
from os import linesep
1819

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

5051

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

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

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

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

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

111113
with open(cfg.o, "w+") as srtf:
112114
srtf.write(compose(intoSrt(result, cfg.sep)))
115+
116+
if __name__ == "__main__": main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,5 @@ def main(args = argv[1:]):
9292
(mode, paths) = (modes["from"], args[0:]) if mname not in modes else (modes[mname], args[1:])
9393
for path in paths:
9494
with open(path, "r") as ins: mode(ins)
95+
96+
if __name__ == "__main__": main()
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ def splitAs(type, transform = int, delim = ","):
2828
playDuration = env("PLAY_DURATION", splitAs(list, transform=float), [0.3, 0.5, 1.5])
2929

3030
from pkg_resources import resource_filename
31-
INSTRUMENT_SF2 = env("SFONT", str, resource_filename(__name__, "instrument.sf2"))
31+
def resInstrumentSf2(name = "instrument.sf2") -> str:
32+
try: sfont = resource_filename(__name__, name)
33+
except ModuleNotFoundError: sfont = name
34+
return env("SFONT", str, sfont)
35+
36+
INSTRUMENT_SF2 = resInstrumentSf2()
3237
sampleRate = env("SAMPLE_RATE", int, 44100)
3338
sfontPreset = 0 #< used twice
3439

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

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

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

129-
synth.setFont(INSTRUMENT_SF2, sfontPreset)
137+
clamlySetFont(synth, INSTRUMENT_SF2, sfontPreset)
130138
synth.start()
131139
playSec(playDuration[1], note_base)
132140

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

206214
synth = NoteSynth(sampleRate)
207-
synth.setFont(INSTRUMENT_SF2, sfontPreset)
215+
clamlySetFont(synth, INSTRUMENT_SF2, sfontPreset)
208216
synth.start()
209217

210218
onPausePlay = CallFlagTimed(mus.unpause, mus.pause)
@@ -258,3 +266,5 @@ def onEvent(event):
258266
for event in pygame.event.get(): onEvent(event)
259267
except NonlocalReturn: break
260268
return reducer.finish()
269+
270+
if __name__ == "__main__": main()

0 commit comments

Comments
 (0)