-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudacity_funcs.py
690 lines (543 loc) · 16.1 KB
/
audacity_funcs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
#!/usr/bin/env python
import glob
import json
import re
from contextlib import contextmanager
from pathlib import Path
from typing import Dict, List
import pyaudacity as pa
import pyperclip
import typer
"""
audacity_funcs.py
References
[1] https://manual.audacityteam.org/man/scripting_reference.html
"""
LABEL_PART = "part"
LABEL_CHORD = "chord"
LABEL_LYRIC = "lyric"
LABEL_BAR = "bar"
LABEL_BEAT = "beat"
LABEL_PRIORITY_ORDER = [LABEL_PART, LABEL_CHORD, LABEL_LYRIC]
AUDACITY_EXTENSION = "aup3"
# when mod-script-pipe worked out fine:
RESPONSE_OK = "\nBatchCommand finshed: OK\n"
KIND_LABEL = "label"
KIND_AUDIO = "wave"
PROPERTY_KIND = "kind"
PROPERTY_FOCUSED = "focused"
PROPERTY_SELECTED = "selected"
PROPERTY_MUTED = "mute"
PROPERTY_SOLO = "solo"
SELECT_MODE_SET = "Set"
SELECT_MODE_ADD = "Add"
SELECT_MODE_REMOVE = "Remove"
GET_INFO_TRACKS = "Tracks"
GET_INFO_JSON = "JSON"
@contextmanager
def save_clipboard():
original_content = pyperclip.paste()
try:
yield
finally:
pyperclip.copy(original_content)
@contextmanager
def save_selection():
idx = get_selected_track_indices()
try:
yield
finally:
select_tracks(idx)
@contextmanager
def save_focus():
idx = get_focused_track_index()
try:
yield
finally:
focus_track(idx)
def is_project_empty() -> bool:
"""
Returns true if project is empty, i.e. has no tracks.
Tested
"""
return get_track_count() == 0
def get_tracks() -> List[Dict]:
"""
Returns a list of dicts representing track meta info.
Tested
"""
return json.loads(pa.get_info(GET_INFO_TRACKS, GET_INFO_JSON)[: -len(RESPONSE_OK)])
def get_track_count() -> int:
"""
Returns number of tracks.
Tested
"""
return len(get_tracks())
def make_label_track(label_track_name: str):
"""
Makes a label track and gives it the given name.
Tested
"""
# note:
# undoing make_label_track will remove the label track
# issuing redo after this will recreate the label track but not set its name as it was!
pa.do("NewLabelTrack:")
pa.do(f'SetTrack: Name="{label_track_name}"')
def select_first_audio_track():
"""
Selects first audio track in a project.
Nyquist needs an audio track to be selected for certain operations, like
ImportLabels (though this is a particular plugin written by SteveDaulton)
Tested
"""
first_audio_track = get_track_indices_by_kind(KIND_AUDIO)[0]
pa.do(f"SelectTracks: Track={first_audio_track} Mode={SELECT_MODE_SET}")
def make_label_track_from_file(label_file: str, label_track_name: str = None):
"""
Makes a new label track from the given file and names the label track according to the given name.
"""
label_track_name = (
label_track_name
if label_track_name
else re.sub(r"_?label_?", "", Path(label_file).stem)
)
abs_path = Path(label_file).expanduser().resolve()
with save_selection():
select_first_audio_track() # needed for nyquist
pa.do("SelTrackStartToEnd:") # needed for nyquist
pa.do(f'ImportLabels: fname="{abs_path}"')
pa.do(f"SelectTracks: Track={get_track_count() - 1} Mode={SELECT_MODE_SET}")
pa.do(f'SetTrack: Name="{label_track_name}"')
def make_label_track_01(label_file: str, label_track_name: str):
"""
Makes a new label track from the given file and names the label track according to the given name.
Uses an unreliable way, hence use not recommended, but might inspire ideas for other funcs.
"""
pa.do("NewLabelTrack:")
pa.do(f'SetTrack: Name="{label_track_name}"')
count = 1
with save_clipboard:
with open(label_file) as f:
for line in f:
s_e_l = line.strip().split("\t")
pa.do(
f"SelectTime: Start={s_e_l[0]} End={s_e_l[1]} RelativeTo=ProjectStart"
)
pyperclip.copy(s_e_l[2] if len(s_e_l) == 3 else str(count))
count += 1
pa.do("PasteNewLabel:")
def get_tracks_by_property(prop: str) -> List[Dict]:
"""
Returns list of meta info dict for tracks conforming to property.
Tested indirectly
"""
return [track for track in get_tracks() if prop in track and track[prop]]
def get_track_indices_by_property(prop: str) -> List[Dict]:
"""
Returns list of indices of tracks conforming to property.
Tested indirectly
"""
return [i for i, track in enumerate(get_tracks()) if prop in track and track[prop]]
def get_focused_tracks():
"""
Returns list of meta info dict for focused tracks.
Tested
"""
return get_tracks_by_property(PROPERTY_FOCUSED)
def get_focused_track_index():
"""
Returns the index of the focused track.
Tested
"""
for i, track in enumerate(get_tracks()):
if track[PROPERTY_FOCUSED]:
return i
def get_selected_tracks():
"""
Returns list of meta info dict for selected tracks.
Tested
"""
return get_tracks_by_property(PROPERTY_SELECTED)
def get_muted_tracks():
"""
Returns list of meta info dict for muted tracks.
Tested
"""
return get_tracks_by_property(PROPERTY_MUTED)
def get_solo_tracks():
"""
Returns list of meta info dict for solo tracks.
Tested
"""
return get_tracks_by_property(PROPERTY_SOLO)
def get_solo_track_indices():
"""
Returns list of indices of solo tracks.
Tested
"""
return get_track_indices_by_property(PROPERTY_SOLO)
def get_muted_track_indices():
"""
Returns list of indices of muted tracks.
Tested
"""
return get_track_indices_by_property(PROPERTY_MUTED)
def toggle_solo_track(track: int):
"""
Toggles solo state of track
Tested indirectly
"""
with save_focus():
focus_track(track)
pa.do("TrackSolo:")
def solo_track(track: int):
"""
Soloes the given track.
Tested
"""
if track in get_solo_track_indices():
return
toggle_solo_track(track)
def solo_tracks(tracks: List[int]):
"""
Soloes the given tracks.
Tested
"""
for track in tracks:
solo_track(track)
def unsolo_track(track: int):
"""
Unsoloes the given track.
Tested
"""
if track not in get_solo_track_indices():
return
toggle_solo_track(track)
def unsolo_tracks(tracks: List[int]):
"""
Unsoloes the given tracks.
Tested
"""
for track in tracks:
unsolo_track(track)
def focus_track(track: int):
"""
Focuses the given track.
Optimizes by finding the closes of first, last, currently focused track to the desired
new target track.
Tested
"""
tc = get_track_count()
current_track = get_focused_track_index()
# Calculate distances
distance_from_first = track
distance_from_last = tc - track - 1
distance_from_current = abs(track - current_track)
# Determine the minimum distance and the corresponding fixpoint
if (
distance_from_first <= distance_from_last
and distance_from_first <= distance_from_current
):
pa.do("FirstTrack:")
for _ in range(distance_from_first):
pa.do("NextTrack:")
elif (
distance_from_last <= distance_from_first
and distance_from_last <= distance_from_current
):
pa.do("LastTrack:")
for _ in range(distance_from_last):
pa.do("PrevTrack:")
else:
if track > current_track:
for _ in range(distance_from_current):
pa.do("NextTrack:")
elif track < current_track:
for _ in range(distance_from_current):
pa.do("PrevTrack:")
def mute_track(track: int):
"""
Mutes the given track.
Tested
"""
with save_selection():
select_track(track)
pa.do("MuteTracks:")
def mute_tracks(tracks: List[int]):
"""
Mutes the given tracks.
Tested
"""
with save_selection():
select_tracks(tracks)
pa.do("MuteTracks:")
def unmute_track(track: int):
"""
Unmutes the given track.
Tested
"""
with save_selection():
select_track(track)
pa.do("UnmuteTracks:")
def unmute_tracks(track: List[int]):
"""
Unmutes the given tracks.
Tested
"""
with save_selection():
select_tracks(track)
pa.do("UnmuteTracks:")
def select_track(track: int):
"""
Selects track
Tested
"""
unselect_tracks()
pa.do(f"SelectTracks: Track={track} Mode={SELECT_MODE_ADD}")
def select_tracks(tracks: List[int]):
"""
Parameters to "SelectTracks:" command (see [1]):
Track - first track to select, tracks are numbered starting from 0
TrackCount - how many tracks to select
Mode - either one of SELECT_MODE_SET, SELECT_MODE_ADD, SELECT_MODE_REMOVE
Tested
"""
unselect_tracks()
for track in tracks:
pa.do(f"SelectTracks: Track={track} Mode={SELECT_MODE_ADD}")
def unselect_track(idx: int):
"""
See select_tracks.
Tested
"""
pa.do(f"SelectTracks: Track={idx} Mode={SELECT_MODE_REMOVE}")
def unselect_tracks():
"""
Unselects all tracks.
Tested
"""
pa.do("SelectNone:")
def select_tracks_by_kind(kind: str):
"""
Selects tracks by kind.
Tested (indirectly)
"""
return select_tracks(get_track_indices_by_kind(kind))
def select_label_tracks():
"""
Selects label tracks.
Tested
"""
return select_tracks_by_kind(KIND_LABEL)
def select_audio_tracks():
"""
Selects audio tracks.
Tested
"""
return select_tracks_by_kind(KIND_AUDIO)
def get_tracks_by_kind(kind: str):
"""
Returns list of track meta info for tracks of given kind.
Tested (indirectly)
"""
return [track for track in get_tracks() if track[PROPERTY_KIND] == kind]
def get_selected_label_track_indices() -> List[int]:
"""
Returns list of selected label track indices.
Tested
"""
return get_selected_track_indices_by_kind(KIND_LABEL)
def get_selected_audio_track_indices() -> List[int]:
"""
Returns list of selected audio track indices.
Tested
"""
return get_selected_track_indices_by_kind(KIND_AUDIO)
def get_selected_track_indices_by_kind(kind) -> List[int]:
"""
Returns list of selected track indices by kind.
Tested indirectly
"""
tracks_kind = get_track_indices_by_kind(kind)
tracks_selected = get_selected_track_indices()
return list(set(tracks_kind) & set(tracks_selected))
def get_selected_track_indices() -> List[int]:
"""
Returns list of selected track indices.
Tested
"""
return [i for i, track in enumerate(get_tracks()) if track[PROPERTY_SELECTED]]
def get_track_indices_by_kind(kind) -> List[int]:
"""
Returns list of selected track indices by kind.
Tested (indirectly)
"""
return [i for i, track in enumerate(get_tracks()) if track[PROPERTY_KIND] == kind]
def get_audio_track_indices() -> List[int]:
"""
Returns list of audio track indices.
Tested
"""
return get_track_indices_by_kind(KIND_AUDIO)
def get_label_track_indices() -> List[int]:
"""
Returns list of label track indices.
Tested
"""
return get_track_indices_by_kind(KIND_LABEL)
def get_label_tracks() -> List[Dict]:
"""
Returns list of track meta info for label tracks.
Tested
"""
return get_tracks_by_kind(KIND_LABEL)
def get_audio_tracks() -> List[Dict]:
"""
Returns list of track meta info for audio tracks.
Tested
"""
return get_tracks_by_kind(KIND_AUDIO)
def remove_selected_tracks():
"""
Removes selected tracks.
Tested
"""
return pa.do("RemoveTracks:")
def undo():
"""
Undo
Tested
"""
return pa.do("Undo:")
def redo():
"""
Redo
Tested
"""
return pa.do("Redo:")
def export_labels():
"""
Unavoidably interactive.
"""
return pa.do("ExportLabels:")
def export_labels_list(labels: List[int]):
"""
Interactive due to export_labels' interactivity
Exports label tracks given by track number.
"""
for idx in labels:
select_label_tracks()
unselect_track(idx)
remove_selected_tracks()
export_labels()
undo()
def export_selected_label_tracks():
"""
Interactive due to export_labels' interactivity
Goes through the selected label tracks one by one:
removes all other label tracks,
exports label (interactively) undoes the removing of all label tracks,
"""
export_labels_list(get_selected_label_track_indices())
def export_label_tracks():
"""
Interactive due to export_labels' interactivity
Goes through the label tracks one by one:
removes all other label tracks,
exports label (interactively) undoes the removing of all label tracks,
"""
export_labels_list(get_label_track_indices())
def export_selected_or_all_label_tracks():
"""
Interactive due to export_labels' interactivity
Goes through the label tracks one by one:
removes all other label tracks,
exports label (interactively) undoes the removing of all label tracks,
"""
label_track_indices = (
get_selected_label_track_indices() or get_label_track_indices()
)
export_labels_list(label_track_indices)
def import_audio(filename: str):
"""
Imports audio into Audacity.
"""
abs_path = Path(filename).expanduser().resolve()
pa.import_audio(abs_path)
def open_project(filename: str):
"""
Opens the Audacity project given by filename.
"""
abs_path = Path(filename).expanduser().resolve()
pa.do(f'OpenProject2: Filename="{abs_path}"')
def is_audacity_project(filename: str) -> bool:
"""
Returns true if the given filename represents an audacity project.
Tested
"""
return filename.lower().endswith(f".{AUDACITY_EXTENSION}")
def create_labels_glob(filename: str) -> List[str]:
"""
Finds all label files associated with the audio
file give by name.
"""
abs_path = Path(filename).expanduser().resolve()
dirname = abs_path.parent
return glob.glob(f"{dirname}/*_{abs_path.stem}.txt")
def reorder_labels(filenames: List[str]) -> List[str]:
"""
Reorders given label files by this order:
1. part
2. chord
3. lyric
4. unrecognized
5. bar (second-to-last)
6. beat (last)
"""
def get_priority(identifier):
# Recognized labels first
for i, substring in enumerate(LABEL_PRIORITY_ORDER):
if substring in identifier:
return i
# bar before beat
if LABEL_BAR in identifier:
return len(LABEL_PRIORITY_ORDER) + 1 # bar come just before beat
# beat come last
if LABEL_BEAT in identifier:
return len(LABEL_PRIORITY_ORDER) + 2 # beat come after bar
# Unrecognized labels come just before bars and beat
return len(
LABEL_PRIORITY_ORDER
) # Unrecognized labels go between recognized and bar/beat
# Sort filenames using the modified priority
return sorted(filenames, key=get_priority)
def open_audio(filename: str, verbose=False):
"""
Opens the audio file given by name.
If it's an audacity project, simply opens it.
If it's any other format, imports it and any
labels associated with it.
"""
if is_audacity_project(filename):
if verbose:
print(f'Opening "{filename}"')
open_project(filename)
if verbose:
print(f'Done opening "{filename}"')
else:
if verbose:
print(f'Importing "{filename}"')
import_audio(filename)
if verbose:
print(f'Done importing "{filename}"')
abs_path = Path(filename).expanduser().resolve()
label_files = reorder_labels(create_labels_glob(filename))
for lfile in label_files:
lblname = Path(lfile).stem.replace(f"_{abs_path.stem}", "")
if verbose:
print(f"labels: >{lblname}<")
make_label_track_from_file(lfile, lblname)
def main():
print("This main is just for testing purposes.")
if __name__ == "__main__":
typer.run(main)