comparison multispectro/assemble_images.py @ 2445:af83aeef8b0a

fancier spectrograms
author drewp@bigasterisk.com
date Sat, 01 Jun 2024 12:58:25 -0700
parents
children 4bab5bbce195
comparison
equal deleted inserted replaced
2444:d087499d7833 2445:af83aeef8b0a
1 # #!/bin/zsh
2
3 import os
4 import subprocess
5
6 import cv2
7 import numpy as np
8
9 for songname in [
10 '01-guest',
11 '02_dancing_happy',
12 '03-bear-trim',
13 '03-bear',
14 '04-disneyswing',
15 '05-encanto',
16 '06-frozen',
17 '07-onejump',
18 '08-lionking',
19 '09-pianoman-mix',
20 '10-disneytap',
21 '11-club',
22 '12-sunnyside2',
23 '13-supercali',
24 '14-groove',
25 '15-mermaid',
26 '16-all',
27 '17-parade-mix',
28 ]:
29
30 WAV_DIR = f"/tmp/htdemucs/{songname}"
31 instruments = [
32 ("vocals", "3k", (1, .3, 0)),
33 ("other", "3k", (1, .8, .5)),
34 ("drums", "1k", (1, .9, 1)),
35 ("bass", "400", (0, .4, 1)),
36 ]
37
38 def generate_spectrogram(input_file, output_file, rate):
39 command = ["sox", input_file, "-n", "remix", "1", "rate", rate, "spectrogram", "-X", "50", "-y", "100", "-z", "80", "-m", "-r", "-o", output_file]
40 subprocess.check_call(command)
41
42 def tint_image(img, tint):
43 img = img.astype(np.float32) / 255
44 tint_img = np.full((img.shape[0], img.shape[1], 3), tint[::-1], dtype=np.float32)
45 return cv2.multiply(img, tint_img)
46
47 stack = []
48 for name, rate, tint in instruments:
49 input_file = f"{WAV_DIR}/{name}.wav"
50 spectro_file = f"/tmp/spectro_{name}.png"
51 generate_spectrogram(input_file, spectro_file, rate)
52 img = cv2.imread(spectro_file)
53 tinted_img = tint_image(img, tint)
54 stack.append(tinted_img)
55
56 out = np.concatenate(stack, axis=0)
57 cv2.imwrite(os.path.expandvars(f"$LIGHT9_SHOW/spectrogram/{songname}.png"), out * 255)